blob: c0d51d910dee38a477453a31cf644c030b6f70f8 [file] [log] [blame]
Guido van Rossum8430c581998-04-03 21:47:12 +00001import sys
Fred Drake8ae9ce52000-08-18 16:09:56 +00002sys.path = ['.'] + sys.path
Guido van Rossum8430c581998-04-03 21:47:12 +00003
Walter Dörwald21d3a322003-05-01 17:45:56 +00004from test.test_support import verbose, run_unittest
Guido van Rossum8e0ce301997-07-11 19:34:44 +00005import re
Skip Montanaro1e703c62003-04-25 15:40:28 +00006from sre import Scanner
Eric S. Raymond2846b0a2001-02-09 12:00:47 +00007import sys, os, traceback
Guido van Rossum8e0ce301997-07-11 19:34:44 +00008
Guido van Rossum23b22571997-07-17 22:36:14 +00009# Misc tests from Tim Peters' re.doc
10
Skip Montanaro8ed06da2003-04-24 19:43:18 +000011import unittest
Guido van Rossum8430c581998-04-03 21:47:12 +000012
Skip Montanaro8ed06da2003-04-24 19:43:18 +000013class ReTests(unittest.TestCase):
14 def test_search_star_plus(self):
15 self.assertEqual(re.search('x*', 'axx').span(0), (0, 0))
16 self.assertEqual(re.search('x*', 'axx').span(), (0, 0))
17 self.assertEqual(re.search('x+', 'axx').span(0), (1, 3))
18 self.assertEqual(re.search('x+', 'axx').span(), (1, 3))
Skip Montanaro5ba00542003-04-25 16:00:14 +000019 self.assertEqual(re.search('x', 'aaa'), None)
Skip Montanaro8ed06da2003-04-24 19:43:18 +000020 self.assertEqual(re.match('a*', 'xxx').span(0), (0, 0))
21 self.assertEqual(re.match('a*', 'xxx').span(), (0, 0))
22 self.assertEqual(re.match('x*', 'xxxa').span(0), (0, 3))
23 self.assertEqual(re.match('x*', 'xxxa').span(), (0, 3))
Skip Montanaro5ba00542003-04-25 16:00:14 +000024 self.assertEqual(re.match('a+', 'xxx'), None)
Guido van Rossum8430c581998-04-03 21:47:12 +000025
Skip Montanaro8ed06da2003-04-24 19:43:18 +000026 def bump_num(self, matchobj):
Guido van Rossum41360a41998-03-26 19:42:58 +000027 int_value = int(matchobj.group(0))
28 return str(int_value + 1)
Guido van Rossum23b22571997-07-17 22:36:14 +000029
Skip Montanaro8ed06da2003-04-24 19:43:18 +000030 def test_basic_re_sub(self):
31 self.assertEqual(re.sub("(?i)b+", "x", "bbbb BBBB"), 'x x')
32 self.assertEqual(re.sub(r'\d+', self.bump_num, '08.2 -2 23x99y'),
33 '9.3 -3 24x100y')
34 self.assertEqual(re.sub(r'\d+', self.bump_num, '08.2 -2 23x99y', 3),
35 '9.3 -3 23x99y')
Fredrik Lundh1151a8c2000-08-08 16:47:42 +000036
Skip Montanaro8ed06da2003-04-24 19:43:18 +000037 self.assertEqual(re.sub('.', lambda m: r"\n", 'x'), '\\n')
38 self.assertEqual(re.sub('.', r"\n", 'x'), '\n')
Guido van Rossumdfa67901997-12-08 17:12:06 +000039
Skip Montanaro8ed06da2003-04-24 19:43:18 +000040 s = r"\1\1"
41 self.assertEqual(re.sub('(.)', s, 'x'), 'xx')
42 self.assertEqual(re.sub('(.)', re.escape(s), 'x'), s)
43 self.assertEqual(re.sub('(.)', lambda m: s, 'x'), s)
Guido van Rossum23b22571997-07-17 22:36:14 +000044
Skip Montanaro8ed06da2003-04-24 19:43:18 +000045 self.assertEqual(re.sub('(?P<a>x)', '\g<a>\g<a>', 'xx'), 'xxxx')
46 self.assertEqual(re.sub('(?P<a>x)', '\g<a>\g<1>', 'xx'), 'xxxx')
47 self.assertEqual(re.sub('(?P<unk>x)', '\g<unk>\g<unk>', 'xx'), 'xxxx')
48 self.assertEqual(re.sub('(?P<unk>x)', '\g<1>\g<1>', 'xx'), 'xxxx')
Guido van Rossum49946571997-07-18 04:26:25 +000049
Skip Montanaro8ed06da2003-04-24 19:43:18 +000050 self.assertEqual(re.sub('a',r'\t\n\v\r\f\a\b\B\Z\a\A\w\W\s\S\d\D','a'),
51 '\t\n\v\r\f\a\b\\B\\Z\a\\A\\w\\W\\s\\S\\d\\D')
52 self.assertEqual(re.sub('a', '\t\n\v\r\f\a', 'a'), '\t\n\v\r\f\a')
53 self.assertEqual(re.sub('a', '\t\n\v\r\f\a', 'a'),
54 (chr(9)+chr(10)+chr(11)+chr(13)+chr(12)+chr(7)))
Guido van Rossum95e80531997-08-13 22:34:14 +000055
Skip Montanaro8ed06da2003-04-24 19:43:18 +000056 self.assertEqual(re.sub('^\s*', 'X', 'test'), 'Xtest')
Guido van Rossume056e4d2001-08-10 14:52:48 +000057
Skip Montanaro2726fcd2003-04-25 14:31:54 +000058 def test_bug_449964(self):
59 # fails for group followed by other escape
60 self.assertEqual(re.sub(r'(?P<unk>x)', '\g<1>\g<1>\\b', 'xx'),
61 'xx\bxx\b')
62
63 def test_bug_449000(self):
64 # Test for sub() on escaped characters
Skip Montanaro8ed06da2003-04-24 19:43:18 +000065 self.assertEqual(re.sub(r'\r\n', r'\n', 'abc\r\ndef\r\n'),
66 'abc\ndef\n')
67 self.assertEqual(re.sub('\r\n', r'\n', 'abc\r\ndef\r\n'),
68 'abc\ndef\n')
69 self.assertEqual(re.sub(r'\r\n', '\n', 'abc\r\ndef\r\n'),
70 'abc\ndef\n')
71 self.assertEqual(re.sub('\r\n', '\n', 'abc\r\ndef\r\n'),
72 'abc\ndef\n')
Guido van Rossum23b22571997-07-17 22:36:14 +000073
Skip Montanaro8ed06da2003-04-24 19:43:18 +000074 def test_qualified_re_sub(self):
75 self.assertEqual(re.sub('a', 'b', 'aaaaa'), 'bbbbb')
76 self.assertEqual(re.sub('a', 'b', 'aaaaa', 1), 'baaaa')
Guido van Rossum8430c581998-04-03 21:47:12 +000077
Skip Montanaro2726fcd2003-04-25 14:31:54 +000078 def test_bug_114660(self):
79 self.assertEqual(re.sub(r'(\S)\s+(\S)', r'\1 \2', 'hello there'),
80 'hello there')
81
82 def test_bug_462270(self):
83 # Test for empty sub() behaviour, see SF bug #462270
84 self.assertEqual(re.sub('x*', '-', 'abxd'), '-a-b-d-')
85 self.assertEqual(re.sub('x+', '-', 'abxd'), 'ab-d')
86
Skip Montanaro8ed06da2003-04-24 19:43:18 +000087 def test_symbolic_refs(self):
88 self.assertRaises(re.error, re.sub, '(?P<a>x)', '\g<a', 'xx')
89 self.assertRaises(re.error, re.sub, '(?P<a>x)', '\g<', 'xx')
90 self.assertRaises(re.error, re.sub, '(?P<a>x)', '\g', 'xx')
91 self.assertRaises(re.error, re.sub, '(?P<a>x)', '\g<a a>', 'xx')
92 self.assertRaises(re.error, re.sub, '(?P<a>x)', '\g<1a1>', 'xx')
93 self.assertRaises(IndexError, re.sub, '(?P<a>x)', '\g<ab>', 'xx')
94 self.assertRaises(re.error, re.sub, '(?P<a>x)|(?P<b>y)', '\g<b>', 'xx')
95 self.assertRaises(re.error, re.sub, '(?P<a>x)|(?P<b>y)', '\\2', 'xx')
Guido van Rossumf473cb01998-01-14 16:42:17 +000096
Skip Montanaro8ed06da2003-04-24 19:43:18 +000097 def test_re_subn(self):
98 self.assertEqual(re.subn("(?i)b+", "x", "bbbb BBBB"), ('x x', 2))
99 self.assertEqual(re.subn("b+", "x", "bbbb BBBB"), ('x BBBB', 1))
100 self.assertEqual(re.subn("b+", "x", "xyz"), ('xyz', 0))
101 self.assertEqual(re.subn("b*", "x", "xyz"), ('xxxyxzx', 4))
102 self.assertEqual(re.subn("b*", "x", "xyz", 2), ('xxxyz', 2))
Guido van Rossum49946571997-07-18 04:26:25 +0000103
Skip Montanaro8ed06da2003-04-24 19:43:18 +0000104 def test_re_split(self):
105 self.assertEqual(re.split(":", ":a:b::c"), ['', 'a', 'b', '', 'c'])
106 self.assertEqual(re.split(":*", ":a:b::c"), ['', 'a', 'b', 'c'])
107 self.assertEqual(re.split("(:*)", ":a:b::c"),
108 ['', ':', 'a', ':', 'b', '::', 'c'])
109 self.assertEqual(re.split("(?::*)", ":a:b::c"), ['', 'a', 'b', 'c'])
110 self.assertEqual(re.split("(:)*", ":a:b::c"),
111 ['', ':', 'a', ':', 'b', ':', 'c'])
112 self.assertEqual(re.split("([b:]+)", ":a:b::c"),
113 ['', ':', 'a', ':b::', 'c'])
114 self.assertEqual(re.split("(b)|(:+)", ":a:b::c"),
115 ['', None, ':', 'a', None, ':', '', 'b', None, '',
116 None, '::', 'c'])
117 self.assertEqual(re.split("(?:b)|(?::+)", ":a:b::c"),
118 ['', 'a', '', '', 'c'])
Guido van Rossum49946571997-07-18 04:26:25 +0000119
Skip Montanaro8ed06da2003-04-24 19:43:18 +0000120 def test_qualified_re_split(self):
121 self.assertEqual(re.split(":", ":a:b::c", 2), ['', 'a', 'b::c'])
122 self.assertEqual(re.split(':', 'a:b:c:d', 2), ['a', 'b', 'c:d'])
123 self.assertEqual(re.split("(:)", ":a:b::c", 2),
124 ['', ':', 'a', ':', 'b::c'])
125 self.assertEqual(re.split("(:*)", ":a:b::c", 2),
126 ['', ':', 'a', ':', 'b::c'])
Guido van Rossum49946571997-07-18 04:26:25 +0000127
Skip Montanaro8ed06da2003-04-24 19:43:18 +0000128 def test_re_findall(self):
129 self.assertEqual(re.findall(":+", "abc"), [])
130 self.assertEqual(re.findall(":+", "a:b::c:::d"), [":", "::", ":::"])
131 self.assertEqual(re.findall("(:+)", "a:b::c:::d"), [":", "::", ":::"])
132 self.assertEqual(re.findall("(:)(:*)", "a:b::c:::d"), [(":", ""),
133 (":", ":"),
134 (":", "::")])
Guido van Rossum49946571997-07-18 04:26:25 +0000135
Skip Montanaro5ba00542003-04-25 16:00:14 +0000136 def test_bug_117612(self):
137 self.assertEqual(re.findall(r"(a|(b))", "aba"),
138 [("a", ""),("b", "b"),("a", "")])
139
Skip Montanaro8ed06da2003-04-24 19:43:18 +0000140 def test_re_match(self):
Skip Montanaro5ba00542003-04-25 16:00:14 +0000141 self.assertEqual(re.match('a', 'a').groups(), ())
142 self.assertEqual(re.match('(a)', 'a').groups(), ('a',))
143 self.assertEqual(re.match(r'(a)', 'a').group(0), 'a')
144 self.assertEqual(re.match(r'(a)', 'a').group(1), 'a')
145 self.assertEqual(re.match(r'(a)', 'a').group(1, 1), ('a', 'a'))
Guido van Rossum49946571997-07-18 04:26:25 +0000146
Skip Montanaro8ed06da2003-04-24 19:43:18 +0000147 pat = re.compile('((a)|(b))(c)?')
148 self.assertEqual(pat.match('a').groups(), ('a', 'a', None, None))
149 self.assertEqual(pat.match('b').groups(), ('b', None, 'b', None))
150 self.assertEqual(pat.match('ac').groups(), ('a', 'a', None, 'c'))
151 self.assertEqual(pat.match('bc').groups(), ('b', None, 'b', 'c'))
152 self.assertEqual(pat.match('bc').groups(""), ('b', "", 'b', 'c'))
Guido van Rossum8430c581998-04-03 21:47:12 +0000153
Skip Montanaro8ed06da2003-04-24 19:43:18 +0000154 # A single group
155 m = re.match('(a)', 'a')
156 self.assertEqual(m.group(0), 'a')
157 self.assertEqual(m.group(0), 'a')
158 self.assertEqual(m.group(1), 'a')
159 self.assertEqual(m.group(1, 1), ('a', 'a'))
Guido van Rossum49946571997-07-18 04:26:25 +0000160
Skip Montanaro8ed06da2003-04-24 19:43:18 +0000161 pat = re.compile('(?:(?P<a1>a)|(?P<b2>b))(?P<c3>c)?')
162 self.assertEqual(pat.match('a').group(1, 2, 3), ('a', None, None))
163 self.assertEqual(pat.match('b').group('a1', 'b2', 'c3'),
164 (None, 'b', None))
165 self.assertEqual(pat.match('ac').group(1, 'b2', 3), ('a', None, 'c'))
Guido van Rossum49946571997-07-18 04:26:25 +0000166
Skip Montanaro8ed06da2003-04-24 19:43:18 +0000167 def test_re_escape(self):
168 p=""
169 for i in range(0, 256):
170 p = p + chr(i)
171 self.assertEqual(re.match(re.escape(chr(i)), chr(i)) is not None,
172 True)
173 self.assertEqual(re.match(re.escape(chr(i)), chr(i)).span(), (0,1))
Guido van Rossum49946571997-07-18 04:26:25 +0000174
Skip Montanaro1e703c62003-04-25 15:40:28 +0000175 pat=re.compile(re.escape(p))
Skip Montanaro8ed06da2003-04-24 19:43:18 +0000176 self.assertEqual(pat.match(p) is not None, True)
177 self.assertEqual(pat.match(p).span(), (0,256))
Guido van Rossum49946571997-07-18 04:26:25 +0000178
Skip Montanaro8ed06da2003-04-24 19:43:18 +0000179 def test_pickling(self):
180 import pickle
Skip Montanaro1e703c62003-04-25 15:40:28 +0000181 self.pickle_test(pickle)
182 import cPickle
183 self.pickle_test(cPickle)
184
185 def pickle_test(self, pickle):
Skip Montanaro8ed06da2003-04-24 19:43:18 +0000186 oldpat = re.compile('a(?:b|(c|e){1,2}?|d)+?(.)')
187 s = pickle.dumps(oldpat)
188 newpat = pickle.loads(s)
189 self.assertEqual(oldpat, newpat)
Guido van Rossum23b22571997-07-17 22:36:14 +0000190
Skip Montanaro8ed06da2003-04-24 19:43:18 +0000191 def test_constants(self):
192 self.assertEqual(re.I, re.IGNORECASE)
193 self.assertEqual(re.L, re.LOCALE)
194 self.assertEqual(re.M, re.MULTILINE)
195 self.assertEqual(re.S, re.DOTALL)
196 self.assertEqual(re.X, re.VERBOSE)
Fredrik Lundh1151a8c2000-08-08 16:47:42 +0000197
Skip Montanaro8ed06da2003-04-24 19:43:18 +0000198 def test_flags(self):
Skip Montanaro1e703c62003-04-25 15:40:28 +0000199 for flag in [re.I, re.M, re.X, re.S, re.L]:
200 self.assertNotEqual(re.compile('^pattern$', flag), None)
Guido van Rossumf473cb01998-01-14 16:42:17 +0000201
Skip Montanaro7d9963f2003-04-25 14:12:40 +0000202 def test_sre_character_literals(self):
203 for i in [0, 8, 16, 32, 64, 127, 128, 255]:
204 self.assertNotEqual(re.match(r"\%03o" % i, chr(i)), None)
205 self.assertNotEqual(re.match(r"\%03o0" % i, chr(i)+"0"), None)
206 self.assertNotEqual(re.match(r"\%03o8" % i, chr(i)+"8"), None)
207 self.assertNotEqual(re.match(r"\x%02x" % i, chr(i)), None)
208 self.assertNotEqual(re.match(r"\x%02x0" % i, chr(i)+"0"), None)
209 self.assertNotEqual(re.match(r"\x%02xz" % i, chr(i)+"z"), None)
210 self.assertRaises(re.error, re.match, "\911", "")
211
212 def test_bug_113254(self):
213 self.assertEqual(re.match(r'(a)|(b)', 'b').start(1), -1)
214 self.assertEqual(re.match(r'(a)|(b)', 'b').end(1), -1)
215 self.assertEqual(re.match(r'(a)|(b)', 'b').span(1), (-1, -1))
216
Skip Montanaro2726fcd2003-04-25 14:31:54 +0000217 def test_bug_527371(self):
218 # bug described in patches 527371/672491
219 self.assertEqual(re.match(r'(a)?a','a').lastindex, None)
220 self.assertEqual(re.match(r'(a)(b)?b','ab').lastindex, 1)
221 self.assertEqual(re.match(r'(?P<a>a)(?P<b>b)?b','ab').lastgroup, 'a')
222 self.assertEqual(re.match("(?P<a>a(b))", "ab").lastgroup, 'a')
223 self.assertEqual(re.match("((a))", "a").lastindex, 1)
224
225 def test_bug_545855(self):
226 # bug 545855 -- This pattern failed to cause a compile error as it
227 # should, instead provoking a TypeError.
228 self.assertRaises(re.error, re.compile, 'foo[a-')
229
230 def test_bug_418626(self):
231 # bugs 418626 at al. -- Testing Greg Chapman's addition of op code
232 # SRE_OP_MIN_REPEAT_ONE for eliminating recursion on simple uses of
233 # pattern '*?' on a long string.
234 self.assertEqual(re.match('.*?c', 10000*'ab'+'cd').end(0), 20001)
235 self.assertEqual(re.match('.*?cd', 5000*'ab'+'c'+5000*'ab'+'cde').end(0),
236 20003)
237 self.assertEqual(re.match('.*?cd', 20000*'abc'+'de').end(0), 60001)
238 # non-simple '*?' still recurses and hits the recursion limit
239 self.assertRaises(RuntimeError, re.search, '(a|b)*?c', 10000*'ab'+'cd')
240
241 def test_bug_612074(self):
242 pat=u"["+re.escape(u"\u2039")+u"]"
243 self.assertEqual(re.compile(pat) and 1, 1)
244
Skip Montanaro1e703c62003-04-25 15:40:28 +0000245 def test_stack_overflow(self):
246 # nasty case that overflows the straightforward recursive
247 # implementation of repeated groups.
248 self.assertRaises(RuntimeError, re.match, '(x)*', 50000*'x')
249 self.assertRaises(RuntimeError, re.match, '(x)*y', 50000*'x'+'y')
250 self.assertRaises(RuntimeError, re.match, '(x)*?y', 50000*'x'+'y')
251
252 def test_scanner(self):
253 def s_ident(scanner, token): return token
254 def s_operator(scanner, token): return "op%s" % token
255 def s_float(scanner, token): return float(token)
256 def s_int(scanner, token): return int(token)
257
258 scanner = Scanner([
259 (r"[a-zA-Z_]\w*", s_ident),
260 (r"\d+\.\d*", s_float),
261 (r"\d+", s_int),
262 (r"=|\+|-|\*|/", s_operator),
263 (r"\s+", None),
264 ])
265
266 self.assertEqual(scanner.scan("sum = 3*foo + 312.50 + bar"),
267 (['sum', 'op=', 3, 'op*', 'foo', 'op+', 312.5,
268 'op+', 'bar'], ''))
269
Skip Montanaro5ba00542003-04-25 16:00:14 +0000270 def test_bug_448951(self):
271 # bug 448951 (similar to 429357, but with single char match)
272 # (Also test greedy matches.)
273 for op in '','?','*':
274 self.assertEqual(re.match(r'((.%s):)?z'%op, 'z').groups(),
275 (None, None))
276 self.assertEqual(re.match(r'((.%s):)?z'%op, 'a:z').groups(),
277 ('a:', 'a'))
278
Gustavo Niemeyerc34f2552003-04-27 12:34:14 +0000279 def test_bug_725106(self):
280 # capturing groups in alternatives in repeats
281 self.assertEqual(re.match('^((a)|b)*', 'abc').groups(),
282 ('b', 'a'))
283 self.assertEqual(re.match('^(([ab])|c)*', 'abc').groups(),
284 ('c', 'b'))
285 self.assertEqual(re.match('^((d)|[ab])*', 'abc').groups(),
286 ('b', None))
287 self.assertEqual(re.match('^((a)c|[ab])*', 'abc').groups(),
288 ('b', None))
289 self.assertEqual(re.match('^((a)|b)*?c', 'abc').groups(),
290 ('b', 'a'))
291 self.assertEqual(re.match('^(([ab])|c)*?d', 'abcd').groups(),
292 ('c', 'b'))
293 self.assertEqual(re.match('^((d)|[ab])*?c', 'abc').groups(),
294 ('b', None))
295 self.assertEqual(re.match('^((a)c|[ab])*?c', 'abc').groups(),
296 ('b', None))
297
Gustavo Niemeyer3646ab92003-04-27 13:25:21 +0000298 def test_bug_725149(self):
299 # mark_stack_base restoring before restoring marks
300 self.assertEqual(re.match('(a)(?:(?=(b)*)c)*', 'abb').groups(),
301 ('a', None))
302 self.assertEqual(re.match('(a)((?!(b)*))*', 'abb').groups(),
303 ('a', None, None))
304
Skip Montanaro5ba00542003-04-25 16:00:14 +0000305 def test_finditer(self):
306 iter = re.finditer(r":+", "a:b::c:::d")
307 self.assertEqual([item.group(0) for item in iter],
308 [":", "::", ":::"])
309
Skip Montanaro8ed06da2003-04-24 19:43:18 +0000310def run_re_tests():
311 from test.re_tests import benchmarks, tests, SUCCEED, FAIL, SYNTAX_ERROR
312 if verbose:
313 print 'Running re_tests test suite'
Guido van Rossum8e0ce301997-07-11 19:34:44 +0000314 else:
Skip Montanaro8ed06da2003-04-24 19:43:18 +0000315 # To save time, only run the first and last 10 tests
316 #tests = tests[:10] + tests[-10:]
317 pass
Guido van Rossum8e0ce301997-07-11 19:34:44 +0000318
Skip Montanaro8ed06da2003-04-24 19:43:18 +0000319 for t in tests:
320 sys.stdout.flush()
321 pattern = s = outcome = repl = expected = None
322 if len(t) == 5:
323 pattern, s, outcome, repl, expected = t
324 elif len(t) == 3:
325 pattern, s, outcome = t
Fredrik Lundh1151a8c2000-08-08 16:47:42 +0000326 else:
Skip Montanaro8ed06da2003-04-24 19:43:18 +0000327 raise ValueError, ('Test tuples should have 3 or 5 fields', t)
328
Guido van Rossum41360a41998-03-26 19:42:58 +0000329 try:
Skip Montanaro8ed06da2003-04-24 19:43:18 +0000330 obj = re.compile(pattern)
331 except re.error:
332 if outcome == SYNTAX_ERROR: pass # Expected a syntax error
Guido van Rossum41360a41998-03-26 19:42:58 +0000333 else:
Skip Montanaro8ed06da2003-04-24 19:43:18 +0000334 print '=== Syntax error:', t
335 except KeyboardInterrupt: raise KeyboardInterrupt
336 except:
337 print '*** Unexpected error ***', t
338 if verbose:
339 traceback.print_exc(file=sys.stdout)
340 else:
Fredrik Lundh17741be2001-03-22 15:51:28 +0000341 try:
Skip Montanaro8ed06da2003-04-24 19:43:18 +0000342 result = obj.search(s)
343 except re.error, msg:
344 print '=== Unexpected exception', t, repr(msg)
345 if outcome == SYNTAX_ERROR:
346 # This should have been a syntax error; forget it.
347 pass
348 elif outcome == FAIL:
349 if result is None: pass # No match, as expected
350 else: print '=== Succeeded incorrectly', t
351 elif outcome == SUCCEED:
352 if result is not None:
353 # Matched, as expected, so now we compute the
354 # result string and compare it to our expected result.
355 start, end = result.span(0)
356 vardict={'found': result.group(0),
357 'groups': result.group(),
358 'flags': result.re.flags}
359 for i in range(1, 100):
360 try:
361 gi = result.group(i)
362 # Special hack because else the string concat fails:
363 if gi is None:
364 gi = "None"
365 except IndexError:
366 gi = "Error"
367 vardict['g%d' % i] = gi
368 for i in result.re.groupindex.keys():
369 try:
370 gi = result.group(i)
371 if gi is None:
372 gi = "None"
373 except IndexError:
374 gi = "Error"
375 vardict[i] = gi
376 repl = eval(repl, vardict)
377 if repl != expected:
378 print '=== grouping error', t,
379 print repr(repl) + ' should be ' + repr(expected)
380 else:
381 print '=== Failed incorrectly', t
382
383 # Try the match on a unicode string, and check that it
384 # still succeeds.
385 try:
386 result = obj.search(unicode(s, "latin-1"))
387 if result is None:
388 print '=== Fails on unicode match', t
389 except NameError:
390 continue # 1.5.2
391 except TypeError:
392 continue # unicode test case
393
394 # Try the match on a unicode pattern, and check that it
395 # still succeeds.
396 obj=re.compile(unicode(pattern, "latin-1"))
397 result = obj.search(s)
Fredrik Lundh17741be2001-03-22 15:51:28 +0000398 if result is None:
Skip Montanaro8ed06da2003-04-24 19:43:18 +0000399 print '=== Fails on unicode pattern match', t
Fredrik Lundh8e6d5712000-08-08 17:06:53 +0000400
Skip Montanaro8ed06da2003-04-24 19:43:18 +0000401 # Try the match with the search area limited to the extent
402 # of the match and see if it still succeeds. \B will
403 # break (because it won't match at the end or start of a
404 # string), so we'll ignore patterns that feature it.
Fredrik Lundh8e6d5712000-08-08 17:06:53 +0000405
Skip Montanaro8ed06da2003-04-24 19:43:18 +0000406 if pattern[:2] != '\\B' and pattern[-2:] != '\\B' \
407 and result is not None:
408 obj = re.compile(pattern)
409 result = obj.search(s, result.start(0), result.end(0) + 1)
410 if result is None:
411 print '=== Failed on range-limited match', t
Fredrik Lundh1151a8c2000-08-08 16:47:42 +0000412
Skip Montanaro8ed06da2003-04-24 19:43:18 +0000413 # Try the match with IGNORECASE enabled, and check that it
414 # still succeeds.
415 obj = re.compile(pattern, re.IGNORECASE)
416 result = obj.search(s)
Fred Drake132dce22000-12-12 23:11:42 +0000417 if result is None:
Skip Montanaro8ed06da2003-04-24 19:43:18 +0000418 print '=== Fails on case-insensitive match', t
Guido van Rossumdfa67901997-12-08 17:12:06 +0000419
Skip Montanaro8ed06da2003-04-24 19:43:18 +0000420 # Try the match with LOCALE enabled, and check that it
421 # still succeeds.
422 obj = re.compile(pattern, re.LOCALE)
423 result = obj.search(s)
424 if result is None:
425 print '=== Fails on locale-sensitive match', t
Guido van Rossumdfa67901997-12-08 17:12:06 +0000426
Skip Montanaro8ed06da2003-04-24 19:43:18 +0000427 # Try the match with UNICODE locale enabled, and check
428 # that it still succeeds.
429 obj = re.compile(pattern, re.UNICODE)
430 result = obj.search(s)
431 if result is None:
432 print '=== Fails on unicode-sensitive match', t
Fredrik Lundh8e6d5712000-08-08 17:06:53 +0000433
Skip Montanaro8ed06da2003-04-24 19:43:18 +0000434def test_main():
Walter Dörwald21d3a322003-05-01 17:45:56 +0000435 run_unittest(ReTests)
Skip Montanaro1e703c62003-04-25 15:40:28 +0000436 run_re_tests()
Skip Montanaro8ed06da2003-04-24 19:43:18 +0000437
438if __name__ == "__main__":
439 test_main()