-
Notifications
You must be signed in to change notification settings - Fork 46
/
Tests.py
180 lines (146 loc) · 5.96 KB
/
Tests.py
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import unittest
from RegExpBuilder import RegExpBuilder
class Test(unittest.TestCase):
def test_start(self):
regex = RegExpBuilder()
regex.start()
regex.exactly(1).of("p")
regex = regex.getRegExp()
self.assertTrue(regex.match("p") is not None)
self.assertTrue(regex.match("qp") is None)
def test_end(self):
regex = RegExpBuilder()
regex.exactly(1).of("p")
regex.end()
regex = regex.getRegExp()
self.assertTrue(regex.match("p") is not None)
self.assertTrue(regex.match("pq") is None)
def test_eitherLike_orLike(self):
regex = RegExpBuilder()
regex.start()
regex.eitherLike(lambda r: r.exactly(1).of("p"))
regex.orLike(lambda r: r.exactly(2).of("q"))
regex.end()
regex = regex.getRegExp()
self.assertTrue(regex.match("p") is not None)
self.assertTrue(regex.match("qq") is not None)
self.assertTrue(regex.match("pqq") is None)
self.assertTrue(regex.match("qqp") is None)
def test_exactly(self):
regex = RegExpBuilder()
regex.start()
regex.exactly(3).of("p")
regex.end()
regex = regex.getRegExp()
self.assertTrue(regex.match("ppp") is not None)
self.assertTrue(regex.match("pp") is None)
self.assertTrue(regex.match("pppp") is None)
def test_min(self):
regex = RegExpBuilder()
regex.start()
regex.min(2).of("p")
regex.end()
regex = regex.getRegExp()
self.assertTrue(regex.match("pp") is not None)
self.assertTrue(regex.match("ppp") is not None)
self.assertTrue(regex.match("ppppppp") is not None)
self.assertTrue(regex.match("p") is None)
def test_max(self):
regex = RegExpBuilder()
regex.start()
regex.max(3).of("p")
regex.end()
regex = regex.getRegExp()
self.assertTrue(regex.match("p") is not None)
self.assertTrue(regex.match("pp") is not None)
self.assertTrue(regex.match("ppp") is not None)
self.assertTrue(regex.match("pppp") is None)
self.assertTrue(regex.match("pppppppp") is None)
def test_min_max(self):
regex = RegExpBuilder()
regex.start()
regex.min(3).max(7).of("p")
regex.end()
regex = regex.getRegExp()
self.assertTrue(regex.match("ppp") is not None)
self.assertTrue(regex.match("ppppp") is not None)
self.assertTrue(regex.match("ppppppp") is not None)
self.assertTrue(regex.match("pp") is None)
self.assertTrue(regex.match("p") is None)
self.assertTrue(regex.match("pppppppp") is None)
self.assertTrue(regex.match("pppppppppppp") is None)
def test_of(self):
regex = RegExpBuilder()
regex.start()
regex.exactly(2).of("p p p ")
regex.end()
regex = regex.getRegExp()
self.assertTrue(regex.match("p p p p p p ") is not None)
self.assertTrue(regex.match("p p p p pp") is None)
def test_ofAny(self):
regex = RegExpBuilder()
regex.start()
regex.exactly(3).ofAny()
regex.end()
regex = regex.getRegExp()
self.assertTrue(regex.match("pqr") is not None)
def test_fromClass(self):
someLetters = ["p", "q", "r"]
regex = RegExpBuilder()
regex.start()
regex.exactly(3).fromClass(someLetters)
regex.end()
regex = regex.getRegExp()
self.assertTrue(regex.match("ppp") is not None)
self.assertTrue(regex.match("qqq") is not None)
self.assertTrue(regex.match("ppq") is not None)
self.assertTrue(regex.match("rqp") is not None)
self.assertTrue(regex.match("pyy") is None)
def test_notFromClass(self):
someLetters = ["p", "q", "r"]
regex = RegExpBuilder()
regex.start()
regex.exactly(3).notFromClass(someLetters)
regex.end()
regex = regex.getRegExp()
self.assertTrue(regex.match("lmn") is not None)
self.assertTrue(regex.match("mnq") is None)
def test_like(self):
pattern = lambda r: r.min(1).of("p").min(2).of("q")
regex = RegExpBuilder()
regex.start()
regex.exactly(2).like(pattern)
regex.end()
regex = regex.getRegExp()
self.assertTrue(regex.match("pqqpqq") is not None)
self.assertTrue(regex.match("qppqpp") is None)
def test_reluctantly(self):
regex = RegExpBuilder()
regex.exactly(2).of("p")
regex.min(2).ofAny().reluctantly()
regex.exactly(2).of("p")
regex = regex.getRegExp()
self.assertTrue(regex.match("pprrrrpprrpp").group() == "pprrrrpp")
def test_behind(self):
regex = RegExpBuilder()
regex.exactly(1).of("dart")
regex.behind(lambda r: r.exactly(1).of("lang"))
regex = regex.getRegExp()
self.assertTrue(regex.match("dartlang").group() == "dart")
self.assertTrue(regex.match("dartpqr") is None)
def test_notBehind(self):
regex = RegExpBuilder()
regex.exactly(1).of("dart")
regex.notBehind(lambda r: r.exactly(1).of("pqr"))
regex = regex.getRegExp()
self.assertTrue(regex.match("dartlang") is not None)
self.assertTrue(regex.match("dartpqr") is None)
def test_asCapturingGroup(self):
regex = RegExpBuilder()
regex.min(1).max(3).of("p")
regex.exactly(1).of("dart").asCapturingGroup()
regex.exactly(1).fromClass(["p", "q", "r"])
regex = regex.getRegExp()
self.assertTrue(regex.match("pdartq").group(1) == "dart")
if __name__ == '__main__':
unittest.main()