blob: f133c988f0031b12114de9a6dbc2093f2acae689 [file] [log] [blame]
Fredrik Lundh143328b2000-09-02 11:03:34 +00001# SRE test harness for the Python regression suite
2
3# this is based on test_re.py, but uses a test function instead
4# of all those asserts
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +00005
6import sys
7sys.path=['.']+sys.path
8
Fredrik Lundhf7850422001-01-17 21:51:36 +00009from test_support import verbose, TestFailed
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +000010import sre
Fredrik Lundhf2989b22001-02-18 12:05:16 +000011import sys, os, string, traceback
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +000012
Fredrik Lundh143328b2000-09-02 11:03:34 +000013#
14# test support
15
16def test(expression, result, exception=None):
17 try:
18 r = eval(expression)
19 except:
20 if exception:
21 if not isinstance(sys.exc_value, exception):
22 print expression, "FAILED"
23 # display name, not actual value
24 if exception is sre.error:
25 print "expected", "sre.error"
26 else:
27 print "expected", exception.__name__
28 print "got", sys.exc_type.__name__, str(sys.exc_value)
29 else:
30 print expression, "FAILED"
31 traceback.print_exc(file=sys.stdout)
32 else:
33 if exception:
34 print expression, "FAILED"
35 if exception is sre.error:
36 print "expected", "sre.error"
37 else:
38 print "expected", exception.__name__
39 print "got result", repr(r)
40 else:
41 if r != result:
42 print expression, "FAILED"
43 print "expected", repr(result)
44 print "got result", repr(r)
45
46if verbose:
47 print 'Running tests on character literals'
48
Fredrik Lundh510c97b2000-09-02 16:36:57 +000049for i in [0, 8, 16, 32, 64, 127, 128, 255]:
Fredrik Lundh538f05c2001-01-14 15:15:37 +000050 test(r"""sre.match(r"\%03o" % i, chr(i)) is not None""", 1)
51 test(r"""sre.match(r"\%03o0" % i, chr(i)+"0") is not None""", 1)
52 test(r"""sre.match(r"\%03o8" % i, chr(i)+"8") is not None""", 1)
53 test(r"""sre.match(r"\x%02x" % i, chr(i)) is not None""", 1)
54 test(r"""sre.match(r"\x%02x0" % i, chr(i)+"0") is not None""", 1)
55 test(r"""sre.match(r"\x%02xz" % i, chr(i)+"z") is not None""", 1)
Fredrik Lundh143328b2000-09-02 11:03:34 +000056test(r"""sre.match("\911", "")""", None, sre.error)
57
58#
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +000059# Misc tests from Tim Peters' re.doc
60
61if verbose:
62 print 'Running tests on sre.search and sre.match'
63
Fredrik Lundh03dd0102000-09-03 10:43:16 +000064test(r"""sre.search(r'x*', 'axx').span(0)""", (0, 0))
65test(r"""sre.search(r'x*', 'axx').span()""", (0, 0))
66test(r"""sre.search(r'x+', 'axx').span(0)""", (1, 3))
67test(r"""sre.search(r'x+', 'axx').span()""", (1, 3))
68test(r"""sre.search(r'x', 'aaa')""", None)
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +000069
Fredrik Lundh03dd0102000-09-03 10:43:16 +000070test(r"""sre.match(r'a*', 'xxx').span(0)""", (0, 0))
71test(r"""sre.match(r'a*', 'xxx').span()""", (0, 0))
72test(r"""sre.match(r'x*', 'xxxa').span(0)""", (0, 3))
73test(r"""sre.match(r'x*', 'xxxa').span()""", (0, 3))
74test(r"""sre.match(r'a+', 'xxx')""", None)
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +000075
Fredrik Lundh510c97b2000-09-02 16:36:57 +000076# bug 113254
Fredrik Lundh03dd0102000-09-03 10:43:16 +000077test(r"""sre.match(r'(a)|(b)', 'b').start(1)""", -1)
78test(r"""sre.match(r'(a)|(b)', 'b').end(1)""", -1)
79test(r"""sre.match(r'(a)|(b)', 'b').span(1)""", (-1, -1))
Fredrik Lundh510c97b2000-09-02 16:36:57 +000080
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +000081if verbose:
82 print 'Running tests on sre.sub'
83
Fredrik Lundh03dd0102000-09-03 10:43:16 +000084test(r"""sre.sub(r"(?i)b+", "x", "bbbb BBBB")""", 'x x')
Fredrik Lundh6f013982000-07-03 18:44:21 +000085
Fredrik Lundh143328b2000-09-02 11:03:34 +000086def bump_num(matchobj):
87 int_value = int(matchobj.group(0))
88 return str(int_value + 1)
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +000089
Fredrik Lundh143328b2000-09-02 11:03:34 +000090test(r"""sre.sub(r'\d+', bump_num, '08.2 -2 23x99y')""", '9.3 -3 24x100y')
91test(r"""sre.sub(r'\d+', bump_num, '08.2 -2 23x99y', 3)""", '9.3 -3 23x99y')
Fredrik Lundh6f013982000-07-03 18:44:21 +000092
Fredrik Lundh03dd0102000-09-03 10:43:16 +000093test(r"""sre.sub(r'.', lambda m: r"\n", 'x')""", '\\n')
94test(r"""sre.sub(r'.', r"\n", 'x')""", '\n')
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +000095
Fredrik Lundh143328b2000-09-02 11:03:34 +000096s = r"\1\1"
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +000097
Fredrik Lundh03dd0102000-09-03 10:43:16 +000098test(r"""sre.sub(r'(.)', s, 'x')""", 'xx')
99test(r"""sre.sub(r'(.)', sre.escape(s), 'x')""", s)
100test(r"""sre.sub(r'(.)', lambda m: s, 'x')""", s)
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000101
Fredrik Lundh03dd0102000-09-03 10:43:16 +0000102test(r"""sre.sub(r'(?P<a>x)', '\g<a>\g<a>', 'xx')""", 'xxxx')
103test(r"""sre.sub(r'(?P<a>x)', '\g<a>\g<1>', 'xx')""", 'xxxx')
104test(r"""sre.sub(r'(?P<unk>x)', '\g<unk>\g<unk>', 'xx')""", 'xxxx')
105test(r"""sre.sub(r'(?P<unk>x)', '\g<1>\g<1>', 'xx')""", 'xxxx')
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000106
Fredrik Lundh03dd0102000-09-03 10:43:16 +0000107test(r"""sre.sub(r'a', r'\t\n\v\r\f\a\b\B\Z\a\A\w\W\s\S\d\D', 'a')""", '\t\n\v\r\f\a\b\\B\\Z\a\\A\\w\\W\\s\\S\\d\\D')
108test(r"""sre.sub(r'a', '\t\n\v\r\f\a', 'a')""", '\t\n\v\r\f\a')
109test(r"""sre.sub(r'a', '\t\n\v\r\f\a', 'a')""", (chr(9)+chr(10)+chr(11)+chr(13)+chr(12)+chr(7)))
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000110
Fredrik Lundh03dd0102000-09-03 10:43:16 +0000111test(r"""sre.sub(r'^\s*', 'X', 'test')""", 'Xtest')
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000112
Fredrik Lundh143328b2000-09-02 11:03:34 +0000113# qualified sub
Fredrik Lundh03dd0102000-09-03 10:43:16 +0000114test(r"""sre.sub(r'a', 'b', 'aaaaa')""", 'bbbbb')
115test(r"""sre.sub(r'a', 'b', 'aaaaa', 1)""", 'baaaa')
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000116
Fredrik Lundh19f977b2000-09-24 14:46:23 +0000117# bug 114660
118test(r"""sre.sub(r'(\S)\s+(\S)', r'\1 \2', 'hello there')""", 'hello there')
119
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000120if verbose:
121 print 'Running tests on symbolic references'
122
Fredrik Lundh03dd0102000-09-03 10:43:16 +0000123test(r"""sre.sub(r'(?P<a>x)', '\g<a', 'xx')""", None, sre.error)
124test(r"""sre.sub(r'(?P<a>x)', '\g<', 'xx')""", None, sre.error)
125test(r"""sre.sub(r'(?P<a>x)', '\g', 'xx')""", None, sre.error)
126test(r"""sre.sub(r'(?P<a>x)', '\g<a a>', 'xx')""", None, sre.error)
127test(r"""sre.sub(r'(?P<a>x)', '\g<1a1>', 'xx')""", None, sre.error)
128test(r"""sre.sub(r'(?P<a>x)', '\g<ab>', 'xx')""", None, IndexError)
129test(r"""sre.sub(r'(?P<a>x)|(?P<b>y)', '\g<b>', 'xx')""", None, sre.error)
130test(r"""sre.sub(r'(?P<a>x)|(?P<b>y)', '\\2', 'xx')""", None, sre.error)
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000131
132if verbose:
133 print 'Running tests on sre.subn'
134
Fredrik Lundh03dd0102000-09-03 10:43:16 +0000135test(r"""sre.subn(r"(?i)b+", "x", "bbbb BBBB")""", ('x x', 2))
136test(r"""sre.subn(r"b+", "x", "bbbb BBBB")""", ('x BBBB', 1))
137test(r"""sre.subn(r"b+", "x", "xyz")""", ('xyz', 0))
138test(r"""sre.subn(r"b*", "x", "xyz")""", ('xxxyxzx', 4))
139test(r"""sre.subn(r"b*", "x", "xyz", 2)""", ('xxxyz', 2))
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000140
141if verbose:
142 print 'Running tests on sre.split'
Fredrik Lundh6f013982000-07-03 18:44:21 +0000143
Fredrik Lundh03dd0102000-09-03 10:43:16 +0000144test(r"""sre.split(r":", ":a:b::c")""", ['', 'a', 'b', '', 'c'])
145test(r"""sre.split(r":*", ":a:b::c")""", ['', 'a', 'b', 'c'])
146test(r"""sre.split(r"(:*)", ":a:b::c")""", ['', ':', 'a', ':', 'b', '::', 'c'])
147test(r"""sre.split(r"(?::*)", ":a:b::c")""", ['', 'a', 'b', 'c'])
148test(r"""sre.split(r"(:)*", ":a:b::c")""", ['', ':', 'a', ':', 'b', ':', 'c'])
149test(r"""sre.split(r"([b:]+)", ":a:b::c")""", ['', ':', 'a', ':b::', 'c'])
150test(r"""sre.split(r"(b)|(:+)", ":a:b::c")""",
Fredrik Lundh143328b2000-09-02 11:03:34 +0000151 ['', None, ':', 'a', None, ':', '', 'b', None, '', None, '::', 'c'])
Fredrik Lundh03dd0102000-09-03 10:43:16 +0000152test(r"""sre.split(r"(?:b)|(?::+)", ":a:b::c")""", ['', 'a', '', '', 'c'])
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000153
Fredrik Lundh03dd0102000-09-03 10:43:16 +0000154test(r"""sre.split(r":", ":a:b::c", 2)""", ['', 'a', 'b::c'])
155test(r"""sre.split(r':', 'a:b:c:d', 2)""", ['a', 'b', 'c:d'])
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000156
Fredrik Lundh03dd0102000-09-03 10:43:16 +0000157test(r"""sre.split(r"(:)", ":a:b::c", 2)""", ['', ':', 'a', ':', 'b::c'])
158test(r"""sre.split(r"(:*)", ":a:b::c", 2)""", ['', ':', 'a', ':', 'b::c'])
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000159
160if verbose:
161 print "Running tests on sre.findall"
162
Fredrik Lundh03dd0102000-09-03 10:43:16 +0000163test(r"""sre.findall(r":+", "abc")""", [])
164test(r"""sre.findall(r":+", "a:b::c:::d")""", [":", "::", ":::"])
165test(r"""sre.findall(r"(:+)", "a:b::c:::d")""", [":", "::", ":::"])
166test(r"""sre.findall(r"(:)(:*)", "a:b::c:::d")""",
Fredrik Lundh143328b2000-09-02 11:03:34 +0000167 [(":", ""), (":", ":"), (":", "::")])
Fredrik Lundh03dd0102000-09-03 10:43:16 +0000168test(r"""sre.findall(r"(a)|(b)", "abc")""", [("a", ""), ("", "b")])
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000169
Fredrik Lundhebc37b22000-10-28 19:30:41 +0000170# bug 117612
171test(r"""sre.findall(r"(a|(b))", "aba")""", [("a", ""),("b", "b"),("a", "")])
172
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000173if verbose:
174 print "Running tests on sre.match"
175
Fredrik Lundh03dd0102000-09-03 10:43:16 +0000176test(r"""sre.match(r'a', 'a').groups()""", ())
177test(r"""sre.match(r'(a)', 'a').groups()""", ('a',))
178test(r"""sre.match(r'(a)', 'a').group(0)""", 'a')
179test(r"""sre.match(r'(a)', 'a').group(1)""", 'a')
180test(r"""sre.match(r'(a)', 'a').group(1, 1)""", ('a', 'a'))
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000181
Fredrik Lundh03dd0102000-09-03 10:43:16 +0000182pat = sre.compile(r'((a)|(b))(c)?')
Fredrik Lundh143328b2000-09-02 11:03:34 +0000183test(r"""pat.match('a').groups()""", ('a', 'a', None, None))
184test(r"""pat.match('b').groups()""", ('b', None, 'b', None))
185test(r"""pat.match('ac').groups()""", ('a', 'a', None, 'c'))
186test(r"""pat.match('bc').groups()""", ('b', None, 'b', 'c'))
187test(r"""pat.match('bc').groups("")""", ('b', "", 'b', 'c'))
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000188
Fredrik Lundh03dd0102000-09-03 10:43:16 +0000189pat = sre.compile(r'(?:(?P<a1>a)|(?P<b2>b))(?P<c3>c)?')
Fredrik Lundh143328b2000-09-02 11:03:34 +0000190test(r"""pat.match('a').group(1, 2, 3)""", ('a', None, None))
191test(r"""pat.match('b').group('a1', 'b2', 'c3')""", (None, 'b', None))
192test(r"""pat.match('ac').group(1, 'b2', 3)""", ('a', None, 'c'))
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000193
194if verbose:
195 print "Running tests on sre.escape"
196
Fredrik Lundh143328b2000-09-02 11:03:34 +0000197p = ""
198for i in range(0, 256):
199 p = p + chr(i)
Fredrik Lundh538f05c2001-01-14 15:15:37 +0000200 test(r"""sre.match(sre.escape(chr(i)), chr(i)) is not None""", 1)
Fredrik Lundh143328b2000-09-02 11:03:34 +0000201 test(r"""sre.match(sre.escape(chr(i)), chr(i)).span()""", (0,1))
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000202
Fredrik Lundh143328b2000-09-02 11:03:34 +0000203pat = sre.compile(sre.escape(p))
Fredrik Lundh538f05c2001-01-14 15:15:37 +0000204test(r"""pat.match(p) is not None""", 1)
Fredrik Lundh143328b2000-09-02 11:03:34 +0000205test(r"""pat.match(p).span()""", (0,256))
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000206
207if verbose:
208 print 'Pickling a SRE_Pattern instance'
209
210try:
211 import pickle
Fredrik Lundh03dd0102000-09-03 10:43:16 +0000212 pat = sre.compile(r'a(?:b|(c|e){1,2}?|d)+?(.)')
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000213 s = pickle.dumps(pat)
214 pat = pickle.loads(s)
215except:
216 print TestFailed, 're module pickle' # expected
217
218try:
219 import cPickle
Fredrik Lundh03dd0102000-09-03 10:43:16 +0000220 pat = sre.compile(r'a(?:b|(c|e){1,2}?|d)+?(.)')
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000221 s = cPickle.dumps(pat)
222 pat = cPickle.loads(s)
223except:
224 print TestFailed, 're module cPickle' # expected
225
Fredrik Lundh143328b2000-09-02 11:03:34 +0000226# constants
227test(r"""sre.I""", sre.IGNORECASE)
228test(r"""sre.L""", sre.LOCALE)
229test(r"""sre.M""", sre.MULTILINE)
230test(r"""sre.S""", sre.DOTALL)
231test(r"""sre.X""", sre.VERBOSE)
232test(r"""sre.T""", sre.TEMPLATE)
233test(r"""sre.U""", sre.UNICODE)
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000234
235for flags in [sre.I, sre.M, sre.X, sre.S, sre.L, sre.T, sre.U]:
236 try:
237 r = sre.compile('^pattern$', flags)
238 except:
239 print 'Exception raised on flag', flags
240
Fredrik Lundh96ab4652000-08-03 16:29:50 +0000241if verbose:
242 print 'Test engine limitations'
243
244# Try nasty case that overflows the straightforward recursive
245# implementation of repeated groups.
Fredrik Lundh015415e2001-03-22 23:48:28 +0000246test("sre.match('(x)*', 50000*'x').span()", (0, 50000), RuntimeError)
247test("sre.match(r'(x)*y', 50000*'x'+'y').span()", (0, 50001), RuntimeError)
248test("sre.match(r'(x)*?y', 50000*'x'+'y').span()", (0, 50001), RuntimeError)
Fredrik Lundh96ab4652000-08-03 16:29:50 +0000249
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000250from re_tests import *
251
252if verbose:
253 print 'Running re_tests test suite'
254else:
255 # To save time, only run the first and last 10 tests
256 #tests = tests[:10] + tests[-10:]
Fredrik Lundh6f013982000-07-03 18:44:21 +0000257 pass
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000258
259for t in tests:
260 sys.stdout.flush()
261 pattern=s=outcome=repl=expected=None
262 if len(t)==5:
263 pattern, s, outcome, repl, expected = t
264 elif len(t)==3:
Fredrik Lundh6f013982000-07-03 18:44:21 +0000265 pattern, s, outcome = t
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000266 else:
267 raise ValueError, ('Test tuples should have 3 or 5 fields',t)
268
269 try:
270 obj=sre.compile(pattern)
271 except sre.error:
272 if outcome==SYNTAX_ERROR: pass # Expected a syntax error
Fredrik Lundh6f013982000-07-03 18:44:21 +0000273 else:
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000274 print '=== Syntax error:', t
275 except KeyboardInterrupt: raise KeyboardInterrupt
276 except:
277 print '*** Unexpected error ***', t
278 if verbose:
279 traceback.print_exc(file=sys.stdout)
280 else:
281 try:
282 result=obj.search(s)
283 except (sre.error), msg:
284 print '=== Unexpected exception', t, repr(msg)
285 if outcome==SYNTAX_ERROR:
Fredrik Lundh03dd0102000-09-03 10:43:16 +0000286 print '=== Compiled incorrectly', t
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000287 elif outcome==FAIL:
288 if result is None: pass # No match, as expected
289 else: print '=== Succeeded incorrectly', t
290 elif outcome==SUCCEED:
291 if result is not None:
292 # Matched, as expected, so now we compute the
293 # result string and compare it to our expected result.
294 start, end = result.span(0)
295 vardict={'found': result.group(0),
296 'groups': result.group(),
297 'flags': result.re.flags}
298 for i in range(1, 100):
299 try:
300 gi = result.group(i)
301 # Special hack because else the string concat fails:
302 if gi is None:
303 gi = "None"
304 except IndexError:
305 gi = "Error"
306 vardict['g%d' % i] = gi
307 for i in result.re.groupindex.keys():
308 try:
309 gi = result.group(i)
310 if gi is None:
311 gi = "None"
312 except IndexError:
313 gi = "Error"
314 vardict[i] = gi
315 repl=eval(repl, vardict)
316 if repl!=expected:
317 print '=== grouping error', t,
318 print repr(repl)+' should be '+repr(expected)
319 else:
320 print '=== Failed incorrectly', t
Fredrik Lundh90a07912000-06-30 07:50:59 +0000321 continue
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000322
323 # Try the match on a unicode string, and check that it
324 # still succeeds.
Fredrik Lundh1c5aa692001-01-16 07:37:30 +0000325 try:
326 u = unicode(s, "latin-1")
327 except NameError:
328 pass
Fredrik Lundhb25e1ad2001-03-22 15:50:10 +0000329 except TypeError:
330 continue # skip unicode test strings
Fredrik Lundh1c5aa692001-01-16 07:37:30 +0000331 else:
332 result=obj.search(u)
333 if result==None:
334 print '=== Fails on unicode match', t
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000335
336 # Try the match on a unicode pattern, and check that it
337 # still succeeds.
Fredrik Lundh1c5aa692001-01-16 07:37:30 +0000338 try:
339 u = unicode(pattern, "latin-1")
340 except NameError:
341 pass
342 else:
343 obj=sre.compile(u)
344 result=obj.search(s)
345 if result==None:
346 print '=== Fails on unicode pattern match', t
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000347
348 # Try the match with the search area limited to the extent
349 # of the match and see if it still succeeds. \B will
350 # break (because it won't match at the end or start of a
351 # string), so we'll ignore patterns that feature it.
Fredrik Lundh6f013982000-07-03 18:44:21 +0000352
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000353 if pattern[:2]!='\\B' and pattern[-2:]!='\\B':
354 obj=sre.compile(pattern)
Fredrik Lundh90a07912000-06-30 07:50:59 +0000355 result=obj.search(s, result.start(0), result.end(0)+1)
356 if result==None:
357 print '=== Failed on range-limited match', t
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000358
359 # Try the match with IGNORECASE enabled, and check that it
360 # still succeeds.
361 obj=sre.compile(pattern, sre.IGNORECASE)
362 result=obj.search(s)
363 if result==None:
364 print '=== Fails on case-insensitive match', t
365
366 # Try the match with LOCALE enabled, and check that it
367 # still succeeds.
368 obj=sre.compile(pattern, sre.LOCALE)
369 result=obj.search(s)
370 if result==None:
371 print '=== Fails on locale-sensitive match', t
372
Fredrik Lundhc2ed6212000-08-01 13:01:43 +0000373 # Try the match with UNICODE locale enabled, and check
374 # that it still succeeds.
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000375 obj=sre.compile(pattern, sre.UNICODE)
376 result=obj.search(s)
377 if result==None:
378 print '=== Fails on unicode-sensitive match', t