mirror of
https://github.com/python/cpython.git
synced 2025-09-27 10:50:04 +00:00
Issue #22838: All test_re tests now work with unittest test discovery.
This commit is contained in:
commit
fb028336f9
3 changed files with 64 additions and 82 deletions
|
@ -86,7 +86,7 @@ tests = [
|
||||||
(r'\a[\b]\f\n\r\t\v', '\a\b\f\n\r\t\v', SUCCEED, 'found', '\a\b\f\n\r\t\v'),
|
(r'\a[\b]\f\n\r\t\v', '\a\b\f\n\r\t\v', SUCCEED, 'found', '\a\b\f\n\r\t\v'),
|
||||||
(r'[\a][\b][\f][\n][\r][\t][\v]', '\a\b\f\n\r\t\v', SUCCEED, 'found', '\a\b\f\n\r\t\v'),
|
(r'[\a][\b][\f][\n][\r][\t][\v]', '\a\b\f\n\r\t\v', SUCCEED, 'found', '\a\b\f\n\r\t\v'),
|
||||||
# NOTE: not an error under PCRE/PRE:
|
# NOTE: not an error under PCRE/PRE:
|
||||||
# (r'\u', '', SYNTAX_ERROR), # A Perl escape
|
(r'\u', '', SYNTAX_ERROR), # A Perl escape
|
||||||
(r'\c\e\g\h\i\j\k\m\o\p\q\y\z', 'ceghijkmopqyz', SUCCEED, 'found', 'ceghijkmopqyz'),
|
(r'\c\e\g\h\i\j\k\m\o\p\q\y\z', 'ceghijkmopqyz', SUCCEED, 'found', 'ceghijkmopqyz'),
|
||||||
(r'\xff', '\377', SUCCEED, 'found', chr(255)),
|
(r'\xff', '\377', SUCCEED, 'found', chr(255)),
|
||||||
# new \x semantics
|
# new \x semantics
|
||||||
|
|
|
@ -1511,55 +1511,55 @@ class ImplementationTest(unittest.TestCase):
|
||||||
self.assertEqual(f("abcabdac"), [0, 0, 0, 1, 2, 0, 1, 0])
|
self.assertEqual(f("abcabdac"), [0, 0, 0, 1, 2, 0, 1, 0])
|
||||||
|
|
||||||
|
|
||||||
def run_re_tests():
|
class ExternalTests(unittest.TestCase):
|
||||||
from test.re_tests import tests, SUCCEED, FAIL, SYNTAX_ERROR
|
|
||||||
if verbose:
|
|
||||||
print('Running re_tests test suite')
|
|
||||||
else:
|
|
||||||
# To save time, only run the first and last 10 tests
|
|
||||||
#tests = tests[:10] + tests[-10:]
|
|
||||||
pass
|
|
||||||
|
|
||||||
for t in tests:
|
def test_re_benchmarks(self):
|
||||||
sys.stdout.flush()
|
're_tests benchmarks'
|
||||||
pattern = s = outcome = repl = expected = None
|
from test.re_tests import benchmarks
|
||||||
if len(t) == 5:
|
for pattern, s in benchmarks:
|
||||||
pattern, s, outcome, repl, expected = t
|
with self.subTest(pattern=pattern, string=s):
|
||||||
elif len(t) == 3:
|
p = re.compile(pattern)
|
||||||
pattern, s, outcome = t
|
self.assertTrue(p.search(s))
|
||||||
else:
|
self.assertTrue(p.match(s))
|
||||||
raise ValueError('Test tuples should have 3 or 5 fields', t)
|
self.assertTrue(p.fullmatch(s))
|
||||||
|
s2 = ' '*10000 + s + ' '*10000
|
||||||
|
self.assertTrue(p.search(s2))
|
||||||
|
self.assertTrue(p.match(s2, 10000))
|
||||||
|
self.assertTrue(p.match(s2, 10000, 10000 + len(s)))
|
||||||
|
self.assertTrue(p.fullmatch(s2, 10000, 10000 + len(s)))
|
||||||
|
|
||||||
try:
|
def test_re_tests(self):
|
||||||
obj = re.compile(pattern)
|
're_tests test suite'
|
||||||
except re.error:
|
from test.re_tests import tests, SUCCEED, FAIL, SYNTAX_ERROR
|
||||||
if outcome == SYNTAX_ERROR: pass # Expected a syntax error
|
for t in tests:
|
||||||
|
pattern = s = outcome = repl = expected = None
|
||||||
|
if len(t) == 5:
|
||||||
|
pattern, s, outcome, repl, expected = t
|
||||||
|
elif len(t) == 3:
|
||||||
|
pattern, s, outcome = t
|
||||||
else:
|
else:
|
||||||
print('=== Syntax error:', t)
|
raise ValueError('Test tuples should have 3 or 5 fields', t)
|
||||||
except KeyboardInterrupt: raise KeyboardInterrupt
|
|
||||||
except:
|
with self.subTest(pattern=pattern, string=s):
|
||||||
print('*** Unexpected error ***', t)
|
if outcome == SYNTAX_ERROR: # Expected a syntax error
|
||||||
if verbose:
|
with self.assertRaises(re.error):
|
||||||
traceback.print_exc(file=sys.stdout)
|
re.compile(pattern)
|
||||||
else:
|
continue
|
||||||
try:
|
|
||||||
|
obj = re.compile(pattern)
|
||||||
result = obj.search(s)
|
result = obj.search(s)
|
||||||
except re.error as msg:
|
if outcome == FAIL:
|
||||||
print('=== Unexpected exception', t, repr(msg))
|
self.assertIsNone(result, 'Succeeded incorrectly')
|
||||||
if outcome == SYNTAX_ERROR:
|
continue
|
||||||
# This should have been a syntax error; forget it.
|
|
||||||
pass
|
with self.subTest():
|
||||||
elif outcome == FAIL:
|
self.assertTrue(result, 'Failed incorrectly')
|
||||||
if result is None: pass # No match, as expected
|
|
||||||
else: print('=== Succeeded incorrectly', t)
|
|
||||||
elif outcome == SUCCEED:
|
|
||||||
if result is not None:
|
|
||||||
# Matched, as expected, so now we compute the
|
# Matched, as expected, so now we compute the
|
||||||
# result string and compare it to our expected result.
|
# result string and compare it to our expected result.
|
||||||
start, end = result.span(0)
|
start, end = result.span(0)
|
||||||
vardict={'found': result.group(0),
|
vardict = {'found': result.group(0),
|
||||||
'groups': result.group(),
|
'groups': result.group(),
|
||||||
'flags': result.re.flags}
|
'flags': result.re.flags}
|
||||||
for i in range(1, 100):
|
for i in range(1, 100):
|
||||||
try:
|
try:
|
||||||
gi = result.group(i)
|
gi = result.group(i)
|
||||||
|
@ -1577,12 +1577,8 @@ def run_re_tests():
|
||||||
except IndexError:
|
except IndexError:
|
||||||
gi = "Error"
|
gi = "Error"
|
||||||
vardict[i] = gi
|
vardict[i] = gi
|
||||||
repl = eval(repl, vardict)
|
self.assertEqual(eval(repl, vardict), expected,
|
||||||
if repl != expected:
|
'grouping error')
|
||||||
print('=== grouping error', t, end=' ')
|
|
||||||
print(repr(repl) + ' should be ' + repr(expected))
|
|
||||||
else:
|
|
||||||
print('=== Failed incorrectly', t)
|
|
||||||
|
|
||||||
# Try the match with both pattern and string converted to
|
# Try the match with both pattern and string converted to
|
||||||
# bytes, and check that it still succeeds.
|
# bytes, and check that it still succeeds.
|
||||||
|
@ -1593,55 +1589,39 @@ def run_re_tests():
|
||||||
# skip non-ascii tests
|
# skip non-ascii tests
|
||||||
pass
|
pass
|
||||||
else:
|
else:
|
||||||
try:
|
with self.subTest('bytes pattern match'):
|
||||||
bpat = re.compile(bpat)
|
bpat = re.compile(bpat)
|
||||||
except Exception:
|
self.assertTrue(bpat.search(bs))
|
||||||
print('=== Fails on bytes pattern compile', t)
|
|
||||||
if verbose:
|
|
||||||
traceback.print_exc(file=sys.stdout)
|
|
||||||
else:
|
|
||||||
bytes_result = bpat.search(bs)
|
|
||||||
if bytes_result is None:
|
|
||||||
print('=== Fails on bytes pattern match', t)
|
|
||||||
|
|
||||||
# Try the match with the search area limited to the extent
|
# Try the match with the search area limited to the extent
|
||||||
# of the match and see if it still succeeds. \B will
|
# of the match and see if it still succeeds. \B will
|
||||||
# break (because it won't match at the end or start of a
|
# break (because it won't match at the end or start of a
|
||||||
# string), so we'll ignore patterns that feature it.
|
# string), so we'll ignore patterns that feature it.
|
||||||
|
if (pattern[:2] != r'\B' and pattern[-2:] != r'\B'
|
||||||
if pattern[:2] != '\\B' and pattern[-2:] != '\\B' \
|
and result is not None):
|
||||||
and result is not None:
|
with self.subTest('range-limited match'):
|
||||||
obj = re.compile(pattern)
|
obj = re.compile(pattern)
|
||||||
result = obj.search(s, result.start(0), result.end(0) + 1)
|
self.assertTrue(obj.search(s, start, end + 1))
|
||||||
if result is None:
|
|
||||||
print('=== Failed on range-limited match', t)
|
|
||||||
|
|
||||||
# Try the match with IGNORECASE enabled, and check that it
|
# Try the match with IGNORECASE enabled, and check that it
|
||||||
# still succeeds.
|
# still succeeds.
|
||||||
obj = re.compile(pattern, re.IGNORECASE)
|
with self.subTest('case-insensitive match'):
|
||||||
result = obj.search(s)
|
obj = re.compile(pattern, re.IGNORECASE)
|
||||||
if result is None:
|
self.assertTrue(obj.search(s))
|
||||||
print('=== Fails on case-insensitive match', t)
|
|
||||||
|
|
||||||
# Try the match with LOCALE enabled, and check that it
|
# Try the match with LOCALE enabled, and check that it
|
||||||
# still succeeds.
|
# still succeeds.
|
||||||
if '(?u)' not in pattern:
|
if '(?u)' not in pattern:
|
||||||
obj = re.compile(pattern, re.LOCALE)
|
with self.subTest('locale-sensitive match'):
|
||||||
result = obj.search(s)
|
obj = re.compile(pattern, re.LOCALE)
|
||||||
if result is None:
|
self.assertTrue(obj.search(s))
|
||||||
print('=== Fails on locale-sensitive match', t)
|
|
||||||
|
|
||||||
# Try the match with UNICODE locale enabled, and check
|
# Try the match with UNICODE locale enabled, and check
|
||||||
# that it still succeeds.
|
# that it still succeeds.
|
||||||
obj = re.compile(pattern, re.UNICODE)
|
with self.subTest('unicode-sensitive match'):
|
||||||
result = obj.search(s)
|
obj = re.compile(pattern, re.UNICODE)
|
||||||
if result is None:
|
self.assertTrue(obj.search(s))
|
||||||
print('=== Fails on unicode-sensitive match', t)
|
|
||||||
|
|
||||||
|
|
||||||
def test_main():
|
|
||||||
run_unittest(__name__)
|
|
||||||
run_re_tests()
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
test_main()
|
unittest.main()
|
||||||
|
|
|
@ -1348,6 +1348,8 @@ Documentation
|
||||||
Tests
|
Tests
|
||||||
-----
|
-----
|
||||||
|
|
||||||
|
- Issue #22838: All test_re tests now work with unittest test discovery.
|
||||||
|
|
||||||
- Issue #22173: Update lib2to3 tests to use unittest test discovery.
|
- Issue #22173: Update lib2to3 tests to use unittest test discovery.
|
||||||
|
|
||||||
- Issue #16000: Convert test_curses to use unittest.
|
- Issue #16000: Convert test_curses to use unittest.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue