-
Notifications
You must be signed in to change notification settings - Fork 379
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Size matchers should provide insightful debugging information
Now, matchers like hasSize and similar, in case of mismatch, print also the actual values contained in the collection being tested. Closes #174
- Loading branch information
Showing
9 changed files
with
136 additions
and
34 deletions.
There are no files selected for viewing
12 changes: 9 additions & 3 deletions
12
hamcrest/src/main/java/org/hamcrest/collection/IsArrayWithSize.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 8 additions & 3 deletions
11
hamcrest/src/main/java/org/hamcrest/collection/IsMapWithSize.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
hamcrest/src/main/java/org/hamcrest/collection/SizeMatcher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package org.hamcrest.collection; | ||
|
||
import org.hamcrest.Description; | ||
import org.hamcrest.FeatureMatcher; | ||
import org.hamcrest.Matcher; | ||
|
||
/** | ||
* Base class for Matchers matching a feature on a collection-like object. | ||
* <p> | ||
* Provides insightful debugging information in case the collection does not match, | ||
* by printing the actual values in the mismatch description. | ||
* | ||
* @param <T> the type of the collection | ||
* @param <E> the type of the elements in the collection | ||
*/ | ||
public abstract class SizeMatcher<T, E> extends FeatureMatcher<T, Integer> { | ||
|
||
public SizeMatcher(Matcher<? super Integer> sizeMatcher, String type) { | ||
super(sizeMatcher, indefiniteArticle(type) + " " + type + " with size", type + " size"); | ||
} | ||
|
||
@Override | ||
protected abstract Integer featureValueOf(T actual); | ||
|
||
@Override | ||
protected boolean matchesSafely(T actual, Description mismatch) { | ||
boolean matchesSafely = super.matchesSafely(actual, mismatch); | ||
if (!matchesSafely) { | ||
mismatch.appendText(". Actual values: "); | ||
mismatch.appendValueList("[", ", ", "]", actualValues(actual)); | ||
} | ||
return matchesSafely; | ||
} | ||
|
||
protected abstract Iterable<? extends E> actualValues(T actual); | ||
|
||
private static String indefiniteArticle(String string) { | ||
// a naive algorithm.. | ||
switch (string.charAt(0)) { | ||
case 'a': | ||
case 'e': | ||
case 'i': | ||
case 'o': | ||
case 'u': | ||
return "an"; | ||
default: | ||
return "a"; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters