Guido van Rossum | a0e4c1b | 1997-07-17 14:52:48 +0000 | [diff] [blame^] | 1 | #!/usr/local/bin/python |
| 2 | # -*- mode: python -*- |
| 3 | # $Id$ |
| 4 | |
Guido van Rossum | 8e0ce30 | 1997-07-11 19:34:44 +0000 | [diff] [blame] | 5 | from test_support import verbose |
| 6 | import re |
Guido van Rossum | a0e4c1b | 1997-07-17 14:52:48 +0000 | [diff] [blame^] | 7 | import reop |
Guido van Rossum | e8b8131 | 1997-07-15 18:47:48 +0000 | [diff] [blame] | 8 | import sys, os, string, traceback |
Guido van Rossum | 8e0ce30 | 1997-07-11 19:34:44 +0000 | [diff] [blame] | 9 | |
| 10 | from re_tests import * |
Guido van Rossum | 035aae0 | 1997-07-11 20:47:58 +0000 | [diff] [blame] | 11 | if verbose: print 'Running re_tests test suite' |
Guido van Rossum | 8e0ce30 | 1997-07-11 19:34:44 +0000 | [diff] [blame] | 12 | |
| 13 | for t in tests: |
| 14 | print t |
Guido van Rossum | a0e4c1b | 1997-07-17 14:52:48 +0000 | [diff] [blame^] | 15 | sys.stdout.flush() |
Guido van Rossum | 8e0ce30 | 1997-07-11 19:34:44 +0000 | [diff] [blame] | 16 | pattern=s=outcome=repl=expected=None |
| 17 | if len(t)==5: |
| 18 | pattern, s, outcome, repl, expected = t |
| 19 | elif len(t)==3: |
| 20 | pattern, s, outcome = t |
| 21 | else: |
| 22 | raise ValueError, ('Test tuples should have 3 or 5 fields',t) |
| 23 | |
| 24 | try: |
| 25 | obj=re.compile(pattern) |
| 26 | except re.error: |
| 27 | if outcome==SYNTAX_ERROR: pass # Expected a syntax error |
| 28 | else: |
| 29 | print '=== Syntax error:', t |
Guido van Rossum | a0e4c1b | 1997-07-17 14:52:48 +0000 | [diff] [blame^] | 30 | except KeyboardInterrupt: |
| 31 | raise KeyboardInterrupt |
Guido van Rossum | 8e0ce30 | 1997-07-11 19:34:44 +0000 | [diff] [blame] | 32 | except: |
| 33 | print '*** Unexpected error ***' |
Guido van Rossum | 1d8b758 | 1997-07-11 21:14:53 +0000 | [diff] [blame] | 34 | if verbose: |
| 35 | traceback.print_exc(file=sys.stdout) |
Guido van Rossum | 8e0ce30 | 1997-07-11 19:34:44 +0000 | [diff] [blame] | 36 | else: |
| 37 | try: |
| 38 | result=obj.search(s) |
Guido van Rossum | a0e4c1b | 1997-07-17 14:52:48 +0000 | [diff] [blame^] | 39 | except (re.error, reop.error), msg: |
Guido van Rossum | 8e0ce30 | 1997-07-11 19:34:44 +0000 | [diff] [blame] | 40 | print '=== Unexpected exception', t, repr(msg) |
| 41 | if outcome==SYNTAX_ERROR: |
| 42 | # This should have been a syntax error; forget it. |
| 43 | pass |
| 44 | elif outcome==FAIL: |
| 45 | if result is None: pass # No match, as expected |
| 46 | else: print '=== Succeeded incorrectly', t |
| 47 | elif outcome==SUCCEED: |
| 48 | if result is not None: |
| 49 | # Matched, as expected, so now we compute the |
| 50 | # result string and compare it to our expected result. |
| 51 | start, end = result.span(0) |
Guido van Rossum | a0e4c1b | 1997-07-17 14:52:48 +0000 | [diff] [blame^] | 52 | vardict={'found': result.group(0), |
| 53 | 'groups': result.group(), |
| 54 | 'flags': result.re.flags} |
Guido van Rossum | 8e0ce30 | 1997-07-11 19:34:44 +0000 | [diff] [blame] | 55 | for i in range(1, 100): |
| 56 | try: |
| 57 | gi = result.group(i) |
Guido van Rossum | 035aae0 | 1997-07-11 20:47:58 +0000 | [diff] [blame] | 58 | # Special hack because else the string concat fails: |
Guido van Rossum | a0e4c1b | 1997-07-17 14:52:48 +0000 | [diff] [blame^] | 59 | if gi is None: |
| 60 | gi = "None" |
Guido van Rossum | 8e0ce30 | 1997-07-11 19:34:44 +0000 | [diff] [blame] | 61 | except IndexError: |
Guido van Rossum | 23b8d4c | 1997-07-15 15:49:52 +0000 | [diff] [blame] | 62 | gi = "Error" |
Guido van Rossum | 847ed4a | 1997-07-15 15:40:57 +0000 | [diff] [blame] | 63 | vardict['g%d' % i] = gi |
Guido van Rossum | e8b8131 | 1997-07-15 18:47:48 +0000 | [diff] [blame] | 64 | for i in result.re.groupindex.keys(): |
| 65 | try: |
| 66 | gi = result.group(i) |
Guido van Rossum | a0e4c1b | 1997-07-17 14:52:48 +0000 | [diff] [blame^] | 67 | if gi is None: |
| 68 | gi = "None" |
Guido van Rossum | e8b8131 | 1997-07-15 18:47:48 +0000 | [diff] [blame] | 69 | except IndexError: |
Guido van Rossum | a0e4c1b | 1997-07-17 14:52:48 +0000 | [diff] [blame^] | 70 | gi = "Error" |
| 71 | vardict[i] = gi |
Guido van Rossum | 8e0ce30 | 1997-07-11 19:34:44 +0000 | [diff] [blame] | 72 | repl=eval(repl, vardict) |
| 73 | if repl!=expected: |
| 74 | print '=== grouping error', t, |
| 75 | print repr(repl)+' should be '+repr(expected) |
| 76 | else: |
| 77 | print '=== Failed incorrectly', t |