File tree Expand file tree Collapse file tree 5 files changed +47
-0
lines changed
hamcrest-core/src/main/java/org/hamcrest/core
hamcrest-unit-test/src/main/java/org/hamcrest/core Expand file tree Collapse file tree 5 files changed +47
-0
lines changed Original file line number Diff line number Diff line change 20
20
* Relaxed isInstanceOf() matcher generic type so it can be used on any kind of
21
21
object. e.g. assertThat(someUnknownObject, isInstanceOf(String.class));
22
22
23
+ * Added any(Class<T>), null(Class<T>) and notNull(Class<T>) matchers, which returns
24
+ Matcher<T>. Helpful when the compiler struggles with type inference.
25
+
23
26
* Modified anyOf() and allOf() to accept mixed-types.
24
27
25
28
* TypeSafeMatcher.matchesSafely() is now public.
Original file line number Diff line number Diff line change @@ -48,4 +48,12 @@ public static <T> Matcher<T> anything() {
48
48
public static <T > Matcher <T > anything (String description ) {
49
49
return new IsAnything <T >(description );
50
50
}
51
+
52
+ /**
53
+ * This matcher always evaluates to true. With type inference.
54
+ */
55
+ @ Factory
56
+ public static <T > Matcher <T > any (@ SuppressWarnings ("unused" )Class <T > type ) {
57
+ return new IsAnything <T >();
58
+ }
51
59
}
Original file line number Diff line number Diff line change @@ -35,5 +35,21 @@ public static <T> Matcher<T> nullValue() {
35
35
public static <T > Matcher <T > notNullValue () {
36
36
return not (IsNull .<T >nullValue ());
37
37
}
38
+
39
+ /**
40
+ * Matches if value is null. With type inference.
41
+ */
42
+ @ Factory
43
+ public static <T > Matcher <T > nullValue (@ SuppressWarnings ("unused" ) Class <T > type ) {
44
+ return nullValue ();
45
+ }
46
+
47
+ /**
48
+ * Matches if value is not null. With type inference.
49
+ */
50
+ @ Factory
51
+ public static <T > Matcher <T > notNullValue (@ SuppressWarnings ("unused" ) Class <T > type ) {
52
+ return notNullValue ();
53
+ }
38
54
}
39
55
Original file line number Diff line number Diff line change 2
2
*/
3
3
package org .hamcrest .core ;
4
4
5
+ import static org .hamcrest .core .IsAnything .any ;
5
6
import org .hamcrest .AbstractMatcherTest ;
6
7
import org .hamcrest .Matcher ;
7
8
import static org .hamcrest .MatcherAssert .assertThat ;
@@ -27,4 +28,12 @@ public void testCanOverrideDescription() {
27
28
String description = "description" ;
28
29
assertDescription (description , anything (description ));
29
30
}
31
+
32
+ public void testSupportsStaticTyping () {
33
+ requiresStringMatcher (any (String .class ));
34
+ }
35
+
36
+ private void requiresStringMatcher (Matcher <String > arg ) {
37
+ // no-op
38
+ }
30
39
}
Original file line number Diff line number Diff line change 8
8
import static org .hamcrest .MatcherAssert .assertThat ;
9
9
import static org .hamcrest .core .IsNull .nullValue ;
10
10
import static org .hamcrest .core .IsNull .notNullValue ;
11
+ import static org .hamcrest .core .IsNull .*;
12
+ import static org .hamcrest .core .IsAnything .any ;
11
13
12
14
public class IsNullTest extends AbstractMatcherTest {
13
15
@@ -22,4 +24,13 @@ public void testEvaluatesToTrueIfArgumentIsNull() {
22
24
assertThat (ANY_NON_NULL_ARGUMENT , notNullValue ());
23
25
assertThat (null , not (notNullValue ()));
24
26
}
27
+
28
+ public void testSupportsStaticTyping () {
29
+ requiresStringMatcher (nullValue (String .class ));
30
+ requiresStringMatcher (notNullValue (String .class ));
31
+ }
32
+
33
+ private void requiresStringMatcher (Matcher <String > arg ) {
34
+ // no-op
35
+ }
25
36
}
You can’t perform that action at this time.
0 commit comments