Skip to content

Commit

Permalink
Fix return type of StreamMatchers.empty & .equalTo
Browse files Browse the repository at this point in the history
Return type is parameterized to yield a Matcher of the specific
Stream-type, instead of just Matcher<BaseStream<_, _>>.
See unruly/java-8-matchers#18 for more
detailed discussion.
  • Loading branch information
facboy authored and runeflobakk committed Jul 29, 2020
1 parent 4966cee commit d5c58cf
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
14 changes: 7 additions & 7 deletions src/main/java/uk/co/probablyfine/matchers/StreamMatchers.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@

public class StreamMatchers {

public static <T,S extends BaseStream<T,S>> Matcher<BaseStream<T,S>> empty() {
return new TypeSafeMatcher<BaseStream<T, S>>() {
public static <T,S extends BaseStream<T,S>> Matcher<S> empty() {
return new TypeSafeMatcher<S>() {

private Iterator<T> actualIterator;

@Override
protected boolean matchesSafely(BaseStream<T, S> actual) {
protected boolean matchesSafely(S actual) {
actualIterator = actual.iterator();
return !actualIterator.hasNext();
}
Expand All @@ -30,7 +30,7 @@ public void describeTo(Description description) {
}

@Override
protected void describeMismatchSafely(BaseStream<T, S> item, Description description) {
protected void describeMismatchSafely(S item, Description description) {
description.appendText("A non empty Stream starting with ").appendValue(actualIterator.next());
}
};
Expand All @@ -50,10 +50,10 @@ protected void describeMismatchSafely(BaseStream<T, S> item, Description descrip
* @see #startsWithLong
* @see #startsWithDouble
*/
public static <T,S extends BaseStream<T,S>> Matcher<BaseStream<T,S>> equalTo(BaseStream<T, S> expected) {
return new BaseStreamMatcher<T,BaseStream<T,S>>() {
public static <T,S extends BaseStream<T,S>> Matcher<S> equalTo(S expected) {
return new BaseStreamMatcher<T,S>() {
@Override
protected boolean matchesSafely(BaseStream<T,S> actual) {
protected boolean matchesSafely(S actual) {
return remainingItemsEqual(expected.iterator(), actual.iterator());
}
};
Expand Down
17 changes: 12 additions & 5 deletions src/test/java/uk/co/probablyfine/matchers/StreamMatchersTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,16 @@
import org.hamcrest.Matchers;
import org.junit.Test;

import java.util.stream.*;

import static org.hamcrest.Matchers.*;
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;

public class StreamMatchersTest {
Expand Down Expand Up @@ -203,7 +210,7 @@ public void startsWithItemsIntStream_success() throws Exception {

@Test
public void equalTo_failureMessages() throws Exception {
Matcher<BaseStream<String, Stream<String>>> matcher = StreamMatchers.equalTo(Stream.of("a", "b", "c", "d", "e", "f", "g", "h"));
Matcher<Stream<String>> matcher = StreamMatchers.equalTo(Stream.of("a", "b", "c", "d", "e", "f", "g", "h"));
Stream<String> 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\"]");
}
Expand All @@ -219,7 +226,7 @@ public void contains_failureMessages() throws Exception {
@Test
public void equalToIntStream_failureMessages() throws Exception {
IntStream testData = IntStream.range(8, 10);
Matcher<BaseStream<Integer, IntStream>> matcher = StreamMatchers.equalTo(IntStream.range(0, 6));
Matcher<IntStream> matcher = StreamMatchers.equalTo(IntStream.range(0, 6));
Helper.testFailingMatcher(testData, matcher, "Stream of [<0>,<1>,<2>,<3>,<4>,<5>]", "Stream of [<8>,<9>]");
}

Expand Down

0 comments on commit d5c58cf

Please sign in to comment.