package org.fungsi.concurrent;

import org.fungsi.Unit;
import org.junit.Test;

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

import static org.fungsi.Matchers.isSuccess;
import static org.hamcrest.CoreMatchers.hasItems;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is;

public class FutureTest {
	@Test(expected = IllegalStateException.class)
	public void testFilter() throws Exception {
		Future<Unit> fut = Futures.unit();

		fut.filter(it -> false).get();
	}

	@Test
	public void testMap() throws Exception {
		Future<String> fut = Futures.success("lol");

		int res = fut.map(String::length).get();

		assertThat(res, is(3));
	}

	@Test
	public void testCollect() throws Exception {
		Future<List<String>> fut = Futures.collect(Arrays.asList(
				Futures.success("lol"),
				Futures.success("mdr"),
				Futures.success("lmao")
		));

		assertThat(fut, isSuccess());
		assertThat(fut.get(), hasItems("lol", "mdr", "lmao"));
	}
}

@@ -64,69 +64,223 @@
 org.
-junit.Test;%0A%0Aimport java.util.Arrays;%0Aimport java.util.List;%0A
+fungsi.function.UnsafeSupplier;%0Aimport org.junit.Test;%0A%0Aimport java.time.Duration;%0Aimport java.util.Arrays;%0Aimport java.util.List;%0Aimport java.util.concurrent.Executors;%0A%0Aimport static org.fungsi.Matchers.isFailure;
 %0Aimp
@@ -1123,11 +1123,1027 @@
 o%22));%0A%09%7D
+%0A%0A    @Test%0A    public void testCollect_threaded() throws Exception %7B%0A        Worker worker = Workers.wrap(Executors.newFixedThreadPool(3));%0A%0A        Future%3CList%3CString%3E%3E fut = Futures.collect(Arrays.asList(%0A                worker.submit(sleepThenReturn(%22lol%22, 1000L)),%0A                worker.submit(sleepThenReturn(%22mdr%22, 1000L)),%0A                worker.submit(sleepThenReturn(%22lmao%22, 1000L))%0A        ));%0A%0A        assertThat(fut.get(Duration.ofMillis(1100L)), hasItems(%22lol%22, %22mdr%22, %22lmao%22));%0A        assertThat(fut, isSuccess());%0A    %7D%0A%0A    private static %3CT%3E UnsafeSupplier%3CT%3E sleepThenReturn(T val, long millis) %7B%0A        return () -%3E %7B Thread.sleep(millis); return val; %7D;%0A    %7D%0A%0A    @Test%0A    public void testCollect_withFailure() throws Exception %7B%0A        Future%3CList%3CString%3E%3E fut = Futures.collect(Arrays.asList(%0A                Futures.%3CString%3Efailure(new Exception()),%0A                Futures.success(%22mdr%22),%0A                Futures.success(%22lmao%22)%0A        ));%0A%0A        assertThat(fut, isFailure());%0A    %7D
 %0A%7D%0A

add new Futures#collect tests