Skip to content

Commit

Permalink
Fixing the handling of (unmatched) optional capture groups (#75)
Browse files Browse the repository at this point in the history
* adding test showing the problematic handling of optional capture groups

* do not return "null"-String

when an optional capture group is actually not matched

* upgrading travis jdk version

since it no longer supports jdk's older than 9:

> $ ~/bin/install-jdk.sh --target "/home/travis/oraclejdk8" --workspace "/home/travis/.cache/install-jdk" --feature "8" --license "BCL"
> Ignoring license option: BCL -- using GPLv2+CE by default
> install-jdk.sh 2020-06-02
> Expected feature release number in range of 9 to 16, but got: 8

* we no longer return the "null" string literal

* reduce assertions

Co-authored-by: Kirill Merkushev <[email protected]>

* Update .travis.yml

Co-authored-by: Kirill Merkushev <[email protected]>
  • Loading branch information
simschla and lanwen authored Mar 19, 2021
1 parent 44af1c7 commit a669493
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ language: java
sudo: false

jdk:
- oraclejdk8
- openjdk8

install:
- mvn --settings .travis/settings.xml install -DskipTests=true -Dgpg.skip -Dmaven.javadoc.skip=true -B -V
Expand Down
10 changes: 8 additions & 2 deletions src/main/java/ru/lanwen/verbalregex/VerbalExpression.java
Original file line number Diff line number Diff line change
Expand Up @@ -762,7 +762,10 @@ public String getText(final String toTest, final int group) {
Matcher m = pattern.matcher(toTest);
StringBuilder result = new StringBuilder();
while (m.find()) {
result.append(m.group(group));
String groupValue = m.group(group);
if (groupValue != null) {
result.append(groupValue);
}
}
return result.toString();
}
Expand All @@ -781,7 +784,10 @@ public String getText(final String toTest, final String group) {
Matcher m = pattern.matcher(toTest);
StringBuilder result = new StringBuilder();
while (m.find()) {
result.append(m.group(group));
String groupValue = m.group(group);
if (groupValue != null) {
result.append(groupValue);
}
}
return result.toString();
}
Expand Down
24 changes: 12 additions & 12 deletions src/test/java/ru/lanwen/verbalregex/BasicFunctionalityUnitTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -497,9 +497,9 @@ public void testOrWithCapture() {
assertThat("Starts with abc or def", testRegex, matchesTo("abczzz"));
assertThat("Doesn't start with abc or def", testRegex, not(matchesExactly("xyzabcefg")));

assertThat(testRegex.getText("xxxabcdefzzz", 1), equalTo("abcnull"));
assertThat(testRegex.getText("xxxdefzzz", 1), equalTo("null"));
assertThat(testRegex.getText("xxxabcdefzzz", 1), equalTo("abcnull"));
assertThat(testRegex.getText("xxxabcdefzzz", 1), equalTo("abc"));
assertThat(testRegex.getText("xxxdefzzz", 1), equalTo(""));
assertThat(testRegex.getText("xxxabcdefzzz", 1), equalTo("abc"));
}

@Test
Expand All @@ -516,11 +516,11 @@ public void testOrWithNamedCapture() {
testRegex, not(matchesExactly("xyzabcefg")));

assertThat(testRegex.getText("xxxabcdefzzz", captureName),
equalTo("abcnull"));
equalTo("abc"));
assertThat(testRegex.getText("xxxdefzzz", captureName),
equalTo("null"));
equalTo(""));
assertThat(testRegex.getText("xxxabcdefzzz", captureName),
equalTo("abcnull"));
equalTo("abc"));
}

@Test
Expand All @@ -535,9 +535,9 @@ public void testOrWithClosedCapture() {
assertThat("Starts with abc or def", testRegex, matchesTo("abczzz"));
assertThat("Doesn't start with abc or def", testRegex, not(matchesExactly("xyzabcefg")));

assertThat(testRegex.getText("xxxabcdefzzz", 1), equalTo("abcnull"));
assertThat(testRegex.getText("xxxdefzzz", 1), equalTo("null"));
assertThat(testRegex.getText("xxxabcdefzzz", 1), equalTo("abcnull"));
assertThat(testRegex.getText("xxxabcdefzzz", 1), equalTo("abc"));
assertThat(testRegex.getText("xxxdefzzz", 1), equalTo(""));
assertThat(testRegex.getText("xxxabcdefzzz", 1), equalTo("abc"));
}

@Test
Expand All @@ -555,11 +555,11 @@ public void testOrWithClosedNamedCapture() {
testRegex, not(matchesExactly("xyzabcefg")));

assertThat(testRegex.getText("xxxabcdefzzz", captureName),
equalTo("abcnull"));
equalTo("abc"));
assertThat(testRegex.getText("xxxdefzzz", captureName),
equalTo("null"));
equalTo(""));
assertThat(testRegex.getText("xxxabcdefzzz", captureName),
equalTo("abcnull"));
equalTo("abc"));
}

@Test
Expand Down
17 changes: 17 additions & 0 deletions src/test/java/ru/lanwen/verbalregex/RealWorldUnitTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.junit.Test;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.not;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static ru.lanwen.verbalregex.VerbalExpression.regex;
Expand Down Expand Up @@ -149,4 +150,20 @@ public void captureAfterNewLineHasANamedGroup() {
assertThat(some,
equalTo(expression.getText(lineBreak + some + text, captureName)));
}

@Test
public void missingOptionalCaptureGroupReturnsEmptyStringNotStringContainingNullLiteral() {
final VerbalExpression expression = VerbalExpression.regex().
startOfLine()
.capture("optionalCapture")
.oneOf("a", "b")
.endCapture()
.count(0, 1)
.then("c")
.endOfLine()
.build();
final String testString = "c";
assertThat(expression, matchesExactly(testString));
assertThat(expression.getText("c", "optionalCapture"), equalTo(""));
}
}

0 comments on commit a669493

Please sign in to comment.