Skip to content

Commit 34dc394

Browse files
committed
Reworked subsequence to avoid pulling in whole iterable, and more detail in mismatch
1 parent cd0b713 commit 34dc394

3 files changed

Lines changed: 71 additions & 33 deletions

File tree

‎hamcrest-core/src/test/java/org/hamcrest/AbstractMatcherTest.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ public static void assertDescription(String expected, Matcher<?> matcher) {
3737
}
3838

3939
public static <T> void assertMismatchDescription(String expected, Matcher<? super T> matcher, T arg) {
40-
Assert.assertFalse("Precondition: Matcher should not match item.", matcher.matches(arg));
41-
Assert.assertEquals("Expected mismatch description", expected, mismatchDescription(matcher, arg));
40+
Assert.assertFalse("Precondition for (" + expected + "): Matcher should not match item.", matcher.matches(arg));
41+
Assert.assertEquals("Mismatch description", expected, mismatchDescription(matcher, arg));
4242
}
4343

4444
public static void assertNullSafe(Matcher<?> matcher) {

‎hamcrest-library/src/main/java/org/hamcrest/collection/HasSubsequence.java‎

Lines changed: 63 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import org.hamcrest.TypeSafeDiagnosingMatcher;
66

77
import java.util.ArrayList;
8+
import java.util.Iterator;
89
import java.util.List;
910

1011
/**
@@ -19,32 +20,13 @@ public HasSubsequence(List<Matcher<? super T>> matchers) {
1920
}
2021

2122
@Override
22-
public boolean matchesSafely(Iterable<? extends T> subsequenceToMatch,
23-
Description mismatchDescription)
24-
{
25-
List<T> subsequenceToMatchList = new ArrayList<>();
26-
for (T e : subsequenceToMatch) {
27-
subsequenceToMatchList.add(e);
28-
}
23+
public boolean matchesSafely(Iterable<? extends T> actual,
24+
Description mismatchDescription) {
25+
final Cursor<T> cursor = new Cursor<>(actual.iterator(), matchers.iterator());
2926

30-
for (int i = 0; i <= subsequenceToMatchList.size() - matchers.size(); i++) {
31-
boolean allMatchersMatched = true;
32-
for (int j = 0; j < matchers.size(); j++) {
33-
Matcher<? super T> matcher = matchers.get(j);
34-
if (!matcher.matches(subsequenceToMatchList.get(i + j))) {
35-
allMatchersMatched = false;
36-
break;
37-
}
38-
}
39-
if (allMatchersMatched) {
40-
return true;
41-
}
42-
}
43-
44-
mismatchDescription
45-
.appendText("could not find subsequence in ")
46-
.appendValueList("[", ", ", "]", subsequenceToMatch);
47-
return false;
27+
final boolean matched = cursor.foundFirst() && cursor.foundRemainder();
28+
if (!matched) cursor.describeNotFound(mismatchDescription);
29+
return matched;
4830
}
4931

5032
@Override
@@ -53,4 +35,60 @@ public void describeTo(Description description) {
5335
.appendText("iterable contains subsequence matching ")
5436
.appendList("[", ", ", "]", matchers);
5537
}
38+
39+
static private class Cursor<T> {
40+
private final List<T> viewedItems = new ArrayList<>();
41+
private Matcher<? super T> currentMatcher;
42+
private final Iterator<? extends T> items;
43+
private final Iterator<Matcher<? super T>> remainingMatchers;
44+
45+
public Cursor(Iterator<? extends T> items, Iterator<Matcher<? super T>> remainingMatchers) {
46+
this.items = items;
47+
this.remainingMatchers = remainingMatchers;
48+
}
49+
50+
boolean foundFirst() {
51+
final Matcher<? super T> firstMatcher = nextMatcher();
52+
while (items.hasNext()) {
53+
if (firstMatcher.matches(nextItem())) return true;
54+
}
55+
return false;
56+
}
57+
58+
59+
boolean foundRemainder() {
60+
while (remainingMatchers.hasNext()) {
61+
if (! nextItemMatches(nextMatcher())) {
62+
return false;
63+
}
64+
}
65+
return true;
66+
}
67+
68+
private boolean nextItemMatches(Matcher<? super T> matcher) {
69+
return items.hasNext() && matcher.matches(nextItem());
70+
}
71+
72+
private T nextItem() {
73+
final T next = items.next();
74+
viewedItems.add(next);
75+
return next;
76+
}
77+
78+
private Matcher<? super T> nextMatcher() {
79+
final Matcher<? super T> matcher = remainingMatchers.next();
80+
currentMatcher = matcher;
81+
return matcher;
82+
}
83+
84+
private void describeNotFound(Description mismatch) {
85+
mismatch.appendText("subsequence not in ")
86+
.appendValueList("[", ", ", "]", viewedItems);
87+
88+
if (null != currentMatcher) {
89+
mismatch.appendText(" because no match for ");
90+
mismatch.appendDescriptionOf(currentMatcher);
91+
}
92+
}
93+
}
5694
}

‎hamcrest-library/src/test/java/org/hamcrest/collection/HasSubsequenceTest.java‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,28 +35,28 @@ public void testMatchesIterableWithExcessElements() throws Exception {
3535

3636
public void testMismatchesIfItemsMissing() throws Exception {
3737
assertMismatchDescription(
38-
"could not find subsequence in [<WithValue 3>]",
38+
"subsequence not in [<WithValue 3>] because no match for value with <4>",
3939
hasSubsequence(value(4)), asList(withValue(3)));
4040
assertMismatchDescription(
41-
"could not find subsequence in [<WithValue 1>, <WithValue 2>, <WithValue 4>]",
41+
"subsequence not in [<WithValue 1>, <WithValue 2>, <WithValue 4>] because no match for value with <3>",
4242
contains123, asList(withValue(1), withValue(2), withValue(4)));
4343
}
4444

4545

4646
public void testMismatchesForAdditionalIntermediateItems() throws Exception {
47-
assertMismatchDescription("could not find subsequence in [<1>, <2>, <3>]",
48-
hasSubsequence(1, 3), asList(1, 2, 3));
47+
assertMismatchDescription("subsequence not in [<1>, <2>] because no match for <3>",
48+
hasSubsequence(1, 3), asList(1, 2, 4));
4949
}
5050

5151

5252
public void testMismatchesWhenTooFewItems() throws Exception {
5353
assertMismatchDescription(
54-
"could not find subsequence in [<WithValue 1>, <WithValue 2>]",
54+
"subsequence not in [<WithValue 1>, <WithValue 2>] because no match for value with <3>",
5555
contains123, asList(withValue(1), withValue(2)));
5656
}
5757

5858
public void testDoesNotMatchEmptyCollection() throws Exception {
59-
assertMismatchDescription("could not find subsequence in []",
59+
assertMismatchDescription("subsequence not in [] because no match for value with <4>",
6060
hasSubsequence(value(4)), new ArrayList<WithValue>());
6161
}
6262

0 commit comments

Comments
 (0)