Merged release21-maint changes.
[python/dscho.git] / Lib / test / test_re.py
blob45bb3b14ec7c0c7189451f36deda3ab788545950
1 import sys
2 sys.path = ['.'] + sys.path
4 from test_support import verify, verbose, TestFailed
5 import re
6 import sys, os, traceback
8 # Misc tests from Tim Peters' re.doc
10 if verbose:
11 print 'Running tests on re.search and re.match'
13 try:
14 verify(re.search('x*', 'axx').span(0) == (0, 0))
15 verify(re.search('x*', 'axx').span() == (0, 0))
16 verify(re.search('x+', 'axx').span(0) == (1, 3))
17 verify(re.search('x+', 'axx').span() == (1, 3))
18 verify(re.search('x', 'aaa') is None)
19 except:
20 raise TestFailed, "re.search"
22 try:
23 verify(re.match('a*', 'xxx').span(0) == (0, 0))
24 verify(re.match('a*', 'xxx').span() == (0, 0))
25 verify(re.match('x*', 'xxxa').span(0) == (0, 3))
26 verify(re.match('x*', 'xxxa').span() == (0, 3))
27 verify(re.match('a+', 'xxx') is None)
28 except:
29 raise TestFailed, "re.search"
31 if verbose:
32 print 'Running tests on re.sub'
34 try:
35 verify(re.sub("(?i)b+", "x", "bbbb BBBB") == 'x x')
37 def bump_num(matchobj):
38 int_value = int(matchobj.group(0))
39 return str(int_value + 1)
41 verify(re.sub(r'\d+', bump_num, '08.2 -2 23x99y') == '9.3 -3 24x100y')
42 verify(re.sub(r'\d+', bump_num, '08.2 -2 23x99y', 3) == '9.3 -3 23x99y')
44 verify(re.sub('.', lambda m: r"\n", 'x') == '\\n')
45 verify(re.sub('.', r"\n", 'x') == '\n')
47 s = r"\1\1"
48 verify(re.sub('(.)', s, 'x') == 'xx')
49 verify(re.sub('(.)', re.escape(s), 'x') == s)
50 verify(re.sub('(.)', lambda m: s, 'x') == s)
52 verify(re.sub('(?P<a>x)', '\g<a>\g<a>', 'xx') == 'xxxx')
53 verify(re.sub('(?P<a>x)', '\g<a>\g<1>', 'xx') == 'xxxx')
54 verify(re.sub('(?P<unk>x)', '\g<unk>\g<unk>', 'xx') == 'xxxx')
55 verify(re.sub('(?P<unk>x)', '\g<1>\g<1>', 'xx') == 'xxxx')
57 verify(re.sub('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')
58 verify(re.sub('a', '\t\n\v\r\f\a', 'a') == '\t\n\v\r\f\a')
59 verify(re.sub('a', '\t\n\v\r\f\a', 'a') == (chr(9)+chr(10)+chr(11)+chr(13)+chr(12)+chr(7)))
61 verify(re.sub('^\s*', 'X', 'test') == 'Xtest')
63 # Test for sub() on escaped characters, see SF bug #449000
64 verify(re.sub(r'\r\n', r'\n', 'abc\r\ndef\r\n') == 'abc\ndef\n')
65 verify(re.sub('\r\n', r'\n', 'abc\r\ndef\r\n') == 'abc\ndef\n')
66 verify(re.sub(r'\r\n', '\n', 'abc\r\ndef\r\n') == 'abc\ndef\n')
67 verify(re.sub('\r\n', '\n', 'abc\r\ndef\r\n') == 'abc\ndef\n')
68 except AssertionError:
69 raise TestFailed, "re.sub"
72 try:
73 verify(re.sub('a', 'b', 'aaaaa') == 'bbbbb')
74 verify(re.sub('a', 'b', 'aaaaa', 1) == 'baaaa')
75 except AssertionError:
76 raise TestFailed, "qualified re.sub"
78 if verbose:
79 print 'Running tests on symbolic references'
81 try:
82 re.sub('(?P<a>x)', '\g<a', 'xx')
83 except re.error, reason:
84 pass
85 else:
86 raise TestFailed, "symbolic reference"
88 try:
89 re.sub('(?P<a>x)', '\g<', 'xx')
90 except re.error, reason:
91 pass
92 else:
93 raise TestFailed, "symbolic reference"
95 try:
96 re.sub('(?P<a>x)', '\g', 'xx')
97 except re.error, reason:
98 pass
99 else:
100 raise TestFailed, "symbolic reference"
102 try:
103 re.sub('(?P<a>x)', '\g<a a>', 'xx')
104 except re.error, reason:
105 pass
106 else:
107 raise TestFailed, "symbolic reference"
109 try:
110 re.sub('(?P<a>x)', '\g<1a1>', 'xx')
111 except re.error, reason:
112 pass
113 else:
114 raise TestFailed, "symbolic reference"
116 try:
117 re.sub('(?P<a>x)', '\g<ab>', 'xx')
118 except IndexError, reason:
119 pass
120 else:
121 raise TestFailed, "symbolic reference"
123 try:
124 re.sub('(?P<a>x)|(?P<b>y)', '\g<b>', 'xx')
125 except re.error, reason:
126 pass
127 else:
128 raise TestFailed, "symbolic reference"
130 try:
131 re.sub('(?P<a>x)|(?P<b>y)', '\\2', 'xx')
132 except re.error, reason:
133 pass
134 else:
135 raise TestFailed, "symbolic reference"
137 if verbose:
138 print 'Running tests on re.subn'
140 try:
141 verify(re.subn("(?i)b+", "x", "bbbb BBBB") == ('x x', 2))
142 verify(re.subn("b+", "x", "bbbb BBBB") == ('x BBBB', 1))
143 verify(re.subn("b+", "x", "xyz") == ('xyz', 0))
144 verify(re.subn("b*", "x", "xyz") == ('xxxyxzx', 4))
145 verify(re.subn("b*", "x", "xyz", 2) == ('xxxyz', 2))
146 except AssertionError:
147 raise TestFailed, "re.subn"
149 if verbose:
150 print 'Running tests on re.split'
152 try:
153 verify(re.split(":", ":a:b::c") == ['', 'a', 'b', '', 'c'])
154 verify(re.split(":*", ":a:b::c") == ['', 'a', 'b', 'c'])
155 verify(re.split("(:*)", ":a:b::c") == ['', ':', 'a', ':', 'b', '::', 'c'])
156 verify(re.split("(?::*)", ":a:b::c") == ['', 'a', 'b', 'c'])
157 verify(re.split("(:)*", ":a:b::c") == ['', ':', 'a', ':', 'b', ':', 'c'])
158 verify(re.split("([b:]+)", ":a:b::c") == ['', ':', 'a', ':b::', 'c'])
159 verify(re.split("(b)|(:+)", ":a:b::c") == \
160 ['', None, ':', 'a', None, ':', '', 'b', None, '', None, '::', 'c'] )
161 verify(re.split("(?:b)|(?::+)", ":a:b::c") == ['', 'a', '', '', 'c'])
162 except AssertionError:
163 raise TestFailed, "re.split"
165 try:
166 verify(re.split(":", ":a:b::c", 2) == ['', 'a', 'b::c'])
167 verify(re.split(':', 'a:b:c:d', 2) == ['a', 'b', 'c:d'])
169 verify(re.split("(:)", ":a:b::c", 2) == ['', ':', 'a', ':', 'b::c'])
170 verify(re.split("(:*)", ":a:b::c", 2) == ['', ':', 'a', ':', 'b::c'])
171 except AssertionError:
172 raise TestFailed, "qualified re.split"
174 if verbose:
175 print "Running tests on re.findall"
177 try:
178 verify(re.findall(":+", "abc") == [])
179 verify(re.findall(":+", "a:b::c:::d") == [":", "::", ":::"])
180 verify(re.findall("(:+)", "a:b::c:::d") == [":", "::", ":::"])
181 verify(re.findall("(:)(:*)", "a:b::c:::d") == [(":", ""),
182 (":", ":"),
183 (":", "::")] )
184 except AssertionError:
185 raise TestFailed, "re.findall"
187 if verbose:
188 print "Running tests on re.match"
190 try:
191 # No groups at all
192 m = re.match('a', 'a') ; verify(m.groups() == ())
193 # A single group
194 m = re.match('(a)', 'a') ; verify(m.groups() == ('a',))
196 pat = re.compile('((a)|(b))(c)?')
197 verify(pat.match('a').groups() == ('a', 'a', None, None))
198 verify(pat.match('b').groups() == ('b', None, 'b', None))
199 verify(pat.match('ac').groups() == ('a', 'a', None, 'c'))
200 verify(pat.match('bc').groups() == ('b', None, 'b', 'c'))
201 verify(pat.match('bc').groups("") == ('b', "", 'b', 'c'))
202 except AssertionError:
203 raise TestFailed, "match .groups() method"
205 try:
206 # A single group
207 m = re.match('(a)', 'a')
208 verify(m.group(0) == 'a')
209 verify(m.group(0) == 'a')
210 verify(m.group(1) == 'a')
211 verify(m.group(1, 1) == ('a', 'a'))
213 pat = re.compile('(?:(?P<a1>a)|(?P<b2>b))(?P<c3>c)?')
214 verify(pat.match('a').group(1, 2, 3) == ('a', None, None))
215 verify(pat.match('b').group('a1', 'b2', 'c3') == (None, 'b', None))
216 verify(pat.match('ac').group(1, 'b2', 3) == ('a', None, 'c'))
217 except AssertionError:
218 raise TestFailed, "match .group() method"
220 if verbose:
221 print "Running tests on re.escape"
223 try:
224 p=""
225 for i in range(0, 256):
226 p = p + chr(i)
227 verify(re.match(re.escape(chr(i)), chr(i)) is not None)
228 verify(re.match(re.escape(chr(i)), chr(i)).span() == (0,1))
230 pat=re.compile( re.escape(p) )
231 verify(pat.match(p) is not None)
232 verify(pat.match(p).span() == (0,256))
233 except AssertionError:
234 raise TestFailed, "re.escape"
237 if verbose:
238 print 'Pickling a RegexObject instance'
240 import pickle
241 pat = re.compile('a(?:b|(c|e){1,2}?|d)+?(.)')
242 s = pickle.dumps(pat)
243 pat = pickle.loads(s)
245 try:
246 verify(re.I == re.IGNORECASE)
247 verify(re.L == re.LOCALE)
248 verify(re.M == re.MULTILINE)
249 verify(re.S == re.DOTALL)
250 verify(re.X == re.VERBOSE)
251 except AssertionError:
252 raise TestFailed, 're module constants'
254 for flags in [re.I, re.M, re.X, re.S, re.L]:
255 try:
256 r = re.compile('^pattern$', flags)
257 except:
258 print 'Exception raised on flag', flags
260 if verbose:
261 print 'Test engine limitations'
263 # Try nasty case that overflows the straightforward recursive
264 # implementation of repeated groups.
265 try:
266 verify(re.match('(x)*', 50000*'x').span() == (0, 50000))
267 except RuntimeError, v:
268 print v
270 from re_tests import *
272 if verbose:
273 print 'Running re_tests test suite'
274 else:
275 # To save time, only run the first and last 10 tests
276 #tests = tests[:10] + tests[-10:]
277 pass
279 for t in tests:
280 sys.stdout.flush()
281 pattern = s = outcome = repl = expected = None
282 if len(t) == 5:
283 pattern, s, outcome, repl, expected = t
284 elif len(t) == 3:
285 pattern, s, outcome = t
286 else:
287 raise ValueError, ('Test tuples should have 3 or 5 fields', t)
289 try:
290 obj = re.compile(pattern)
291 except re.error:
292 if outcome == SYNTAX_ERROR: pass # Expected a syntax error
293 else:
294 print '=== Syntax error:', t
295 except KeyboardInterrupt: raise KeyboardInterrupt
296 except:
297 print '*** Unexpected error ***', t
298 if verbose:
299 traceback.print_exc(file=sys.stdout)
300 else:
301 try:
302 result = obj.search(s)
303 except re.error, msg:
304 print '=== Unexpected exception', t, repr(msg)
305 if outcome == SYNTAX_ERROR:
306 # This should have been a syntax error; forget it.
307 pass
308 elif outcome == FAIL:
309 if result is None: pass # No match, as expected
310 else: print '=== Succeeded incorrectly', t
311 elif outcome == SUCCEED:
312 if result is not None:
313 # Matched, as expected, so now we compute the
314 # result string and compare it to our expected result.
315 start, end = result.span(0)
316 vardict={'found': result.group(0),
317 'groups': result.group(),
318 'flags': result.re.flags}
319 for i in range(1, 100):
320 try:
321 gi = result.group(i)
322 # Special hack because else the string concat fails:
323 if gi is None:
324 gi = "None"
325 except IndexError:
326 gi = "Error"
327 vardict['g%d' % i] = gi
328 for i in result.re.groupindex.keys():
329 try:
330 gi = result.group(i)
331 if gi is None:
332 gi = "None"
333 except IndexError:
334 gi = "Error"
335 vardict[i] = gi
336 repl = eval(repl, vardict)
337 if repl != expected:
338 print '=== grouping error', t,
339 print repr(repl) + ' should be ' + repr(expected)
340 else:
341 print '=== Failed incorrectly', t
343 # Try the match on a unicode string, and check that it
344 # still succeeds.
345 try:
346 result = obj.search(unicode(s, "latin-1"))
347 if result is None:
348 print '=== Fails on unicode match', t
349 except NameError:
350 continue # 1.5.2
351 except TypeError:
352 continue # unicode test case
354 # Try the match on a unicode pattern, and check that it
355 # still succeeds.
356 obj=re.compile(unicode(pattern, "latin-1"))
357 result = obj.search(s)
358 if result is None:
359 print '=== Fails on unicode pattern match', t
361 # Try the match with the search area limited to the extent
362 # of the match and see if it still succeeds. \B will
363 # break (because it won't match at the end or start of a
364 # string), so we'll ignore patterns that feature it.
366 if pattern[:2] != '\\B' and pattern[-2:] != '\\B' \
367 and result is not None:
368 obj = re.compile(pattern)
369 result = obj.search(s, result.start(0), result.end(0) + 1)
370 if result is None:
371 print '=== Failed on range-limited match', t
373 # Try the match with IGNORECASE enabled, and check that it
374 # still succeeds.
375 obj = re.compile(pattern, re.IGNORECASE)
376 result = obj.search(s)
377 if result is None:
378 print '=== Fails on case-insensitive match', t
380 # Try the match with LOCALE enabled, and check that it
381 # still succeeds.
382 obj = re.compile(pattern, re.LOCALE)
383 result = obj.search(s)
384 if result is None:
385 print '=== Fails on locale-sensitive match', t
387 # Try the match with UNICODE locale enabled, and check
388 # that it still succeeds.
389 obj = re.compile(pattern, re.UNICODE)
390 result = obj.search(s)
391 if result is None:
392 print '=== Fails on unicode-sensitive match', t