2 sys
.path
=['.']+sys
.path
4 from test_support
import verbose
, TestFailed
6 import sys
, os
, string
, traceback
8 # Misc tests from Tim Peters' re.doc
11 print 'Running tests on re.search and re.match'
14 assert re
.search('x*', 'axx').span(0) == (0, 0)
15 assert re
.search('x*', 'axx').span() == (0, 0)
16 assert re
.search('x+', 'axx').span(0) == (1, 3)
17 assert re
.search('x+', 'axx').span() == (1, 3)
18 assert re
.search('x', 'aaa') == None
20 raise TestFailed
, "re.search"
23 assert re
.match('a*', 'xxx').span(0) == (0, 0)
24 assert re
.match('a*', 'xxx').span() == (0, 0)
25 assert re
.match('x*', 'xxxa').span(0) == (0, 3)
26 assert re
.match('x*', 'xxxa').span() == (0, 3)
27 assert re
.match('a+', 'xxx') == None
29 raise TestFailed
, "re.search"
32 print 'Running tests on re.sub'
35 assert 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 assert re
.sub(r
'\d+', bump_num
, '08.2 -2 23x99y') == '9.3 -3 24x100y'
42 assert re
.sub(r
'\d+', bump_num
, '08.2 -2 23x99y', 3) == '9.3 -3 23x99y'
44 assert re
.sub('.', lambda m
: r
"\n", 'x') == '\\n'
45 assert re
.sub('.', r
"\n", 'x') == '\n'
48 assert re
.sub('(.)', s
, 'x') == 'xx'
49 assert re
.sub('(.)', re
.escape(s
), 'x') == s
50 assert re
.sub('(.)', lambda m
: s
, 'x') == s
52 assert re
.sub('(?P<a>x)', '\g<a>\g<a>', 'xx') == 'xxxx'
53 assert re
.sub('(?P<a>x)', '\g<a>\g<1>', 'xx') == 'xxxx'
54 assert re
.sub('(?P<unk>x)', '\g<unk>\g<unk>', 'xx') == 'xxxx'
55 assert re
.sub('(?P<unk>x)', '\g<1>\g<1>', 'xx') == 'xxxx'
57 assert 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 assert re
.sub('a', '\t\n\v\r\f\a', 'a') == '\t\n\v\r\f\a'
59 assert re
.sub('a', '\t\n\v\r\f\a', 'a') == (chr(9)+chr(10)+chr(11)+chr(13)+chr(12)+chr(7))
61 assert re
.sub('^\s*', 'X', 'test') == 'Xtest'
62 except AssertionError:
63 raise TestFailed
, "re.sub"
67 assert re
.sub('a', 'b', 'aaaaa') == 'bbbbb'
68 assert re
.sub('a', 'b', 'aaaaa', 1) == 'baaaa'
69 except AssertionError:
70 raise TestFailed
, "qualified re.sub"
73 print 'Running tests on symbolic references'
76 re
.sub('(?P<a>x)', '\g<a', 'xx')
77 except re
.error
, reason
:
80 raise TestFailed
, "symbolic reference"
83 re
.sub('(?P<a>x)', '\g<', 'xx')
84 except re
.error
, reason
:
87 raise TestFailed
, "symbolic reference"
90 re
.sub('(?P<a>x)', '\g', 'xx')
91 except re
.error
, reason
:
94 raise TestFailed
, "symbolic reference"
97 re
.sub('(?P<a>x)', '\g<a a>', 'xx')
98 except re
.error
, reason
:
101 raise TestFailed
, "symbolic reference"
104 re
.sub('(?P<a>x)', '\g<1a1>', 'xx')
105 except re
.error
, reason
:
108 raise TestFailed
, "symbolic reference"
111 re
.sub('(?P<a>x)', '\g<ab>', 'xx')
112 except IndexError, reason
:
115 raise TestFailed
, "symbolic reference"
118 re
.sub('(?P<a>x)|(?P<b>y)', '\g<b>', 'xx')
119 except re
.error
, reason
:
122 raise TestFailed
, "symbolic reference"
125 re
.sub('(?P<a>x)|(?P<b>y)', '\\2', 'xx')
126 except re
.error
, reason
:
129 raise TestFailed
, "symbolic reference"
132 print 'Running tests on re.subn'
135 assert re
.subn("(?i)b+", "x", "bbbb BBBB") == ('x x', 2)
136 assert re
.subn("b+", "x", "bbbb BBBB") == ('x BBBB', 1)
137 assert re
.subn("b+", "x", "xyz") == ('xyz', 0)
138 assert re
.subn("b*", "x", "xyz") == ('xxxyxzx', 4)
139 assert re
.subn("b*", "x", "xyz", 2) == ('xxxyz', 2)
140 except AssertionError:
141 raise TestFailed
, "re.subn"
144 print 'Running tests on re.split'
147 assert re
.split(":", ":a:b::c") == ['', 'a', 'b', '', 'c']
148 assert re
.split(":*", ":a:b::c") == ['', 'a', 'b', 'c']
149 assert re
.split("(:*)", ":a:b::c") == ['', ':', 'a', ':', 'b', '::', 'c']
150 assert re
.split("(?::*)", ":a:b::c") == ['', 'a', 'b', 'c']
151 assert re
.split("(:)*", ":a:b::c") == ['', ':', 'a', ':', 'b', ':', 'c']
152 assert re
.split("([b:]+)", ":a:b::c") == ['', ':', 'a', ':b::', 'c']
153 assert re
.split("(b)|(:+)", ":a:b::c") == \
154 ['', None, ':', 'a', None, ':', '', 'b', None, '', None, '::', 'c']
155 assert re
.split("(?:b)|(?::+)", ":a:b::c") == ['', 'a', '', '', 'c']
156 except AssertionError:
157 raise TestFailed
, "re.split"
160 assert re
.split(":", ":a:b::c", 2) == ['', 'a', 'b::c']
161 assert re
.split(':', 'a:b:c:d', 2) == ['a', 'b', 'c:d']
163 assert re
.split("(:)", ":a:b::c", 2) == ['', ':', 'a', ':', 'b::c']
164 assert re
.split("(:*)", ":a:b::c", 2) == ['', ':', 'a', ':', 'b::c']
165 except AssertionError:
166 raise TestFailed
, "qualified re.split"
169 print "Running tests on re.findall"
172 assert re
.findall(":+", "abc") == []
173 assert re
.findall(":+", "a:b::c:::d") == [":", "::", ":::"]
174 assert re
.findall("(:+)", "a:b::c:::d") == [":", "::", ":::"]
175 assert re
.findall("(:)(:*)", "a:b::c:::d") == [(":", ""),
178 except AssertionError:
179 raise TestFailed
, "re.findall"
182 print "Running tests on re.match"
186 m
= re
.match('a', 'a') ; assert m
.groups() == ()
188 m
= re
.match('(a)', 'a') ; assert m
.groups() == ('a',)
190 pat
= re
.compile('((a)|(b))(c)?')
191 assert pat
.match('a').groups() == ('a', 'a', None, None)
192 assert pat
.match('b').groups() == ('b', None, 'b', None)
193 assert pat
.match('ac').groups() == ('a', 'a', None, 'c')
194 assert pat
.match('bc').groups() == ('b', None, 'b', 'c')
195 assert pat
.match('bc').groups("") == ('b', "", 'b', 'c')
196 except AssertionError:
197 raise TestFailed
, "match .groups() method"
201 m
= re
.match('(a)', 'a')
202 assert m
.group(0) == 'a' ; assert m
.group(0) == 'a'
203 assert m
.group(1) == 'a' ; assert m
.group(1, 1) == ('a', 'a')
205 pat
= re
.compile('(?:(?P<a1>a)|(?P<b2>b))(?P<c3>c)?')
206 assert pat
.match('a').group(1, 2, 3) == ('a', None, None)
207 assert pat
.match('b').group('a1', 'b2', 'c3') == (None, 'b', None)
208 assert pat
.match('ac').group(1, 'b2', 3) == ('a', None, 'c')
209 except AssertionError:
210 raise TestFailed
, "match .group() method"
213 print "Running tests on re.escape"
217 for i
in range(0, 256):
219 assert re
.match(re
.escape(chr(i
)), chr(i
)) != None
220 assert re
.match(re
.escape(chr(i
)), chr(i
)).span() == (0,1)
222 pat
=re
.compile( re
.escape(p
) )
223 assert pat
.match(p
) != None
224 assert pat
.match(p
).span() == (0,256)
225 except AssertionError:
226 raise TestFailed
, "re.escape"
230 print 'Pickling a RegexObject instance'
233 pat
= re
.compile('a(?:b|(c|e){1,2}?|d)+?(.)')
234 s
= pickle
.dumps(pat
)
235 pat
= pickle
.loads(s
)
238 assert re
.I
== re
.IGNORECASE
239 assert re
.L
== re
.LOCALE
240 assert re
.M
== re
.MULTILINE
241 assert re
.S
== re
.DOTALL
242 assert re
.X
== re
.VERBOSE
243 except AssertionError:
244 raise TestFailed
, 're module constants'
246 for flags
in [re
.I
, re
.M
, re
.X
, re
.S
, re
.L
]:
248 r
= re
.compile('^pattern$', flags
)
250 print 'Exception raised on flag', flags
252 from re_tests
import *
255 print 'Running re_tests test suite'
257 # To save time, only run the first and last 10 tests
258 #tests = tests[:10] + tests[-10:]
263 pattern
=s
=outcome
=repl
=expected
=None
265 pattern
, s
, outcome
, repl
, expected
= t
267 pattern
, s
, outcome
= t
269 raise ValueError, ('Test tuples should have 3 or 5 fields',t
)
272 obj
=re
.compile(pattern
)
274 if outcome
==SYNTAX_ERROR
: pass # Expected a syntax error
276 print '=== Syntax error:', t
277 except KeyboardInterrupt: raise KeyboardInterrupt
279 print '*** Unexpected error ***', t
281 traceback
.print_exc(file=sys
.stdout
)
285 except (re
.error
), msg
:
286 print '=== Unexpected exception', t
, repr(msg
)
287 if outcome
==SYNTAX_ERROR
:
288 # This should have been a syntax error; forget it.
291 if result
is None: pass # No match, as expected
292 else: print '=== Succeeded incorrectly', t
293 elif outcome
==SUCCEED
:
294 if result
is not None:
295 # Matched, as expected, so now we compute the
296 # result string and compare it to our expected result.
297 start
, end
= result
.span(0)
298 vardict
={'found': result
.group(0),
299 'groups': result
.group(),
300 'flags': result
.re
.flags
}
301 for i
in range(1, 100):
304 # Special hack because else the string concat fails:
309 vardict
['g%d' % i
] = gi
310 for i
in result
.re
.groupindex
.keys():
318 repl
=eval(repl
, vardict
)
320 print '=== grouping error', t
,
321 print repr(repl
)+' should be '+repr(expected
)
323 print '=== Failed incorrectly', t
325 # Try the match with the search area limited to the extent
326 # of the match and see if it still succeeds. \B will
327 # break (because it won't match at the end or start of a
328 # string), so we'll ignore patterns that feature it.
330 if pattern
[:2]!='\\B' and pattern
[-2:]!='\\B':
331 obj
=re
.compile(pattern
)
332 result
=obj
.search(s
, pos
=result
.start(0), endpos
=result
.end(0)+1)
334 print '=== Failed on range-limited match', t
336 # Try the match with IGNORECASE enabled, and check that it
338 obj
=re
.compile(pattern
, re
.IGNORECASE
)
341 print '=== Fails on case-insensitive match', t
343 # Try the match with LOCALE enabled, and check that it
345 obj
=re
.compile(pattern
, re
.LOCALE
)
348 print '=== Fails on locale-sensitive match', t