Skip to content

Commit

Permalink
attempt to improve the mismatch description of the hasItem/hasItems m…
Browse files Browse the repository at this point in the history
…atchers
  • Loading branch information
scarytom committed Jul 19, 2012
1 parent 8dd870c commit 96e113d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,31 @@ public IsCollectionContaining(Matcher<? super T> elementMatcher) {

@Override
protected boolean matchesSafely(Iterable<? super T> collection, Description mismatchDescription) {
boolean isPastFirst = false;
boolean empty = true;
for (Object item : collection) {
if (elementMatcher.matches(item)){
empty = false;
if (elementMatcher.matches(item)) {
return true;
}
}

if (empty) {
mismatchDescription.appendText("was an empty collection");
return false;
}

mismatchDescription.appendText("was a collection that did not contain ")
.appendDescriptionOf(elementMatcher)
.appendText(" -- mismatches were: [");
boolean isPastFirst = false;
for (Object item : collection) {
if (isPastFirst) {
mismatchDescription.appendText(", ");
}
elementMatcher.describeMismatch(item, mismatchDescription);
isPastFirst = true;
}
mismatchDescription.appendText("]");
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ public void testMatchesACollectionThatContainsAnElementMatchingTheGivenMatcher()

public void testDoesNotMatchCollectionThatDoesntContainAnElementMatchingTheGivenMatcher() {
final Matcher<Iterable<? super String>> matcher1 = hasItem(mismatchable("a"));
assertMismatchDescription("mismatched: b, mismatched: c", matcher1, asList("b", "c"));

assertMismatchDescription("was a collection that did not contain mismatchable: a -- mismatches were: [mismatched: b, mismatched: c]",
matcher1, asList("b", "c"));

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

public void testDoesNotMatchNull() {
Expand Down Expand Up @@ -84,9 +84,10 @@ public void testMatchesAllItemsInCollection() {
}

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

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

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

3 comments on commit 96e113d

@sf105
Copy link
Member

@sf105 sf105 commented on 96e113d Dec 14, 2012

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to be very careful about verbose mismatch warnings as they can build up to be unreadable, especially with collections. For the record, Description can handle printing collections with the right commas.

@scarytom
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the insight Steve. Any reason you picked up on this now? Has it bitten you today or something?

@sf105
Copy link
Member

@sf105 sf105 commented on 96e113d Dec 14, 2012

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was looking at the pull request for the generics of hasItem() and looking for comparisons. I suggest we have a little discuss sometime...

Please sign in to comment.