Skip to content

Commit 6b5489e

Browse files
committed
Merge pull request hamcrest#57 from josephw/pattern-matcher
Add a matcher to check a string against a regular expression
2 parents 5859a75 + 2e2df5c commit 6b5489e

3 files changed

Lines changed: 108 additions & 0 deletions

File tree

‎hamcrest-library/matchers.xml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
<factory class="org.hamcrest.text.IsEqualIgnoringWhiteSpace"/>
4646
<factory class="org.hamcrest.text.IsEmptyString"/>
4747
<factory class="org.hamcrest.text.IsBlankString"/>
48+
<factory class="org.hamcrest.text.MatchesPattern"/>
4849
<factory class="org.hamcrest.text.StringContainsInOrder"/>
4950

5051
<!-- Object -->
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package org.hamcrest.text;
2+
3+
import java.util.regex.Pattern;
4+
5+
import org.hamcrest.Description;
6+
import org.hamcrest.Factory;
7+
import org.hamcrest.Matcher;
8+
import org.hamcrest.TypeSafeMatcher;
9+
10+
public class MatchesPattern extends TypeSafeMatcher<String> {
11+
private final Pattern pattern;
12+
13+
public MatchesPattern(Pattern pattern) {
14+
this.pattern = pattern;
15+
}
16+
17+
@Override
18+
protected boolean matchesSafely(String item) {
19+
return pattern.matcher(item).matches();
20+
}
21+
22+
@Override
23+
public void describeTo(Description description) {
24+
description.appendText("a string matching the pattern '" + pattern + "'");
25+
}
26+
27+
/**
28+
* Creates a matcher of {@link String} that matches when the examined string
29+
* exactly matches the given {@link Pattern}.
30+
*/
31+
@Factory
32+
public static Matcher<String> matchesPattern(Pattern pattern) {
33+
return new MatchesPattern(pattern);
34+
}
35+
36+
/**
37+
* Creates a matcher of {@link String} that matches when the examined string
38+
* exactly matches the given regular expression, treated as a {@link Pattern}.
39+
*/
40+
@Factory
41+
public static Matcher<String> matchesPattern(String regex) {
42+
return new MatchesPattern(Pattern.compile(regex));
43+
}
44+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package org.hamcrest.object;
2+
3+
import static org.hamcrest.AbstractMatcherTest.assertDescription;
4+
import static org.hamcrest.AbstractMatcherTest.assertDoesNotMatch;
5+
import static org.hamcrest.AbstractMatcherTest.assertMismatchDescription;
6+
import static org.hamcrest.AbstractMatcherTest.assertNullSafe;
7+
import static org.hamcrest.AbstractMatcherTest.assertUnknownTypeSafe;
8+
import static org.junit.Assert.assertThat;
9+
10+
import java.util.regex.Pattern;
11+
12+
import org.hamcrest.Matcher;
13+
import org.hamcrest.text.MatchesPattern;
14+
import org.junit.Test;
15+
16+
public class MatchesPatternTest {
17+
@Test
18+
public void copesWithNullsAndUnknownTypes() {
19+
Matcher<String> matcher = new MatchesPattern(Pattern.compile("."));
20+
21+
assertNullSafe(matcher);
22+
assertUnknownTypeSafe(matcher);
23+
}
24+
25+
@Test
26+
public void matchesExactString() {
27+
assertThat("a", new MatchesPattern(Pattern.compile("a")));
28+
}
29+
30+
@Test
31+
public void doesNotMatchADifferentString() {
32+
assertDoesNotMatch("A different string does not match", new MatchesPattern(Pattern.compile("a")), "b");
33+
}
34+
35+
@Test
36+
public void doesNotMatchSubstring() {
37+
assertDoesNotMatch("A substring does not match", new MatchesPattern(Pattern.compile("a")), "ab");
38+
}
39+
40+
@Test
41+
public void hasAReadableDescription() {
42+
Matcher<?> m = new MatchesPattern(Pattern.compile("a[bc](d|e)"));
43+
assertDescription("a string matching the pattern 'a[bc](d|e)'", m );
44+
}
45+
46+
@Test
47+
public void describesAMismatch() {
48+
final Matcher<String> matcher = new MatchesPattern(Pattern.compile("a"));
49+
assertMismatchDescription("was \"Cheese\"", matcher, "Cheese");
50+
}
51+
52+
@Test
53+
public void factoryMethodAllowsCreationWithPattern() {
54+
Matcher<?> m = MatchesPattern.matchesPattern(Pattern.compile("a[bc](d|e)"));
55+
assertDescription("a string matching the pattern 'a[bc](d|e)'", m );
56+
}
57+
58+
@Test
59+
public void factoryMethodAllowsCreationWithString() {
60+
Matcher<?> m = MatchesPattern.matchesPattern("a[bc](d|e)");
61+
assertDescription("a string matching the pattern 'a[bc](d|e)'", m );
62+
}
63+
}

0 commit comments

Comments
 (0)