Skip to content

Commit e1e1890

Browse files
committed
TypeSafeDiagnosingMatcher can't detect generic types for subclass
1 parent fd7ab40 commit e1e1890

File tree

1 file changed

+42
-33
lines changed

1 file changed

+42
-33
lines changed
Lines changed: 42 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package org.hamcrest;
22

3-
import org.junit.Ignore;
43
import org.junit.Test;
54

65
import static org.hamcrest.AbstractMatcherTest.*;
@@ -10,27 +9,15 @@
109
*/
1110
public class TypeSafeDiagnosingMatcherTest {
1211

13-
private final TypeSafeDiagnosingMatcher stringMatcher = new TypeSafeDiagnosingMatcher<String>() {
14-
@Override
15-
protected boolean matchesSafely(String item, Description mismatchDescription) {
16-
mismatchDescription.appendText("mismatching");
17-
return false;
12+
@Test public void
13+
describesMismatches() {
14+
assertMismatchDescription("was null", STRING_MATCHER, null);
15+
assertMismatchDescription("was Character \"c\"", STRING_MATCHER, 'c');
16+
assertMismatchDescription("mismatching", STRING_MATCHER, "other");
1817
}
1918

20-
@Override
21-
public void describeTo(Description description) { }
22-
};
23-
24-
25-
@Test public void
26-
describesMismatches() {
27-
assertMismatchDescription("was null", stringMatcher, null);
28-
assertMismatchDescription("was Character \"c\"", stringMatcher, 'c');
29-
assertMismatchDescription("mismatching", stringMatcher, "other");
30-
}
31-
32-
@Test public void
33-
detects_non_builtin_types() {
19+
@Test public void
20+
detects_non_builtin_types() {
3421
final Matcher<NotBuiltIn> matcher = new TypeSafeDiagnosingMatcher<NotBuiltIn>() {
3522
@Override
3623
protected boolean matchesSafely(NotBuiltIn item, Description mismatchDescription) {
@@ -42,30 +29,52 @@ protected boolean matchesSafely(NotBuiltIn item, Description mismatchDescription
4229

4330
assertMatches("not built in", matcher, new NotBuiltIn());
4431
assertDoesNotMatch("other not built in", (Matcher)matcher, new OtherNotBuiltIn());
45-
}
32+
}
4633

47-
@Ignore("not yet") @Test public void
48-
detects_for_subtypes_of_matcher() {
49-
final Matcher<NotBuiltIn> matcher = new SubMatcher<>();
34+
@Test public void
35+
filters_type_for_subclassed_matcher_when_expected_type_passed_in() {
36+
final Matcher<NotBuiltIn> matcher = new SubMatcher<>(new NotBuiltIn());
5037

5138
assertMatches("not built in", matcher, new NotBuiltIn());
5239
assertDoesNotMatch("other not built in", (Matcher)matcher, new OtherNotBuiltIn());
5340
}
5441

42+
@Test public void
43+
but_cannot_detect_generic_type_in_subclassed_matcher_using_reflection() {
44+
final Matcher<NotBuiltIn> matcher = new SubMatcher<>();
5545

56-
public static class SubMatcher<T> extends TypeSafeDiagnosingMatcher<T> {
57-
@Override protected boolean matchesSafely(T item, Description mismatchDescription) { return true; }
46+
assertMatches("not built in", matcher, new NotBuiltIn());
47+
assertMatches("other not built in", (Matcher)matcher, new OtherNotBuiltIn());
48+
}
5849

59-
@Override public void describeTo(Description description) { description.appendText("sub type"); }
60-
}
50+
private static final TypeSafeDiagnosingMatcher STRING_MATCHER = new TypeSafeDiagnosingMatcher<String>() {
51+
@Override
52+
protected boolean matchesSafely(String item, Description mismatchDescription) {
53+
mismatchDescription.appendText("mismatching");
54+
return false;
55+
}
6156

62-
public static class NotBuiltIn {
57+
@Override
58+
public void describeTo(Description description) { }
59+
};
60+
61+
public static class SubMatcher<T> extends TypeSafeDiagnosingMatcher<T> {
62+
public SubMatcher() {
63+
super();
64+
}
65+
public SubMatcher(T expectedObject) {
66+
super(expectedObject.getClass());
67+
}
68+
@Override protected boolean matchesSafely(T item, Description mismatchDescription) { return true; }
69+
@Override public void describeTo(Description description) { description.appendText("sub type"); }
70+
}
71+
72+
public static class NotBuiltIn {
6373
public final String value = "not built in";
6474
@Override public String toString() { return "NotBuiltIn"; }
65-
}
66-
67-
public static class OtherNotBuiltIn {
75+
}
6876

69-
}
77+
public static class OtherNotBuiltIn { // empty
78+
}
7079

7180
}

0 commit comments

Comments
 (0)