Back to index

python3.2  3.2.2
Classes | Functions
test.test_re Namespace Reference

Classes

class  ReTests

Functions

def run_re_tests
def test_main

Function Documentation

Definition at line 783 of file test_re.py.

00783 
00784 def run_re_tests():
00785     from test.re_tests import tests, SUCCEED, FAIL, SYNTAX_ERROR
00786     if verbose:
00787         print('Running re_tests test suite')
00788     else:
00789         # To save time, only run the first and last 10 tests
00790         #tests = tests[:10] + tests[-10:]
00791         pass
00792 
00793     for t in tests:
00794         sys.stdout.flush()
00795         pattern = s = outcome = repl = expected = None
00796         if len(t) == 5:
00797             pattern, s, outcome, repl, expected = t
00798         elif len(t) == 3:
00799             pattern, s, outcome = t
00800         else:
00801             raise ValueError('Test tuples should have 3 or 5 fields', t)
00802 
00803         try:
00804             obj = re.compile(pattern)
00805         except re.error:
00806             if outcome == SYNTAX_ERROR: pass  # Expected a syntax error
00807             else:
00808                 print('=== Syntax error:', t)
00809         except KeyboardInterrupt: raise KeyboardInterrupt
00810         except:
00811             print('*** Unexpected error ***', t)
00812             if verbose:
00813                 traceback.print_exc(file=sys.stdout)
00814         else:
00815             try:
00816                 result = obj.search(s)
00817             except re.error as msg:
00818                 print('=== Unexpected exception', t, repr(msg))
00819             if outcome == SYNTAX_ERROR:
00820                 # This should have been a syntax error; forget it.
00821                 pass
00822             elif outcome == FAIL:
00823                 if result is None: pass   # No match, as expected
00824                 else: print('=== Succeeded incorrectly', t)
00825             elif outcome == SUCCEED:
00826                 if result is not None:
00827                     # Matched, as expected, so now we compute the
00828                     # result string and compare it to our expected result.
00829                     start, end = result.span(0)
00830                     vardict={'found': result.group(0),
00831                              'groups': result.group(),
00832                              'flags': result.re.flags}
00833                     for i in range(1, 100):
00834                         try:
00835                             gi = result.group(i)
00836                             # Special hack because else the string concat fails:
00837                             if gi is None:
00838                                 gi = "None"
00839                         except IndexError:
00840                             gi = "Error"
00841                         vardict['g%d' % i] = gi
00842                     for i in result.re.groupindex.keys():
00843                         try:
00844                             gi = result.group(i)
00845                             if gi is None:
00846                                 gi = "None"
00847                         except IndexError:
00848                             gi = "Error"
00849                         vardict[i] = gi
00850                     repl = eval(repl, vardict)
00851                     if repl != expected:
00852                         print('=== grouping error', t, end=' ')
00853                         print(repr(repl) + ' should be ' + repr(expected))
00854                 else:
00855                     print('=== Failed incorrectly', t)
00856 
00857                 # Try the match with both pattern and string converted to
00858                 # bytes, and check that it still succeeds.
00859                 try:
00860                     bpat = bytes(pattern, "ascii")
00861                     bs = bytes(s, "ascii")
00862                 except UnicodeEncodeError:
00863                     # skip non-ascii tests
00864                     pass
00865                 else:
00866                     try:
00867                         bpat = re.compile(bpat)
00868                     except Exception:
00869                         print('=== Fails on bytes pattern compile', t)
00870                         if verbose:
00871                             traceback.print_exc(file=sys.stdout)
00872                     else:
00873                         bytes_result = bpat.search(bs)
00874                         if bytes_result is None:
00875                             print('=== Fails on bytes pattern match', t)
00876 
00877                 # Try the match with the search area limited to the extent
00878                 # of the match and see if it still succeeds.  \B will
00879                 # break (because it won't match at the end or start of a
00880                 # string), so we'll ignore patterns that feature it.
00881 
00882                 if pattern[:2] != '\\B' and pattern[-2:] != '\\B' \
00883                                and result is not None:
00884                     obj = re.compile(pattern)
00885                     result = obj.search(s, result.start(0), result.end(0) + 1)
00886                     if result is None:
00887                         print('=== Failed on range-limited match', t)
00888 
00889                 # Try the match with IGNORECASE enabled, and check that it
00890                 # still succeeds.
00891                 obj = re.compile(pattern, re.IGNORECASE)
00892                 result = obj.search(s)
00893                 if result is None:
00894                     print('=== Fails on case-insensitive match', t)
00895 
00896                 # Try the match with LOCALE enabled, and check that it
00897                 # still succeeds.
00898                 if '(?u)' not in pattern:
00899                     obj = re.compile(pattern, re.LOCALE)
00900                     result = obj.search(s)
00901                     if result is None:
00902                         print('=== Fails on locale-sensitive match', t)
00903 
00904                 # Try the match with UNICODE locale enabled, and check
00905                 # that it still succeeds.
00906                 obj = re.compile(pattern, re.UNICODE)
00907                 result = obj.search(s)
00908                 if result is None:
00909                     print('=== Fails on unicode-sensitive match', t)
00910 

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 911 of file test_re.py.

00911 
00912 def test_main():
00913     run_unittest(ReTests)
00914     run_re_tests()

Here is the call graph for this function: