2 sys
.path
= ['.'] + sys
.path
4 from test_support
import verify
, verbose
, TestFailed
6 import sys
, os
, traceback
8 # Misc tests from Tim Peters' re.doc
11 print 'Running tests on re.search and re.match'
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)
20 raise TestFailed
, "re.search"
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)
29 raise TestFailed
, "re.search"
32 print 'Running tests on re.sub'
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')
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"
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"
79 print 'Running tests on symbolic references'
82 re
.sub('(?P<a>x)', '\g<a', 'xx')
83 except re
.error
, reason
:
86 raise TestFailed
, "symbolic reference"
89 re
.sub('(?P<a>x)', '\g<', 'xx')
90 except re
.error
, reason
:
93 raise TestFailed
, "symbolic reference"
96 re
.sub('(?P<a>x)', '\g', 'xx')
97 except re
.error
, reason
:
100 raise TestFailed
, "symbolic reference"
103 re
.sub('(?P<a>x)', '\g<a a>', 'xx')
104 except re
.error
, reason
:
107 raise TestFailed
, "symbolic reference"
110 re
.sub('(?P<a>x)', '\g<1a1>', 'xx')
111 except re
.error
, reason
:
114 raise TestFailed
, "symbolic reference"
117 re
.sub('(?P<a>x)', '\g<ab>', 'xx')
118 except IndexError, reason
:
121 raise TestFailed
, "symbolic reference"
124 re
.sub('(?P<a>x)|(?P<b>y)', '\g<b>', 'xx')
125 except re
.error
, reason
:
128 raise TestFailed
, "symbolic reference"
131 re
.sub('(?P<a>x)|(?P<b>y)', '\\2', 'xx')
132 except re
.error
, reason
:
135 raise TestFailed
, "symbolic reference"
138 print 'Running tests on re.subn'
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"
150 print 'Running tests on re.split'
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"
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"
175 print "Running tests on re.findall"
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") == [(":", ""),
184 except AssertionError:
185 raise TestFailed
, "re.findall"
188 print "Running tests on re.match"
192 m
= re
.match('a', 'a') ; verify(m
.groups() == ())
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"
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"
221 print "Running tests on re.escape"
225 for i
in range(0, 256):
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"
238 print 'Pickling a RegexObject instance'
241 pat
= re
.compile('a(?:b|(c|e){1,2}?|d)+?(.)')
242 s
= pickle
.dumps(pat
)
243 pat
= pickle
.loads(s
)
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
]:
256 r
= re
.compile('^pattern$', flags
)
258 print 'Exception raised on flag', flags
261 print 'Test engine limitations'
263 # Try nasty case that overflows the straightforward recursive
264 # implementation of repeated groups.
266 verify(re
.match('(x)*', 50000*'x').span() == (0, 50000))
267 except RuntimeError, v
:
270 from re_tests
import *
273 print 'Running re_tests test suite'
275 # To save time, only run the first and last 10 tests
276 #tests = tests[:10] + tests[-10:]
281 pattern
= s
= outcome
= repl
= expected
= None
283 pattern
, s
, outcome
, repl
, expected
= t
285 pattern
, s
, outcome
= t
287 raise ValueError, ('Test tuples should have 3 or 5 fields', t
)
290 obj
= re
.compile(pattern
)
292 if outcome
== SYNTAX_ERROR
: pass # Expected a syntax error
294 print '=== Syntax error:', t
295 except KeyboardInterrupt: raise KeyboardInterrupt
297 print '*** Unexpected error ***', t
299 traceback
.print_exc(file=sys
.stdout
)
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.
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):
322 # Special hack because else the string concat fails:
327 vardict
['g%d' % i
] = gi
328 for i
in result
.re
.groupindex
.keys():
336 repl
= eval(repl
, vardict
)
338 print '=== grouping error', t
,
339 print repr(repl
) + ' should be ' + repr(expected
)
341 print '=== Failed incorrectly', t
343 # Try the match on a unicode string, and check that it
346 result
= obj
.search(unicode(s
, "latin-1"))
348 print '=== Fails on unicode match', t
352 continue # unicode test case
354 # Try the match on a unicode pattern, and check that it
356 obj
=re
.compile(unicode(pattern
, "latin-1"))
357 result
= obj
.search(s
)
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)
371 print '=== Failed on range-limited match', t
373 # Try the match with IGNORECASE enabled, and check that it
375 obj
= re
.compile(pattern
, re
.IGNORECASE
)
376 result
= obj
.search(s
)
378 print '=== Fails on case-insensitive match', t
380 # Try the match with LOCALE enabled, and check that it
382 obj
= re
.compile(pattern
, re
.LOCALE
)
383 result
= obj
.search(s
)
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
)
392 print '=== Fails on unicode-sensitive match', t