-
Notifications
You must be signed in to change notification settings - Fork 241
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24 from lanwen/tests
add - tests for range method and case, any, start and end of line
- Loading branch information
Showing
5 changed files
with
188 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
src/test/java/ru/lanwen/verbalregex/NegativeCasesTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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("")); | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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())); | ||
} | ||
|
||
} |