|
1 | 1 | package org.hamcrest.object; |
2 | 2 |
|
3 | | -import static org.hamcrest.MatcherAssert.assertThat; |
| 3 | +import static org.hamcrest.AbstractMatcherTest.assertDescription; |
| 4 | +import static org.hamcrest.AbstractMatcherTest.assertDoesNotMatch; |
| 5 | +import static org.hamcrest.AbstractMatcherTest.assertMatches; |
| 6 | +import static org.hamcrest.AbstractMatcherTest.assertMismatchDescription; |
| 7 | +import static org.hamcrest.AbstractMatcherTest.assertNullSafe; |
| 8 | +import static org.hamcrest.AbstractMatcherTest.assertUnknownTypeSafe; |
4 | 9 | import static org.hamcrest.core.IsEqual.equalTo; |
5 | | -import static org.hamcrest.core.IsNot.not; |
6 | 10 | import static org.hamcrest.object.HasToString.hasToString; |
7 | 11 |
|
8 | | -import org.hamcrest.AbstractMatcherTest; |
9 | 12 | import org.hamcrest.Matcher; |
10 | | -import org.hamcrest.StringDescription; |
| 13 | +import org.junit.Test; |
11 | 14 |
|
12 | | -public class HasToStringTest extends AbstractMatcherTest { |
| 15 | +public final class HasToStringTest { |
13 | 16 | private static final String TO_STRING_RESULT = "toString result"; |
14 | | - private static final Object ARG = new Object() { |
| 17 | + private static final Object TEST_OBJECT = new Object() { |
15 | 18 | @Override |
16 | 19 | public String toString() { |
17 | 20 | return TO_STRING_RESULT; |
18 | 21 | } |
19 | 22 | }; |
20 | 23 |
|
21 | | - @Override |
22 | | - protected Matcher<?> createMatcher() { |
23 | | - return hasToString(equalTo("irrelevant")); |
| 24 | + @Test public void |
| 25 | + copesWithNullsAndUnknownTypes() { |
| 26 | + Matcher<Object> matcher = hasToString(equalTo("irrelevant")); |
| 27 | + |
| 28 | + assertNullSafe(matcher); |
| 29 | + assertUnknownTypeSafe(matcher); |
24 | 30 | } |
| 31 | + |
| 32 | + @Test public void |
| 33 | + matchesWhenUtilisingANestedMatcher() { |
| 34 | + final Matcher<Object> matcher = hasToString(equalTo(TO_STRING_RESULT)); |
25 | 35 |
|
26 | | - public void testPassesResultOfToStringToNestedMatcher() { |
27 | | - assertThat(ARG, hasToString(equalTo(TO_STRING_RESULT))); |
28 | | - assertThat(ARG, not(hasToString(equalTo("OTHER STRING")))); |
| 36 | + assertMatches("didn't match", matcher, TEST_OBJECT); |
| 37 | + assertDoesNotMatch("matched unexpectedly", matcher, new Object()); |
29 | 38 | } |
30 | 39 |
|
31 | | - public void testProvidesConvenientShortcutForHasToStringEqualTo() { |
32 | | - assertThat(ARG, hasToString(TO_STRING_RESULT)); |
33 | | - assertThat(ARG, not(hasToString("OTHER STRING"))); |
| 40 | + @Test public void |
| 41 | + matchesWhenUsingShortcutForHasToStringEqualTo() { |
| 42 | + final Matcher<Object> matcher = hasToString(TO_STRING_RESULT); |
| 43 | + |
| 44 | + assertMatches("didn't match", matcher, TEST_OBJECT); |
| 45 | + assertDoesNotMatch("matched unexpectedly", matcher, new Object()); |
34 | 46 | } |
35 | 47 |
|
36 | | - public void testHasReadableDescription() { |
37 | | - Matcher<? super String> toStringMatcher = equalTo(TO_STRING_RESULT); |
38 | | - Matcher<Matcher<String>> matcher = hasToString(toStringMatcher); |
39 | | - |
40 | | - assertEquals("with toString() " + descriptionOf(toStringMatcher), descriptionOf(matcher)); |
41 | | - } |
42 | | - |
43 | | - public void testMismatchContainsToStringValue() { |
44 | | - String expectedMismatchString = "toString() was \"Cheese\""; |
45 | | - assertMismatchDescription(expectedMismatchString, hasToString(TO_STRING_RESULT), "Cheese"); |
| 48 | + @Test public void |
| 49 | + describesItself() { |
| 50 | + final Matcher<Object> matcher = hasToString(equalTo(TO_STRING_RESULT)); |
| 51 | + assertDescription("with toString() \"toString result\"", matcher); |
46 | 52 | } |
47 | 53 |
|
48 | | - private static String descriptionOf(Matcher<?> matcher) { |
49 | | - return StringDescription.asString(matcher); |
| 54 | + @Test public void |
| 55 | + describesAMismatch() { |
| 56 | + final Matcher<Object> matcher = hasToString(equalTo(TO_STRING_RESULT)); |
| 57 | + String expectedMismatchString = "toString() was \"Cheese\""; |
| 58 | + assertMismatchDescription(expectedMismatchString, matcher, "Cheese"); |
50 | 59 | } |
51 | 60 | } |
0 commit comments