|
4 | 4 |
|
5 | 5 | import java.util.regex.PatternSyntaxException;
|
6 | 6 |
|
| 7 | +import static org.hamcrest.CoreMatchers.containsString; |
7 | 8 | import static org.hamcrest.CoreMatchers.equalTo;
|
| 9 | +import static org.hamcrest.CoreMatchers.is; |
8 | 10 | import static org.junit.Assert.assertThat;
|
9 | 11 |
|
10 | 12 | /**
|
@@ -43,4 +45,25 @@ public void rangeWithThreeArgsUsesOnlyFirstTwo() throws Exception {
|
43 | 45 |
|
44 | 46 | assertThat("Range with three args differs from expected", regex.toString(), equalTo("^[a-z]"));
|
45 | 47 | }
|
| 48 | + |
| 49 | + @Test |
| 50 | + public void orWithNullMatchesAny() throws Exception { |
| 51 | + VerbalExpression regex = VerbalExpression.regex().startOfLine().then("a").or(null).build(); |
| 52 | + assertThat("regex don't matches writed letter", regex.test("a"), is(true)); |
| 53 | + assertThat("or(null) should match any", regex.test("bcd"), is(true)); |
| 54 | + |
| 55 | + assertThat("or(null) extract only first", regex.getText("abcd"), equalTo("a")); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + public void orAfterCaptureProduceEmptyGroup() throws Exception { |
| 60 | + VerbalExpression regex = VerbalExpression.regex().startOfLine().then("a").capture().or("b").build(); |
| 61 | + |
| 62 | + assertThat(regex.toString(), containsString("()|")); |
| 63 | + |
| 64 | + assertThat("regex dont matches string abcd", regex.getText("abcd", 0), equalTo("a")); |
| 65 | + assertThat("regex dont extract a by first group", regex.getText("abcd", 1), equalTo("a")); |
| 66 | + assertThat("second group should produce empty string", regex.getText("abcd", 2), equalTo("")); |
| 67 | + |
| 68 | + } |
46 | 69 | }
|
0 commit comments