Skip to content

Commit

Permalink
Allow S to be inferred to supertype
Browse files Browse the repository at this point in the history
  • Loading branch information
runeflobakk authored and mrwilson committed Aug 1, 2020
1 parent 2c24316 commit fe3302f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
14 changes: 9 additions & 5 deletions src/main/java/uk/co/probablyfine/matchers/StreamMatchers.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@
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 <T,S extends BaseStream<T,S>> Matcher<S> empty() {
public static <T,S extends BaseStream<T,? extends S>> Matcher<S> empty() {
return new TypeSafeMatcher<S>() {

private Iterator<T> actualIterator;
Expand Down Expand Up @@ -50,7 +54,7 @@ protected void describeMismatchSafely(S item, Description description) {
* @see #startsWithLong
* @see #startsWithDouble
*/
public static <T,S extends BaseStream<T,S>> Matcher<S> equalTo(S expected) {
public static <T,S extends BaseStream<T,? extends S>> Matcher<S> equalTo(S expected) {
return new BaseStreamMatcher<T,S>() {
@Override
protected boolean matchesSafely(S actual) {
Expand Down Expand Up @@ -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;
}
Expand Down
15 changes: 15 additions & 0 deletions src/test/java/uk/co/probablyfine/matchers/StreamMatchersTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import org.hamcrest.Matcher;
import org.hamcrest.Matchers;
import org.junit.Test;
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;
Expand All @@ -15,6 +17,7 @@
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
Expand Down Expand Up @@ -215,6 +218,18 @@ public void equalTo_failureMessages() throws Exception {
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<Character> expectedStream = Stream.of('x', 'y', 'z');
assertThat("xyz", where(s -> s.chars().mapToObj(i -> (char) i), StreamMatchers.equalTo(expectedStream)));

BaseStream<Character, Stream<Character>> expectedBaseStream = Stream.of('x', 'y', 'z');
assertThat("xyz", where(s -> s.chars().mapToObj(i -> (char) i), StreamMatchers.equalTo(expectedBaseStream)));

DescribableFunction<String, BaseStream<Character, Stream<Character>>> characters = s -> s.chars().mapToObj(i -> (char) i);
assertThat("xyz", where(characters, StreamMatchers.equalTo(Stream.of('x', 'y', 'z'))));
}


@Test
public void contains_failureMessages() throws Exception {
Expand Down

0 comments on commit fe3302f

Please sign in to comment.