Skip to content

Commit 32e2c2e

Browse files
author
neildunn
committed
Added CustomMatcher classes for writing throw away matchers
1 parent 850420f commit 32e2c2e

5 files changed

Lines changed: 119 additions & 3 deletions

File tree

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package org.hamcrest;
2+
3+
/**
4+
* Utility class for writing one off matchers.
5+
* For example:
6+
* <pre>
7+
* Matcher&lt;String&gt; aNonEmptyString = new CustomMatcher&lt;String&gt;("a non empty string") {
8+
* public boolean matches(Object object) {
9+
* return ((object instanceof String) && !((String) object).isEmpty();
10+
* }
11+
* };
12+
* </pre>
13+
* <p>
14+
* This class is designed for scenarios where an anonymous inner class
15+
* matcher makes sense. It should not be used by API designers implementing
16+
* matchers.
17+
*
18+
* @author Neil Dunn
19+
* @see CustomTypeSafeMatcher for a type safe variant of this class that you probably
20+
* want to use.
21+
* @param <T> The type of object being matched.
22+
*/
23+
public abstract class CustomMatcher<T> extends BaseMatcher<T> {
24+
private final String fixedDescription;
25+
26+
public CustomMatcher(String description) {
27+
if (description == null) {
28+
throw new IllegalArgumentException("Description should be non null!");
29+
}
30+
this.fixedDescription = description;
31+
}
32+
33+
public final void describeTo(Description description) {
34+
description.appendText(fixedDescription);
35+
}
36+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package org.hamcrest;
2+
3+
4+
/**
5+
* Utility class for writing one off matchers.
6+
* For example:
7+
* <pre>
8+
* Matcher&lt;String&gt; aNonEmptyString = new CustomTypeSafeMatcher&lt;String&gt;("a non empty string") {
9+
* public boolean matchesSafely(String string) {
10+
* return !string.isEmpty();
11+
* }
12+
* };
13+
* </pre>
14+
* This is a variant of {@link CustomMatcher} that first type checks
15+
* the argument being matched. By the time {@link #matchesSafely(T)} is
16+
* is called the argument is guaranteed to be non-null and of the correct
17+
* type.
18+
*
19+
* @author Neil Dunn
20+
* @param <T> The type of object being matched
21+
*/
22+
public abstract class CustomTypeSafeMatcher<T> extends TypeSafeMatcher<T> {
23+
private final String fixedDescription;
24+
25+
public CustomTypeSafeMatcher(String description) {
26+
if (description == null) {
27+
throw new IllegalArgumentException("Description must be non null!");
28+
}
29+
this.fixedDescription = description;
30+
}
31+
32+
public final void describeTo(Description description) {
33+
description.appendText(fixedDescription);
34+
}
35+
}

‎hamcrest-unit-test/src/main/java/org/hamcrest/AbstractMatcherTest.java‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@ public abstract class AbstractMatcherTest extends TestCase {
1414
protected static final Object ARGUMENT_IGNORED = new Object();
1515
protected static final Object ANY_NON_NULL_ARGUMENT = new Object();
1616

17-
public <T> void assertMatches(String message, Matcher<T> c, T arg) {
17+
public static <T> void assertMatches(String message, Matcher<T> c, T arg) {
1818
assertTrue(message, c.matches(arg));
1919
}
2020

21-
public <T> void assertDoesNotMatch(String message, Matcher<T> c, T arg) {
21+
public static <T> void assertDoesNotMatch(String message, Matcher<T> c, T arg) {
2222
assertFalse(message, c.matches(arg));
2323
}
2424

25-
public void assertDescription(String expected, Matcher<?> matcher) {
25+
public static void assertDescription(String expected, Matcher<?> matcher) {
2626
Description description = new StringDescription();
2727
description.appendDescriptionOf(matcher);
2828
assertEquals("Expected description", expected, description.toString());
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package org.hamcrest;
2+
3+
import static org.hamcrest.AbstractMatcherTest.assertDescription;
4+
import junit.framework.TestCase;
5+
6+
public class CustomMatcherTest extends TestCase {
7+
public void testUsesStaticDescription() throws Exception {
8+
Matcher<String> matcher = new CustomMatcher<String>("I match strings") {
9+
public boolean matches(Object item) {
10+
return (item instanceof String);
11+
}
12+
};
13+
14+
assertDescription("I match strings", matcher);
15+
}
16+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package org.hamcrest;
2+
3+
4+
public class CustomTypeSafeMatcherTest extends AbstractMatcherTest {
5+
6+
private static final String STATIC_DESCRIPTION = "I match non empty strings";
7+
8+
private Matcher<String> customMatcher;
9+
10+
@Override
11+
protected void setUp() throws Exception {
12+
customMatcher = new CustomTypeSafeMatcher<String>(STATIC_DESCRIPTION) {
13+
@Override
14+
public boolean matchesSafely(String item) {
15+
return false;
16+
}
17+
};
18+
}
19+
20+
public void testUsesStaticDescription() throws Exception {
21+
assertDescription(STATIC_DESCRIPTION, customMatcher);
22+
}
23+
24+
@Override
25+
protected Matcher<?> createMatcher() {
26+
return customMatcher;
27+
}
28+
29+
}

0 commit comments

Comments
 (0)