Guido van Rossum | 8e0ce30 | 1997-07-11 19:34:44 +0000 | [diff] [blame^] | 1 | from test_support import verbose |
| 2 | import re |
| 3 | import traceback |
| 4 | |
| 5 | from re_tests import * |
| 6 | if verbose: print 'Running regex_tests test suite' |
| 7 | |
| 8 | for t in tests: |
| 9 | print t |
| 10 | pattern=s=outcome=repl=expected=None |
| 11 | if len(t)==5: |
| 12 | pattern, s, outcome, repl, expected = t |
| 13 | elif len(t)==3: |
| 14 | pattern, s, outcome = t |
| 15 | else: |
| 16 | raise ValueError, ('Test tuples should have 3 or 5 fields',t) |
| 17 | |
| 18 | try: |
| 19 | obj=re.compile(pattern) |
| 20 | except re.error: |
| 21 | if outcome==SYNTAX_ERROR: pass # Expected a syntax error |
| 22 | else: |
| 23 | print '=== Syntax error:', t |
| 24 | except: |
| 25 | print '*** Unexpected error ***' |
| 26 | traceback.print_exc() |
| 27 | else: |
| 28 | try: |
| 29 | result=obj.search(s) |
| 30 | except regex.error, msg: |
| 31 | print '=== Unexpected exception', t, repr(msg) |
| 32 | if outcome==SYNTAX_ERROR: |
| 33 | # This should have been a syntax error; forget it. |
| 34 | pass |
| 35 | elif outcome==FAIL: |
| 36 | if result is None: pass # No match, as expected |
| 37 | else: print '=== Succeeded incorrectly', t |
| 38 | elif outcome==SUCCEED: |
| 39 | if result is not None: |
| 40 | # Matched, as expected, so now we compute the |
| 41 | # result string and compare it to our expected result. |
| 42 | start, end = result.span(0) |
| 43 | vardict={'found': result.group(0)} |
| 44 | for i in range(1, 100): |
| 45 | try: |
| 46 | gi = result.group(i) |
| 47 | except IndexError: |
| 48 | break |
| 49 | else: |
| 50 | vardict['g%d' % i] = gi |
| 51 | repl=eval(repl, vardict) |
| 52 | if repl!=expected: |
| 53 | print '=== grouping error', t, |
| 54 | print repr(repl)+' should be '+repr(expected) |
| 55 | else: |
| 56 | print '=== Failed incorrectly', t |