|
| 1 | +package com.jnape.palatable.lambda.functions; |
| 2 | + |
| 3 | +import com.jnape.palatable.lambda.MonadicFunction; |
| 4 | +import com.jnape.palatable.lambda.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.builtin.monadic.Always.always; |
| 12 | +import static com.jnape.palatable.lambda.functions.All.all; |
| 13 | +import static com.jnape.palatable.lambda.functions.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 AllTest { |
| 20 | + |
| 21 | + private static final MonadicFunction<? super Number, Boolean> EVEN = new Predicate<Number>() { |
| 22 | + @Override |
| 23 | + public Boolean apply(Number number) { |
| 24 | + return number.doubleValue() % 2 == 0; |
| 25 | + } |
| 26 | + }; |
| 27 | + |
| 28 | + @TestTraits({EmptyIterableSupport.class}) |
| 29 | + public MonadicFunction<Iterable<Object>, Boolean> createTestSubject() { |
| 30 | + return all(always(true)); |
| 31 | + } |
| 32 | + |
| 33 | + @Test |
| 34 | + public void trueIfAllElementsMatchPredicate() { |
| 35 | + Iterable<Integer> numbers = asList(2, 4, 6, 8); |
| 36 | + assertThat(all(EVEN, numbers), is(true)); |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + public void falseIfAnyElementsFailPredicate() { |
| 41 | + Iterable<Integer> numbers = asList(1, 2, 4, 6, 8); |
| 42 | + assertThat(all(EVEN, numbers), is(false)); |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + public void terminatesIterationImmediatelyUponPredicateFailure() { |
| 47 | + assertThat(all(EVEN, repeat(1)), is(false)); |
| 48 | + } |
| 49 | +} |
0 commit comments