|
2 | 2 | */ |
3 | 3 | package org.hamcrest.core; |
4 | 4 |
|
5 | | -import org.hamcrest.AbstractMatcherTest; |
| 5 | +import static org.hamcrest.AbstractMatcherTest.assertDescription; |
| 6 | +import static org.hamcrest.AbstractMatcherTest.assertDoesNotMatch; |
| 7 | +import static org.hamcrest.AbstractMatcherTest.assertMatches; |
| 8 | +import static org.hamcrest.AbstractMatcherTest.assertMismatchDescription; |
| 9 | +import static org.hamcrest.AbstractMatcherTest.assertNullSafe; |
| 10 | +import static org.hamcrest.AbstractMatcherTest.assertUnknownTypeSafe; |
| 11 | +import static org.hamcrest.core.IsInstanceOf.any; |
| 12 | +import static org.hamcrest.core.IsInstanceOf.instanceOf; |
| 13 | + |
6 | 14 | import org.hamcrest.Matcher; |
7 | | -import static org.hamcrest.MatcherAssert.assertThat; |
8 | | -import static org.hamcrest.core.IsInstanceOf.*; |
9 | | -import static org.hamcrest.core.IsNot.not; |
| 15 | +import org.junit.Test; |
| 16 | + |
| 17 | +public final class IsInstanceOfTest { |
10 | 18 |
|
11 | | -public class IsInstanceOfTest extends AbstractMatcherTest { |
| 19 | + @Test public void |
| 20 | + copesWithNullsAndUnknownTypes() { |
| 21 | + Matcher<?> matcher = instanceOf(Number.class); |
12 | 22 |
|
13 | | - @Override |
14 | | - protected Matcher<?> createMatcher() { |
15 | | - return instanceOf(Number.class); |
| 23 | + assertNullSafe(matcher); |
| 24 | + assertUnknownTypeSafe(matcher); |
16 | 25 | } |
17 | 26 |
|
18 | | - public void testEvaluatesToTrueIfArgumentIsInstanceOfASpecificClass() { |
19 | | - assertThat(1, instanceOf(Number.class)); |
20 | | - assertThat(1.0, instanceOf(Number.class)); |
21 | | - assertThat(null, not(instanceOf(Number.class))); |
22 | | - assertThat(new Object(), not(instanceOf(Number.class))); |
| 27 | + @Test public void |
| 28 | + evaluatesToTrueIfArgumentIsInstanceOfASpecificClass() { |
| 29 | + final Matcher<Object> matcher = instanceOf(Number.class); |
| 30 | + |
| 31 | + assertMatches("didn't match", matcher, 1); |
| 32 | + assertMatches("didn't match", matcher, 1.1); |
| 33 | + assertDoesNotMatch("matched unexpectedly", matcher, null); |
| 34 | + assertDoesNotMatch("matched unexpectedly", matcher, new Object()); |
23 | 35 | } |
24 | 36 |
|
25 | | - public void testHasAReadableDescription() { |
| 37 | + @Test public void |
| 38 | + hasAReadableDescription() { |
26 | 39 | assertDescription("an instance of java.lang.Number", instanceOf(Number.class)); |
27 | 40 | } |
28 | 41 |
|
29 | | - public void testDescribesActualClassInMismatchMessage() { |
30 | | - assertMismatchDescription("\"some text\" is a java.lang.String", instanceOf(Number.class), "some text"); |
| 42 | + @Test public void |
| 43 | + describesActualClassInMismatchMessage() { |
| 44 | + assertMismatchDescription("\"some text\" is a java.lang.String", instanceOf(Number.class), "some text"); |
31 | 45 | } |
32 | 46 |
|
33 | | - public void testMatchesPrimitiveTypes() { |
34 | | - assertThat(true, any(boolean.class)); |
35 | | - assertThat((byte)1, any(byte.class)); |
36 | | - assertThat('x', any(char.class)); |
37 | | - assertThat(5.0, any(double.class)); |
38 | | - assertThat(5.0f, any(float.class)); |
39 | | - assertThat(2, any(int.class)); |
40 | | - assertThat(4L, any(long.class)); |
41 | | - assertThat((short)1, any(short.class)); |
| 47 | + @Test public void |
| 48 | + matchesPrimitiveTypes() { |
| 49 | + assertMatches("didn't match", any(boolean.class), true); |
| 50 | + assertMatches("didn't match", any(byte.class), (byte)1); |
| 51 | + assertMatches("didn't match", any(char.class), 'x'); |
| 52 | + assertMatches("didn't match", any(double.class), 5.0); |
| 53 | + assertMatches("didn't match", any(float.class), 5.0f); |
| 54 | + assertMatches("didn't match", any(int.class), 2); |
| 55 | + assertMatches("didn't match", any(long.class), 4L); |
| 56 | + assertMatches("didn't match", any(short.class), (short)1); |
42 | 57 | } |
43 | | - |
44 | | - public void testInstanceOfRequiresACastToReturnTheCorrectTypeForUseInJMock() { |
45 | | - @SuppressWarnings("unused") |
46 | | - Integer anInteger = (Integer)with(instanceOf(Integer.class)); |
| 58 | + |
| 59 | + @Test public void |
| 60 | + instanceOfRequiresACastToReturnTheCorrectTypeForUseInJMock() { |
| 61 | + @SuppressWarnings("unused") |
| 62 | + Integer anInteger = (Integer)with(instanceOf(Integer.class)); |
47 | 63 | } |
48 | | - |
49 | | - public void testAnyWillReturnTheCorrectTypeForUseInJMock() { |
50 | | - @SuppressWarnings("unused") |
51 | | - Integer anInteger = with(any(Integer.class)); |
| 64 | + |
| 65 | + @Test public void |
| 66 | + anyWillReturnTheCorrectTypeForUseInJMock() { |
| 67 | + @SuppressWarnings("unused") |
| 68 | + Integer anInteger = with(any(Integer.class)); |
52 | 69 | } |
53 | | - |
54 | | - |
| 70 | + |
| 71 | + |
55 | 72 | private static <T> T with(@SuppressWarnings("unused") Matcher<T> matcher) { |
56 | | - return null; |
| 73 | + return null; |
57 | 74 | } |
58 | | - |
59 | 75 | } |
60 | 76 |
|
0 commit comments