Skip to content

Commit 96e113d

Browse files
committed
attempt to improve the mismatch description of the hasItem/hasItems matchers
1 parent 8dd870c commit 96e113d

2 files changed

Lines changed: 22 additions & 7 deletions

File tree

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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,31 @@ public IsCollectionContaining(Matcher<? super T> elementMatcher) {
2020

2121
@Override
2222
protected boolean matchesSafely(Iterable<? super T> collection, Description mismatchDescription) {
23-
boolean isPastFirst = false;
23+
boolean empty = true;
2424
for (Object item : collection) {
25-
if (elementMatcher.matches(item)){
25+
empty = false;
26+
if (elementMatcher.matches(item)) {
2627
return true;
2728
}
29+
}
30+
31+
if (empty) {
32+
mismatchDescription.appendText("was an empty collection");
33+
return false;
34+
}
35+
36+
mismatchDescription.appendText("was a collection that did not contain ")
37+
.appendDescriptionOf(elementMatcher)
38+
.appendText(" -- mismatches were: [");
39+
boolean isPastFirst = false;
40+
for (Object item : collection) {
2841
if (isPastFirst) {
2942
mismatchDescription.appendText(", ");
3043
}
3144
elementMatcher.describeMismatch(item, mismatchDescription);
3245
isPastFirst = true;
3346
}
47+
mismatchDescription.appendText("]");
3448
return false;
3549
}
3650

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ public void testMatchesACollectionThatContainsAnElementMatchingTheGivenMatcher()
3232

3333
public void testDoesNotMatchCollectionThatDoesntContainAnElementMatchingTheGivenMatcher() {
3434
final Matcher<Iterable<? super String>> matcher1 = hasItem(mismatchable("a"));
35-
assertMismatchDescription("mismatched: b, mismatched: c", matcher1, asList("b", "c"));
36-
35+
assertMismatchDescription("was a collection that did not contain mismatchable: a -- mismatches were: [mismatched: b, mismatched: c]",
36+
matcher1, asList("b", "c"));
3737

3838
final Matcher<Iterable<? super String>> matcher2 = hasItem(equalTo("a"));
39-
assertMismatchDescription("", matcher2, new ArrayList<String>());
39+
assertMismatchDescription("was an empty collection", matcher2, new ArrayList<String>());
4040
}
4141

4242
public void testDoesNotMatchNull() {
@@ -84,9 +84,10 @@ public void testMatchesAllItemsInCollection() {
8484
}
8585

8686
public void testReportsMismatchWithAReadableDescription() {
87-
final Matcher<Iterable<Integer>> matcher = hasItems(1, 4);
87+
final Matcher<Iterable<Integer>> matcher = hasItems(3, 4);
8888

89-
assertMismatchDescription("a collection containing <4> was <1>, was <2>, was <3>", matcher, asList(1, 2, 3));
89+
assertMismatchDescription("a collection containing <4> was a collection that did not contain <4> -- mismatches were: [was <1>, was <2>, was <3>]",
90+
matcher, asList(1, 2, 3));
9091
}
9192

9293
private static Matcher<? super String> mismatchable(final String string) {

0 commit comments

Comments
 (0)