File tree Expand file tree Collapse file tree
main/java/org/hamcrest/text
test/java/org/hamcrest/object Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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 -->
Original file line number Diff line number Diff line change 33import java .util .regex .Pattern ;
44
55import org .hamcrest .Description ;
6+ import org .hamcrest .Factory ;
7+ import org .hamcrest .Matcher ;
68import org .hamcrest .TypeSafeMatcher ;
79
810public 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}
Original file line number Diff line number Diff 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}
You can’t perform that action at this time.
0 commit comments