Debian Codegraph @

rpython/rlib/rsre/test/test_match.py

raw symbols SCIP
1 import re, random, py
2 from rpython.rlib.rsre import rsre_char, rsre_constants
3 from rpython.rlib.rsre.rpy import get_code, VERSION
4 from rpython.rlib.rsre.test.support import match, fullmatch, Position as P
5
6
7 def get_code_and_re(regexp):
8 return get_code(regexp), re.compile(regexp)
9
10 def test_get_code_repetition():
11 c1 = get_code(r"a+")
12 c2 = get_code(r"a+")
13 assert c1.pattern == c2.pattern
14
15
16 class TestMatch:
17
18 def test_or(self):
19 r = get_code(r"a|bc|def")
20 assert match(r, "a")
21 assert match(r, "bc")
22 assert match(r, "def")
23 assert not match(r, "ghij")
24
25 def test_any(self):
26 r = get_code(r"ab.cd")
27 assert match(r, "abXcdef")
28 assert not match(r, "ab\ncdef")
29 assert not match(r, "abXcDef")
30
31 def test_any_repetition(self):
32 r = get_code(r"ab.*cd")
33 assert match(r, "abXXXXcdef")
34 assert match(r, "abcdef")
35 assert not match(r, "abX\nXcdef")
36 assert not match(r, "abXXXXcDef")
37
38 def test_any_all(self):
39 r = get_code(r"(?s)ab.cd")
40 assert match(r, "abXcdef")
41 assert match(r, "ab\ncdef")
42 assert not match(r, "ab\ncDef")
43
44 def test_any_all_repetition(self):
45 r = get_code(r"(?s)ab.*cd")
46 assert match(r, "abXXXXcdef")
47 assert match(r, "abcdef")
48 assert match(r, "abX\nXcdef")
49 assert not match(r, "abX\nXcDef")
50
51 def test_assert(self):
52 r = get_code(r"abc(?=def)(.)")
53 res = match(r, "abcdefghi")
54 assert res is not None and res.get_mark(1) == P(4)
55 assert not match(r, "abcdeFghi")
56
57 def test_assert_not(self):
58 r = get_code(r"abc(?!def)(.)")
59 res = match(r, "abcdeFghi")
60 assert res is not None and res.get_mark(1) == P(4)
61 assert not match(r, "abcdefghi")
62
63 def test_lookbehind(self):
64 r = get_code(r"([a-z]*)(?<=de)")
65 assert match(r, "ade")
66 res = match(r, "adefg")
67 assert res is not None and res.get_mark(1) == P(3)
68 assert not match(r, "abc")
69 assert not match(r, "X")
70 assert not match(r, "eX")
71
72 def test_negative_lookbehind(self):
73 def found(s):
74 res = match(r, s)
75 assert res is not None
76 return res.get_mark(1)
77 r = get_code(r"([a-z]*)(?<!dd)")
78 assert found("ade") == P(3)
79 assert found("adefg") == P(5)
80 assert found("abcdd") == P(4)
81 assert found("abddd") == P(3)
82 assert found("adddd") == P(2)
83 assert found("ddddd") == P(1)
84 assert found("abXde") == P(2)
85
86 def test_at(self):
87 r = get_code(r"abc$")
88 assert match(r, "abc")
89 assert not match(r, "abcd")
90 assert not match(r, "ab")
91
92 def test_repeated_set(self):
93 r = get_code(r"[a0x]+f")
94 assert match(r, "a0af")
95 assert not match(r, "a0yaf")
96
97 def test_category(self):
98 r = get_code(r"[\sx]")
99 assert match(r, "x")
100 assert match(r, " ")
101 assert not match(r, "n")
102
103 def test_groupref(self):
104 r = get_code(r"(xx+)\1+$") # match non-prime numbers of x
105 assert not match(r, "xx")
106 assert not match(r, "xxx")
107 assert match(r, "xxxx")
108 assert not match(r, "xxxxx")
109 assert match(r, "xxxxxx")
110 assert not match(r, "xxxxxxx")
111 assert match(r, "xxxxxxxx")
112 assert match(r, "xxxxxxxxx")
113
114 def test_groupref_ignore(self):
115 r = get_code(r"(?i)(xx+)\1+$") # match non-prime numbers of x
116 assert not match(r, "xX")
117 assert not match(r, "xxX")
118 assert match(r, "Xxxx")
119 assert not match(r, "xxxXx")
120 assert match(r, "xXxxxx")
121 assert not match(r, "xxxXxxx")
122 assert match(r, "xxxxxxXx")
123 assert match(r, "xxxXxxxxx")
124
125 def test_groupref_exists(self):
126 r = get_code(r"((a)|(b))c(?(2)d)$")
127 assert not match(r, "ac")
128 assert match(r, "acd")
129 assert match(r, "bc")
130 assert not match(r, "bcd")
131 #
132 r = get_code(r"((a)|(b))c(?(2)d|e)$")
133 assert not match(r, "ac")
134 assert match(r, "acd")
135 assert not match(r, "ace")
136 assert not match(r, "bc")
137 assert not match(r, "bcd")
138 assert match(r, "bce")
139
140 def test_in_ignore(self):
141 r = get_code(r"(?i)[a-f]")
142 assert match(r, "b")
143 assert match(r, "C")
144 assert not match(r, "g")
145 r = get_code(r"(?i)[a-f]+$")
146 assert match(r, "bCdEf")
147 assert not match(r, "g")
148 assert not match(r, "aaagaaa")
149
150 def test_not_literal(self):
151 r = get_code(r"[^a]")
152 assert match(r, "A")
153 assert not match(r, "a")
154 r = get_code(r"[^a]+$")
155 assert match(r, "Bx123")
156 assert not match(r, "--a--")
157
158 def test_not_literal_ignore(self):
159 r = get_code(r"(?i)[^a]")
160 assert match(r, "G")
161 assert not match(r, "a")
162 assert not match(r, "A")
163 r = get_code(r"(?i)[^a]+$")
164 assert match(r, "Gx123")
165 assert not match(r, "--A--")
166
167 def test_repeated_single_character_pattern(self):
168 r = get_code(r"foo(?:(?<=foo)x)+$")
169 assert match(r, "foox")
170
171 def test_flatten_marks(self):
172 r = get_code(r"a(b)c((d)(e))+$")
173 res = match(r, "abcdedede")
174 assert res.flatten_marks() == map(P, [0, 9, 1, 2, 7, 9, 7, 8, 8, 9])
175 assert res.flatten_marks() == map(P, [0, 9, 1, 2, 7, 9, 7, 8, 8, 9])
176
177 def test_bug1(self):
178 # REPEAT_ONE inside REPEAT
179 r = get_code(r"(?:.+)?B")
180 assert match(r, "AB") is not None
181 r = get_code(r"(?:AA+?)+B")
182 assert match(r, "AAAB") is not None
183 r = get_code(r"(?:AA+)+?B")
184 assert match(r, "AAAB") is not None
185 r = get_code(r"(?:AA+?)+?B")
186 assert match(r, "AAAB") is not None
187 # REPEAT inside REPEAT
188 r = get_code(r"(?:(?:xy)+)?B")
189 assert match(r, "xyB") is not None
190 r = get_code(r"(?:xy(?:xy)+?)+B")
191 assert match(r, "xyxyxyB") is not None
192 r = get_code(r"(?:xy(?:xy)+)+?B")
193 assert match(r, "xyxyxyB") is not None
194 r = get_code(r"(?:xy(?:xy)+?)+?B")
195 assert match(r, "xyxyxyB") is not None
196
197 def test_assert_group(self):
198 r = get_code(r"abc(?=(..)f)(.)")
199 res = match(r, "abcdefghi")
200 assert res is not None
201 assert res.span(2) == (P(3), P(4))
202 assert res.span(1) == (P(3), P(5))
203
204 def test_assert_not_group(self):
205 r = get_code(r"abc(?!(de)f)(.)")
206 res = match(r, "abcdeFghi")
207 assert res is not None
208 assert res.span(2) == (P(3), P(4))
209 # this I definitely classify as Horrendously Implementation Dependent.
210 # CPython answers (3, 5).
211 assert res.span(1) == (-1, -1)
212
213 def test_match_start(self):
214 r = get_code(r"^ab")
215 assert match(r, "abc")
216 assert not match(r, "xxxabc", start=3)
217 assert not match(r, "xx\nabc", start=3)
218 #
219 r = get_code(r"(?m)^ab")
220 assert match(r, "abc")
221 assert not match(r, "xxxabc", start=3)
222 assert match(r, "xx\nabc", start=3)
223
224 def test_match_end(self):
225 r = get_code("ab")
226 assert match(r, "abc")
227 assert match(r, "abc", end=333)
228 assert match(r, "abc", end=3)
229 assert match(r, "abc", end=2)
230 assert not match(r, "abc", end=1)
231 assert not match(r, "abc", end=0)
232 assert not match(r, "abc", end=-1)
233
234 def test_match_bug1(self):
235 r = get_code(r'(x??)?$')
236 assert match(r, "x")
237
238 def test_match_bug2(self):
239 r = get_code(r'(x??)??$')
240 assert match(r, "x")
241
242 def test_match_bug3(self):
243 if VERSION == "2.7.5":
244 py.test.skip("pattern fails to compile with exactly 2.7.5 "
245 "(works on 2.7.3 and on 2.7.trunk though)")
246 r = get_code(r'([ax]*?x*)?$')
247 assert match(r, "aaxaa")
248
249 def test_bigcharset(self):
250 for i in range(100):
251 chars = [unichr(random.randrange(0x100, 0xD000))
252 for n in range(random.randrange(1, 25))]
253 pattern = u'[%s]' % (u''.join(chars),)
254 r = get_code(pattern)
255 for c in chars:
256 assert match(r, c)
257 for i in range(200):
258 c = unichr(random.randrange(0x0, 0xD000))
259 res = match(r, c)
260 if c in chars:
261 assert res is not None
262 else:
263 assert res is None
264
265 def test_simple_match_1(self):
266 r = get_code(r"ab*bbbbbbbc")
267 print r
268 m = match(r, "abbbbbbbbbcdef")
269 assert m
270 assert m.match_end == P(11)
271
272 def test_empty_maxuntil(self):
273 r = get_code("\\{\\{((?:.*?)+)\\}\\}")
274 m = match(r, "{{a}}{{b}}")
275 assert m.group(1) == "a"
276
277 def test_fullmatch_1(self):
278 r = get_code(r"ab*c")
279 assert not fullmatch(r, "abbbcdef")
280 assert fullmatch(r, "abbbc")
281
282 def test_fullmatch_2(self):
283 r = get_code(r"a(b*?)")
284 match = fullmatch(r, "abbb")
285 assert match.group(1) == "bbb"
286 assert not fullmatch(r, "abbbc")
287
288 def test_fullmatch_3(self):
289 r = get_code(r"a((bp)*?)c")
290 match = fullmatch(r, "abpbpbpc")
291 assert match.group(1) == "bpbpbp"
292
293 def test_fullmatch_4(self):
294 r = get_code(r"a((bp)*)c")
295 match = fullmatch(r, "abpbpbpc")
296 assert match.group(1) == "bpbpbp"
297
298 def test_fullmatch_assertion(self):
299 r = get_code(r"(?=a).b")
300 assert fullmatch(r, "ab")
301 r = get_code(r"(?!a)..")
302 assert not fullmatch(r, "ab")
303
304 def test_range_ignore(self):
305 from rpython.rlib.unicodedata import unicodedb
306 rsre_char.set_unicode_db(unicodedb)
307 #
308 r = get_code(u"[\U00010428-\U0001044f]", re.I)
309 assert r.pattern.count(rsre_constants.OPCODE_RANGE) == 1
310 if rsre_constants.V37:
311 repl = rsre_constants.OPCODE37_RANGE_UNI_IGNORE
312 else:
313 repl = rsre_constants.OPCODE27_RANGE_IGNORE
314 r.pattern[r.pattern.index(rsre_constants.OPCODE_RANGE)] = repl
315 assert match(r, u"\U00010428")