Skip to content

Commit c7f83db

Browse files
committed
Add factory methods for MatchesPattern.
1 parent 54ed566 commit c7f83db

3 files changed

Lines changed: 25 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 -->

‎hamcrest-library/src/main/java/org/hamcrest/text/MatchesPattern.java‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import java.util.regex.Pattern;
44

55
import org.hamcrest.Description;
6+
import org.hamcrest.Factory;
7+
import org.hamcrest.Matcher;
68
import org.hamcrest.TypeSafeMatcher;
79

810
public class MatchesPattern extends TypeSafeMatcher<String> {
@@ -21,4 +23,14 @@ protected boolean matchesSafely(String item) {
2123
public void describeTo(Description description) {
2224
description.appendText("a string matching the pattern '" + pattern + "'");
2325
}
26+
27+
@Factory
28+
public static Matcher<String> matchesPattern(Pattern pattern) {
29+
return new MatchesPattern(pattern);
30+
}
31+
32+
@Factory
33+
public static Matcher<String> matchesPattern(String regex) {
34+
return new MatchesPattern(Pattern.compile(regex));
35+
}
2436
}

‎hamcrest-library/src/test/java/org/hamcrest/object/MatchesPatternTest.java‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,16 @@ public void describesAMismatch() {
4848
final Matcher<String> matcher = new MatchesPattern(Pattern.compile("a"));
4949
assertMismatchDescription("was \"Cheese\"", matcher, "Cheese");
5050
}
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+
}
5163
}

0 commit comments

Comments
 (0)