package com.github.davidmoten.rx.internal.operators;

import static org.junit.Assert.assertEquals;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.junit.Test;

import com.github.davidmoten.rx.Actions;
import com.github.davidmoten.rx.Transformers;
import com.github.davidmoten.rx.testing.TestingHelper;

import rx.Observable;
import rx.functions.Func1;

public class OnSubscribeMapLastTest {

    @Test
    public void testMapLastRequestAmount() {
        List<Long> list = new ArrayList<Long>();
        Observable.range(1, 10) //
                .doOnRequest(Actions.addTo(list))//
                .compose(Transformers.mapLast(new Func1<Integer, Integer>() {
                    @Override
                    public Integer call(Integer x) {
                        return x + 1;
                    }
                })).to(TestingHelper.<Integer>testWithRequest(1)) //
                .assertNotCompleted() //
                .assertValuesAndClear(1) //
                .requestMore(3) //
                .assertValues(2, 3, 4);
        assertEquals(Arrays.asList(2L, 3L), list);
    }

    @Test
    public void testHandlesRequestOverflow() {
        List<Long> list = new ArrayList<Long>();
        Observable.range(1, 5) //
                .doOnRequest(Actions.addTo(list))//
                .compose(Transformers.mapLast(new Func1<Integer, Integer>() {
                    @Override
                    public Integer call(Integer x) {
                        return x + 1;
                    }
                })).to(TestingHelper.<Integer>testWithRequest(Long.MAX_VALUE)) //
                .assertCompleted() //
                .assertValues(1, 2, 3, 4, 6);
        assertEquals(Arrays.asList(Long.MAX_VALUE), list);
    }

}

@@ -348,16 +348,74 @@
 Helper;%0D
+%0Aimport com.github.davidmoten.rx.util.IORuntimeException;%0D
 %0A%0D%0Aimpor
@@ -1251,16 +1251,23 @@
 oid test
+MapLast
 HandlesR
@@ -1815,24 +1815,24 @@
  3, 4, 6);%0D%0A
-
         asse
@@ -1888,11 +1888,908 @@
    %7D%0D%0A%0D%0A
+    @Test(expected = OutOfMemoryError.class)%0D%0A    public void testMapLastHandlesFatalError() %7B%0D%0A        Observable.range(1, 5) //%0D%0A                .compose(Transformers.mapLast(new Func1%3CInteger, Integer%3E() %7B%0D%0A                    @Override%0D%0A                    public Integer call(Integer x) %7B%0D%0A                        throw new OutOfMemoryError();%0D%0A                    %7D%0D%0A                %7D)).subscribe();%0D%0A    %7D%0D%0A%0D%0A    @Test%0D%0A    public void testMapLastHandlesNonFatalError() %7B%0D%0A        final RuntimeException e = new RuntimeException();%0D%0A        Observable.range(1, 5) //%0D%0A                .compose(Transformers.mapLast(new Func1%3CInteger, Integer%3E() %7B%0D%0A                    @Override%0D%0A                    public Integer call(Integer x) %7B%0D%0A                        throw e;%0D%0A                    %7D%0D%0A                %7D)).to(TestingHelper.%3CInteger%3Etest()) //%0D%0A                .assertError(e);%0D%0A    %7D%0D%0A%0D%0A
 %7D%0D%0A

add tests for mapLast