Skip to content

Commit d30d7f4

Browse files
author
MerkushevKirill
committed
add - simplify endCapture, add tests for null or
1 parent 55f0c6a commit d30d7f4

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

src/main/java/ru/lanwen/verbalregex/VerbalExpression.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ public Builder capture() {
280280
* @return this builder
281281
*/
282282
public Builder endCapture() {
283-
if(this.suffixes.length() > 0 && this.suffixes.indexOf(")") + 1 == this.suffixes.length()) {
283+
if(this.suffixes.indexOf(")") != -1) {
284284
this.suffixes.setLength(suffixes.length() - 1);
285285
return this.add(")");
286286
} else {

src/test/java/ru/lanwen/verbalregex/NegativeCasesTest.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

55
import java.util.regex.PatternSyntaxException;
66

7+
import static org.hamcrest.CoreMatchers.containsString;
78
import static org.hamcrest.CoreMatchers.equalTo;
9+
import static org.hamcrest.CoreMatchers.is;
810
import static org.junit.Assert.assertThat;
911

1012
/**
@@ -43,4 +45,25 @@ public void rangeWithThreeArgsUsesOnlyFirstTwo() throws Exception {
4345

4446
assertThat("Range with three args differs from expected", regex.toString(), equalTo("^[a-z]"));
4547
}
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+
}
4669
}

0 commit comments

Comments
 (0)