Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing zeroOrMore() and oneOrMore() methods #37

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions src/main/java/ru/lanwen/verbalregex/VerbalExpression.java
Original file line number Diff line number Diff line change
Expand Up @@ -473,15 +473,15 @@ public Builder searchOneLine(final boolean pEnable) {
*/
public Builder multiple(final String pValue, final int... count) {
if (count == null) {
return this.then(pValue).oneOrMore();
return this.oneOrMore(pValue);
}
switch (count.length) {
case 1:
return this.then(pValue).count(count[0]);
case 2:
return this.then(pValue).count(count[0], count[1]);
default:
return this.then(pValue).oneOrMore();
return this.oneOrMore(pValue);
}
}

Expand All @@ -493,8 +493,8 @@ public Builder multiple(final String pValue, final int... count) {
* @return this builder
* @since 1.2
*/
public Builder oneOrMore() {
return this.add("+");
public Builder oneOrMore(String pValue) {
return this.then(pValue).add("+");
}

/**
Expand All @@ -504,8 +504,8 @@ public Builder oneOrMore() {
* @return this builder
* @since 1.2
*/
public Builder zeroOrMore() {
return this.add("*");
public Builder zeroOrMore(String pValue) {
return this.then(pValue).add("*");
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,7 @@ public void atLeast1HaveSameEffectAsOneOrMore() throws Exception {

@Test
public void oneOreMoreSameAsAtLeast1() throws Exception {
VerbalExpression regexWithOneOrMore = regex().find("a").oneOrMore().build();
VerbalExpression regexWithOneOrMore = regex().oneOrMore("a").build();

String matched = "aaaaaa";
String oneMatchedExactly = "a";
Expand Down Expand Up @@ -524,7 +524,7 @@ public void atLeast0HaveSameEffectAsZeroOrMore() throws Exception {

@Test
public void zeroOreMoreSameAsAtLeast0() throws Exception {
VerbalExpression regexWithOneOrMore = regex().find("a").zeroOrMore().build();
VerbalExpression regexWithOneOrMore = regex().zeroOrMore("a").build();

String matched = "aaaaaa";
String oneMatchedExactly = "a";
Expand Down Expand Up @@ -585,10 +585,8 @@ public void shouldAddMaybeWithOneOfFromAnotherBuilder() {
VerbalExpression.Builder namePrefix = regex().oneOf("Mr.", "Ms.");
VerbalExpression name = regex()
.maybe(namePrefix)
.space()
.zeroOrMore()
.word()
.oneOrMore()
.zeroOrMore(" ")
.add("\\w+")
.build();

assertThat("Is a name with prefix", name, matchesTo("Mr. Bond"));
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/ru/lanwen/verbalregex/NegativeCasesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,15 @@ public void multiplyWithNullOnCountEqualToWithOneAndMore() throws Exception {
VerbalExpression regex = regex().multiple("some", null).build();

assertThat("Multiply with null should be equal to oneOrMore",
regex.toString(), equalTo(regex().find("some").oneOrMore().build().toString()));
regex.toString(), equalTo(regex().oneOrMore("some").build().toString()));
}

@Test
public void multiplyWithMoreThan3ParamsOnCountEqualToWithOneAndMore() throws Exception {
VerbalExpression regex = regex().multiple("some", 1, 2, 3).build();

assertThat("Multiply with 3 args should be equal to oneOrMore",
regex.toString(), equalTo(regex().find("some").oneOrMore().build().toString()));
regex.toString(), equalTo(regex().oneOrMore("some").build().toString()));
}

@Test(expected = java.util.regex.PatternSyntaxException.class)
Expand Down
14 changes: 7 additions & 7 deletions src/test/java/ru/lanwen/verbalregex/RealWorldUnitTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,22 +57,22 @@ public void complexPatternWithMultiplyCaptures() throws Exception {
String logLine = "3\t4\t1\thttp://localhost:20001\t1\t63528800\t0\t63528800\t1000000000\t0\t63528800\tSTR1";

VerbalExpression regex = regex()
.capt().digit().oneOrMore().endCapture().tab()
.capt().digit().oneOrMore().endCapture().tab()
.capt().add("\\d+").endCapture().tab()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's bad

.capt().add("\\d+").endCapture().tab()
.capt().range("0", "1").count(1).endCapture().tab()
.capt().find("http://localhost:20").digit().count(3).endCapture().tab()
.capt().range("0", "1").count(1).endCapture().tab()
.capt().digit().oneOrMore().endCapture().tab()
.capt().add("\\d+").endCapture().tab()
.capt().range("0", "1").count(1).endCapture().tab()
.capt().digit().oneOrMore().endCapture().tab()
.capt().digit().oneOrMore().endCapture().tab()
.capt().add("\\d+").endCapture().tab()
.capt().add("\\d+").endCapture().tab()
.capt().range("0", "1").count(1).endCapture().tab()
.capt().digit().oneOrMore().endCapture().tab()
.capt().add("\\d+").endCapture().tab()
.capt().find("STR").range("0", "2").count(1).endCapture().build();

assertThat(regex, matchesExactly(logLine));

VerbalExpression.Builder digits = regex().capt().digit().oneOrMore().endCapt().tab();
VerbalExpression.Builder digits = regex().capt().add("\\d+").endCapt().tab();
VerbalExpression.Builder range = regex().capt().range("0", "1").count(1).endCapt().tab();
VerbalExpression.Builder host = regex().capt().find("http://localhost:20").digit().count(3).endCapt().tab();
VerbalExpression.Builder fake = regex().capt().find("STR").range("0", "2").count(1);
Expand Down