Skip to content

Commit e878329

Browse files
author
smgfreeman
committed
Issue 74: Mismatch description for FeatureMatcher should use mismatchDescription of submatcher
1 parent dff7af8 commit e878329

File tree

2 files changed

+33
-22
lines changed

2 files changed

+33
-22
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ public FeatureMatcher(Matcher<? super U> subMatcher, String featureDescription,
3939
protected boolean matchesSafely(T actual, Description mismatchDescription) {
4040
final U featureValue = featureValueOf(actual);
4141
if (!subMatcher.matches(featureValue)) {
42-
mismatchDescription.appendText(featureName).appendText(" was ").appendValue(featureValue);
42+
mismatchDescription.appendText(featureName).appendText(" was ");
43+
subMatcher.describeMismatch(featureValue, mismatchDescription);
4344
return false;
4445
}
4546
return true;

hamcrest-unit-test/src/main/java/org/hamcrest/FeatureMatcherTest.java

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,42 @@
11
package org.hamcrest;
22

3-
import static org.hamcrest.AbstractMatcherTest.*;
4-
import static org.hamcrest.Matchers.equalTo;
3+
import static org.hamcrest.AbstractMatcherTest.assertDescription;
4+
import static org.hamcrest.AbstractMatcherTest.assertMatches;
5+
import static org.hamcrest.AbstractMatcherTest.assertMismatchDescription;
56
import junit.framework.TestCase;
67

8+
import org.hamcrest.core.IsEqual;
9+
710
public class FeatureMatcherTest extends TestCase {
811
private final FeatureMatcher<Thingy, String> resultMatcher = resultMatcher();
912

10-
public void testMatchesPartOfAnObject() {
11-
assertMatches("feature", resultMatcher, new Thingy("bar"));
12-
assertDescription("Thingy with result \"bar\"", resultMatcher);
13-
}
13+
public void testMatchesPartOfAnObject() {
14+
assertMatches("feature", resultMatcher, new Thingy("bar"));
15+
assertDescription("Thingy with result \"bar\"", resultMatcher);
16+
}
17+
18+
public void testMismatchesPartOfAnObject() {
19+
assertMismatchDescription("result was mismatch-description", resultMatcher, new Thingy("foo"));
20+
}
21+
22+
public void testDoesNotThrowNullPointerException() {
23+
assertMismatchDescription("was null", resultMatcher, null);
24+
}
25+
26+
public void testDoesNotThrowClassCastException() {
27+
resultMatcher.matches(new ShouldNotMatch());
28+
StringDescription mismatchDescription = new StringDescription();
29+
resultMatcher.describeMismatch(new ShouldNotMatch(), mismatchDescription);
30+
assertEquals("was <ShouldNotMatch>", mismatchDescription.toString());
31+
}
1432

15-
public void testMismatchesPartOfAnObject() {
16-
assertMismatchDescription("result was \"foo\"", resultMatcher, new Thingy("foo"));
33+
34+
public static class Match extends IsEqual<String> {
35+
public Match(String equalArg) { super(equalArg); }
36+
@Override public void describeMismatch(Object item, Description description) {
37+
description.appendText("mismatch-description");
1738
}
18-
19-
public void testDoesNotThrowNullPointerException() {
20-
assertMismatchDescription("was null", resultMatcher, null);
21-
}
22-
23-
public void testDoesNotThrowClassCastException() {
24-
resultMatcher.matches(new ShouldNotMatch());
25-
StringDescription mismatchDescription = new StringDescription();
26-
resultMatcher.describeMismatch(new ShouldNotMatch(), mismatchDescription);
27-
assertEquals("was <ShouldNotMatch>", mismatchDescription.toString());
28-
}
29-
39+
}
3040
public static class Thingy {
3141
private final String result;
3242

@@ -44,7 +54,7 @@ public static class ShouldNotMatch {
4454
}
4555

4656
private static FeatureMatcher<Thingy, String> resultMatcher() {
47-
return new FeatureMatcher<Thingy, String>(equalTo("bar"), "Thingy with result", "result") {
57+
return new FeatureMatcher<Thingy, String>(new Match("bar"), "Thingy with result", "result") {
4858
@Override
4959
public String featureValueOf(Thingy actual) {
5060
return actual.getResult();

0 commit comments

Comments
 (0)