-
Notifications
You must be signed in to change notification settings - Fork 240
Expand file tree
/
Copy pathNegativeCasesTest.java
More file actions
90 lines (69 loc) · 3.2 KB
/
NegativeCasesTest.java
File metadata and controls
90 lines (69 loc) · 3.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
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.junit.Assert.assertThat;
import static ru.lanwen.verbalregex.VerbalExpression.regex;
import static ru.lanwen.verbalregex.matchers.TestMatchMatcher.matchesTo;
/**
* User: lanwen
* Date: 11.05.14
* Time: 3:37
*/
public class NegativeCasesTest {
@Test(expected = IllegalStateException.class)
public void testEndCaptureOnEmptyRegex() {
regex().endCapture().build();
}
@Test(expected = IndexOutOfBoundsException.class)
public void shouldExceptionWhenTryGetMoreThanCapturedGroup() {
String text = "abc";
VerbalExpression regex = regex().find("b").capture().find("c").build();
regex.getText(text, 2);
}
@Test(expected = PatternSyntaxException.class)
public void testRangeWithoutArgs() throws Exception {
regex().startOfLine().range().build();
}
@Test(expected = PatternSyntaxException.class)
public void testRangeWithOneArg() throws Exception {
regex().startOfLine().range("a").build();
}
@Test
public void rangeWithThreeArgsUsesOnlyFirstTwo() throws Exception {
VerbalExpression regex = 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 = regex().startOfLine().then("a").or(null).build();
assertThat("regex don't matches writed letter", regex, matchesTo("a"));
assertThat("or(null) should match any", regex, matchesTo("bcd"));
assertThat("or(null) extract only first", regex.getText("abcd"), equalTo("a"));
}
@Test
public void orAfterCaptureProduceEmptyGroup() throws Exception {
VerbalExpression regex = 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(""));
}
@Test
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().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().oneOrMore("some").build().toString()));
}
@Test(expected = java.util.regex.PatternSyntaxException.class)
public void twoOpenCaptsWithOrThrowSyntaxException() throws Exception {
VerbalExpression regex = regex().capt().capt().or("0").build();
String ignored = regex.toString();
}
}