1997-07-17 14:52:48 +00:00
|
|
|
#!/usr/local/bin/python
|
|
|
|
# -*- mode: python -*-
|
|
|
|
# $Id$
|
|
|
|
|
1997-07-11 19:34:44 +00:00
|
|
|
from test_support import verbose
|
|
|
|
import re
|
1997-07-17 14:52:48 +00:00
|
|
|
import reop
|
1997-07-15 18:47:48 +00:00
|
|
|
import sys, os, string, traceback
|
1997-07-11 19:34:44 +00:00
|
|
|
|
|
|
|
from re_tests import *
|
1997-07-11 20:47:58 +00:00
|
|
|
if verbose: print 'Running re_tests test suite'
|
1997-07-11 19:34:44 +00:00
|
|
|
|
|
|
|
for t in tests:
|
|
|
|
print t
|
1997-07-17 14:52:48 +00:00
|
|
|
sys.stdout.flush()
|
1997-07-11 19:34:44 +00:00
|
|
|
pattern=s=outcome=repl=expected=None
|
|
|
|
if len(t)==5:
|
|
|
|
pattern, s, outcome, repl, expected = t
|
|
|
|
elif len(t)==3:
|
|
|
|
pattern, s, outcome = t
|
|
|
|
else:
|
|
|
|
raise ValueError, ('Test tuples should have 3 or 5 fields',t)
|
|
|
|
|
|
|
|
try:
|
|
|
|
obj=re.compile(pattern)
|
|
|
|
except re.error:
|
|
|
|
if outcome==SYNTAX_ERROR: pass # Expected a syntax error
|
|
|
|
else:
|
|
|
|
print '=== Syntax error:', t
|
1997-07-17 14:52:48 +00:00
|
|
|
except KeyboardInterrupt:
|
|
|
|
raise KeyboardInterrupt
|
1997-07-11 19:34:44 +00:00
|
|
|
except:
|
|
|
|
print '*** Unexpected error ***'
|
1997-07-11 21:14:53 +00:00
|
|
|
if verbose:
|
|
|
|
traceback.print_exc(file=sys.stdout)
|
1997-07-11 19:34:44 +00:00
|
|
|
else:
|
|
|
|
try:
|
|
|
|
result=obj.search(s)
|
1997-07-17 14:52:48 +00:00
|
|
|
except (re.error, reop.error), msg:
|
1997-07-11 19:34:44 +00:00
|
|
|
print '=== Unexpected exception', t, repr(msg)
|
|
|
|
if outcome==SYNTAX_ERROR:
|
|
|
|
# This should have been a syntax error; forget it.
|
|
|
|
pass
|
|
|
|
elif outcome==FAIL:
|
|
|
|
if result is None: pass # No match, as expected
|
|
|
|
else: print '=== Succeeded incorrectly', t
|
|
|
|
elif outcome==SUCCEED:
|
|
|
|
if result is not None:
|
|
|
|
# Matched, as expected, so now we compute the
|
|
|
|
# result string and compare it to our expected result.
|
|
|
|
start, end = result.span(0)
|
1997-07-17 14:52:48 +00:00
|
|
|
vardict={'found': result.group(0),
|
|
|
|
'groups': result.group(),
|
|
|
|
'flags': result.re.flags}
|
1997-07-11 19:34:44 +00:00
|
|
|
for i in range(1, 100):
|
|
|
|
try:
|
|
|
|
gi = result.group(i)
|
1997-07-11 20:47:58 +00:00
|
|
|
# Special hack because else the string concat fails:
|
1997-07-17 14:52:48 +00:00
|
|
|
if gi is None:
|
|
|
|
gi = "None"
|
1997-07-11 19:34:44 +00:00
|
|
|
except IndexError:
|
1997-07-15 15:49:52 +00:00
|
|
|
gi = "Error"
|
1997-07-15 15:40:57 +00:00
|
|
|
vardict['g%d' % i] = gi
|
1997-07-15 18:47:48 +00:00
|
|
|
for i in result.re.groupindex.keys():
|
|
|
|
try:
|
|
|
|
gi = result.group(i)
|
1997-07-17 14:52:48 +00:00
|
|
|
if gi is None:
|
|
|
|
gi = "None"
|
1997-07-15 18:47:48 +00:00
|
|
|
except IndexError:
|
1997-07-17 14:52:48 +00:00
|
|
|
gi = "Error"
|
|
|
|
vardict[i] = gi
|
1997-07-11 19:34:44 +00:00
|
|
|
repl=eval(repl, vardict)
|
|
|
|
if repl!=expected:
|
|
|
|
print '=== grouping error', t,
|
|
|
|
print repr(repl)+' should be '+repr(expected)
|
|
|
|
else:
|
|
|
|
print '=== Failed incorrectly', t
|