package com.github.jeysal.java.util.function;

import org.junit.Test;

import java.util.Optional;
import java.util.function.Supplier;

import static com.github.jeysal.java.util.function.SupplierUtils.firstPresent;
import static com.github.jeysal.java.util.function.SupplierUtils.trying;
import static org.junit.gen5.api.Assertions.assertEquals;

/**
 * @author Tim Seckinger
 * @since 13.03.2016
 */
public class SupplierUtilsTest {

    @Test
    public void testFirstPresent() {
        assertEquals(Optional.of(1),
                firstPresent(
                        (Supplier<Optional<Integer>>) Optional::empty,
                        () -> Optional.of(1)
                ).get()
        );
    }

    @Test
    public void testFirstPresentFirstMatch() {
        assertEquals(Optional.of(0),
                firstPresent(
                        (Supplier<Optional<Integer>>) () -> Optional.of(0),
                        () -> Optional.of(1)
                ).get()
        );
    }

    @Test
    public void testFirstPresentNoMatch() {
        assertEquals(Optional.empty(),
                firstPresent(
                        (Supplier<Optional<Integer>>) Optional::empty,
                        (Supplier<Optional<Integer>>) Optional::empty
                ).get()
        );
    }


    @Test
    public void testFirstPresentFallback() {
        assertEquals(1, firstPresent(
                () -> -1,
                Optional::empty,
                () -> Optional.of(1)
                ).get()
        );
    }

    @Test
    public void testFirstPresentFallbackFirstMatch() {
        assertEquals(0, firstPresent(
                () -> -1,
                () -> Optional.of(0),
                () -> Optional.of(1)
                ).get()
        );
    }

    @Test
    public void testFirstPresentFallbackNoMatch() {
        assertEquals(-1, firstPresent(
                () -> -1,
                Optional::empty,
                Optional::empty
                ).get()
        );
    }

    @Test(expected = RuntimeException.class)
    public void testFirstPresentFallbackNull() {
        firstPresent(
                (Supplier<Integer>) () -> null,
                Optional::empty,
                Optional::empty
        ).get();
    }


    @Test
    public void testTrying() {
        assertEquals(Optional.of(1),
                trying(
                        () -> 1
                ).get()
        );
    }

    @Test
    public void testTryingThrows() {
        assertEquals(Optional.empty(),
                trying(() -> {
                    throw new Exception();
                }).get()
        );
    }

    @Test
    public void testTryingNull() {
        assertEquals(Optional.empty(),
                trying(
                        () -> null
                ).get()
        );
    }
}

@@ -193,28 +193,17 @@
 erUtils.
-firstPresent
+*
 ;%0Aimport
@@ -214,65 +214,50 @@
 tic 
-com.github.jeysal.java.util.function.SupplierUtils.trying
+org.junit.gen5.api.Assertions.assertEquals
 ;%0Aim
@@ -298,28 +298,20 @@
 ertions.
-assertEquals
+fail
 ;%0A%0A/**%0A 
@@ -2787,10 +2787,1189 @@
 ;%0A    %7D%0A
+%0A    @Test%0A    public void testRethrowing() %7B%0A        assertEquals(1,%0A                rethrowing(%0A                        () -%3E 1%0A                ).get()%0A        );%0A    %7D%0A%0A    @Test%0A    public void testRethrowingThrows() %7B%0A        try %7B%0A            rethrowing(() -%3E %7B%0A                throw new Exception(%22asdf%22);%0A            %7D).get();%0A        %7D catch (RuntimeException e) %7B%0A            assertEquals(RuntimeException.class, e.getClass());%0A            assertEquals(Exception.class, e.getCause().getClass());%0A            assertEquals(%22asdf%22, e.getCause().getMessage());%0A            return;%0A        %7D%0A%0A        fail(%22Rethrowing supplier did not throw%22);%0A    %7D%0A%0A    @Test%0A    public void testRethrowingMapper() %7B%0A        try %7B%0A            rethrowing(() -%3E %7B%0A                throw new Exception(%22asdf%22);%0A            %7D, IllegalArgumentException::new).get();%0A        %7D catch (RuntimeException e) %7B%0A            assertEquals(IllegalArgumentException.class, e.getClass());%0A            assertEquals(Exception.class, e.getCause().getClass());%0A            assertEquals(%22asdf%22, e.getCause().getMessage());%0A            return;%0A        %7D%0A%0A        fail(%22Rethrowing supplier did not throw%22);%0A    %7D%0A%0A
 %7D%0A

add SupplierUtils.rethrowing tests