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 1+ package org .hamcrest .text ;
2+
3+ import java .util .regex .Pattern ;
4+
5+ import org .hamcrest .Description ;
6+ import org .hamcrest .TypeSafeMatcher ;
7+
8+ public class MatchesPattern extends TypeSafeMatcher <String > {
9+ private final Pattern pattern ;
10+
11+ public MatchesPattern (Pattern pattern ) {
12+ this .pattern = pattern ;
13+ }
14+
15+ @ Override
16+ protected boolean matchesSafely (String item ) {
17+ return pattern .matcher (item ).matches ();
18+ }
19+
20+ @ Override
21+ protected void describeMismatchSafely (String item , Description mismatchDescription ) {
22+ // TODO Auto-generated method stub
23+ super .describeMismatchSafely (item , mismatchDescription );
24+ }
25+
26+ @ Override
27+ public void describeTo (Description description ) {
28+ // TODO Auto-generated method stub
29+ }
30+ }
Original file line number Diff line number Diff line change 1+ package org .hamcrest .object ;
2+
3+ import static org .junit .Assert .assertThat ;
4+
5+ import java .util .regex .Pattern ;
6+
7+ import org .hamcrest .AbstractMatcherTest ;
8+ import org .hamcrest .Matcher ;
9+ import org .hamcrest .text .MatchesPattern ;
10+
11+ public class MatchesPatternTest extends AbstractMatcherTest {
12+
13+ @ Override
14+ protected Matcher <?> createMatcher () {
15+ return new MatchesPattern (Pattern .compile ("." ));
16+ }
17+
18+ public void testMatchesExactMatch () {
19+ assertThat ("a" , new MatchesPattern (Pattern .compile ("a" )));
20+ }
21+
22+ public void testDoesNotMatchesDifferentString () {
23+ assertDoesNotMatch ("A different string does not match" , new MatchesPattern (Pattern .compile ("a" )), "b" );
24+ }
25+
26+ public void testDoesNotMatchSubstring () {
27+ assertDoesNotMatch ("A substring does not match" , new MatchesPattern (Pattern .compile ("a" )), "ab" );
28+ }
29+ }
You can’t perform that action at this time.
0 commit comments