forked from hamcrest/JavaHamcrest
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request hamcrest#57 from josephw/pattern-matcher
Add a matcher to check a string against a regular expression
- Loading branch information
Showing
3 changed files
with
108 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
hamcrest-library/src/main/java/org/hamcrest/text/MatchesPattern.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package org.hamcrest.text; | ||
|
||
import java.util.regex.Pattern; | ||
|
||
import org.hamcrest.Description; | ||
import org.hamcrest.Factory; | ||
import org.hamcrest.Matcher; | ||
import org.hamcrest.TypeSafeMatcher; | ||
|
||
public class MatchesPattern extends TypeSafeMatcher<String> { | ||
private final Pattern pattern; | ||
|
||
public MatchesPattern(Pattern pattern) { | ||
this.pattern = pattern; | ||
} | ||
|
||
@Override | ||
protected boolean matchesSafely(String item) { | ||
return pattern.matcher(item).matches(); | ||
} | ||
|
||
@Override | ||
public void describeTo(Description description) { | ||
description.appendText("a string matching the pattern '" + pattern + "'"); | ||
} | ||
|
||
/** | ||
* Creates a matcher of {@link String} that matches when the examined string | ||
* exactly matches the given {@link Pattern}. | ||
*/ | ||
@Factory | ||
public static Matcher<String> matchesPattern(Pattern pattern) { | ||
return new MatchesPattern(pattern); | ||
} | ||
|
||
/** | ||
* Creates a matcher of {@link String} that matches when the examined string | ||
* exactly matches the given regular expression, treated as a {@link Pattern}. | ||
*/ | ||
@Factory | ||
public static Matcher<String> matchesPattern(String regex) { | ||
return new MatchesPattern(Pattern.compile(regex)); | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
hamcrest-library/src/test/java/org/hamcrest/object/MatchesPatternTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package org.hamcrest.object; | ||
|
||
import static org.hamcrest.AbstractMatcherTest.assertDescription; | ||
import static org.hamcrest.AbstractMatcherTest.assertDoesNotMatch; | ||
import static org.hamcrest.AbstractMatcherTest.assertMismatchDescription; | ||
import static org.hamcrest.AbstractMatcherTest.assertNullSafe; | ||
import static org.hamcrest.AbstractMatcherTest.assertUnknownTypeSafe; | ||
import static org.junit.Assert.assertThat; | ||
|
||
import java.util.regex.Pattern; | ||
|
||
import org.hamcrest.Matcher; | ||
import org.hamcrest.text.MatchesPattern; | ||
import org.junit.Test; | ||
|
||
public class MatchesPatternTest { | ||
@Test | ||
public void copesWithNullsAndUnknownTypes() { | ||
Matcher<String> matcher = new MatchesPattern(Pattern.compile(".")); | ||
|
||
assertNullSafe(matcher); | ||
assertUnknownTypeSafe(matcher); | ||
} | ||
|
||
@Test | ||
public void matchesExactString() { | ||
assertThat("a", new MatchesPattern(Pattern.compile("a"))); | ||
} | ||
|
||
@Test | ||
public void doesNotMatchADifferentString() { | ||
assertDoesNotMatch("A different string does not match", new MatchesPattern(Pattern.compile("a")), "b"); | ||
} | ||
|
||
@Test | ||
public void doesNotMatchSubstring() { | ||
assertDoesNotMatch("A substring does not match", new MatchesPattern(Pattern.compile("a")), "ab"); | ||
} | ||
|
||
@Test | ||
public void hasAReadableDescription() { | ||
Matcher<?> m = new MatchesPattern(Pattern.compile("a[bc](d|e)")); | ||
assertDescription("a string matching the pattern 'a[bc](d|e)'", m ); | ||
} | ||
|
||
@Test | ||
public void describesAMismatch() { | ||
final Matcher<String> matcher = new MatchesPattern(Pattern.compile("a")); | ||
assertMismatchDescription("was \"Cheese\"", matcher, "Cheese"); | ||
} | ||
|
||
@Test | ||
public void factoryMethodAllowsCreationWithPattern() { | ||
Matcher<?> m = MatchesPattern.matchesPattern(Pattern.compile("a[bc](d|e)")); | ||
assertDescription("a string matching the pattern 'a[bc](d|e)'", m ); | ||
} | ||
|
||
@Test | ||
public void factoryMethodAllowsCreationWithString() { | ||
Matcher<?> m = MatchesPattern.matchesPattern("a[bc](d|e)"); | ||
assertDescription("a string matching the pattern 'a[bc](d|e)'", m ); | ||
} | ||
} |