📄 test_sre.py
字号:
# SRE test harness for the Python regression suite# this is based on test_re.py, but uses a test function instead# of all those assertsimport syssys.path=['.']+sys.pathfrom test_support import verbose, TestFailed, have_unicodeimport sreimport sys, os, string, traceback## test supportdef 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: print "expected", "sre.error" else: print "expected", exception.__name__ print "got result", repr(r) else: if r != result: print expression, "FAILED" print "expected", repr(result) print "got result", repr(r)if verbose: print 'Running tests on character literals'for i in [0, 8, 16, 32, 64, 127, 128, 255]: test(r"""sre.match(r"\%03o" % i, chr(i)) is not None""", 1) test(r"""sre.match(r"\%03o0" % i, chr(i)+"0") is not None""", 1) test(r"""sre.match(r"\%03o8" % i, chr(i)+"8") is not None""", 1) test(r"""sre.match(r"\x%02x" % i, chr(i)) is not None""", 1) test(r"""sre.match(r"\x%02x0" % i, chr(i)+"0") is not None""", 1) test(r"""sre.match(r"\x%02xz" % i, chr(i)+"z") is not None""", 1)test(r"""sre.match("\911", "")""", None, sre.error)## Misc tests from Tim Peters' re.docif verbose: print 'Running tests on sre.search and sre.match'test(r"""sre.search(r'x*', 'axx').span(0)""", (0, 0))test(r"""sre.search(r'x*', 'axx').span()""", (0, 0))test(r"""sre.search(r'x+', 'axx').span(0)""", (1, 3))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 113254test(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 612074pat=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.sub(r'.', r"\n", 'x')""", '\n')s = r"\1\1"test(r"""sre.sub(r'(.)', s, 'x')""", 'xx')test(r"""sre.sub(r'(.)', sre.escape(s), 'x')""", s)test(r"""sre.sub(r'(.)', lambda m: s, 'x')""", s)test(r"""sre.sub(r'(?P<a>x)', '\g<a>\g<a>', 'xx')""", 'xxxx')test(r"""sre.sub(r'(?P<a>x)', '\g<a>\g<1>', 'xx')""", 'xxxx')test(r"""sre.sub(r'(?P<unk>x)', '\g<unk>\g<unk>', 'xx')""", 'xxxx')test(r"""sre.sub(r'(?P<unk>x)', '\g<1>\g<1>', 'xx')""", 'xxxx')# bug 449964: fails for group followed by other escapetest(r"""sre.sub(r'(?P<unk>x)', '\g<1>\g<1>\\b', 'xx')""", 'xx\bxx\b')test(r"""sre.sub(r'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')test(r"""sre.sub(r'a', '\t\n\v\r\f\a', 'a')""", '\t\n\v\r\f\a')test(r"""sre.sub(r'a', '\t\n\v\r\f\a', 'a')""", (chr(9)+chr(10)+chr(11)+chr(13)+chr(12)+chr(7)))test(r"""sre.sub(r'^\s*', 'X', 'test')""", 'Xtest')# qualified subtest(r"""sre.sub(r'a', 'b', 'aaaaa')""", 'bbbbb')test(r"""sre.sub(r'a', 'b', 'aaaaa', 1)""", 'baaaa')# bug 114660test(r"""sre.sub(r'(\S)\s+(\S)', r'\1 \2', 'hello there')""", 'hello there')# Test for sub() on escaped characters, see SF bug #449000test(r"""sre.sub(r'\r\n', r'\n', 'abc\r\ndef\r\n')""", 'abc\ndef\n')test(r"""sre.sub('\r\n', r'\n', 'abc\r\ndef\r\n')""", 'abc\ndef\n')test(r"""sre.sub(r'\r\n', '\n', 'abc\r\ndef\r\n')""", 'abc\ndef\n')test(r"""sre.sub('\r\n', '\n', 'abc\r\ndef\r\n')""", 'abc\ndef\n')# Test for empty sub() behaviour, see SF bug #462270test(r"""sre.sub('x*', '-', 'abxd')""", '-a-b-d-')test(r"""sre.sub('x+', '-', 'abxd')""", 'ab-d')if verbose: print 'Running tests on symbolic references'test(r"""sre.sub(r'(?P<a>x)', '\g<a', 'xx')""", None, sre.error)test(r"""sre.sub(r'(?P<a>x)', '\g<', 'xx')""", None, sre.error)test(r"""sre.sub(r'(?P<a>x)', '\g', 'xx')""", None, sre.error)test(r"""sre.sub(r'(?P<a>x)', '\g<a a>', 'xx')""", None, sre.error)test(r"""sre.sub(r'(?P<a>x)', '\g<1a1>', 'xx')""", None, sre.error)test(r"""sre.sub(r'(?P<a>x)', '\g<ab>', 'xx')""", None, IndexError)test(r"""sre.sub(r'(?P<a>x)|(?P<b>y)', '\g<b>', 'xx')""", None, sre.error)test(r"""sre.sub(r'(?P<a>x)|(?P<b>y)', '\\2', 'xx')""", None, sre.error)if verbose: print 'Running tests on sre.subn'test(r"""sre.subn(r"(?i)b+", "x", "bbbb BBBB")""", ('x x', 2))test(r"""sre.subn(r"b+", "x", "bbbb BBBB")""", ('x BBBB', 1))test(r"""sre.subn(r"b+", "x", "xyz")""", ('xyz', 0))test(r"""sre.subn(r"b*", "x", "xyz")""", ('xxxyxzx', 4))test(r"""sre.subn(r"b*", "x", "xyz", 2)""", ('xxxyz', 2))if verbose: print 'Running tests on sre.split'test(r"""sre.split(r":", ":a:b::c")""", ['', 'a', 'b', '', 'c'])test(r"""sre.split(r":+", ":a:b:::")""", ['', 'a', 'b', ''])test(r"""sre.split(r":*", ":a:b::c")""", ['', 'a', 'b', 'c'])test(r"""sre.split(r"(:*)", ":a:b::c")""", ['', ':', 'a', ':', 'b', '::', 'c'])test(r"""sre.split(r"(?::*)", ":a:b::c")""", ['', 'a', 'b', 'c'])test(r"""sre.split(r"(:)*", ":a:b::c")""", ['', ':', 'a', ':', 'b', ':', 'c'])test(r"""sre.split(r"([b:]+)", ":a:b::c")""", ['', ':', 'a', ':b::', 'c'])test(r"""sre.split(r"(b)|(:+)", ":a:b::c")""", ['', None, ':', 'a', None, ':', '', 'b', None, '', None, '::', 'c'])test(r"""sre.split(r"(?:b)|(?::+)", ":a:b::c")""", ['', 'a', '', '', 'c'])test(r"""sre.split(r":", ":a:b::c", 2)""", ['', 'a', 'b::c'])test(r"""sre.split(r':', 'a:b:c:d', 2)""", ['a', 'b', 'c:d'])test(r"""sre.split(r"(:)", ":a:b::c", 2)""", ['', ':', 'a', ':', 'b::c'])test(r"""sre.split(r"(:*)", ":a:b::c", 2)""", ['', ':', 'a', ':', 'b::c'])if verbose: print "Running tests on sre.findall"test(r"""sre.findall(r":+", "abc")""", [])test(r"""sre.findall(r":+", "a:b::c:::d")""", [":", "::", ":::"])test(r"""sre.findall(r"(:+)", "a:b::c:::d")""", [":", "::", ":::"])test(r"""sre.findall(r"(:)(:*)", "a:b::c:::d")""", [(":", ""), (":", ":"), (":", "::")])test(r"""sre.findall(r"(a)|(b)", "abc")""", [("a", ""), ("", "b")])# bug 117612test(r"""sre.findall(r"(a|(b))", "aba")""", [("a", ""),("b", "b"),("a", "")])if sys.hexversion >= 0x02020000: if verbose: print "Running tests on sre.finditer" def fixup(seq): # convert iterator to list if not hasattr(seq, "next") or not hasattr(seq, "__iter__"): print "finditer returned", type(seq) return map(lambda item: item.group(0), seq) # sanity test(r"""fixup(sre.finditer(r":+", "a:b::c:::d"))""", [":", "::", ":::"])if verbose: print "Running tests on sre.match"test(r"""sre.match(r'a', 'a').groups()""", ())test(r"""sre.match(r'(a)', 'a').groups()""", ('a',))test(r"""sre.match(r'(a)', 'a').group(0)""", 'a')test(r"""sre.match(r'(a)', 'a').group(1)""", 'a')test(r"""sre.match(r'(a)', 'a').group(1, 1)""", ('a', 'a'))pat = sre.compile(r'((a)|(b))(c)?')test(r"""pat.match('a').groups()""", ('a', 'a', None, None))test(r"""pat.match('b').groups()""", ('b', None, 'b', None))test(r"""pat.match('ac').groups()""", ('a', 'a', None, 'c'))test(r"""pat.match('bc').groups()""", ('b', None, 'b', 'c'))test(r"""pat.match('bc').groups("")""", ('b', "", 'b', 'c'))pat = sre.compile(r'(?:(?P<a1>a)|(?P<b2>b))(?P<c3>c)?')
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -