Update NamePatternFilter to detect regular expression character class

Previously, NamePatternFilter looked for “*”, “$”, “^”, or “+” when
trying to identify a string as being a regular expression. This meant
that it missed the use of a character class ([a-z], for example). This
commit adds “[“} to the list of characters that are considered to be
part of a regular expression.

Closes gh-4233
pull/4293/merge
Andy Wilkinson 9 years ago
parent 89df4946f7
commit aa3313cfc5

@ -35,7 +35,7 @@ import java.util.regex.Pattern;
*/ */
abstract class NamePatternFilter<T> { abstract class NamePatternFilter<T> {
private static final String[] REGEX_PARTS = { "*", "$", "^", "+" }; private static final String[] REGEX_PARTS = { "*", "$", "^", "+", "[" };
private final T source; private final T source;

@ -22,6 +22,8 @@ import org.junit.Test;
import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasEntry; import static org.hamcrest.Matchers.hasEntry;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
/** /**
@ -41,13 +43,48 @@ public class NamePatternFilterTests {
} }
@Test @Test
public void regex() throws Exception { public void regexRepetitionZeroOrMore() {
MockNamePatternFilter filter = new MockNamePatternFilter(); MockNamePatternFilter filter = new MockNamePatternFilter();
Map<String, Object> results = filter.getResults("fo.*"); Map<String, Object> results = filter.getResults("fo.*");
assertThat(results.get("foo"), equalTo((Object) "foo")); assertThat(results.get("foo"), equalTo((Object) "foo"));
assertThat(results.get("fool"), equalTo((Object) "fool")); assertThat(results.get("fool"), equalTo((Object) "fool"));
assertThat(filter.isGetNamesCalled(), equalTo(true)); assertThat(filter.isGetNamesCalled(), equalTo(true));
}
@Test
public void regexRepetitionOneOrMore() {
MockNamePatternFilter filter = new MockNamePatternFilter();
Map<String, Object> results = filter.getResults("fo.+");
assertThat(results.get("foo"), equalTo((Object) "foo"));
assertThat(results.get("fool"), equalTo((Object) "fool"));
assertThat(filter.isGetNamesCalled(), equalTo(true));
}
@Test
public void regexEndAnchor() {
MockNamePatternFilter filter = new MockNamePatternFilter();
Map<String, Object> results = filter.getResults("foo$");
assertThat(results.get("foo"), equalTo((Object) "foo"));
assertThat(results.get("fool"), is(nullValue()));
assertThat(filter.isGetNamesCalled(), equalTo(true));
}
@Test
public void regexStartAnchor() {
MockNamePatternFilter filter = new MockNamePatternFilter();
Map<String, Object> results = filter.getResults("^foo");
assertThat(results.get("foo"), equalTo((Object) "foo"));
assertThat(results.get("fool"), is(nullValue()));
assertThat(filter.isGetNamesCalled(), equalTo(true));
}
@Test
public void regexCharacterClass() {
MockNamePatternFilter filter = new MockNamePatternFilter();
Map<String, Object> results = filter.getResults("fo[a-z]l");
assertThat(results.get("foo"), is(nullValue()));
assertThat(results.get("fool"), equalTo((Object) "fool"));
assertThat(filter.isGetNamesCalled(), equalTo(true));
} }
private static class MockNamePatternFilter extends NamePatternFilter<Object> { private static class MockNamePatternFilter extends NamePatternFilter<Object> {

Loading…
Cancel
Save