|
| 1 | +package com.jnape.palatable.lambda.iterators; |
| 2 | + |
| 3 | +import com.jnape.palatable.lambda.MonadicFunction; |
| 4 | +import org.junit.Test; |
| 5 | + |
| 6 | +import static com.jnape.palatable.lambda.builtin.monadic.Identity.id; |
| 7 | +import static com.jnape.palatable.lambda.functions.Map.map; |
| 8 | +import static com.jnape.palatable.lambda.staticfactory.IterableFactory.iterable; |
| 9 | +import static org.junit.Assert.assertThat; |
| 10 | +import static testsupport.Mocking.mockIterable; |
| 11 | +import static testsupport.matchers.IterableMatcher.isEmpty; |
| 12 | +import static testsupport.matchers.IterableMatcher.iterates; |
| 13 | +import static testsupport.matchers.ZeroInvocationsMatcher.wasNeverInteractedWith; |
| 14 | + |
| 15 | +public class MappingIteratorTest { |
| 16 | + |
| 17 | + @Test |
| 18 | + public void mapsInputsIntoOutputs() { |
| 19 | + MonadicFunction<String, Integer> length = new MonadicFunction<String, Integer>() { |
| 20 | + @Override |
| 21 | + public Integer apply(String string) { |
| 22 | + return string.length(); |
| 23 | + } |
| 24 | + }; |
| 25 | + assertThat( |
| 26 | + map(length, iterable("one", "two", "three")), |
| 27 | + iterates(3, 3, 5) |
| 28 | + ); |
| 29 | + } |
| 30 | + |
| 31 | + @Test |
| 32 | + @SuppressWarnings("unchecked") |
| 33 | + public void worksOnEmptyIterables() { |
| 34 | + assertThat(map(id(), iterable()), isEmpty()); |
| 35 | + } |
| 36 | + |
| 37 | + @Test |
| 38 | + public void defersIteration() { |
| 39 | + Iterable<Object> iterable = mockIterable(); |
| 40 | + map(id(), iterable); |
| 41 | + assertThat(iterable, wasNeverInteractedWith()); |
| 42 | + assertThat(iterable.iterator(), wasNeverInteractedWith()); |
| 43 | + } |
| 44 | +} |
0 commit comments