Commit b6f6c1e6 authored by Stein Eldar Johnsen's avatar Stein Eldar Johnsen 💬

Add matchesRegex to ExtraMatchers. Fix generic.

parent 5d89bfb5
......@@ -23,13 +23,14 @@ import net.morimekta.testing.matchers.DistinctFrom;
import net.morimekta.testing.matchers.EqualIgnoreIndent;
import net.morimekta.testing.matchers.EqualToLines;
import net.morimekta.testing.matchers.InRange;
import net.morimekta.testing.matchers.MatchesRegex;
import org.hamcrest.Matcher;
import org.hamcrest.Matchers;
import javax.annotation.Nonnull;
import java.util.Collection;
import java.util.Set;
import java.util.regex.Pattern;
/**
* Extra hamcrest matchers.
......@@ -106,5 +107,25 @@ public class ExtraMatchers {
return new AllItemsMatch<>(itemMatcher);
}
/**
* Matcher that uses a pattern to match against a string.
*
* @param pattern The pattern to use.
* @return The matcher.
*/
public static Matcher<String> matchesRegex(String pattern) {
return new MatchesRegex(pattern);
}
/**
* Matcher that uses a pattern to match against a string.
*
* @param pattern The pattern to use.
* @return The matcher.
*/
public static Matcher<String> matchesRegex(Pattern pattern) {
return new MatchesRegex(pattern);
}
private ExtraMatchers() {}
}
......@@ -21,13 +21,12 @@ package net.morimekta.testing.matchers;
import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;
import java.util.Collection;
import java.util.regex.Pattern;
/**
* Checks that the matching string matches the regex.
*/
public class MatchesRegex extends BaseMatcher<Collection<String>> {
public class MatchesRegex extends BaseMatcher<String> {
private final Pattern pattern;
public MatchesRegex(String regex) {
......
......@@ -4,7 +4,6 @@ import net.morimekta.testing.matchers.DistinctFrom;
import net.morimekta.testing.matchers.EqualIgnoreIndent;
import net.morimekta.testing.matchers.EqualToLines;
import net.morimekta.testing.matchers.InRange;
import org.hamcrest.Matcher;
import org.hamcrest.collection.IsIn;
import org.junit.Test;
......@@ -15,7 +14,9 @@ import java.util.Collection;
import java.util.Collections;
import java.util.Set;
import java.util.TreeSet;
import java.util.regex.Pattern;
import static net.morimekta.testing.ExtraMatchers.matchesRegex;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.isOneOf;
......@@ -102,6 +103,12 @@ public class ExtraMatchersTest {
assertThat(matcher.matches(null), is(true));
}
@Test
public void testMatchesRegex() {
assertThat("ayb5", matchesRegex("a.*b[1-7]"));
assertThat("ayb5", matchesRegex(Pattern.compile("a.*b[1-7]")));
}
@Test
public void testConstructor()
throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment