|
| 1 | +package fj.function; |
| 2 | + |
| 3 | +import fj.Equal; |
| 4 | +import fj.F; |
| 5 | +import fj.P; |
| 6 | +import fj.P1; |
| 7 | +import fj.data.List; |
| 8 | +import org.junit.Test; |
| 9 | + |
| 10 | +import static fj.data.Option.none; |
| 11 | +import static fj.data.Option.some; |
| 12 | +import static fj.function.Visitor.*; |
| 13 | +import static org.hamcrest.core.Is.is; |
| 14 | +import static org.junit.Assert.assertThat; |
| 15 | + |
| 16 | +public class VisitorTest { |
| 17 | + @Test |
| 18 | + public void testFindFirst() { |
| 19 | + assertThat(findFirst(List.list(none(), some(1), none()), () -> -1), is(1)); |
| 20 | + } |
| 21 | + |
| 22 | + @Test |
| 23 | + public void testFindFirstDef() { |
| 24 | + assertThat(findFirst(List.list(none(), none(), none()), () -> -1), is(-1)); |
| 25 | + } |
| 26 | + |
| 27 | + @Test |
| 28 | + public void testNullableFindFirst() { |
| 29 | + assertThat(nullablefindFirst(List.list(null, 1, null), () -> -1), is(1)); |
| 30 | + } |
| 31 | + |
| 32 | + @Test |
| 33 | + public void testNullableFindFirstDef() { |
| 34 | + assertThat(nullablefindFirst(List.list(null, null, null), () -> -1), is(-1)); |
| 35 | + } |
| 36 | + |
| 37 | + @Test |
| 38 | + public void testVisitor() { |
| 39 | + assertThat(visitor(List.list(i -> some(2 * i)), () -> -1, 10), is(20)); |
| 40 | + } |
| 41 | + |
| 42 | + @Test |
| 43 | + public void testVisitorDef() { |
| 44 | + assertThat(visitor(List.list(i -> none()), () -> "foo", 10), is("foo")); |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + public void testNullableVisitor() { |
| 49 | + assertThat(nullableVisitor(List.list(i -> 2 * i), () -> -1, 10), is(20)); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + public void testNullableVisitorDef() { |
| 54 | + assertThat(nullableVisitor(List.list(i -> null), () -> "foo", 10), is("foo")); |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + public void testAssociation() { |
| 59 | + final F<String, F<Integer, String>> a = association(List.list(P.p(1, "one"), P.p(2, "two")), Equal.intEqual); |
| 60 | + assertThat(a.f("foo").f(2), is("two")); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + public void testAssociationDef() { |
| 65 | + final F<String, F<Integer, String>> a = association(List.list(P.p(1, "one"), P.p(2, "two")), Equal.intEqual); |
| 66 | + assertThat(a.f("foo").f(3), is("foo")); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + public void testAssociationLazy() { |
| 71 | + final F<P1<String>, F<Integer, String>> a = associationLazy(List.list(P.p(1, "one"), P.p(2, "two")), Equal.intEqual); |
| 72 | + assertThat(a.f(P.p("foo")).f(2), is("two")); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + public void testAssociationLazyDef() { |
| 77 | + final F<P1<String>, F<Integer, String>> a = associationLazy(List.list(P.p(1, "one"), P.p(2, "two")), Equal.intEqual); |
| 78 | + assertThat(a.f(P.p("foo")).f(3), is("foo")); |
| 79 | + } |
| 80 | +} |
0 commit comments