Skip to content

Commit 5992038

Browse files
author
smgfreeman
committed
Better mismatch reporting in IsCollectionContaining
1 parent 312617b commit 5992038

File tree

2 files changed

+33
-13
lines changed

2 files changed

+33
-13
lines changed

hamcrest-core/src/main/java/org/hamcrest/core/IsCollectionContaining.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,30 +9,31 @@
99
import org.hamcrest.Description;
1010
import org.hamcrest.Factory;
1111
import org.hamcrest.Matcher;
12-
import org.hamcrest.TypeSafeMatcher;
12+
import org.hamcrest.TypeSafeDiagnosingMatcher;
1313

14-
public class IsCollectionContaining<T> extends TypeSafeMatcher<Iterable<? super T>> {
14+
public class IsCollectionContaining<T> extends TypeSafeDiagnosingMatcher<Iterable<? super T>> {
1515
private final Matcher<? super T> elementMatcher;
1616

1717
public IsCollectionContaining(Matcher<? super T> elementMatcher) {
1818
this.elementMatcher = elementMatcher;
1919
}
2020

2121
@Override
22-
public boolean matchesSafely(Iterable<? super T> collection) {
22+
protected boolean matchesSafely(Iterable<? super T> collection, Description mismatchDescription) {
23+
boolean isPastFirst = false;
2324
for (Object item : collection) {
2425
if (elementMatcher.matches(item)){
2526
return true;
2627
}
28+
if (isPastFirst) {
29+
mismatchDescription.appendText(", ");
30+
}
31+
elementMatcher.describeMismatch(item, mismatchDescription);
32+
isPastFirst = true;
2733
}
2834
return false;
2935
}
3036

31-
@Override
32-
public void describeMismatchSafely(Iterable<? super T> item, Description mismatchDescription) {
33-
mismatchDescription.appendValue(item);
34-
}
35-
3637
public void describeTo(Description description) {
3738
description
3839
.appendText("a collection containing ")
@@ -71,4 +72,5 @@ public static <T> Matcher<Iterable<T>> hasItems(T... elements) {
7172

7273
return allOf(all);
7374
}
75+
7476
}

hamcrest-unit-test/src/main/java/org/hamcrest/collection/IsCollectionContainingTest.java

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111
import java.util.Set;
1212

1313
import org.hamcrest.AbstractMatcherTest;
14+
import org.hamcrest.Description;
1415
import org.hamcrest.Matcher;
16+
import org.hamcrest.TypeSafeDiagnosingMatcher;
1517
import org.hamcrest.core.IsCollectionContaining;
1618
import org.hamcrest.core.IsEqual;
1719

@@ -29,14 +31,12 @@ public void testMatchesACollectionThatContainsAnElementMatchingTheGivenMatcher()
2931
}
3032

3133
public void testDoesNotMatchCollectionThatDoesntContainAnElementMatchingTheGivenMatcher() {
32-
final Matcher<Iterable<? super String>> matcher1 = hasItem(equalTo("a"));
33-
assertDoesNotMatch("should not match list that doesn't contain 'a'",
34-
matcher1, asList("b", "c"));
34+
final Matcher<Iterable<? super String>> matcher1 = hasItem(mismatchable("a"));
35+
assertMismatchDescription("mismatched: b, mismatched: c", matcher1, asList("b", "c"));
3536

3637

3738
final Matcher<Iterable<? super String>> matcher2 = hasItem(equalTo("a"));
38-
assertDoesNotMatch("should not match the empty list",
39-
matcher2, new ArrayList<String>());
39+
assertMismatchDescription("", matcher2, new ArrayList<String>());
4040
}
4141

4242
public void testDoesNotMatchNull() {
@@ -82,5 +82,23 @@ public void testMatchesAllItemsInCollection() {
8282
matcher5,
8383
asList("e", "c", "b", "d")); // 'a' missing
8484
}
85+
86+
87+
private Matcher<? super String> mismatchable(final String string) {
88+
return new TypeSafeDiagnosingMatcher<String>() {
89+
@Override
90+
protected boolean matchesSafely(String item, Description mismatchDescription) {
91+
if (string.equals(item))
92+
return true;
93+
94+
mismatchDescription.appendText("mismatched: " + item);
95+
return false;
96+
}
97+
98+
public void describeTo(Description description) {
99+
description.appendText("mismatchable: " + string);
100+
}
101+
};
102+
}
85103
}
86104

0 commit comments

Comments
 (0)