@@ -1439,55 +1439,55 @@ def test_overlap_table(self):
14391439 self .assertEqual (f ("abcabdac" ), [0 , 0 , 0 , 1 , 2 , 0 , 1 , 0 ])
14401440
14411441
1442- def run_re_tests ():
1443- from test .re_tests import tests , SUCCEED , FAIL , SYNTAX_ERROR
1444- if verbose :
1445- print ('Running re_tests test suite' )
1446- else :
1447- # To save time, only run the first and last 10 tests
1448- #tests = tests[:10] + tests[-10:]
1449- pass
1450-
1451- for t in tests :
1452- sys .stdout .flush ()
1453- pattern = s = outcome = repl = expected = None
1454- if len (t ) == 5 :
1455- pattern , s , outcome , repl , expected = t
1456- elif len (t ) == 3 :
1457- pattern , s , outcome = t
1458- else :
1459- raise ValueError ('Test tuples should have 3 or 5 fields' , t )
1460-
1461- try :
1462- obj = re .compile (pattern )
1463- except re .error :
1464- if outcome == SYNTAX_ERROR : pass # Expected a syntax error
1442+ class ExternalTests (unittest .TestCase ):
1443+
1444+ def test_re_benchmarks (self ):
1445+ 're_tests benchmarks'
1446+ from test .re_tests import benchmarks
1447+ for pattern , s in benchmarks :
1448+ with self .subTest (pattern = pattern , string = s ):
1449+ p = re .compile (pattern )
1450+ self .assertTrue (p .search (s ))
1451+ self .assertTrue (p .match (s ))
1452+ self .assertTrue (p .fullmatch (s ))
1453+ s2 = ' ' * 10000 + s + ' ' * 10000
1454+ self .assertTrue (p .search (s2 ))
1455+ self .assertTrue (p .match (s2 , 10000 ))
1456+ self .assertTrue (p .match (s2 , 10000 , 10000 + len (s )))
1457+ self .assertTrue (p .fullmatch (s2 , 10000 , 10000 + len (s )))
1458+
1459+ def test_re_tests (self ):
1460+ 're_tests test suite'
1461+ from test .re_tests import tests , SUCCEED , FAIL , SYNTAX_ERROR
1462+ for t in tests :
1463+ pattern = s = outcome = repl = expected = None
1464+ if len (t ) == 5 :
1465+ pattern , s , outcome , repl , expected = t
1466+ elif len (t ) == 3 :
1467+ pattern , s , outcome = t
14651468 else :
1466- print ('=== Syntax error:' , t )
1467- except KeyboardInterrupt : raise KeyboardInterrupt
1468- except :
1469- print ('*** Unexpected error ***' , t )
1470- if verbose :
1471- traceback .print_exc (file = sys .stdout )
1472- else :
1473- try :
1469+ raise ValueError ('Test tuples should have 3 or 5 fields' , t )
1470+
1471+ with self .subTest (pattern = pattern , string = s ):
1472+ if outcome == SYNTAX_ERROR : # Expected a syntax error
1473+ with self .assertRaises (re .error ):
1474+ re .compile (pattern )
1475+ continue
1476+
1477+ obj = re .compile (pattern )
14741478 result = obj .search (s )
1475- except re .error as msg :
1476- print ('=== Unexpected exception' , t , repr (msg ))
1477- if outcome == SYNTAX_ERROR :
1478- # This should have been a syntax error; forget it.
1479- pass
1480- elif outcome == FAIL :
1481- if result is None : pass # No match, as expected
1482- else : print ('=== Succeeded incorrectly' , t )
1483- elif outcome == SUCCEED :
1484- if result is not None :
1479+ if outcome == FAIL :
1480+ self .assertIsNone (result , 'Succeeded incorrectly' )
1481+ continue
1482+
1483+ with self .subTest ():
1484+ self .assertTrue (result , 'Failed incorrectly' )
14851485 # Matched, as expected, so now we compute the
14861486 # result string and compare it to our expected result.
14871487 start , end = result .span (0 )
1488- vardict = {'found' : result .group (0 ),
1489- 'groups' : result .group (),
1490- 'flags' : result .re .flags }
1488+ vardict = {'found' : result .group (0 ),
1489+ 'groups' : result .group (),
1490+ 'flags' : result .re .flags }
14911491 for i in range (1 , 100 ):
14921492 try :
14931493 gi = result .group (i )
@@ -1505,12 +1505,8 @@ def run_re_tests():
15051505 except IndexError :
15061506 gi = "Error"
15071507 vardict [i ] = gi
1508- repl = eval (repl , vardict )
1509- if repl != expected :
1510- print ('=== grouping error' , t , end = ' ' )
1511- print (repr (repl ) + ' should be ' + repr (expected ))
1512- else :
1513- print ('=== Failed incorrectly' , t )
1508+ self .assertEqual (eval (repl , vardict ), expected ,
1509+ 'grouping error' )
15141510
15151511 # Try the match with both pattern and string converted to
15161512 # bytes, and check that it still succeeds.
@@ -1521,55 +1517,39 @@ def run_re_tests():
15211517 # skip non-ascii tests
15221518 pass
15231519 else :
1524- try :
1520+ with self . subTest ( 'bytes pattern match' ) :
15251521 bpat = re .compile (bpat )
1526- except Exception :
1527- print ('=== Fails on bytes pattern compile' , t )
1528- if verbose :
1529- traceback .print_exc (file = sys .stdout )
1530- else :
1531- bytes_result = bpat .search (bs )
1532- if bytes_result is None :
1533- print ('=== Fails on bytes pattern match' , t )
1522+ self .assertTrue (bpat .search (bs ))
15341523
15351524 # Try the match with the search area limited to the extent
15361525 # of the match and see if it still succeeds. \B will
15371526 # break (because it won't match at the end or start of a
15381527 # string), so we'll ignore patterns that feature it.
1539-
1540- if pattern [:2 ] != '\\ B' and pattern [- 2 :] != '\\ B' \
1541- and result is not None :
1542- obj = re .compile (pattern )
1543- result = obj .search (s , result .start (0 ), result .end (0 ) + 1 )
1544- if result is None :
1545- print ('=== Failed on range-limited match' , t )
1528+ if (pattern [:2 ] != r'\B' and pattern [- 2 :] != r'\B'
1529+ and result is not None ):
1530+ with self .subTest ('range-limited match' ):
1531+ obj = re .compile (pattern )
1532+ self .assertTrue (obj .search (s , start , end + 1 ))
15461533
15471534 # Try the match with IGNORECASE enabled, and check that it
15481535 # still succeeds.
1549- obj = re .compile (pattern , re .IGNORECASE )
1550- result = obj .search (s )
1551- if result is None :
1552- print ('=== Fails on case-insensitive match' , t )
1536+ with self .subTest ('case-insensitive match' ):
1537+ obj = re .compile (pattern , re .IGNORECASE )
1538+ self .assertTrue (obj .search (s ))
15531539
15541540 # Try the match with LOCALE enabled, and check that it
15551541 # still succeeds.
15561542 if '(?u)' not in pattern :
1557- obj = re .compile (pattern , re .LOCALE )
1558- result = obj .search (s )
1559- if result is None :
1560- print ('=== Fails on locale-sensitive match' , t )
1543+ with self .subTest ('locale-sensitive match' ):
1544+ obj = re .compile (pattern , re .LOCALE )
1545+ self .assertTrue (obj .search (s ))
15611546
15621547 # Try the match with UNICODE locale enabled, and check
15631548 # that it still succeeds.
1564- obj = re .compile (pattern , re .UNICODE )
1565- result = obj .search (s )
1566- if result is None :
1567- print ('=== Fails on unicode-sensitive match' , t )
1568-
1549+ with self .subTest ('unicode-sensitive match' ):
1550+ obj = re .compile (pattern , re .UNICODE )
1551+ self .assertTrue (obj .search (s ))
15691552
1570- def test_main ():
1571- run_unittest (__name__ )
1572- run_re_tests ()
15731553
15741554if __name__ == "__main__" :
1575- test_main ()
1555+ unittest . main ()
0 commit comments