diff --git a/src/main/java/uk/co/probablyfine/matchers/StreamMatchers.java b/src/main/java/uk/co/probablyfine/matchers/StreamMatchers.java index 469b895..9907658 100644 --- a/src/main/java/uk/co/probablyfine/matchers/StreamMatchers.java +++ b/src/main/java/uk/co/probablyfine/matchers/StreamMatchers.java @@ -7,19 +7,23 @@ import java.util.Iterator; import java.util.LinkedList; import java.util.List; -import java.util.PrimitiveIterator; import java.util.Objects; -import java.util.stream.*; +import java.util.PrimitiveIterator; +import java.util.stream.BaseStream; +import java.util.stream.DoubleStream; +import java.util.stream.IntStream; +import java.util.stream.LongStream; +import java.util.stream.Stream; public class StreamMatchers { - public static > Matcher> empty() { - return new TypeSafeMatcher>() { + public static > Matcher empty() { + return new TypeSafeMatcher() { private Iterator actualIterator; @Override - protected boolean matchesSafely(BaseStream actual) { + protected boolean matchesSafely(S actual) { actualIterator = actual.iterator(); return !actualIterator.hasNext(); } @@ -30,7 +34,7 @@ public void describeTo(Description description) { } @Override - protected void describeMismatchSafely(BaseStream item, Description description) { + protected void describeMismatchSafely(S item, Description description) { description.appendText("A non empty Stream starting with ").appendValue(actualIterator.next()); } }; @@ -50,10 +54,10 @@ protected void describeMismatchSafely(BaseStream item, Description descrip * @see #startsWithLong * @see #startsWithDouble */ - public static > Matcher> equalTo(BaseStream expected) { - return new BaseStreamMatcher>() { + public static > Matcher equalTo(S expected) { + return new BaseStreamMatcher() { @Override - protected boolean matchesSafely(BaseStream actual) { + protected boolean matchesSafely(S actual) { return remainingItemsEqual(expected.iterator(), actual.iterator()); } }; @@ -366,7 +370,7 @@ public void describeTo(Description description) { * @see #startsWithDouble(double...) */ @SafeVarargs - public static > Matcher contains(Matcher... expectedMatchers) { + public static > Matcher contains(Matcher... expectedMatchers) { return new BaseMatcherStreamMatcher() { @Override @@ -389,7 +393,7 @@ protected boolean matchesSafely(S actual) { * @see #startsWithDouble(double...) */ @SafeVarargs - public static > Matcher contains(T... expected) { + public static > Matcher contains(T... expected) { return new BaseStreamMatcher() { @Override protected boolean matchesSafely(S actual) { @@ -912,7 +916,7 @@ public T next() { private static class IntArrayIterator implements PrimitiveIterator.OfInt { private final int[] expected; private int currentPos = 0; - + public IntArrayIterator(int... expected) { this.expected = expected; } diff --git a/src/test/java/uk/co/probablyfine/matchers/StreamMatchersTest.java b/src/test/java/uk/co/probablyfine/matchers/StreamMatchersTest.java index ed64b3e..29da849 100644 --- a/src/test/java/uk/co/probablyfine/matchers/StreamMatchersTest.java +++ b/src/test/java/uk/co/probablyfine/matchers/StreamMatchersTest.java @@ -3,11 +3,21 @@ import org.hamcrest.Matcher; import org.hamcrest.Matchers; import org.junit.Test; - -import java.util.stream.*; - -import static org.hamcrest.Matchers.*; +import uk.co.probablyfine.matchers.function.DescribableFunction; + +import java.util.stream.BaseStream; +import java.util.stream.DoubleStream; +import java.util.stream.IntStream; +import java.util.stream.LongStream; +import java.util.stream.Stream; + +import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.lessThanOrEqualTo; +import static org.hamcrest.Matchers.not; import static org.junit.Assert.assertThat; +import static uk.co.probablyfine.matchers.Java8Matchers.where; public class StreamMatchersTest { @Test @@ -17,7 +27,7 @@ public void equalTo_failureDifferingSingleItem() throws Exception { @Test public void contains_failureDifferingSingleItem() throws Exception { - assertThat(Stream.of("a"), is(Matchers.not(StreamMatchers.contains("b")))); + assertThat(Stream.of("a"), not(StreamMatchers.contains("b"))); } @Test @@ -27,7 +37,7 @@ public void equalTo_failureDifferingLength() throws Exception { @Test public void contains_failureDifferingLength() throws Exception { - assertThat(Stream.of("a"), is(Matchers.not(StreamMatchers.contains("a", "b")))); + assertThat(Stream.of("a"), not(StreamMatchers.contains("a", "b"))); } @Test @@ -37,7 +47,7 @@ public void equalTo_failureDifferingItems() throws Exception { @Test public void contains_failureDifferingItems() throws Exception { - assertThat(Stream.of("a","c"), is(Matchers.not(StreamMatchers.contains("a", "b")))); + assertThat(Stream.of("a","c"), not(StreamMatchers.contains("a", "b"))); } @Test @@ -203,11 +213,32 @@ public void startsWithItemsIntStream_success() throws Exception { @Test public void equalTo_failureMessages() throws Exception { - Matcher>> matcher = StreamMatchers.equalTo(Stream.of("a", "b", "c", "d", "e", "f", "g", "h")); + Matcher> matcher = StreamMatchers.equalTo(Stream.of("a", "b", "c", "d", "e", "f", "g", "h")); Stream testData = Stream.of("a", "b", "c", "d", "e"); Helper.testFailingMatcher(testData, matcher, "Stream of [\"a\",\"b\",\"c\",\"d\",\"e\",\"f\",\"g\",\"h\"]", "Stream of [\"a\",\"b\",\"c\",\"d\",\"e\"]"); } + @Test + public void equalTo_handles_types() { + Stream expectedStream = Stream.of('x', 'y', 'z'); + assertThat("xyz", where(s -> s.chars().mapToObj(i -> (char) i), StreamMatchers.equalTo(expectedStream))); + + BaseStream> expectedBaseStream = Stream.of('x', 'y', 'z'); + assertThat("xyz", where(s -> s.chars().mapToObj(i -> (char) i), StreamMatchers.equalTo(expectedBaseStream))); + + DescribableFunction>> characters = s -> s.chars().mapToObj(i -> (char) i); + assertThat("xyz", where(characters, StreamMatchers.equalTo(Stream.of('x', 'y', 'z')))); + } + + @Test + public void contains_handles_types() { + assertThat("xyz", where(s -> s.chars().mapToObj(i -> (char) i), StreamMatchers.contains('x', 'y', 'z'))); + + DescribableFunction>> characters = s -> s.chars().mapToObj(i -> (char) i); + assertThat("xyz", where(characters, StreamMatchers.contains('x', 'y', 'z'))); + assertThat("xyz", where(characters, not(StreamMatchers.contains('x', 'y')))); + } + @Test public void contains_failureMessages() throws Exception { @@ -219,7 +250,7 @@ public void contains_failureMessages() throws Exception { @Test public void equalToIntStream_failureMessages() throws Exception { IntStream testData = IntStream.range(8, 10); - Matcher> matcher = StreamMatchers.equalTo(IntStream.range(0, 6)); + Matcher matcher = StreamMatchers.equalTo(IntStream.range(0, 6)); Helper.testFailingMatcher(testData, matcher, "Stream of [<0>,<1>,<2>,<3>,<4>,<5>]", "Stream of [<8>,<9>]"); }