blob: 6b8c65d71a4397d404ae68f9281d5c21d4ec0431 [file] [log] [blame]
Guido van Rossuma0e4c1b1997-07-17 14:52:48 +00001#!/usr/local/bin/python
2# -*- mode: python -*-
3# $Id$
4
Guido van Rossum8e0ce301997-07-11 19:34:44 +00005from test_support import verbose
6import re
Guido van Rossuma0e4c1b1997-07-17 14:52:48 +00007import reop
Guido van Rossume8b81311997-07-15 18:47:48 +00008import sys, os, string, traceback
Guido van Rossum8e0ce301997-07-11 19:34:44 +00009
10from re_tests import *
Guido van Rossum035aae01997-07-11 20:47:58 +000011if verbose: print 'Running re_tests test suite'
Guido van Rossum8e0ce301997-07-11 19:34:44 +000012
13for t in tests:
14 print t
Guido van Rossuma0e4c1b1997-07-17 14:52:48 +000015 sys.stdout.flush()
Guido van Rossum8e0ce301997-07-11 19:34:44 +000016 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 Rossuma0e4c1b1997-07-17 14:52:48 +000030 except KeyboardInterrupt:
31 raise KeyboardInterrupt
Guido van Rossum8e0ce301997-07-11 19:34:44 +000032 except:
33 print '*** Unexpected error ***'
Guido van Rossum1d8b7581997-07-11 21:14:53 +000034 if verbose:
35 traceback.print_exc(file=sys.stdout)
Guido van Rossum8e0ce301997-07-11 19:34:44 +000036 else:
37 try:
38 result=obj.search(s)
Guido van Rossuma0e4c1b1997-07-17 14:52:48 +000039 except (re.error, reop.error), msg:
Guido van Rossum8e0ce301997-07-11 19:34:44 +000040 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 Rossuma0e4c1b1997-07-17 14:52:48 +000052 vardict={'found': result.group(0),
53 'groups': result.group(),
54 'flags': result.re.flags}
Guido van Rossum8e0ce301997-07-11 19:34:44 +000055 for i in range(1, 100):
56 try:
57 gi = result.group(i)
Guido van Rossum035aae01997-07-11 20:47:58 +000058 # Special hack because else the string concat fails:
Guido van Rossuma0e4c1b1997-07-17 14:52:48 +000059 if gi is None:
60 gi = "None"
Guido van Rossum8e0ce301997-07-11 19:34:44 +000061 except IndexError:
Guido van Rossum23b8d4c1997-07-15 15:49:52 +000062 gi = "Error"
Guido van Rossum847ed4a1997-07-15 15:40:57 +000063 vardict['g%d' % i] = gi
Guido van Rossume8b81311997-07-15 18:47:48 +000064 for i in result.re.groupindex.keys():
65 try:
66 gi = result.group(i)
Guido van Rossuma0e4c1b1997-07-17 14:52:48 +000067 if gi is None:
68 gi = "None"
Guido van Rossume8b81311997-07-15 18:47:48 +000069 except IndexError:
Guido van Rossuma0e4c1b1997-07-17 14:52:48 +000070 gi = "Error"
71 vardict[i] = gi
Guido van Rossum8e0ce301997-07-11 19:34:44 +000072 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