Skip to content

Commit bc04aff

Browse files
author
nat.pryce
committed
Fix for issue 134: DescribedAs does not delegate describeMismatch
1 parent 473f16a commit bc04aff

2 files changed

Lines changed: 26 additions & 9 deletions

File tree

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
*/
33
package org.hamcrest.core;
44

5-
import java.util.regex.Pattern;
6-
75
import org.hamcrest.BaseMatcher;
86
import org.hamcrest.Description;
97
import org.hamcrest.Factory;
108
import org.hamcrest.Matcher;
119

10+
import java.util.regex.Pattern;
11+
1212
/**
1313
* Provides a custom description to another matcher.
1414
*/
@@ -44,6 +44,11 @@ public void describeTo(Description description) {
4444
description.appendText(descriptionTemplate.substring(textStart));
4545
}
4646
}
47+
48+
@Override
49+
public void describeMismatch(Object item, Description description) {
50+
matcher.describeMismatch(item, description);
51+
}
4752

4853
/**
4954
* Wraps an existing matcher and overrides the description when it fails.

hamcrest-unit-test/src/main/java/org/hamcrest/core/DescribedAsTest.java

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,18 @@
22
*/
33
package org.hamcrest.core;
44

5+
import org.hamcrest.AbstractMatcherTest;
6+
import org.hamcrest.Matcher;
7+
import org.hamcrest.StringDescription;
8+
59
import static org.hamcrest.core.DescribedAs.describedAs;
610
import static org.hamcrest.core.IsAnything.anything;
11+
import static org.hamcrest.core.IsEqual.equalTo;
712
import static org.hamcrest.core.IsNot.not;
813

9-
import org.hamcrest.AbstractMatcherTest;
10-
import org.hamcrest.Matcher;
11-
1214
public class DescribedAsTest extends AbstractMatcherTest {
13-
@Override protected Matcher<?> createMatcher() {
15+
@Override
16+
protected Matcher<?> createMatcher() {
1417
return describedAs("irrelevant", anything());
1518
}
1619

@@ -24,16 +27,25 @@ public void testOverridesDescriptionOfOtherMatcherWithThatPassedToConstructor()
2427

2528
public void testAppendsValuesToDescription() {
2629
Matcher<?> m = describedAs("value 1 = %0, value 2 = %1",
27-
anything(), 33, 97);
28-
30+
anything(), 33, 97);
31+
2932
assertDescription("value 1 = <33>, value 2 = <97>", m);
3033
}
31-
34+
3235
public void testDelegatesMatchingToAnotherMatcher() {
3336
Matcher<Object> m1 = describedAs("irrelevant", anything());
3437
Matcher<Object> m2 = describedAs("irrelevant", not(anything()));
3538

3639
assertTrue(m1.matches(new Object()));
3740
assertFalse(m2.matches("hi"));
3841
}
42+
43+
public void testDelegatesMismatchDescriptionToAnotherMatcher() {
44+
Matcher<Integer> m1 = describedAs("irrelevant", equalTo(2));
45+
46+
StringDescription description = new StringDescription();
47+
m1.describeMismatch(1, description);
48+
49+
assertEquals("was <1>", description.toString());
50+
}
3951
}

0 commit comments

Comments
 (0)