|
7 | 7 |
|
8 | 8 | public abstract class AbstractMatcherTest extends TestCase { |
9 | 9 |
|
10 | | - /** |
11 | | - * Create an instance of the Matcher so some generic safety-net tests can be run on it. |
12 | | - */ |
13 | | - protected abstract Matcher<?> createMatcher(); |
14 | | - |
15 | | - public static <T> void assertMatches(String message, Matcher<? super T> c, T arg) { |
16 | | - Assert.assertTrue(message, c.matches(arg)); |
17 | | - } |
18 | | - |
19 | | - public static <T> void assertDoesNotMatch(String message, Matcher<? super T> c, T arg) { |
20 | | - Assert.assertFalse(message, c.matches(arg)); |
21 | | - } |
22 | | - |
23 | | - public static void assertDescription(String expected, Matcher<?> matcher) { |
24 | | - Description description = new StringDescription(); |
25 | | - description.appendDescriptionOf(matcher); |
26 | | - Assert.assertEquals("Expected description", expected, description.toString().trim()); |
27 | | - } |
28 | | - |
29 | | - public static <T> void assertMismatchDescription(String expected, Matcher<? super T> matcher, T arg) { |
30 | | - Assert.assertFalse("Precondtion: Matcher should not match item.", matcher.matches(arg)); |
31 | | - Description description = new StringDescription(); |
32 | | - matcher.describeMismatch(arg, description); |
33 | | - Assert.assertEquals("Expected mismatch description", expected, description.toString().trim()); |
34 | | - } |
35 | | - |
36 | | - public void testIsNullSafe() { |
37 | | - // should not throw a NullPointerException |
38 | | - createMatcher().matches(null); |
39 | | - } |
40 | | - |
41 | | - public void testCopesWithUnknownTypes() { |
42 | | - // should not throw ClassCastException |
43 | | - createMatcher().matches(new UnknownType()); |
44 | | - } |
45 | | - |
46 | | - public static class UnknownType { |
| 10 | + /** |
| 11 | + * Create an instance of the Matcher so some generic safety-net tests can be run on it. |
| 12 | + */ |
| 13 | + protected abstract Matcher<?> createMatcher(); |
| 14 | + |
| 15 | + public static <T> void assertMatches(String message, Matcher<? super T> matcher, T arg) { |
| 16 | + if (!matcher.matches(arg)) { |
| 17 | + Assert.fail(message + " because: '" + mismatchDescription(matcher, arg) + "'"); |
47 | 18 | } |
| 19 | + } |
| 20 | + |
| 21 | + public static <T> void assertDoesNotMatch(String message, Matcher<? super T> c, T arg) { |
| 22 | + Assert.assertFalse(message, c.matches(arg)); |
| 23 | + } |
| 24 | + |
| 25 | + public static void assertDescription(String expected, Matcher<?> matcher) { |
| 26 | + Description description = new StringDescription(); |
| 27 | + description.appendDescriptionOf(matcher); |
| 28 | + Assert.assertEquals("Expected description", expected, description.toString().trim()); |
| 29 | + } |
| 30 | + |
| 31 | + public static <T> void assertMismatchDescription(String expected, Matcher<? super T> matcher, T arg) { |
| 32 | + Assert.assertFalse("Precondtion: Matcher should not match item.", matcher.matches(arg)); |
| 33 | + Assert.assertEquals("Expected mismatch description", expected, mismatchDescription(matcher, arg)); |
| 34 | + } |
| 35 | + |
| 36 | + private static <T> String mismatchDescription(Matcher<? super T> matcher, T arg) { |
| 37 | + Description description = new StringDescription(); |
| 38 | + matcher.describeMismatch(arg, description); |
| 39 | + return description.toString().trim(); |
| 40 | + } |
| 41 | + |
| 42 | + public void testIsNullSafe() { |
| 43 | + // should not throw a NullPointerException |
| 44 | + createMatcher().matches(null); |
| 45 | + } |
| 46 | + |
| 47 | + public void testCopesWithUnknownTypes() { |
| 48 | + // should not throw ClassCastException |
| 49 | + createMatcher().matches(new UnknownType()); |
| 50 | + } |
| 51 | + |
| 52 | + public static class UnknownType { |
| 53 | + } |
48 | 54 |
|
49 | 55 | } |
0 commit comments