|
| 1 | +package com.jnape.palatable.lambda.functions.builtin.dyadic; |
| 2 | + |
| 3 | +import com.jnape.palatable.lambda.functions.MonadicFunction; |
| 4 | +import com.jnape.palatable.lambda.functions.specialized.Predicate; |
| 5 | +import com.jnape.palatable.traitor.annotations.TestTraits; |
| 6 | +import com.jnape.palatable.traitor.runners.Traits; |
| 7 | +import org.junit.Test; |
| 8 | +import org.junit.runner.RunWith; |
| 9 | +import testsupport.traits.EmptyIterableSupport; |
| 10 | + |
| 11 | +import static com.jnape.palatable.lambda.functions.builtin.dyadic.Any.any; |
| 12 | +import static com.jnape.palatable.lambda.functions.builtin.monadic.Always.always; |
| 13 | +import static com.jnape.palatable.lambda.functions.builtin.monadic.Repeat.repeat; |
| 14 | +import static java.util.Arrays.asList; |
| 15 | +import static org.hamcrest.core.Is.is; |
| 16 | +import static org.junit.Assert.assertThat; |
| 17 | + |
| 18 | +@RunWith(Traits.class) |
| 19 | +public class AnyTest { |
| 20 | + |
| 21 | + public static final MonadicFunction<? super Integer, Boolean> EVEN = new Predicate<Integer>() { |
| 22 | + @Override |
| 23 | + public Boolean apply(Integer integer) { |
| 24 | + return integer % 2 == 0; |
| 25 | + } |
| 26 | + }; |
| 27 | + |
| 28 | + @TestTraits({EmptyIterableSupport.class}) |
| 29 | + public MonadicFunction<Iterable<Object>, Boolean> createTestSubject() { |
| 30 | + return any(always(true)); |
| 31 | + } |
| 32 | + |
| 33 | + @Test |
| 34 | + public void trueIfAnyElementsMatchPredicate() { |
| 35 | + assertThat(any(EVEN, asList(1, 2)), is(true)); |
| 36 | + } |
| 37 | + |
| 38 | + @Test |
| 39 | + public void falseIfNoElementsMatchPredicate() { |
| 40 | + assertThat(any(EVEN, asList(1, 3, 5)), is(false)); |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + public void terminatesIterationImmediatelyUponPredicateSuccess() { |
| 45 | + assertThat(any(EVEN, repeat(2)), is(true)); |
| 46 | + } |
| 47 | +} |
0 commit comments