Skip to content

Commit

Permalink
Merge pull request #24 from lanwen/tests
Browse files Browse the repository at this point in the history
add - tests for range method and case, any, start and end of line
  • Loading branch information
lanwen committed May 11, 2014
2 parents d7a226c + d30d7f4 commit b1739ae
Show file tree
Hide file tree
Showing 5 changed files with 188 additions and 47 deletions.
17 changes: 8 additions & 9 deletions src/main/java/ru/lanwen/verbalregex/VerbalExpression.java
Original file line number Diff line number Diff line change
Expand Up @@ -116,17 +116,16 @@ public Builder any(final String value) {
}

public Builder range(String... pArgs) {
String value = "[";
for (int _to = 1; _to < pArgs.length; _to += 2) {
String from = sanitize((String) pArgs[_to - 1]);
String to = sanitize((String) pArgs[_to]);
StringBuilder value = new StringBuilder("[");
for (int firstInPairPosition = 1; firstInPairPosition < pArgs.length; firstInPairPosition += 2) {
String from = sanitize(pArgs[firstInPairPosition - 1]);
String to = sanitize(pArgs[firstInPairPosition]);

value += from + "-" + to;
value.append(from).append("-").append(to);
}
value += "]";
value.append("]");

this.add(value);
return this;
return this.add(value.toString());
}

public Builder addModifier(final char pModifier) {
Expand Down Expand Up @@ -281,7 +280,7 @@ public Builder capture() {
* @return this builder
*/
public Builder endCapture() {
if(this.suffixes.length() > 0 && this.suffixes.indexOf(")") + 1 == this.suffixes.length()) {
if(this.suffixes.indexOf(")") != -1) {
this.suffixes.setLength(suffixes.length() - 1);
return this.add(")");
} else {
Expand Down
69 changes: 53 additions & 16 deletions src/test/java/ru/lanwen/verbalregex/BasicFunctionalityUnitTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,25 @@ public void testStartOfLine() {
assertFalse("Doesn't start with a", testRegex.test("ba"));
}

@Test
public void testStartOfLineFalse() {
VerbalExpression testRegex = VerbalExpression.regex()
.startOfLine(false)
.then("a")
.build();
assertThat(testRegex.test("ba"), is(true));
assertThat(testRegex.test("ab"), is(true));
}

@Test
public void testRangeWithMultiplyRanges() throws Exception {
VerbalExpression regex = VerbalExpression.regex().range("a", "z", "A", "Z").build();

assertThat("Regex with multi-range differs from expected", regex.toString(), equalTo("[a-zA-Z]"));
assertThat("Regex don't matches letter", regex.test("b"), is(true));
assertThat("Regex matches digit, but should match only letter", regex.test("1"), is(false));
}

@Test
public void testEndOfLine() {
VerbalExpression testRegex = new VerbalExpression.Builder()
Expand All @@ -80,6 +99,18 @@ public void testEndOfLine() {
assertFalse("Doesn't end with a", testRegex.test("ab"));
}


@Test
public void testEndOfLineIsFalse() {
VerbalExpression testRegex = VerbalExpression.regex()
.find("a")
.endOfLine(false)
.build();
assertThat(testRegex.test("ba"), is(true));
assertThat(testRegex.test("ab"), is(true));
}


@Test
public void testMaybe() {
VerbalExpression testRegex = new VerbalExpression.Builder()
Expand Down Expand Up @@ -107,6 +138,15 @@ public void testAnyOf() {
assertFalse("Doesn't have an x, y, or z after a", testRegex.test("abc"));
}


@Test
public void testAnySameAsAnyOf() {
VerbalExpression any = VerbalExpression.regex().any("abc").build();
VerbalExpression anyOf = VerbalExpression.regex().anyOf("abc").build();

assertThat("any differs from anyOf", any.toString(), equalTo(anyOf.toString()));
}

@Test
public void testOr() {
VerbalExpression testRegex = new VerbalExpression.Builder()
Expand Down Expand Up @@ -182,6 +222,18 @@ public void testWithAnyCase() {
assertTrue("case insensitive", testRegex.test("a"));
}

@Test
public void testWithAnyCaseIsFalse() {
VerbalExpression testRegex = VerbalExpression.regex()
.withAnyCase()
.startOfLine()
.then("a")
.withAnyCase(false)
.build();

assertThat(testRegex.test("A"), is(false));
}

@Test
public void testSearchOneLine() {
VerbalExpression testRegex = VerbalExpression.regex()
Expand Down Expand Up @@ -253,16 +305,7 @@ public void testCountWithRange() {
assertThat("regex don't match string", regex.test(text1c), is(false));
}


@Test(expected = IndexOutOfBoundsException.class)
public void shouldExceptionWhenTryGetMoreThanCapturedGroup() {
String text = "abc";
VerbalExpression regex = VerbalExpression.regex().find("b").capture().find("c").build();

regex.getText(text, 2);
}

@Test
@Test
public void testEndCapture() {
String text = "aaabcd";
VerbalExpression regex = VerbalExpression.regex()
Expand All @@ -284,12 +327,6 @@ public void testMultiplyCapture() {
assertThat("can't get first captured group", regex.getText(text, 1), equalTo("b"));
assertThat("can't get second captured group", regex.getText(text, 2), equalTo("d"));
}

@Test(expected = IllegalStateException.class)
public void testEndCaptureOnEmptyRegex() {
VerbalExpression.regex().endCapture().build();
}

@Test
public void testOrWithCapture() {
VerbalExpression testRegex = VerbalExpression.regex()
Expand Down
69 changes: 69 additions & 0 deletions src/test/java/ru/lanwen/verbalregex/NegativeCasesTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package ru.lanwen.verbalregex;

import org.junit.Test;

import java.util.regex.PatternSyntaxException;

import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

/**
* User: lanwen
* Date: 11.05.14
* Time: 3:37
*/
public class NegativeCasesTest {

@Test(expected = IllegalStateException.class)
public void testEndCaptureOnEmptyRegex() {
VerbalExpression.regex().endCapture().build();
}

@Test(expected = IndexOutOfBoundsException.class)
public void shouldExceptionWhenTryGetMoreThanCapturedGroup() {
String text = "abc";
VerbalExpression regex = VerbalExpression.regex().find("b").capture().find("c").build();

regex.getText(text, 2);
}

@Test(expected = PatternSyntaxException.class)
public void testRangeWithoutArgs() throws Exception {
VerbalExpression.regex().startOfLine().range().build();
}

@Test(expected = PatternSyntaxException.class)
public void testRangeWithOneArg() throws Exception {
VerbalExpression.regex().startOfLine().range("a").build();
}

@Test
public void rangeWithThreeArgsUsesOnlyFirstTwo() throws Exception {
VerbalExpression regex = VerbalExpression.regex().startOfLine().range("a", "z", "A").build();

assertThat("Range with three args differs from expected", regex.toString(), equalTo("^[a-z]"));
}

@Test
public void orWithNullMatchesAny() throws Exception {
VerbalExpression regex = VerbalExpression.regex().startOfLine().then("a").or(null).build();
assertThat("regex don't matches writed letter", regex.test("a"), is(true));
assertThat("or(null) should match any", regex.test("bcd"), is(true));

assertThat("or(null) extract only first", regex.getText("abcd"), equalTo("a"));
}

@Test
public void orAfterCaptureProduceEmptyGroup() throws Exception {
VerbalExpression regex = VerbalExpression.regex().startOfLine().then("a").capture().or("b").build();

assertThat(regex.toString(), containsString("()|"));

assertThat("regex dont matches string abcd", regex.getText("abcd", 0), equalTo("a"));
assertThat("regex dont extract a by first group", regex.getText("abcd", 1), equalTo("a"));
assertThat("second group should produce empty string", regex.getText("abcd", 2), equalTo(""));

}
}
36 changes: 14 additions & 22 deletions src/test/java/ru/lanwen/verbalregex/RealWorldUnitTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@


import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.core.IsNot.not;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;

Expand Down Expand Up @@ -33,29 +33,21 @@ public void testUrl() {
}

@Test
public void staticFabricsRetunSameAsConstructorExpressions() {
VerbalExpression regexViaFactory = VerbalExpression.regex().anything().build();
VerbalExpression regexViaConstructor = new VerbalExpression.Builder().anything().build();

assertThat("Factory builder method produce not same as constructor regex",
regexViaFactory.toString(), equalTo(regexViaConstructor.toString()));
}

@Test
public void clonedBuilderEqualsOriginal() {
VerbalExpression.Builder builder = VerbalExpression.regex().anything().addModifier('i');
VerbalExpression.Builder clonedBuilder = VerbalExpression.regex(builder);
public void testTelephoneNumber() {
VerbalExpression regex = VerbalExpression.regex()
.startOfLine()
.then("+")
.capture().range("0", "9").count(3).maybe("-").maybe(" ").endCapture()
.count(3)
.endOfLine().build();

assertThat("Cloned builder changed after creating new one",
builder.build().toString(), equalTo(clonedBuilder.build().toString()));
}
String phoneWithSpace = "+097 234 243";
String phoneWithoutSpace = "+097234243";
String phoneWithDash = "+097-234-243";

@Test
public void clonedBuilderCantChangeOriginal() {
VerbalExpression.Builder builder = VerbalExpression.regex().anything().addModifier('i');
VerbalExpression.Builder clonedBuilder = VerbalExpression.regex(builder).endOfLine();
assertThat(regex.testExact(phoneWithSpace), is(true));
assertThat(regex.testExact(phoneWithoutSpace), is(true));
assertThat(regex.testExact(phoneWithDash), is(true));

assertThat("Cloned builder changed after creating new one",
builder.build().toString(), not(clonedBuilder.build().toString()));
}
}
44 changes: 44 additions & 0 deletions src/test/java/ru/lanwen/verbalregex/UsageLibTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package ru.lanwen.verbalregex;

import org.junit.Test;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.core.IsNot.not;
import static org.junit.Assert.assertThat;

/**
* User: lanwen
* Date: 11.05.14
* Time: 3:30
*/
public class UsageLibTest {


@Test
public void staticFabricsRetunSameAsConstructorExpressions() {
VerbalExpression regexViaFactory = VerbalExpression.regex().anything().build();
VerbalExpression regexViaConstructor = new VerbalExpression.Builder().anything().build();

assertThat("Factory builder method produce not same as constructor regex",
regexViaFactory.toString(), equalTo(regexViaConstructor.toString()));
}

@Test
public void clonedBuilderEqualsOriginal() {
VerbalExpression.Builder builder = VerbalExpression.regex().anything().addModifier('i');
VerbalExpression.Builder clonedBuilder = VerbalExpression.regex(builder);

assertThat("Cloned builder changed after creating new one",
builder.build().toString(), equalTo(clonedBuilder.build().toString()));
}

@Test
public void clonedBuilderCantChangeOriginal() {
VerbalExpression.Builder builder = VerbalExpression.regex().anything().addModifier('i');
VerbalExpression.Builder clonedBuilder = VerbalExpression.regex(builder).endOfLine();

assertThat("Cloned builder changed after creating new one",
builder.build().toString(), not(clonedBuilder.build().toString()));
}

}

0 comments on commit b1739ae

Please sign in to comment.