pat = sre.compile(r"(?:(?P<a1>a)|(?P<b2>b))(?P<c3>c)?") test(r"""pat.match('a').group(1, 2, 3)""", ("a", None, None)) test(r"""pat.match('b').group('a1', 'b2', 'c3')""", (None, "b", None)) test(r"""pat.match('ac').group(1, 'b2', 3)""", ("a", None, "c")) if verbose: print "Running tests on sre.escape" p = "" for i in range(0, 256): p = p + chr(i) test(r"""sre.match(sre.escape(chr(i)), chr(i)) != None""", 1) test(r"""sre.match(sre.escape(chr(i)), chr(i)).span()""", (0, 1)) pat = sre.compile(sre.escape(p)) test(r"""pat.match(p) != None""", 1) test(r"""pat.match(p).span()""", (0, 256)) if verbose: print "Pickling a SRE_Pattern instance" try: import pickle pat = sre.compile(r"a(?:b|(c|e){1,2}?|d)+?(.)") s = pickle.dumps(pat) pat = pickle.loads(s) except: print TestFailed, "re module pickle" # expected
# SRE test harness for the Python regression suite # this is based on test_re.py, but uses a test function instead # of all those asserts import sys sys.path = ['.'] + sys.path from test_support import verbose, TestFailed, have_unicode import sre import sys, os, string, traceback # # test support def test(expression, result, exception=None): try: r = eval(expression) except: if exception: if not isinstance(sys.exc_value, exception): print expression, "FAILED" # display name, not actual value if exception is sre.error: print "expected", "sre.error" else: print "expected", exception.__name__ print "got", sys.exc_type.__name__, str(sys.exc_value) else: print expression, "FAILED" traceback.print_exc(file=sys.stdout) else: if exception: print expression, "FAILED"
# SRE test harness for the Python regression suite # this is based on test_re.py, but uses a test function instead # of all those asserts import sys sys.path=['.']+sys.path from test_support import verbose, TestFailed, have_unicode import sre import sys, os, string, traceback # # test support def test(expression, result, exception=None): try: r = eval(expression) except: if exception: if not isinstance(sys.exc_value, exception): print expression, "FAILED" # display name, not actual value if exception is sre.error: print "expected", "sre.error" else: print "expected", exception.__name__ print "got", sys.exc_type.__name__, str(sys.exc_value) else: print expression, "FAILED" traceback.print_exc(file=sys.stdout) else: if exception: print expression, "FAILED" if exception is sre.error:
try: assert sre.sub("(?i)b+", "x", "bbbb BBBB") == 'x x' def bump_num(matchobj): int_value = int(matchobj.group(0)) return str(int_value + 1) assert sre.sub(r'\d+', bump_num, '08.2 -2 23x99y') == '9.3 -3 24x100y' assert sre.sub(r'\d+', bump_num, '08.2 -2 23x99y', 3) == '9.3 -3 23x99y' assert sre.sub('.', lambda m: r"\n", 'x') == '\\n' assert sre.sub('.', r"\n", 'x') == '\n' s = r"\1\1" assert sre.sub('(.)', s, 'x') == 'xx' assert sre.sub('(.)', sre.escape(s), 'x') == s assert sre.sub('(.)', lambda m: s, 'x') == s assert sre.sub('(?P<a>x)', '\g<a>\g<a>', 'xx') == 'xxxx' assert sre.sub('(?P<a>x)', '\g<a>\g<1>', 'xx') == 'xxxx' assert sre.sub('(?P<unk>x)', '\g<unk>\g<unk>', 'xx') == 'xxxx' assert sre.sub('(?P<unk>x)', '\g<1>\g<1>', 'xx') == 'xxxx' assert sre.sub('a', r'\t\n\v\r\f\a\b\B\Z\a\A\w\W\s\S\d\D', 'a') == '\t\n\v\r\f\a\b\\B\\Z\a\\A\\w\\W\\s\\S\\d\\D' assert sre.sub('a', '\t\n\v\r\f\a', 'a') == '\t\n\v\r\f\a' assert sre.sub('a', '\t\n\v\r\f\a', 'a') == (chr(9)+chr(10)+chr(11)+chr(13)+chr(12)+chr(7)) assert sre.sub('^\s*', 'X', 'test') == 'Xtest' except AssertionError: raise TestFailed, "sre.sub"
test(r"""sre.search(r'x+', 'axx').span()""", (1, 3)) test(r"""sre.search(r'x', 'aaa')""", None) test(r"""sre.match(r'a*', 'xxx').span(0)""", (0, 0)) test(r"""sre.match(r'a*', 'xxx').span()""", (0, 0)) test(r"""sre.match(r'x*', 'xxxa').span(0)""", (0, 3)) test(r"""sre.match(r'x*', 'xxxa').span()""", (0, 3)) test(r"""sre.match(r'a+', 'xxx')""", None) # bug 113254 test(r"""sre.match(r'(a)|(b)', 'b').start(1)""", -1) test(r"""sre.match(r'(a)|(b)', 'b').end(1)""", -1) test(r"""sre.match(r'(a)|(b)', 'b').span(1)""", (-1, -1)) # bug 612074 pat=u"["+sre.escape(u"\u2039")+u"]" test(r"""sre.compile(pat) and 1""", 1, None) if verbose: print 'Running tests on sre.sub' test(r"""sre.sub(r"(?i)b+", "x", "bbbb BBBB")""", 'x x') def bump_num(matchobj): int_value = int(matchobj.group(0)) return str(int_value + 1) test(r"""sre.sub(r'\d+', bump_num, '08.2 -2 23x99y')""", '9.3 -3 24x100y') test(r"""sre.sub(r'\d+', bump_num, '08.2 -2 23x99y', 3)""", '9.3 -3 23x99y') test(r"""sre.sub(r'.', lambda m: r"\n", 'x')""", '\\n')
test(r"""sre.search(r'x+', 'axx').span()""", (1, 3)) test(r"""sre.search(r'x', 'aaa')""", None) test(r"""sre.match(r'a*', 'xxx').span(0)""", (0, 0)) test(r"""sre.match(r'a*', 'xxx').span()""", (0, 0)) test(r"""sre.match(r'x*', 'xxxa').span(0)""", (0, 3)) test(r"""sre.match(r'x*', 'xxxa').span()""", (0, 3)) test(r"""sre.match(r'a+', 'xxx')""", None) # bug 113254 test(r"""sre.match(r'(a)|(b)', 'b').start(1)""", -1) test(r"""sre.match(r'(a)|(b)', 'b').end(1)""", -1) test(r"""sre.match(r'(a)|(b)', 'b').span(1)""", (-1, -1)) # bug 612074 pat = u"[" + sre.escape(u"\u2039") + u"]" test(r"""sre.compile(pat) and 1""", 1, None) if verbose: print 'Running tests on sre.sub' test(r"""sre.sub(r"(?i)b+", "x", "bbbb BBBB")""", 'x x') def bump_num(matchobj): int_value = int(matchobj.group(0)) return str(int_value + 1) test(r"""sre.sub(r'\d+', bump_num, '08.2 -2 23x99y')""", '9.3 -3 24x100y') test(r"""sre.sub(r'\d+', bump_num, '08.2 -2 23x99y', 3)""", '9.3 -3 23x99y')
# bug 448951 (similar to 429357, but with single char match) # (Also test greedy matches.) for op in '', '?', '*': test(r"""sre.match(r'((.%s):)?z', 'z').groups()""" % op, (None, None)) test(r"""sre.match(r'((.%s):)?z', 'a:z').groups()""" % op, ('a:', 'a')) if verbose: print "Running tests on sre.escape" p = "" for i in range(0, 256): p = p + chr(i) test(r"""sre.match(sre.escape(chr(i)), chr(i)) is not None""", 1) test(r"""sre.match(sre.escape(chr(i)), chr(i)).span()""", (0, 1)) pat = sre.compile(sre.escape(p)) test(r"""pat.match(p) is not None""", 1) test(r"""pat.match(p).span()""", (0, 256)) if verbose: print 'Running tests on sre.Scanner' def s_ident(scanner, token): return token def s_operator(scanner, token): return "op%s" % token