Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(167450)

Delta Between Two Patch Sets: Lib/test/test_re.py

Issue 16510: Using appropriate checks in tests
Left Patch Set: Created 6 years ago
Right Patch Set: Created 5 years, 10 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « Lib/test/test_pyexpat.py ('k') | Lib/test/test_richcmp.py » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 from test.support import verbose, run_unittest, gc_collect, bigmemtest, _2G, \ 1 from test.support import verbose, run_unittest, gc_collect, bigmemtest, _2G, \
2 cpython_only 2 cpython_only, captured_stdout
3 import io 3 import io
4 import re 4 import re
5 from re import Scanner 5 from re import Scanner
6 import sre_compile 6 import sre_compile
7 import sre_constants 7 import sre_constants
8 import sys 8 import sys
9 import string 9 import string
10 import traceback 10 import traceback
11 import unittest 11 import unittest
12 from weakref import proxy 12 from weakref import proxy
(...skipping 328 matching lines...) Expand 10 before | Expand all | Expand 10 after
341 self.assertEqual(m.group(0), 'a') 341 self.assertEqual(m.group(0), 'a')
342 self.assertEqual(m.group(0), 'a') 342 self.assertEqual(m.group(0), 'a')
343 self.assertEqual(m.group(1), 'a') 343 self.assertEqual(m.group(1), 'a')
344 self.assertEqual(m.group(1, 1), ('a', 'a')) 344 self.assertEqual(m.group(1, 1), ('a', 'a'))
345 345
346 pat = re.compile('(?:(?P<a1>a)|(?P<b2>b))(?P<c3>c)?') 346 pat = re.compile('(?:(?P<a1>a)|(?P<b2>b))(?P<c3>c)?')
347 self.assertEqual(pat.match('a').group(1, 2, 3), ('a', None, None)) 347 self.assertEqual(pat.match('a').group(1, 2, 3), ('a', None, None))
348 self.assertEqual(pat.match('b').group('a1', 'b2', 'c3'), 348 self.assertEqual(pat.match('b').group('a1', 'b2', 'c3'),
349 (None, 'b', None)) 349 (None, 'b', None))
350 self.assertEqual(pat.match('ac').group(1, 'b2', 3), ('a', None, 'c')) 350 self.assertEqual(pat.match('ac').group(1, 'b2', 3), ('a', None, 'c'))
351
352 def test_re_fullmatch(self):
353 # Issue 16203: Proposal: add re.fullmatch() method.
354 self.assertEqual(re.fullmatch(r"a", "a").span(), (0, 1))
355 for string in "ab", S("ab"):
356 self.assertEqual(re.fullmatch(r"a|ab", string).span(), (0, 2))
357 for string in b"ab", B(b"ab"), bytearray(b"ab"), memoryview(b"ab"):
358 self.assertEqual(re.fullmatch(br"a|ab", string).span(), (0, 2))
359 for a, b in "\xe0\xdf", "\u0430\u0431", "\U0001d49c\U0001d49e":
360 r = r"%s|%s" % (a, a + b)
361 self.assertEqual(re.fullmatch(r, a + b).span(), (0, 2))
362 self.assertEqual(re.fullmatch(r".*?$", "abc").span(), (0, 3))
363 self.assertEqual(re.fullmatch(r".*?", "abc").span(), (0, 3))
364 self.assertEqual(re.fullmatch(r"a.*?b", "ab").span(), (0, 2))
365 self.assertEqual(re.fullmatch(r"a.*?b", "abb").span(), (0, 3))
366 self.assertEqual(re.fullmatch(r"a.*?b", "axxb").span(), (0, 4))
367 self.assertIsNone(re.fullmatch(r"a+", "ab"))
368 self.assertIsNone(re.fullmatch(r"abc$", "abc\n"))
369 self.assertIsNone(re.fullmatch(r"abc\Z", "abc\n"))
370 self.assertIsNone(re.fullmatch(r"(?m)abc$", "abc\n"))
371 self.assertEqual(re.fullmatch(r"ab(?=c)cd", "abcd").span(), (0, 4))
372 self.assertEqual(re.fullmatch(r"ab(?<=b)cd", "abcd").span(), (0, 4))
373 self.assertEqual(re.fullmatch(r"(?=a|ab)ab", "ab").span(), (0, 2))
374
375 self.assertEqual(
376 re.compile(r"bc").fullmatch("abcd", pos=1, endpos=3).span(), (1, 3))
377 self.assertEqual(
378 re.compile(r".*?$").fullmatch("abcd", pos=1, endpos=3).span(), (1, 3 ))
379 self.assertEqual(
380 re.compile(r".*?").fullmatch("abcd", pos=1, endpos=3).span(), (1, 3) )
351 381
352 def test_re_groupref_exists(self): 382 def test_re_groupref_exists(self):
353 self.assertEqual(re.match('^(\()?([^()]+)(?(1)\))$', '(a)').groups(), 383 self.assertEqual(re.match('^(\()?([^()]+)(?(1)\))$', '(a)').groups(),
354 ('(', 'a')) 384 ('(', 'a'))
355 self.assertEqual(re.match('^(\()?([^()]+)(?(1)\))$', 'a').groups(), 385 self.assertEqual(re.match('^(\()?([^()]+)(?(1)\))$', 'a').groups(),
356 (None, 'a')) 386 (None, 'a'))
357 self.assertEqual(re.match('^(\()?([^()]+)(?(1)\))$', 'a)'), None) 387 self.assertEqual(re.match('^(\()?([^()]+)(?(1)\))$', 'a)'), None)
358 self.assertEqual(re.match('^(\()?([^()]+)(?(1)\))$', '(a'), None) 388 self.assertEqual(re.match('^(\()?([^()]+)(?(1)\))$', '(a'), None)
359 self.assertEqual(re.match('^(?:(a)|c)((?(1)b|d))$', 'ab').groups(), 389 self.assertEqual(re.match('^(?:(a)|c)((?(1)b|d))$', 'ab').groups(),
360 ('a', 'b')) 390 ('a', 'b'))
(...skipping 794 matching lines...) Expand 10 before | Expand all | Expand 10 after
1155 1185
1156 def test_bug_2537(self): 1186 def test_bug_2537(self):
1157 # issue 2537: empty submatches 1187 # issue 2537: empty submatches
1158 for outer_op in ('{0,}', '*', '+', '{1,187}'): 1188 for outer_op in ('{0,}', '*', '+', '{1,187}'):
1159 for inner_op in ('{0,}', '*', '?'): 1189 for inner_op in ('{0,}', '*', '?'):
1160 r = re.compile("^((x|y)%s)%s" % (inner_op, outer_op)) 1190 r = re.compile("^((x|y)%s)%s" % (inner_op, outer_op))
1161 m = r.match("xyyzy") 1191 m = r.match("xyyzy")
1162 self.assertEqual(m.group(0), "xyy") 1192 self.assertEqual(m.group(0), "xyy")
1163 self.assertEqual(m.group(1), "") 1193 self.assertEqual(m.group(1), "")
1164 self.assertEqual(m.group(2), "y") 1194 self.assertEqual(m.group(2), "y")
1195
1196 def test_debug_flag(self):
1197 with captured_stdout() as out:
1198 re.compile('foo', re.DEBUG)
1199 self.assertEqual(out.getvalue().splitlines(),
1200 ['literal 102 ', 'literal 111 ', 'literal 111 '])
1201 # Debug output is output again even a second time (bypassing
1202 # the cache -- issue #20426).
1203 with captured_stdout() as out:
1204 re.compile('foo', re.DEBUG)
1205 self.assertEqual(out.getvalue().splitlines(),
1206 ['literal 102 ', 'literal 111 ', 'literal 111 '])
1207
1208
1209 class PatternReprTests(unittest.TestCase):
1210 def check(self, pattern, expected):
1211 self.assertEqual(repr(re.compile(pattern)), expected)
1212
1213 def check_flags(self, pattern, flags, expected):
1214 self.assertEqual(repr(re.compile(pattern, flags)), expected)
1215
1216 def test_without_flags(self):
1217 self.check('random pattern',
1218 "re.compile('random pattern')")
1219
1220 def test_single_flag(self):
1221 self.check_flags('random pattern', re.IGNORECASE,
1222 "re.compile('random pattern', re.IGNORECASE)")
1223
1224 def test_multiple_flags(self):
1225 self.check_flags('random pattern', re.I|re.S|re.X,
1226 "re.compile('random pattern', "
1227 "re.IGNORECASE|re.DOTALL|re.VERBOSE)")
1228
1229 def test_unicode_flag(self):
1230 self.check_flags('random pattern', re.U,
1231 "re.compile('random pattern')")
1232 self.check_flags('random pattern', re.I|re.S|re.U,
1233 "re.compile('random pattern', "
1234 "re.IGNORECASE|re.DOTALL)")
1235
1236 def test_inline_flags(self):
1237 self.check('(?i)pattern',
1238 "re.compile('(?i)pattern', re.IGNORECASE)")
1239
1240 def test_unknown_flags(self):
1241 self.check_flags('random pattern', 0x123000,
1242 "re.compile('random pattern', 0x123000)")
1243 self.check_flags('random pattern', 0x123000|re.I,
1244 "re.compile('random pattern', re.IGNORECASE|0x123000)")
1245
1246 def test_bytes(self):
1247 self.check(b'bytes pattern',
1248 "re.compile(b'bytes pattern')")
1249 self.check_flags(b'bytes pattern', re.A,
1250 "re.compile(b'bytes pattern', re.ASCII)")
1251
1252 def test_quotes(self):
1253 self.check('random "double quoted" pattern',
1254 '''re.compile('random "double quoted" pattern')''')
1255 self.check("random 'single quoted' pattern",
1256 '''re.compile("random 'single quoted' pattern")''')
1257 self.check('''both 'single' and "double" quotes''',
1258 '''re.compile('both \\'single\\' and "double" quotes')''')
1259
1260 def test_long_pattern(self):
1261 pattern = 'Very %spattern' % ('long ' * 1000)
1262 r = repr(re.compile(pattern))
1263 self.assertLess(len(r), 300)
1264 self.assertEqual(r[:30], "re.compile('Very long long lon")
1265 r = repr(re.compile(pattern, re.I))
1266 self.assertLess(len(r), 300)
1267 self.assertEqual(r[:30], "re.compile('Very long long lon")
1268 self.assertEqual(r[-16:], ", re.IGNORECASE)")
1165 1269
1166 1270
1167 class ImplementationTest(unittest.TestCase): 1271 class ImplementationTest(unittest.TestCase):
1168 """ 1272 """
1169 Test implementation details of the re module. 1273 Test implementation details of the re module.
1170 """ 1274 """
1171 1275
1172 def test_overlap_table(self): 1276 def test_overlap_table(self):
1173 f = sre_compile._generate_overlap_table 1277 f = sre_compile._generate_overlap_table
1174 self.assertEqual(f(""), []) 1278 self.assertEqual(f(""), [])
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after
1306 if result is None: 1410 if result is None:
1307 print('=== Fails on unicode-sensitive match', t) 1411 print('=== Fails on unicode-sensitive match', t)
1308 1412
1309 1413
1310 def test_main(): 1414 def test_main():
1311 run_unittest(__name__) 1415 run_unittest(__name__)
1312 run_re_tests() 1416 run_re_tests()
1313 1417
1314 if __name__ == "__main__": 1418 if __name__ == "__main__":
1315 test_main() 1419 test_main()
LEFTRIGHT

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+