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