-
Notifications
You must be signed in to change notification settings - Fork 241
/
Copy pathRealWorldUnitTest.java
121 lines (99 loc) · 5.09 KB
/
RealWorldUnitTest.java
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package ru.lanwen.verbalregex;
import org.junit.Ignore;
import org.junit.Test;
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;
import static ru.lanwen.verbalregex.matchers.TestsExactMatcher.matchesExactly;
public class RealWorldUnitTest {
@Test
public void testUrl() {
VerbalExpression testRegex = new VerbalExpression.Builder()
.startOfLine()
.then("http")
.maybe("s")
.then("://")
.maybe("www.")
.anythingBut(" ")
.endOfLine()
.build();
// Create an example URL
String testUrl = "https://www.google.com";
assertThat("Matches Google's url", testRegex, matchesTo(testUrl)); //True
assertThat("Regex doesn't match same regex as in example",
testRegex.toString(),
equalTo("^(?:http)(?:s)?(?:\\:\\/\\/)(?:www\\.)?(?:[^\\ ]*)$"));
}
@Test
public void testTelephoneNumber() {
VerbalExpression regex = regex()
.startOfLine()
.then("+")
.capture().range("0", "9").count(3).maybe("-").maybe(" ").endCapture()
.count(3)
.endOfLine().build();
String phoneWithSpace = "+097 234 243";
String phoneWithoutSpace = "+097234243";
String phoneWithDash = "+097-234-243";
assertThat(regex, matchesExactly(phoneWithSpace));
assertThat(regex, matchesExactly(phoneWithoutSpace));
assertThat(regex, matchesExactly(phoneWithDash));
}
@Test
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().add("\\d+").endCapture().tab()
.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().add("\\d+").endCapture().tab()
.capt().range("0", "1").count(1).endCapture().tab()
.capt().add("\\d+").endCapture().tab()
.capt().add("\\d+").endCapture().tab()
.capt().range("0", "1").count(1).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().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);
VerbalExpression regex2 = regex()
.add(digits).add(digits)
.add(range).add(host).add(range).add(digits).add(range)
.add(digits).add(digits)
.add(range).add(digits).add(fake).build();
assertThat(regex2, matchesExactly(logLine));
//(\\d+)\\t(\\d+)\\t([0-1]{1})\\t(http://localhost:20\\d{3})\\t([0-1]{1})
// \\t(\\d+)\\t([0-1]{1})\\t(\\d+)\\t(\\d+)\\t([0-1]{1})\\t(\\d+)\\t(FAKE[1-2]{1})
/*
3 4 1 http://localhost:20001 1 28800 0 528800 1000000000 0 528800 STR1
3 5 1 http://localhost:20002 1 28800 0 528800 1000020002 0 528800 STR2
4 6 0 http://localhost:20002 1 48800 0 528800 1000000000 0 528800 STR1
4 7 0 http://localhost:20003 1 48800 0 528800 1000020003 0 528800 STR2
5 8 1 http://localhost:20003 1 68800 0 528800 1000000000 0 528800 STR1
5 9 1 http://localhost:20004 1 28800 0 528800 1000020004 0 528800 STR2
*/
}
@Test
public void unusualRegex() throws Exception {
assertThat(regex().add("[A-Z0-1!-|]").build().toString(), equalTo("[A-Z0-1!-|]"));
}
@Test
@Ignore("Planned in 1.3")
public void captureWithName() throws Exception {
}
@Test
public void oneOfShouldFindEpisodeTitleOfStarWarsMovies() {
VerbalExpression regex = VerbalExpression.regex()
.find("Star Wars: ")
.oneOf("The Phantom Menace", "Attack of the Clones", "Revenge of the Sith",
"The Force Awakens", "A New Hope", "The Empire Strikes Back", "Return of the Jedi")
.build();
assertThat(regex, matchesTo("Star Wars: The Empire Strikes Back"));
assertThat(regex, matchesTo("Star Wars: Return of the Jedi"));
}
}