11package org .hamcrest ;
22
3+ import org .junit .Ignore ;
34import org .junit .Test ;
45
5- import static org .hamcrest .AbstractMatcherTest .assertMismatchDescription ;
6+ import static org .hamcrest .AbstractMatcherTest .* ;
67
78/**
89 * @author Steve Freeman 2016 http://www.hamcrest.com
910 */
1011public class TypeSafeDiagnosingMatcherTest {
1112
12- private final TypeSafeDiagnosingMatcher matcher = new TypeSafeDiagnosingMatcher <String >() {
13+ private final TypeSafeDiagnosingMatcher stringMatcher = new TypeSafeDiagnosingMatcher <String >() {
1314 @ Override
1415 protected boolean matchesSafely (String item , Description mismatchDescription ) {
1516 mismatchDescription .appendText ("mismatching" );
@@ -23,8 +24,48 @@ public void describeTo(Description description) { }
2324
2425 @ Test public void
2526 describesMismatches () {
26- assertMismatchDescription ("was null" , matcher , null );
27- assertMismatchDescription ("was Character \" c\" " , matcher , 'c' );
28- assertMismatchDescription ("mismatching" , matcher , "other" );
27+ assertMismatchDescription ("was null" , stringMatcher , null );
28+ assertMismatchDescription ("was Character \" c\" " , stringMatcher , 'c' );
29+ assertMismatchDescription ("mismatching" , stringMatcher , "other" );
2930 }
31+
32+ @ Test public void
33+ detects_non_builtin_types () {
34+ final Matcher <NotBuiltIn > matcher = new TypeSafeDiagnosingMatcher <NotBuiltIn >() {
35+ @ Override
36+ protected boolean matchesSafely (NotBuiltIn item , Description mismatchDescription ) {
37+ return true ;
38+ }
39+
40+ @ Override public void describeTo (Description description ) { description .appendText ("a not builtin" ); }
41+ };
42+
43+ assertMatches ("not built in" , matcher , new NotBuiltIn ());
44+ assertDoesNotMatch ("other not built in" , (Matcher )matcher , new OtherNotBuiltIn ());
45+ }
46+
47+ @ Ignore ("not yet" ) @ Test public void
48+ detects_for_subtypes_of_matcher () {
49+ final Matcher <NotBuiltIn > matcher = new SubMatcher <>();
50+
51+ assertMatches ("not built in" , matcher , new NotBuiltIn ());
52+ assertDoesNotMatch ("other not built in" , (Matcher )matcher , new OtherNotBuiltIn ());
53+ }
54+
55+
56+ public static class SubMatcher <T > extends TypeSafeDiagnosingMatcher <T > {
57+ @ Override protected boolean matchesSafely (T item , Description mismatchDescription ) { return true ; }
58+
59+ @ Override public void describeTo (Description description ) { description .appendText ("sub type" ); }
60+ }
61+
62+ public static class NotBuiltIn {
63+ public final String value = "not built in" ;
64+ @ Override public String toString () { return "NotBuiltIn" ; }
65+ }
66+
67+ public static class OtherNotBuiltIn {
68+
69+ }
70+
3071}
0 commit comments