blob: 6a00affa369f641f8bb271fff9dbcd439ef66e03 [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
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +000086if verbose:
87 print 'Running tests on sre.sub'
88
Fredrik Lundh03dd0102000-09-03 10:43:16 +000089test(r"""sre.sub(r"(?i)b+", "x", "bbbb BBBB")""", 'x x')
Fredrik Lundh6f013982000-07-03 18:44:21 +000090
Fredrik Lundh143328b2000-09-02 11:03:34 +000091def bump_num(matchobj):
92 int_value = int(matchobj.group(0))
93 return str(int_value + 1)
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +000094
Fredrik Lundh143328b2000-09-02 11:03:34 +000095test(r"""sre.sub(r'\d+', bump_num, '08.2 -2 23x99y')""", '9.3 -3 24x100y')
96test(r"""sre.sub(r'\d+', bump_num, '08.2 -2 23x99y', 3)""", '9.3 -3 23x99y')
Fredrik Lundh6f013982000-07-03 18:44:21 +000097
Fredrik Lundh03dd0102000-09-03 10:43:16 +000098test(r"""sre.sub(r'.', lambda m: r"\n", 'x')""", '\\n')
99test(r"""sre.sub(r'.', r"\n", 'x')""", '\n')
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000100
Fredrik Lundh143328b2000-09-02 11:03:34 +0000101s = r"\1\1"
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000102
Fredrik Lundh03dd0102000-09-03 10:43:16 +0000103test(r"""sre.sub(r'(.)', s, 'x')""", 'xx')
104test(r"""sre.sub(r'(.)', sre.escape(s), 'x')""", s)
105test(r"""sre.sub(r'(.)', lambda m: s, 'x')""", s)
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000106
Fredrik Lundh03dd0102000-09-03 10:43:16 +0000107test(r"""sre.sub(r'(?P<a>x)', '\g<a>\g<a>', 'xx')""", 'xxxx')
108test(r"""sre.sub(r'(?P<a>x)', '\g<a>\g<1>', 'xx')""", 'xxxx')
109test(r"""sre.sub(r'(?P<unk>x)', '\g<unk>\g<unk>', 'xx')""", 'xxxx')
110test(r"""sre.sub(r'(?P<unk>x)', '\g<1>\g<1>', 'xx')""", 'xxxx')
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000111
Fredrik Lundh59b68652001-09-18 20:55:24 +0000112# bug 449964: fails for group followed by other escape
113test(r"""sre.sub(r'(?P<unk>x)', '\g<1>\g<1>\\b', 'xx')""", 'xx\bxx\b')
114
Fredrik Lundh03dd0102000-09-03 10:43:16 +0000115test(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')
116test(r"""sre.sub(r'a', '\t\n\v\r\f\a', 'a')""", '\t\n\v\r\f\a')
117test(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 +0000118
Fredrik Lundh03dd0102000-09-03 10:43:16 +0000119test(r"""sre.sub(r'^\s*', 'X', 'test')""", 'Xtest')
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000120
Fredrik Lundh143328b2000-09-02 11:03:34 +0000121# qualified sub
Fredrik Lundh03dd0102000-09-03 10:43:16 +0000122test(r"""sre.sub(r'a', 'b', 'aaaaa')""", 'bbbbb')
123test(r"""sre.sub(r'a', 'b', 'aaaaa', 1)""", 'baaaa')
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000124
Fredrik Lundh19f977b2000-09-24 14:46:23 +0000125# bug 114660
126test(r"""sre.sub(r'(\S)\s+(\S)', r'\1 \2', 'hello there')""", 'hello there')
127
Guido van Rossume056e4d2001-08-10 14:52:48 +0000128# Test for sub() on escaped characters, see SF bug #449000
129test(r"""sre.sub(r'\r\n', r'\n', 'abc\r\ndef\r\n')""", 'abc\ndef\n')
130test(r"""sre.sub('\r\n', r'\n', 'abc\r\ndef\r\n')""", 'abc\ndef\n')
131test(r"""sre.sub(r'\r\n', '\n', 'abc\r\ndef\r\n')""", 'abc\ndef\n')
132test(r"""sre.sub('\r\n', '\n', 'abc\r\ndef\r\n')""", 'abc\ndef\n')
133
Fredrik Lundh21009b92001-09-18 18:47:09 +0000134# Test for empty sub() behaviour, see SF bug #462270
135test(r"""sre.sub('x*', '-', 'abxd')""", '-a-b-d-')
136test(r"""sre.sub('x+', '-', 'abxd')""", 'ab-d')
137
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000138if verbose:
139 print 'Running tests on symbolic references'
140
Fredrik Lundh03dd0102000-09-03 10:43:16 +0000141test(r"""sre.sub(r'(?P<a>x)', '\g<a', 'xx')""", None, sre.error)
142test(r"""sre.sub(r'(?P<a>x)', '\g<', 'xx')""", None, sre.error)
143test(r"""sre.sub(r'(?P<a>x)', '\g', 'xx')""", None, sre.error)
144test(r"""sre.sub(r'(?P<a>x)', '\g<a a>', 'xx')""", None, sre.error)
145test(r"""sre.sub(r'(?P<a>x)', '\g<1a1>', 'xx')""", None, sre.error)
146test(r"""sre.sub(r'(?P<a>x)', '\g<ab>', 'xx')""", None, IndexError)
147test(r"""sre.sub(r'(?P<a>x)|(?P<b>y)', '\g<b>', 'xx')""", None, sre.error)
148test(r"""sre.sub(r'(?P<a>x)|(?P<b>y)', '\\2', 'xx')""", None, sre.error)
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000149
150if verbose:
151 print 'Running tests on sre.subn'
152
Fredrik Lundh03dd0102000-09-03 10:43:16 +0000153test(r"""sre.subn(r"(?i)b+", "x", "bbbb BBBB")""", ('x x', 2))
154test(r"""sre.subn(r"b+", "x", "bbbb BBBB")""", ('x BBBB', 1))
155test(r"""sre.subn(r"b+", "x", "xyz")""", ('xyz', 0))
156test(r"""sre.subn(r"b*", "x", "xyz")""", ('xxxyxzx', 4))
157test(r"""sre.subn(r"b*", "x", "xyz", 2)""", ('xxxyz', 2))
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000158
159if verbose:
160 print 'Running tests on sre.split'
Fredrik Lundh6f013982000-07-03 18:44:21 +0000161
Fredrik Lundh03dd0102000-09-03 10:43:16 +0000162test(r"""sre.split(r":", ":a:b::c")""", ['', 'a', 'b', '', 'c'])
Fredrik Lundhf864aa82001-10-22 06:01:56 +0000163test(r"""sre.split(r":+", ":a:b:::")""", ['', 'a', 'b', ''])
Fredrik Lundh03dd0102000-09-03 10:43:16 +0000164test(r"""sre.split(r":*", ":a:b::c")""", ['', 'a', 'b', 'c'])
165test(r"""sre.split(r"(:*)", ":a:b::c")""", ['', ':', 'a', ':', 'b', '::', 'c'])
166test(r"""sre.split(r"(?::*)", ":a:b::c")""", ['', 'a', 'b', 'c'])
167test(r"""sre.split(r"(:)*", ":a:b::c")""", ['', ':', 'a', ':', 'b', ':', 'c'])
168test(r"""sre.split(r"([b:]+)", ":a:b::c")""", ['', ':', 'a', ':b::', 'c'])
169test(r"""sre.split(r"(b)|(:+)", ":a:b::c")""",
Fredrik Lundh143328b2000-09-02 11:03:34 +0000170 ['', None, ':', 'a', None, ':', '', 'b', None, '', None, '::', 'c'])
Fredrik Lundh03dd0102000-09-03 10:43:16 +0000171test(r"""sre.split(r"(?:b)|(?::+)", ":a:b::c")""", ['', 'a', '', '', 'c'])
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000172
Fredrik Lundh03dd0102000-09-03 10:43:16 +0000173test(r"""sre.split(r":", ":a:b::c", 2)""", ['', 'a', 'b::c'])
174test(r"""sre.split(r':', 'a:b:c:d', 2)""", ['a', 'b', 'c:d'])
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000175
Fredrik Lundh03dd0102000-09-03 10:43:16 +0000176test(r"""sre.split(r"(:)", ":a:b::c", 2)""", ['', ':', 'a', ':', 'b::c'])
177test(r"""sre.split(r"(:*)", ":a:b::c", 2)""", ['', ':', 'a', ':', 'b::c'])
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000178
179if verbose:
180 print "Running tests on sre.findall"
181
Fredrik Lundh03dd0102000-09-03 10:43:16 +0000182test(r"""sre.findall(r":+", "abc")""", [])
183test(r"""sre.findall(r":+", "a:b::c:::d")""", [":", "::", ":::"])
184test(r"""sre.findall(r"(:+)", "a:b::c:::d")""", [":", "::", ":::"])
185test(r"""sre.findall(r"(:)(:*)", "a:b::c:::d")""",
Fredrik Lundh143328b2000-09-02 11:03:34 +0000186 [(":", ""), (":", ":"), (":", "::")])
Fredrik Lundh03dd0102000-09-03 10:43:16 +0000187test(r"""sre.findall(r"(a)|(b)", "abc")""", [("a", ""), ("", "b")])
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000188
Fredrik Lundhebc37b22000-10-28 19:30:41 +0000189# bug 117612
190test(r"""sre.findall(r"(a|(b))", "aba")""", [("a", ""),("b", "b"),("a", "")])
191
Fredrik Lundhb7747e22001-10-28 20:15:40 +0000192if sys.hexversion >= 0x02020000:
193 if verbose:
194 print "Running tests on sre.finditer"
195 def fixup(seq):
196 # convert iterator to list
197 if not hasattr(seq, "next") or not hasattr(seq, "__iter__"):
198 print "finditer returned", type(seq)
199 return map(lambda item: item.group(0), seq)
200 # sanity
201 test(r"""fixup(sre.finditer(r":+", "a:b::c:::d"))""", [":", "::", ":::"])
202
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000203if verbose:
204 print "Running tests on sre.match"
205
Fredrik Lundh03dd0102000-09-03 10:43:16 +0000206test(r"""sre.match(r'a', 'a').groups()""", ())
207test(r"""sre.match(r'(a)', 'a').groups()""", ('a',))
208test(r"""sre.match(r'(a)', 'a').group(0)""", 'a')
209test(r"""sre.match(r'(a)', 'a').group(1)""", 'a')
210test(r"""sre.match(r'(a)', 'a').group(1, 1)""", ('a', 'a'))
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000211
Fredrik Lundh03dd0102000-09-03 10:43:16 +0000212pat = sre.compile(r'((a)|(b))(c)?')
Fredrik Lundh143328b2000-09-02 11:03:34 +0000213test(r"""pat.match('a').groups()""", ('a', 'a', None, None))
214test(r"""pat.match('b').groups()""", ('b', None, 'b', None))
215test(r"""pat.match('ac').groups()""", ('a', 'a', None, 'c'))
216test(r"""pat.match('bc').groups()""", ('b', None, 'b', 'c'))
217test(r"""pat.match('bc').groups("")""", ('b', "", 'b', 'c'))
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000218
Fredrik Lundh03dd0102000-09-03 10:43:16 +0000219pat = sre.compile(r'(?:(?P<a1>a)|(?P<b2>b))(?P<c3>c)?')
Fredrik Lundh143328b2000-09-02 11:03:34 +0000220test(r"""pat.match('a').group(1, 2, 3)""", ('a', None, None))
221test(r"""pat.match('b').group('a1', 'b2', 'c3')""", (None, 'b', None))
222test(r"""pat.match('ac').group(1, 'b2', 3)""", ('a', None, 'c'))
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000223
Fredrik Lundh397a6542001-10-18 19:30:16 +0000224# bug 448951 (similar to 429357, but with single char match)
225# (Also test greedy matches.)
226for op in '','?','*':
227 test(r"""sre.match(r'((.%s):)?z', 'z').groups()"""%op, (None, None))
228 test(r"""sre.match(r'((.%s):)?z', 'a:z').groups()"""%op, ('a:', 'a'))
229
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000230if verbose:
231 print "Running tests on sre.escape"
232
Fredrik Lundh143328b2000-09-02 11:03:34 +0000233p = ""
234for i in range(0, 256):
235 p = p + chr(i)
Fredrik Lundh538f05c2001-01-14 15:15:37 +0000236 test(r"""sre.match(sre.escape(chr(i)), chr(i)) is not None""", 1)
Fredrik Lundh143328b2000-09-02 11:03:34 +0000237 test(r"""sre.match(sre.escape(chr(i)), chr(i)).span()""", (0,1))
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000238
Fredrik Lundh143328b2000-09-02 11:03:34 +0000239pat = sre.compile(sre.escape(p))
Fredrik Lundh538f05c2001-01-14 15:15:37 +0000240test(r"""pat.match(p) is not None""", 1)
Fredrik Lundh143328b2000-09-02 11:03:34 +0000241test(r"""pat.match(p).span()""", (0,256))
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000242
243if verbose:
Fredrik Lundh1296a8d2001-10-21 18:04:11 +0000244 print 'Running tests on sre.Scanner'
245
246def s_ident(scanner, token): return token
247def s_operator(scanner, token): return "op%s" % token
248def s_float(scanner, token): return float(token)
249def s_int(scanner, token): return int(token)
250
251scanner = sre.Scanner([
252 (r"[a-zA-Z_]\w*", s_ident),
253 (r"\d+\.\d*", s_float),
254 (r"\d+", s_int),
255 (r"=|\+|-|\*|/", s_operator),
256 (r"\s+", None),
257 ])
258
259# sanity check
260test('scanner.scan("sum = 3*foo + 312.50 + bar")',
261 (['sum', 'op=', 3, 'op*', 'foo', 'op+', 312.5, 'op+', 'bar'], ''))
262
263if verbose:
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000264 print 'Pickling a SRE_Pattern instance'
265
266try:
267 import pickle
Fredrik Lundh03dd0102000-09-03 10:43:16 +0000268 pat = sre.compile(r'a(?:b|(c|e){1,2}?|d)+?(.)')
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000269 s = pickle.dumps(pat)
270 pat = pickle.loads(s)
271except:
Guido van Rossumbaefceb2001-12-08 05:11:15 +0000272 print TestFailed, 're module pickle'
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000273
274try:
275 import cPickle
Fredrik Lundh03dd0102000-09-03 10:43:16 +0000276 pat = sre.compile(r'a(?:b|(c|e){1,2}?|d)+?(.)')
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000277 s = cPickle.dumps(pat)
278 pat = cPickle.loads(s)
279except:
Guido van Rossumbaefceb2001-12-08 05:11:15 +0000280 print TestFailed, 're module cPickle'
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000281
Fredrik Lundh143328b2000-09-02 11:03:34 +0000282# constants
283test(r"""sre.I""", sre.IGNORECASE)
284test(r"""sre.L""", sre.LOCALE)
285test(r"""sre.M""", sre.MULTILINE)
286test(r"""sre.S""", sre.DOTALL)
287test(r"""sre.X""", sre.VERBOSE)
288test(r"""sre.T""", sre.TEMPLATE)
289test(r"""sre.U""", sre.UNICODE)
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000290
291for flags in [sre.I, sre.M, sre.X, sre.S, sre.L, sre.T, sre.U]:
292 try:
293 r = sre.compile('^pattern$', flags)
294 except:
295 print 'Exception raised on flag', flags
296
Fredrik Lundh96ab4652000-08-03 16:29:50 +0000297if verbose:
298 print 'Test engine limitations'
299
300# Try nasty case that overflows the straightforward recursive
301# implementation of repeated groups.
Fredrik Lundh015415e2001-03-22 23:48:28 +0000302test("sre.match('(x)*', 50000*'x').span()", (0, 50000), RuntimeError)
303test("sre.match(r'(x)*y', 50000*'x'+'y').span()", (0, 50001), RuntimeError)
Fredrik Lundh82b23072001-12-09 16:13:15 +0000304test("sre.match(r'(x)*?y', 50000*'x'+'y').span()", (0, 50001), RuntimeError)
Fredrik Lundh96ab4652000-08-03 16:29:50 +0000305
Barry Warsaw408b6d32002-07-30 23:27:12 +0000306from test.re_tests import *
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000307
308if verbose:
309 print 'Running re_tests test suite'
310else:
311 # To save time, only run the first and last 10 tests
312 #tests = tests[:10] + tests[-10:]
Fredrik Lundh6f013982000-07-03 18:44:21 +0000313 pass
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000314
315for t in tests:
316 sys.stdout.flush()
317 pattern=s=outcome=repl=expected=None
318 if len(t)==5:
319 pattern, s, outcome, repl, expected = t
320 elif len(t)==3:
Fredrik Lundh6f013982000-07-03 18:44:21 +0000321 pattern, s, outcome = t
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000322 else:
323 raise ValueError, ('Test tuples should have 3 or 5 fields',t)
324
325 try:
326 obj=sre.compile(pattern)
327 except sre.error:
328 if outcome==SYNTAX_ERROR: pass # Expected a syntax error
Fredrik Lundh6f013982000-07-03 18:44:21 +0000329 else:
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000330 print '=== Syntax error:', t
331 except KeyboardInterrupt: raise KeyboardInterrupt
332 except:
333 print '*** Unexpected error ***', t
334 if verbose:
335 traceback.print_exc(file=sys.stdout)
336 else:
337 try:
338 result=obj.search(s)
339 except (sre.error), msg:
340 print '=== Unexpected exception', t, repr(msg)
341 if outcome==SYNTAX_ERROR:
Fredrik Lundh03dd0102000-09-03 10:43:16 +0000342 print '=== Compiled incorrectly', t
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000343 elif outcome==FAIL:
344 if result is None: pass # No match, as expected
345 else: print '=== Succeeded incorrectly', t
346 elif outcome==SUCCEED:
347 if result is not None:
348 # Matched, as expected, so now we compute the
349 # result string and compare it to our expected result.
350 start, end = result.span(0)
351 vardict={'found': result.group(0),
352 'groups': result.group(),
353 'flags': result.re.flags}
354 for i in range(1, 100):
355 try:
356 gi = result.group(i)
357 # Special hack because else the string concat fails:
358 if gi is None:
359 gi = "None"
360 except IndexError:
361 gi = "Error"
362 vardict['g%d' % i] = gi
363 for i in result.re.groupindex.keys():
364 try:
365 gi = result.group(i)
366 if gi is None:
367 gi = "None"
368 except IndexError:
369 gi = "Error"
370 vardict[i] = gi
371 repl=eval(repl, vardict)
372 if repl!=expected:
373 print '=== grouping error', t,
374 print repr(repl)+' should be '+repr(expected)
375 else:
376 print '=== Failed incorrectly', t
Fredrik Lundh90a07912000-06-30 07:50:59 +0000377 continue
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000378
379 # Try the match on a unicode string, and check that it
380 # still succeeds.
Fredrik Lundh1c5aa692001-01-16 07:37:30 +0000381 try:
382 u = unicode(s, "latin-1")
383 except NameError:
384 pass
Fredrik Lundhb25e1ad2001-03-22 15:50:10 +0000385 except TypeError:
386 continue # skip unicode test strings
Fredrik Lundh1c5aa692001-01-16 07:37:30 +0000387 else:
388 result=obj.search(u)
389 if result==None:
390 print '=== Fails on unicode match', t
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000391
392 # Try the match on a unicode pattern, and check that it
393 # still succeeds.
Fredrik Lundh1c5aa692001-01-16 07:37:30 +0000394 try:
395 u = unicode(pattern, "latin-1")
396 except NameError:
397 pass
398 else:
399 obj=sre.compile(u)
400 result=obj.search(s)
401 if result==None:
402 print '=== Fails on unicode pattern match', t
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000403
404 # Try the match with the search area limited to the extent
405 # of the match and see if it still succeeds. \B will
406 # break (because it won't match at the end or start of a
407 # string), so we'll ignore patterns that feature it.
Fredrik Lundh6f013982000-07-03 18:44:21 +0000408
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000409 if pattern[:2]!='\\B' and pattern[-2:]!='\\B':
410 obj=sre.compile(pattern)
Fredrik Lundh90a07912000-06-30 07:50:59 +0000411 result=obj.search(s, result.start(0), result.end(0)+1)
412 if result==None:
413 print '=== Failed on range-limited match', t
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000414
415 # Try the match with IGNORECASE enabled, and check that it
416 # still succeeds.
417 obj=sre.compile(pattern, sre.IGNORECASE)
418 result=obj.search(s)
419 if result==None:
420 print '=== Fails on case-insensitive match', t
421
422 # Try the match with LOCALE enabled, and check that it
423 # still succeeds.
424 obj=sre.compile(pattern, sre.LOCALE)
425 result=obj.search(s)
426 if result==None:
427 print '=== Fails on locale-sensitive match', t
428
Fredrik Lundhc2ed6212000-08-01 13:01:43 +0000429 # Try the match with UNICODE locale enabled, and check
430 # that it still succeeds.
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000431 if have_unicode:
432 obj=sre.compile(pattern, sre.UNICODE)
433 result=obj.search(s)
434 if result==None:
435 print '=== Fails on unicode-sensitive match', t