1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
|
import re
from rpython.rlib.rsre.test.test_match import get_code
from rpython.rlib.rsre import rsre_core
def test_external_match():
from rpython.rlib.rsre.test.re_tests import tests
for t in tests:
yield run_external, t, False
def test_external_search():
from rpython.rlib.rsre.test.re_tests import tests
for t in tests:
yield run_external, t, True
def run_external(t, use_search):
from rpython.rlib.rsre.test.re_tests import SUCCEED, FAIL, SYNTAX_ERROR
pattern, s, outcome = t[:3]
if len(t) == 5:
repl, expected = t[3:5]
else:
assert len(t) == 3
print 'trying:', t
try:
obj = get_code(pattern)
except re.error:
if outcome == SYNTAX_ERROR:
return # Expected a syntax error
raise
if outcome == SYNTAX_ERROR:
raise Exception("this should have been a syntax error")
#
if use_search:
result = rsre_core.search(obj, s)
else:
# Emulate a poor man's search() with repeated match()s
for i in range(len(s)+1):
result = rsre_core.match(obj, s, start=i)
if result:
break
#
if outcome == FAIL:
if result is not None:
raise Exception("succeeded incorrectly")
elif outcome == SUCCEED:
if result is None:
raise Exception("failed incorrectly")
# Matched, as expected, so now we compute the
# result string and compare it to our expected result.
start, end = result.span(0)
vardict={'found': result.group(0),
'groups': result.group(),
}#'flags': result.re.flags}
for i in range(1, 100):
try:
gi = result.group(i)
# Special hack because else the string concat fails:
if gi is None:
gi = "None"
except IndexError:
gi = "Error"
vardict['g%d' % i] = gi
#for i in result.re.groupindex.keys():
# try:
# gi = result.group(i)
# if gi is None:
# gi = "None"
# except IndexError:
# gi = "Error"
# vardict[i] = gi
repl = eval(repl, vardict)
if repl != expected:
raise Exception("grouping error: %r should be %r" % (repl,
expected))
|