1
1
package org .hamcrest ;
2
2
3
- import org .junit .Ignore ;
4
3
import org .junit .Test ;
5
4
6
5
import static org .hamcrest .AbstractMatcherTest .*;
10
9
*/
11
10
public class TypeSafeDiagnosingMatcherTest {
12
11
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" ) ;
18
17
}
19
18
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 () {
34
21
final Matcher <NotBuiltIn > matcher = new TypeSafeDiagnosingMatcher <NotBuiltIn >() {
35
22
@ Override
36
23
protected boolean matchesSafely (NotBuiltIn item , Description mismatchDescription ) {
@@ -42,30 +29,52 @@ protected boolean matchesSafely(NotBuiltIn item, Description mismatchDescription
42
29
43
30
assertMatches ("not built in" , matcher , new NotBuiltIn ());
44
31
assertDoesNotMatch ("other not built in" , (Matcher )matcher , new OtherNotBuiltIn ());
45
- }
32
+ }
46
33
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 () );
50
37
51
38
assertMatches ("not built in" , matcher , new NotBuiltIn ());
52
39
assertDoesNotMatch ("other not built in" , (Matcher )matcher , new OtherNotBuiltIn ());
53
40
}
54
41
42
+ @ Test public void
43
+ but_cannot_detect_generic_type_in_subclassed_matcher_using_reflection () {
44
+ final Matcher <NotBuiltIn > matcher = new SubMatcher <>();
55
45
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
+ }
58
49
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
+ }
61
56
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 {
63
73
public final String value = "not built in" ;
64
74
@ Override public String toString () { return "NotBuiltIn" ; }
65
- }
66
-
67
- public static class OtherNotBuiltIn {
75
+ }
68
76
69
- }
77
+ public static class OtherNotBuiltIn { // empty
78
+ }
70
79
71
80
}
0 commit comments