blob: db871620fac6597a676fa7cad1bc5f783d6d9c38 [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
Barry Warsaw04f357c2002-07-23 19:04:11 +00009from test.test_support import verbose, TestFailed, have_unicode
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
Gustavo Niemeyer4e7be062002-11-06 14:06:53 +000081# bug described in patch 527371
82test(r"""sre.match(r'(a)?a','a').lastindex""", None)
83test(r"""sre.match(r'(a)(b)?b','ab').lastindex""", 1)
84test(r"""sre.match(r'(?P<a>a)(?P<b>b)?b','ab').lastgroup""", 'a')
85
Guido van Rossum41c99e72003-04-14 17:59:34 +000086# bug 545855 -- This pattern failed to cause a compile error as it
87# should, instead provoking a TypeError.
88test(r"""sre.compile('foo[a-')""", None, sre.error)
89
90# bugs 418626 at al. -- Testing Greg Chapman's addition of op code
91# SRE_OP_MIN_REPEAT_ONE for eliminating recursion on simple uses of
92# pattern '*?' on a long string.
93test(r"""sre.match('.*?c', 10000*'ab'+'cd').end(0)""", 20001)
94test(r"""sre.match('.*?cd', 5000*'ab'+'c'+5000*'ab'+'cde').end(0)""", 20003)
95test(r"""sre.match('.*?cd', 20000*'abc'+'de').end(0)""", 60001)
96# non-simple '*?' still recurses and hits the recursion limit
97test(r"""sre.search('(a|b)*?c', 10000*'ab'+'cd').end(0)""", None, RuntimeError)
98
Martin v. Löwis53d93ad2003-04-19 08:37:24 +000099# bug 612074
100pat=u"["+sre.escape(u"\u2039")+u"]"
101test(r"""sre.compile(pat) and 1""", 1, None)
102
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000103if verbose:
104 print 'Running tests on sre.sub'
105
Fredrik Lundh03dd0102000-09-03 10:43:16 +0000106test(r"""sre.sub(r"(?i)b+", "x", "bbbb BBBB")""", 'x x')
Fredrik Lundh6f013982000-07-03 18:44:21 +0000107
Fredrik Lundh143328b2000-09-02 11:03:34 +0000108def bump_num(matchobj):
109 int_value = int(matchobj.group(0))
110 return str(int_value + 1)
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000111
Fredrik Lundh143328b2000-09-02 11:03:34 +0000112test(r"""sre.sub(r'\d+', bump_num, '08.2 -2 23x99y')""", '9.3 -3 24x100y')
113test(r"""sre.sub(r'\d+', bump_num, '08.2 -2 23x99y', 3)""", '9.3 -3 23x99y')
Fredrik Lundh6f013982000-07-03 18:44:21 +0000114
Fredrik Lundh03dd0102000-09-03 10:43:16 +0000115test(r"""sre.sub(r'.', lambda m: r"\n", 'x')""", '\\n')
116test(r"""sre.sub(r'.', r"\n", 'x')""", '\n')
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000117
Fredrik Lundh143328b2000-09-02 11:03:34 +0000118s = r"\1\1"
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000119
Fredrik Lundh03dd0102000-09-03 10:43:16 +0000120test(r"""sre.sub(r'(.)', s, 'x')""", 'xx')
121test(r"""sre.sub(r'(.)', sre.escape(s), 'x')""", s)
122test(r"""sre.sub(r'(.)', lambda m: s, 'x')""", s)
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000123
Fredrik Lundh03dd0102000-09-03 10:43:16 +0000124test(r"""sre.sub(r'(?P<a>x)', '\g<a>\g<a>', 'xx')""", 'xxxx')
125test(r"""sre.sub(r'(?P<a>x)', '\g<a>\g<1>', 'xx')""", 'xxxx')
126test(r"""sre.sub(r'(?P<unk>x)', '\g<unk>\g<unk>', 'xx')""", 'xxxx')
127test(r"""sre.sub(r'(?P<unk>x)', '\g<1>\g<1>', 'xx')""", 'xxxx')
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000128
Fredrik Lundh59b68652001-09-18 20:55:24 +0000129# bug 449964: fails for group followed by other escape
130test(r"""sre.sub(r'(?P<unk>x)', '\g<1>\g<1>\\b', 'xx')""", 'xx\bxx\b')
131
Fredrik Lundh03dd0102000-09-03 10:43:16 +0000132test(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')
133test(r"""sre.sub(r'a', '\t\n\v\r\f\a', 'a')""", '\t\n\v\r\f\a')
134test(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 +0000135
Fredrik Lundh03dd0102000-09-03 10:43:16 +0000136test(r"""sre.sub(r'^\s*', 'X', 'test')""", 'Xtest')
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000137
Fredrik Lundh143328b2000-09-02 11:03:34 +0000138# qualified sub
Fredrik Lundh03dd0102000-09-03 10:43:16 +0000139test(r"""sre.sub(r'a', 'b', 'aaaaa')""", 'bbbbb')
140test(r"""sre.sub(r'a', 'b', 'aaaaa', 1)""", 'baaaa')
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000141
Fredrik Lundh19f977b2000-09-24 14:46:23 +0000142# bug 114660
143test(r"""sre.sub(r'(\S)\s+(\S)', r'\1 \2', 'hello there')""", 'hello there')
144
Guido van Rossume056e4d2001-08-10 14:52:48 +0000145# Test for sub() on escaped characters, see SF bug #449000
146test(r"""sre.sub(r'\r\n', r'\n', 'abc\r\ndef\r\n')""", 'abc\ndef\n')
147test(r"""sre.sub('\r\n', r'\n', 'abc\r\ndef\r\n')""", 'abc\ndef\n')
148test(r"""sre.sub(r'\r\n', '\n', 'abc\r\ndef\r\n')""", 'abc\ndef\n')
149test(r"""sre.sub('\r\n', '\n', 'abc\r\ndef\r\n')""", 'abc\ndef\n')
150
Fredrik Lundh21009b92001-09-18 18:47:09 +0000151# Test for empty sub() behaviour, see SF bug #462270
152test(r"""sre.sub('x*', '-', 'abxd')""", '-a-b-d-')
153test(r"""sre.sub('x+', '-', 'abxd')""", 'ab-d')
154
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000155if verbose:
156 print 'Running tests on symbolic references'
157
Fredrik Lundh03dd0102000-09-03 10:43:16 +0000158test(r"""sre.sub(r'(?P<a>x)', '\g<a', 'xx')""", None, sre.error)
159test(r"""sre.sub(r'(?P<a>x)', '\g<', 'xx')""", None, sre.error)
160test(r"""sre.sub(r'(?P<a>x)', '\g', 'xx')""", None, sre.error)
161test(r"""sre.sub(r'(?P<a>x)', '\g<a a>', 'xx')""", None, sre.error)
162test(r"""sre.sub(r'(?P<a>x)', '\g<1a1>', 'xx')""", None, sre.error)
163test(r"""sre.sub(r'(?P<a>x)', '\g<ab>', 'xx')""", None, IndexError)
164test(r"""sre.sub(r'(?P<a>x)|(?P<b>y)', '\g<b>', 'xx')""", None, sre.error)
165test(r"""sre.sub(r'(?P<a>x)|(?P<b>y)', '\\2', 'xx')""", None, sre.error)
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000166
167if verbose:
168 print 'Running tests on sre.subn'
169
Fredrik Lundh03dd0102000-09-03 10:43:16 +0000170test(r"""sre.subn(r"(?i)b+", "x", "bbbb BBBB")""", ('x x', 2))
171test(r"""sre.subn(r"b+", "x", "bbbb BBBB")""", ('x BBBB', 1))
172test(r"""sre.subn(r"b+", "x", "xyz")""", ('xyz', 0))
173test(r"""sre.subn(r"b*", "x", "xyz")""", ('xxxyxzx', 4))
174test(r"""sre.subn(r"b*", "x", "xyz", 2)""", ('xxxyz', 2))
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000175
176if verbose:
177 print 'Running tests on sre.split'
Fredrik Lundh6f013982000-07-03 18:44:21 +0000178
Fredrik Lundh03dd0102000-09-03 10:43:16 +0000179test(r"""sre.split(r":", ":a:b::c")""", ['', 'a', 'b', '', 'c'])
Fredrik Lundhf864aa82001-10-22 06:01:56 +0000180test(r"""sre.split(r":+", ":a:b:::")""", ['', 'a', 'b', ''])
Fredrik Lundh03dd0102000-09-03 10:43:16 +0000181test(r"""sre.split(r":*", ":a:b::c")""", ['', 'a', 'b', 'c'])
182test(r"""sre.split(r"(:*)", ":a:b::c")""", ['', ':', 'a', ':', 'b', '::', 'c'])
183test(r"""sre.split(r"(?::*)", ":a:b::c")""", ['', 'a', 'b', 'c'])
184test(r"""sre.split(r"(:)*", ":a:b::c")""", ['', ':', 'a', ':', 'b', ':', 'c'])
185test(r"""sre.split(r"([b:]+)", ":a:b::c")""", ['', ':', 'a', ':b::', 'c'])
186test(r"""sre.split(r"(b)|(:+)", ":a:b::c")""",
Fredrik Lundh143328b2000-09-02 11:03:34 +0000187 ['', None, ':', 'a', None, ':', '', 'b', None, '', None, '::', 'c'])
Fredrik Lundh03dd0102000-09-03 10:43:16 +0000188test(r"""sre.split(r"(?:b)|(?::+)", ":a:b::c")""", ['', 'a', '', '', 'c'])
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000189
Fredrik Lundh03dd0102000-09-03 10:43:16 +0000190test(r"""sre.split(r":", ":a:b::c", 2)""", ['', 'a', 'b::c'])
191test(r"""sre.split(r':', 'a:b:c:d', 2)""", ['a', 'b', 'c:d'])
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000192
Fredrik Lundh03dd0102000-09-03 10:43:16 +0000193test(r"""sre.split(r"(:)", ":a:b::c", 2)""", ['', ':', 'a', ':', 'b::c'])
194test(r"""sre.split(r"(:*)", ":a:b::c", 2)""", ['', ':', 'a', ':', 'b::c'])
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000195
196if verbose:
197 print "Running tests on sre.findall"
198
Fredrik Lundh03dd0102000-09-03 10:43:16 +0000199test(r"""sre.findall(r":+", "abc")""", [])
200test(r"""sre.findall(r":+", "a:b::c:::d")""", [":", "::", ":::"])
201test(r"""sre.findall(r"(:+)", "a:b::c:::d")""", [":", "::", ":::"])
202test(r"""sre.findall(r"(:)(:*)", "a:b::c:::d")""",
Fredrik Lundh143328b2000-09-02 11:03:34 +0000203 [(":", ""), (":", ":"), (":", "::")])
Fredrik Lundh03dd0102000-09-03 10:43:16 +0000204test(r"""sre.findall(r"(a)|(b)", "abc")""", [("a", ""), ("", "b")])
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000205
Fredrik Lundhebc37b22000-10-28 19:30:41 +0000206# bug 117612
207test(r"""sre.findall(r"(a|(b))", "aba")""", [("a", ""),("b", "b"),("a", "")])
208
Fredrik Lundhb7747e22001-10-28 20:15:40 +0000209if sys.hexversion >= 0x02020000:
210 if verbose:
211 print "Running tests on sre.finditer"
212 def fixup(seq):
213 # convert iterator to list
214 if not hasattr(seq, "next") or not hasattr(seq, "__iter__"):
215 print "finditer returned", type(seq)
216 return map(lambda item: item.group(0), seq)
217 # sanity
218 test(r"""fixup(sre.finditer(r":+", "a:b::c:::d"))""", [":", "::", ":::"])
219
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000220if verbose:
221 print "Running tests on sre.match"
222
Fredrik Lundh03dd0102000-09-03 10:43:16 +0000223test(r"""sre.match(r'a', 'a').groups()""", ())
224test(r"""sre.match(r'(a)', 'a').groups()""", ('a',))
225test(r"""sre.match(r'(a)', 'a').group(0)""", 'a')
226test(r"""sre.match(r'(a)', 'a').group(1)""", 'a')
227test(r"""sre.match(r'(a)', 'a').group(1, 1)""", ('a', 'a'))
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000228
Fredrik Lundh03dd0102000-09-03 10:43:16 +0000229pat = sre.compile(r'((a)|(b))(c)?')
Fredrik Lundh143328b2000-09-02 11:03:34 +0000230test(r"""pat.match('a').groups()""", ('a', 'a', None, None))
231test(r"""pat.match('b').groups()""", ('b', None, 'b', None))
232test(r"""pat.match('ac').groups()""", ('a', 'a', None, 'c'))
233test(r"""pat.match('bc').groups()""", ('b', None, 'b', 'c'))
234test(r"""pat.match('bc').groups("")""", ('b', "", 'b', 'c'))
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000235
Fredrik Lundh03dd0102000-09-03 10:43:16 +0000236pat = sre.compile(r'(?:(?P<a1>a)|(?P<b2>b))(?P<c3>c)?')
Fredrik Lundh143328b2000-09-02 11:03:34 +0000237test(r"""pat.match('a').group(1, 2, 3)""", ('a', None, None))
238test(r"""pat.match('b').group('a1', 'b2', 'c3')""", (None, 'b', None))
239test(r"""pat.match('ac').group(1, 'b2', 3)""", ('a', None, 'c'))
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000240
Fredrik Lundh397a6542001-10-18 19:30:16 +0000241# bug 448951 (similar to 429357, but with single char match)
242# (Also test greedy matches.)
243for op in '','?','*':
244 test(r"""sre.match(r'((.%s):)?z', 'z').groups()"""%op, (None, None))
245 test(r"""sre.match(r'((.%s):)?z', 'a:z').groups()"""%op, ('a:', 'a'))
246
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000247if verbose:
248 print "Running tests on sre.escape"
249
Fredrik Lundh143328b2000-09-02 11:03:34 +0000250p = ""
251for i in range(0, 256):
252 p = p + chr(i)
Fredrik Lundh538f05c2001-01-14 15:15:37 +0000253 test(r"""sre.match(sre.escape(chr(i)), chr(i)) is not None""", 1)
Fredrik Lundh143328b2000-09-02 11:03:34 +0000254 test(r"""sre.match(sre.escape(chr(i)), chr(i)).span()""", (0,1))
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000255
Fredrik Lundh143328b2000-09-02 11:03:34 +0000256pat = sre.compile(sre.escape(p))
Fredrik Lundh538f05c2001-01-14 15:15:37 +0000257test(r"""pat.match(p) is not None""", 1)
Fredrik Lundh143328b2000-09-02 11:03:34 +0000258test(r"""pat.match(p).span()""", (0,256))
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000259
260if verbose:
Fredrik Lundh1296a8d2001-10-21 18:04:11 +0000261 print 'Running tests on sre.Scanner'
262
263def s_ident(scanner, token): return token
264def s_operator(scanner, token): return "op%s" % token
265def s_float(scanner, token): return float(token)
266def s_int(scanner, token): return int(token)
267
268scanner = sre.Scanner([
269 (r"[a-zA-Z_]\w*", s_ident),
270 (r"\d+\.\d*", s_float),
271 (r"\d+", s_int),
272 (r"=|\+|-|\*|/", s_operator),
273 (r"\s+", None),
274 ])
275
276# sanity check
277test('scanner.scan("sum = 3*foo + 312.50 + bar")',
278 (['sum', 'op=', 3, 'op*', 'foo', 'op+', 312.5, 'op+', 'bar'], ''))
279
280if verbose:
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000281 print 'Pickling a SRE_Pattern instance'
282
283try:
284 import pickle
Fredrik Lundh03dd0102000-09-03 10:43:16 +0000285 pat = sre.compile(r'a(?:b|(c|e){1,2}?|d)+?(.)')
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000286 s = pickle.dumps(pat)
287 pat = pickle.loads(s)
288except:
Guido van Rossumbaefceb2001-12-08 05:11:15 +0000289 print TestFailed, 're module pickle'
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000290
291try:
292 import cPickle
Fredrik Lundh03dd0102000-09-03 10:43:16 +0000293 pat = sre.compile(r'a(?:b|(c|e){1,2}?|d)+?(.)')
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000294 s = cPickle.dumps(pat)
295 pat = cPickle.loads(s)
296except:
Guido van Rossumbaefceb2001-12-08 05:11:15 +0000297 print TestFailed, 're module cPickle'
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000298
Fredrik Lundh143328b2000-09-02 11:03:34 +0000299# constants
300test(r"""sre.I""", sre.IGNORECASE)
301test(r"""sre.L""", sre.LOCALE)
302test(r"""sre.M""", sre.MULTILINE)
303test(r"""sre.S""", sre.DOTALL)
304test(r"""sre.X""", sre.VERBOSE)
305test(r"""sre.T""", sre.TEMPLATE)
306test(r"""sre.U""", sre.UNICODE)
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000307
308for flags in [sre.I, sre.M, sre.X, sre.S, sre.L, sre.T, sre.U]:
309 try:
310 r = sre.compile('^pattern$', flags)
311 except:
312 print 'Exception raised on flag', flags
313
Fredrik Lundh96ab4652000-08-03 16:29:50 +0000314if verbose:
315 print 'Test engine limitations'
316
317# Try nasty case that overflows the straightforward recursive
318# implementation of repeated groups.
Fredrik Lundh015415e2001-03-22 23:48:28 +0000319test("sre.match('(x)*', 50000*'x').span()", (0, 50000), RuntimeError)
320test("sre.match(r'(x)*y', 50000*'x'+'y').span()", (0, 50001), RuntimeError)
Fredrik Lundh82b23072001-12-09 16:13:15 +0000321test("sre.match(r'(x)*?y', 50000*'x'+'y').span()", (0, 50001), RuntimeError)
Fredrik Lundh96ab4652000-08-03 16:29:50 +0000322
Barry Warsaw408b6d32002-07-30 23:27:12 +0000323from test.re_tests import *
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000324
325if verbose:
326 print 'Running re_tests test suite'
327else:
328 # To save time, only run the first and last 10 tests
329 #tests = tests[:10] + tests[-10:]
Fredrik Lundh6f013982000-07-03 18:44:21 +0000330 pass
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000331
332for t in tests:
333 sys.stdout.flush()
334 pattern=s=outcome=repl=expected=None
335 if len(t)==5:
336 pattern, s, outcome, repl, expected = t
337 elif len(t)==3:
Fredrik Lundh6f013982000-07-03 18:44:21 +0000338 pattern, s, outcome = t
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000339 else:
340 raise ValueError, ('Test tuples should have 3 or 5 fields',t)
341
342 try:
343 obj=sre.compile(pattern)
344 except sre.error:
345 if outcome==SYNTAX_ERROR: pass # Expected a syntax error
Fredrik Lundh6f013982000-07-03 18:44:21 +0000346 else:
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000347 print '=== Syntax error:', t
348 except KeyboardInterrupt: raise KeyboardInterrupt
349 except:
350 print '*** Unexpected error ***', t
351 if verbose:
352 traceback.print_exc(file=sys.stdout)
353 else:
354 try:
355 result=obj.search(s)
356 except (sre.error), msg:
357 print '=== Unexpected exception', t, repr(msg)
358 if outcome==SYNTAX_ERROR:
Fredrik Lundh03dd0102000-09-03 10:43:16 +0000359 print '=== Compiled incorrectly', t
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000360 elif outcome==FAIL:
361 if result is None: pass # No match, as expected
362 else: print '=== Succeeded incorrectly', t
363 elif outcome==SUCCEED:
364 if result is not None:
365 # Matched, as expected, so now we compute the
366 # result string and compare it to our expected result.
367 start, end = result.span(0)
368 vardict={'found': result.group(0),
369 'groups': result.group(),
370 'flags': result.re.flags}
371 for i in range(1, 100):
372 try:
373 gi = result.group(i)
374 # Special hack because else the string concat fails:
375 if gi is None:
376 gi = "None"
377 except IndexError:
378 gi = "Error"
379 vardict['g%d' % i] = gi
380 for i in result.re.groupindex.keys():
381 try:
382 gi = result.group(i)
383 if gi is None:
384 gi = "None"
385 except IndexError:
386 gi = "Error"
387 vardict[i] = gi
388 repl=eval(repl, vardict)
389 if repl!=expected:
390 print '=== grouping error', t,
391 print repr(repl)+' should be '+repr(expected)
392 else:
393 print '=== Failed incorrectly', t
Fredrik Lundh90a07912000-06-30 07:50:59 +0000394 continue
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000395
396 # Try the match on a unicode string, and check that it
397 # still succeeds.
Fredrik Lundh1c5aa692001-01-16 07:37:30 +0000398 try:
399 u = unicode(s, "latin-1")
400 except NameError:
401 pass
Fredrik Lundhb25e1ad2001-03-22 15:50:10 +0000402 except TypeError:
403 continue # skip unicode test strings
Fredrik Lundh1c5aa692001-01-16 07:37:30 +0000404 else:
405 result=obj.search(u)
406 if result==None:
407 print '=== Fails on unicode match', t
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000408
409 # Try the match on a unicode pattern, and check that it
410 # still succeeds.
Fredrik Lundh1c5aa692001-01-16 07:37:30 +0000411 try:
412 u = unicode(pattern, "latin-1")
413 except NameError:
414 pass
415 else:
416 obj=sre.compile(u)
417 result=obj.search(s)
418 if result==None:
419 print '=== Fails on unicode pattern match', t
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000420
421 # Try the match with the search area limited to the extent
422 # of the match and see if it still succeeds. \B will
423 # break (because it won't match at the end or start of a
424 # string), so we'll ignore patterns that feature it.
Fredrik Lundh6f013982000-07-03 18:44:21 +0000425
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000426 if pattern[:2]!='\\B' and pattern[-2:]!='\\B':
427 obj=sre.compile(pattern)
Fredrik Lundh90a07912000-06-30 07:50:59 +0000428 result=obj.search(s, result.start(0), result.end(0)+1)
429 if result==None:
430 print '=== Failed on range-limited match', t
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000431
432 # Try the match with IGNORECASE enabled, and check that it
433 # still succeeds.
434 obj=sre.compile(pattern, sre.IGNORECASE)
435 result=obj.search(s)
436 if result==None:
437 print '=== Fails on case-insensitive match', t
438
439 # Try the match with LOCALE enabled, and check that it
440 # still succeeds.
441 obj=sre.compile(pattern, sre.LOCALE)
442 result=obj.search(s)
443 if result==None:
444 print '=== Fails on locale-sensitive match', t
445
Fredrik Lundhc2ed6212000-08-01 13:01:43 +0000446 # Try the match with UNICODE locale enabled, and check
447 # that it still succeeds.
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000448 if have_unicode:
449 obj=sre.compile(pattern, sre.UNICODE)
450 result=obj.search(s)
451 if result==None:
452 print '=== Fails on unicode-sensitive match', t