Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 03dd010

Browse files
author
Fredrik Lundh
committed
updated SRE test suite (fixes PEP223 problem, shows syntax errors)
1 parent acee486 commit 03dd010

1 file changed

Lines changed: 78 additions & 76 deletions

File tree

Lib/test/test_sre.py

Lines changed: 78 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ def test(expression, result, exception=None):
4747
print 'Running tests on character literals'
4848

4949
for i in [0, 8, 16, 32, 64, 127, 128, 255]:
50-
test(r"""sre.match("\%03o" % i, chr(i)) != None""", 1)
51-
test(r"""sre.match("\%03o0" % i, chr(i)+"0") != None""", 1)
52-
test(r"""sre.match("\%03o8" % i, chr(i)+"8") != None""", 1)
50+
test(r"""sre.match(r"\%03o" % i, chr(i)) != None""", 1)
51+
test(r"""sre.match(r"\%03o0" % i, chr(i)+"0") != None""", 1)
52+
test(r"""sre.match(r"\%03o8" % i, chr(i)+"8") != None""", 1)
5353
test(r"""sre.match(r"\x%02x" % i, chr(i)) != None""", 1)
5454
test(r"""sre.match(r"\x%02x0" % i, chr(i)+"0") != None""", 1)
5555
test(r"""sre.match(r"\x%02xz" % i, chr(i)+"z") != None""", 1)
@@ -61,27 +61,27 @@ def test(expression, result, exception=None):
6161
if verbose:
6262
print 'Running tests on sre.search and sre.match'
6363

64-
test(r"""sre.search('x*', 'axx').span(0)""", (0, 0))
65-
test(r"""sre.search('x*', 'axx').span()""", (0, 0))
66-
test(r"""sre.search('x+', 'axx').span(0)""", (1, 3))
67-
test(r"""sre.search('x+', 'axx').span()""", (1, 3))
68-
test(r"""sre.search('x', 'aaa')""", None)
64+
test(r"""sre.search(r'x*', 'axx').span(0)""", (0, 0))
65+
test(r"""sre.search(r'x*', 'axx').span()""", (0, 0))
66+
test(r"""sre.search(r'x+', 'axx').span(0)""", (1, 3))
67+
test(r"""sre.search(r'x+', 'axx').span()""", (1, 3))
68+
test(r"""sre.search(r'x', 'aaa')""", None)
6969

70-
test(r"""sre.match('a*', 'xxx').span(0)""", (0, 0))
71-
test(r"""sre.match('a*', 'xxx').span()""", (0, 0))
72-
test(r"""sre.match('x*', 'xxxa').span(0)""", (0, 3))
73-
test(r"""sre.match('x*', 'xxxa').span()""", (0, 3))
74-
test(r"""sre.match('a+', 'xxx')""", None)
70+
test(r"""sre.match(r'a*', 'xxx').span(0)""", (0, 0))
71+
test(r"""sre.match(r'a*', 'xxx').span()""", (0, 0))
72+
test(r"""sre.match(r'x*', 'xxxa').span(0)""", (0, 3))
73+
test(r"""sre.match(r'x*', 'xxxa').span()""", (0, 3))
74+
test(r"""sre.match(r'a+', 'xxx')""", None)
7575

7676
# bug 113254
77-
test(r"""sre.match('(a)|(b)', 'b').start(1)""", -1)
78-
test(r"""sre.match('(a)|(b)', 'b').end(1)""", -1)
79-
test(r"""sre.match('(a)|(b)', 'b').span(1)""", (-1, -1))
77+
test(r"""sre.match(r'(a)|(b)', 'b').start(1)""", -1)
78+
test(r"""sre.match(r'(a)|(b)', 'b').end(1)""", -1)
79+
test(r"""sre.match(r'(a)|(b)', 'b').span(1)""", (-1, -1))
8080

8181
if verbose:
8282
print 'Running tests on sre.sub'
8383

84-
test(r"""sre.sub("(?i)b+", "x", "bbbb BBBB")""", 'x x')
84+
test(r"""sre.sub(r"(?i)b+", "x", "bbbb BBBB")""", 'x x')
8585

8686
def bump_num(matchobj):
8787
int_value = int(matchobj.group(0))
@@ -90,97 +90,97 @@ def bump_num(matchobj):
9090
test(r"""sre.sub(r'\d+', bump_num, '08.2 -2 23x99y')""", '9.3 -3 24x100y')
9191
test(r"""sre.sub(r'\d+', bump_num, '08.2 -2 23x99y', 3)""", '9.3 -3 23x99y')
9292

93-
test(r"""sre.sub('.', lambda m: r"\n", 'x')""", '\\n')
94-
test(r"""sre.sub('.', r"\n", 'x')""", '\n')
93+
test(r"""sre.sub(r'.', lambda m: r"\n", 'x')""", '\\n')
94+
test(r"""sre.sub(r'.', r"\n", 'x')""", '\n')
9595

9696
s = r"\1\1"
9797

98-
test(r"""sre.sub('(.)', s, 'x')""", 'xx')
99-
test(r"""sre.sub('(.)', sre.escape(s), 'x')""", s)
100-
test(r"""sre.sub('(.)', lambda m: s, 'x')""", s)
98+
test(r"""sre.sub(r'(.)', s, 'x')""", 'xx')
99+
test(r"""sre.sub(r'(.)', sre.escape(s), 'x')""", s)
100+
test(r"""sre.sub(r'(.)', lambda m: s, 'x')""", s)
101101

102-
test(r"""sre.sub('(?P<a>x)', '\g<a>\g<a>', 'xx')""", 'xxxx')
103-
test(r"""sre.sub('(?P<a>x)', '\g<a>\g<1>', 'xx')""", 'xxxx')
104-
test(r"""sre.sub('(?P<unk>x)', '\g<unk>\g<unk>', 'xx')""", 'xxxx')
105-
test(r"""sre.sub('(?P<unk>x)', '\g<1>\g<1>', 'xx')""", 'xxxx')
102+
test(r"""sre.sub(r'(?P<a>x)', '\g<a>\g<a>', 'xx')""", 'xxxx')
103+
test(r"""sre.sub(r'(?P<a>x)', '\g<a>\g<1>', 'xx')""", 'xxxx')
104+
test(r"""sre.sub(r'(?P<unk>x)', '\g<unk>\g<unk>', 'xx')""", 'xxxx')
105+
test(r"""sre.sub(r'(?P<unk>x)', '\g<1>\g<1>', 'xx')""", 'xxxx')
106106

107-
test(r"""sre.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')
108-
test(r"""sre.sub('a', '\t\n\v\r\f\a', 'a')""", '\t\n\v\r\f\a')
109-
test(r"""sre.sub('a', '\t\n\v\r\f\a', 'a')""", (chr(9)+chr(10)+chr(11)+chr(13)+chr(12)+chr(7)))
107+
test(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')
108+
test(r"""sre.sub(r'a', '\t\n\v\r\f\a', 'a')""", '\t\n\v\r\f\a')
109+
test(r"""sre.sub(r'a', '\t\n\v\r\f\a', 'a')""", (chr(9)+chr(10)+chr(11)+chr(13)+chr(12)+chr(7)))
110110

111-
test(r"""sre.sub('^\s*', 'X', 'test')""", 'Xtest')
111+
test(r"""sre.sub(r'^\s*', 'X', 'test')""", 'Xtest')
112112

113113
# qualified sub
114-
test(r"""sre.sub('a', 'b', 'aaaaa')""", 'bbbbb')
115-
test(r"""sre.sub('a', 'b', 'aaaaa', 1)""", 'baaaa')
114+
test(r"""sre.sub(r'a', 'b', 'aaaaa')""", 'bbbbb')
115+
test(r"""sre.sub(r'a', 'b', 'aaaaa', 1)""", 'baaaa')
116116

117117
if verbose:
118118
print 'Running tests on symbolic references'
119119

120-
test(r"""sre.sub('(?P<a>x)', '\g<a', 'xx')""", None, sre.error)
121-
test(r"""sre.sub('(?P<a>x)', '\g<', 'xx')""", None, sre.error)
122-
test(r"""sre.sub('(?P<a>x)', '\g', 'xx')""", None, sre.error)
123-
test(r"""sre.sub('(?P<a>x)', '\g<a a>', 'xx')""", None, sre.error)
124-
test(r"""sre.sub('(?P<a>x)', '\g<1a1>', 'xx')""", None, sre.error)
125-
test(r"""sre.sub('(?P<a>x)', '\g<ab>', 'xx')""", None, IndexError)
126-
test(r"""sre.sub('(?P<a>x)|(?P<b>y)', '\g<b>', 'xx')""", None, sre.error)
127-
test(r"""sre.sub('(?P<a>x)|(?P<b>y)', '\\2', 'xx')""", None, sre.error)
120+
test(r"""sre.sub(r'(?P<a>x)', '\g<a', 'xx')""", None, sre.error)
121+
test(r"""sre.sub(r'(?P<a>x)', '\g<', 'xx')""", None, sre.error)
122+
test(r"""sre.sub(r'(?P<a>x)', '\g', 'xx')""", None, sre.error)
123+
test(r"""sre.sub(r'(?P<a>x)', '\g<a a>', 'xx')""", None, sre.error)
124+
test(r"""sre.sub(r'(?P<a>x)', '\g<1a1>', 'xx')""", None, sre.error)
125+
test(r"""sre.sub(r'(?P<a>x)', '\g<ab>', 'xx')""", None, IndexError)
126+
test(r"""sre.sub(r'(?P<a>x)|(?P<b>y)', '\g<b>', 'xx')""", None, sre.error)
127+
test(r"""sre.sub(r'(?P<a>x)|(?P<b>y)', '\\2', 'xx')""", None, sre.error)
128128

129129
if verbose:
130130
print 'Running tests on sre.subn'
131131

132-
test(r"""sre.subn("(?i)b+", "x", "bbbb BBBB")""", ('x x', 2))
133-
test(r"""sre.subn("b+", "x", "bbbb BBBB")""", ('x BBBB', 1))
134-
test(r"""sre.subn("b+", "x", "xyz")""", ('xyz', 0))
135-
test(r"""sre.subn("b*", "x", "xyz")""", ('xxxyxzx', 4))
136-
test(r"""sre.subn("b*", "x", "xyz", 2)""", ('xxxyz', 2))
132+
test(r"""sre.subn(r"(?i)b+", "x", "bbbb BBBB")""", ('x x', 2))
133+
test(r"""sre.subn(r"b+", "x", "bbbb BBBB")""", ('x BBBB', 1))
134+
test(r"""sre.subn(r"b+", "x", "xyz")""", ('xyz', 0))
135+
test(r"""sre.subn(r"b*", "x", "xyz")""", ('xxxyxzx', 4))
136+
test(r"""sre.subn(r"b*", "x", "xyz", 2)""", ('xxxyz', 2))
137137

138138
if verbose:
139139
print 'Running tests on sre.split'
140140

141-
test(r"""sre.split(":", ":a:b::c")""", ['', 'a', 'b', '', 'c'])
142-
test(r"""sre.split(":*", ":a:b::c")""", ['', 'a', 'b', 'c'])
143-
test(r"""sre.split("(:*)", ":a:b::c")""", ['', ':', 'a', ':', 'b', '::', 'c'])
144-
test(r"""sre.split("(?::*)", ":a:b::c")""", ['', 'a', 'b', 'c'])
145-
test(r"""sre.split("(:)*", ":a:b::c")""", ['', ':', 'a', ':', 'b', ':', 'c'])
146-
test(r"""sre.split("([b:]+)", ":a:b::c")""", ['', ':', 'a', ':b::', 'c'])
147-
test(r"""sre.split("(b)|(:+)", ":a:b::c")""",
141+
test(r"""sre.split(r":", ":a:b::c")""", ['', 'a', 'b', '', 'c'])
142+
test(r"""sre.split(r":*", ":a:b::c")""", ['', 'a', 'b', 'c'])
143+
test(r"""sre.split(r"(:*)", ":a:b::c")""", ['', ':', 'a', ':', 'b', '::', 'c'])
144+
test(r"""sre.split(r"(?::*)", ":a:b::c")""", ['', 'a', 'b', 'c'])
145+
test(r"""sre.split(r"(:)*", ":a:b::c")""", ['', ':', 'a', ':', 'b', ':', 'c'])
146+
test(r"""sre.split(r"([b:]+)", ":a:b::c")""", ['', ':', 'a', ':b::', 'c'])
147+
test(r"""sre.split(r"(b)|(:+)", ":a:b::c")""",
148148
['', None, ':', 'a', None, ':', '', 'b', None, '', None, '::', 'c'])
149-
test(r"""sre.split("(?:b)|(?::+)", ":a:b::c")""", ['', 'a', '', '', 'c'])
149+
test(r"""sre.split(r"(?:b)|(?::+)", ":a:b::c")""", ['', 'a', '', '', 'c'])
150150

151-
test(r"""sre.split(":", ":a:b::c", 2)""", ['', 'a', 'b::c'])
152-
test(r"""sre.split(':', 'a:b:c:d', 2)""", ['a', 'b', 'c:d'])
151+
test(r"""sre.split(r":", ":a:b::c", 2)""", ['', 'a', 'b::c'])
152+
test(r"""sre.split(r':', 'a:b:c:d', 2)""", ['a', 'b', 'c:d'])
153153

154-
test(r"""sre.split("(:)", ":a:b::c", 2)""", ['', ':', 'a', ':', 'b::c'])
155-
test(r"""sre.split("(:*)", ":a:b::c", 2)""", ['', ':', 'a', ':', 'b::c'])
154+
test(r"""sre.split(r"(:)", ":a:b::c", 2)""", ['', ':', 'a', ':', 'b::c'])
155+
test(r"""sre.split(r"(:*)", ":a:b::c", 2)""", ['', ':', 'a', ':', 'b::c'])
156156

157157
if verbose:
158158
print "Running tests on sre.findall"
159159

160-
test(r"""sre.findall(":+", "abc")""", [])
161-
test(r"""sre.findall(":+", "a:b::c:::d")""", [":", "::", ":::"])
162-
test(r"""sre.findall("(:+)", "a:b::c:::d")""", [":", "::", ":::"])
163-
test(r"""sre.findall("(:)(:*)", "a:b::c:::d")""",
160+
test(r"""sre.findall(r":+", "abc")""", [])
161+
test(r"""sre.findall(r":+", "a:b::c:::d")""", [":", "::", ":::"])
162+
test(r"""sre.findall(r"(:+)", "a:b::c:::d")""", [":", "::", ":::"])
163+
test(r"""sre.findall(r"(:)(:*)", "a:b::c:::d")""",
164164
[(":", ""), (":", ":"), (":", "::")])
165-
test(r"""sre.findall("(a)|(b)", "abc")""", [("a", ""), ("", "b")])
165+
test(r"""sre.findall(r"(a)|(b)", "abc")""", [("a", ""), ("", "b")])
166166

167167
if verbose:
168168
print "Running tests on sre.match"
169169

170-
test(r"""sre.match('a', 'a').groups()""", ())
171-
test(r"""sre.match('(a)', 'a').groups()""", ('a',))
172-
test(r"""sre.match('(a)', 'a').group(0)""", 'a')
173-
test(r"""sre.match('(a)', 'a').group(1)""", 'a')
174-
test(r"""sre.match('(a)', 'a').group(1, 1)""", ('a', 'a'))
170+
test(r"""sre.match(r'a', 'a').groups()""", ())
171+
test(r"""sre.match(r'(a)', 'a').groups()""", ('a',))
172+
test(r"""sre.match(r'(a)', 'a').group(0)""", 'a')
173+
test(r"""sre.match(r'(a)', 'a').group(1)""", 'a')
174+
test(r"""sre.match(r'(a)', 'a').group(1, 1)""", ('a', 'a'))
175175

176-
pat = sre.compile('((a)|(b))(c)?')
176+
pat = sre.compile(r'((a)|(b))(c)?')
177177
test(r"""pat.match('a').groups()""", ('a', 'a', None, None))
178178
test(r"""pat.match('b').groups()""", ('b', None, 'b', None))
179179
test(r"""pat.match('ac').groups()""", ('a', 'a', None, 'c'))
180180
test(r"""pat.match('bc').groups()""", ('b', None, 'b', 'c'))
181181
test(r"""pat.match('bc').groups("")""", ('b', "", 'b', 'c'))
182182

183-
pat = sre.compile('(?:(?P<a1>a)|(?P<b2>b))(?P<c3>c)?')
183+
pat = sre.compile(r'(?:(?P<a1>a)|(?P<b2>b))(?P<c3>c)?')
184184
test(r"""pat.match('a').group(1, 2, 3)""", ('a', None, None))
185185
test(r"""pat.match('b').group('a1', 'b2', 'c3')""", (None, 'b', None))
186186
test(r"""pat.match('ac').group(1, 'b2', 3)""", ('a', None, 'c'))
@@ -203,15 +203,15 @@ def bump_num(matchobj):
203203

204204
try:
205205
import pickle
206-
pat = sre.compile('a(?:b|(c|e){1,2}?|d)+?(.)')
206+
pat = sre.compile(r'a(?:b|(c|e){1,2}?|d)+?(.)')
207207
s = pickle.dumps(pat)
208208
pat = pickle.loads(s)
209209
except:
210210
print TestFailed, 're module pickle' # expected
211211

212212
try:
213213
import cPickle
214-
pat = sre.compile('a(?:b|(c|e){1,2}?|d)+?(.)')
214+
pat = sre.compile(r'a(?:b|(c|e){1,2}?|d)+?(.)')
215215
s = cPickle.dumps(pat)
216216
pat = cPickle.loads(s)
217217
except:
@@ -237,9 +237,12 @@ def bump_num(matchobj):
237237

238238
# Try nasty case that overflows the straightforward recursive
239239
# implementation of repeated groups.
240-
test(r"""sre.match('(x)*', 50000*'x').span()""", (0, 50000), RuntimeError)
241-
test(r"""sre.match('(x)*y', 50000*'x'+'y').span()""", (0, 50001), RuntimeError)
242-
test(r"""sre.match('(x)*?y', 50000*'x'+'y').span()""", (0, 50001), RuntimeError)
240+
test(r"""sre.match(r'(x)*', 50000*'x').span()""",
241+
(0, 50000), RuntimeError)
242+
test(r"""sre.match(r'(x)*y', 50000*'x'+'y').span()""",
243+
(0, 50001), RuntimeError)
244+
test(r"""sre.match(r'(x)*?y', 50000*'x'+'y').span()""",
245+
(0, 50001), RuntimeError)
243246

244247
from re_tests import *
245248

@@ -277,8 +280,7 @@ def bump_num(matchobj):
277280
except (sre.error), msg:
278281
print '=== Unexpected exception', t, repr(msg)
279282
if outcome==SYNTAX_ERROR:
280-
# This should have been a syntax error; forget it.
281-
pass
283+
print '=== Compiled incorrectly', t
282284
elif outcome==FAIL:
283285
if result is None: pass # No match, as expected
284286
else: print '=== Succeeded incorrectly', t

0 commit comments

Comments
 (0)