Fredrik Lundh | 143328b | 2000-09-02 11:03:34 +0000 | [diff] [blame] | 1 | # SRE test harness for the Python regression suite |
| 2 | |
| 3 | # this is based on test_re.py, but uses a test function instead |
| 4 | # of all those asserts |
Fredrik Lundh | df02d0b | 2000-06-30 07:08:20 +0000 | [diff] [blame] | 5 | |
| 6 | import sys |
| 7 | sys.path=['.']+sys.path |
| 8 | |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 9 | from test_support import verbose, TestFailed, have_unicode |
Fredrik Lundh | df02d0b | 2000-06-30 07:08:20 +0000 | [diff] [blame] | 10 | import sre |
Fredrik Lundh | f2989b2 | 2001-02-18 12:05:16 +0000 | [diff] [blame] | 11 | import sys, os, string, traceback |
Fredrik Lundh | df02d0b | 2000-06-30 07:08:20 +0000 | [diff] [blame] | 12 | |
Fredrik Lundh | 143328b | 2000-09-02 11:03:34 +0000 | [diff] [blame] | 13 | # |
| 14 | # test support |
| 15 | |
| 16 | def test(expression, result, exception=None): |
| 17 | try: |
| 18 | r = eval(expression) |
| 19 | except: |
| 20 | if exception: |
| 21 | if not isinstance(sys.exc_value, exception): |
| 22 | print expression, "FAILED" |
| 23 | # display name, not actual value |
| 24 | if exception is sre.error: |
| 25 | print "expected", "sre.error" |
| 26 | else: |
| 27 | print "expected", exception.__name__ |
| 28 | print "got", sys.exc_type.__name__, str(sys.exc_value) |
| 29 | else: |
| 30 | print expression, "FAILED" |
| 31 | traceback.print_exc(file=sys.stdout) |
| 32 | else: |
| 33 | if exception: |
| 34 | print expression, "FAILED" |
| 35 | if exception is sre.error: |
| 36 | print "expected", "sre.error" |
| 37 | else: |
| 38 | print "expected", exception.__name__ |
| 39 | print "got result", repr(r) |
| 40 | else: |
| 41 | if r != result: |
| 42 | print expression, "FAILED" |
| 43 | print "expected", repr(result) |
| 44 | print "got result", repr(r) |
| 45 | |
| 46 | if verbose: |
| 47 | print 'Running tests on character literals' |
| 48 | |
Fredrik Lundh | 510c97b | 2000-09-02 16:36:57 +0000 | [diff] [blame] | 49 | for i in [0, 8, 16, 32, 64, 127, 128, 255]: |
Fredrik Lundh | 538f05c | 2001-01-14 15:15:37 +0000 | [diff] [blame] | 50 | test(r"""sre.match(r"\%03o" % i, chr(i)) is not None""", 1) |
| 51 | test(r"""sre.match(r"\%03o0" % i, chr(i)+"0") is not None""", 1) |
| 52 | test(r"""sre.match(r"\%03o8" % i, chr(i)+"8") is not None""", 1) |
| 53 | test(r"""sre.match(r"\x%02x" % i, chr(i)) is not None""", 1) |
| 54 | test(r"""sre.match(r"\x%02x0" % i, chr(i)+"0") is not None""", 1) |
| 55 | test(r"""sre.match(r"\x%02xz" % i, chr(i)+"z") is not None""", 1) |
Fredrik Lundh | 143328b | 2000-09-02 11:03:34 +0000 | [diff] [blame] | 56 | test(r"""sre.match("\911", "")""", None, sre.error) |
| 57 | |
| 58 | # |
Fredrik Lundh | df02d0b | 2000-06-30 07:08:20 +0000 | [diff] [blame] | 59 | # Misc tests from Tim Peters' re.doc |
| 60 | |
| 61 | if verbose: |
| 62 | print 'Running tests on sre.search and sre.match' |
| 63 | |
Fredrik Lundh | 03dd010 | 2000-09-03 10:43:16 +0000 | [diff] [blame] | 64 | test(r"""sre.search(r'x*', 'axx').span(0)""", (0, 0)) |
| 65 | test(r"""sre.search(r'x*', 'axx').span()""", (0, 0)) |
| 66 | test(r"""sre.search(r'x+', 'axx').span(0)""", (1, 3)) |
| 67 | test(r"""sre.search(r'x+', 'axx').span()""", (1, 3)) |
| 68 | test(r"""sre.search(r'x', 'aaa')""", None) |
Fredrik Lundh | df02d0b | 2000-06-30 07:08:20 +0000 | [diff] [blame] | 69 | |
Fredrik Lundh | 03dd010 | 2000-09-03 10:43:16 +0000 | [diff] [blame] | 70 | test(r"""sre.match(r'a*', 'xxx').span(0)""", (0, 0)) |
| 71 | test(r"""sre.match(r'a*', 'xxx').span()""", (0, 0)) |
| 72 | test(r"""sre.match(r'x*', 'xxxa').span(0)""", (0, 3)) |
| 73 | test(r"""sre.match(r'x*', 'xxxa').span()""", (0, 3)) |
| 74 | test(r"""sre.match(r'a+', 'xxx')""", None) |
Fredrik Lundh | df02d0b | 2000-06-30 07:08:20 +0000 | [diff] [blame] | 75 | |
Fredrik Lundh | 510c97b | 2000-09-02 16:36:57 +0000 | [diff] [blame] | 76 | # bug 113254 |
Fredrik Lundh | 03dd010 | 2000-09-03 10:43:16 +0000 | [diff] [blame] | 77 | test(r"""sre.match(r'(a)|(b)', 'b').start(1)""", -1) |
| 78 | test(r"""sre.match(r'(a)|(b)', 'b').end(1)""", -1) |
| 79 | test(r"""sre.match(r'(a)|(b)', 'b').span(1)""", (-1, -1)) |
Fredrik Lundh | 510c97b | 2000-09-02 16:36:57 +0000 | [diff] [blame] | 80 | |
Fredrik Lundh | df02d0b | 2000-06-30 07:08:20 +0000 | [diff] [blame] | 81 | if verbose: |
| 82 | print 'Running tests on sre.sub' |
| 83 | |
Fredrik Lundh | 03dd010 | 2000-09-03 10:43:16 +0000 | [diff] [blame] | 84 | test(r"""sre.sub(r"(?i)b+", "x", "bbbb BBBB")""", 'x x') |
Fredrik Lundh | 6f01398 | 2000-07-03 18:44:21 +0000 | [diff] [blame] | 85 | |
Fredrik Lundh | 143328b | 2000-09-02 11:03:34 +0000 | [diff] [blame] | 86 | def bump_num(matchobj): |
| 87 | int_value = int(matchobj.group(0)) |
| 88 | return str(int_value + 1) |
Fredrik Lundh | df02d0b | 2000-06-30 07:08:20 +0000 | [diff] [blame] | 89 | |
Fredrik Lundh | 143328b | 2000-09-02 11:03:34 +0000 | [diff] [blame] | 90 | test(r"""sre.sub(r'\d+', bump_num, '08.2 -2 23x99y')""", '9.3 -3 24x100y') |
| 91 | test(r"""sre.sub(r'\d+', bump_num, '08.2 -2 23x99y', 3)""", '9.3 -3 23x99y') |
Fredrik Lundh | 6f01398 | 2000-07-03 18:44:21 +0000 | [diff] [blame] | 92 | |
Fredrik Lundh | 03dd010 | 2000-09-03 10:43:16 +0000 | [diff] [blame] | 93 | test(r"""sre.sub(r'.', lambda m: r"\n", 'x')""", '\\n') |
| 94 | test(r"""sre.sub(r'.', r"\n", 'x')""", '\n') |
Fredrik Lundh | df02d0b | 2000-06-30 07:08:20 +0000 | [diff] [blame] | 95 | |
Fredrik Lundh | 143328b | 2000-09-02 11:03:34 +0000 | [diff] [blame] | 96 | s = r"\1\1" |
Fredrik Lundh | df02d0b | 2000-06-30 07:08:20 +0000 | [diff] [blame] | 97 | |
Fredrik Lundh | 03dd010 | 2000-09-03 10:43:16 +0000 | [diff] [blame] | 98 | test(r"""sre.sub(r'(.)', s, 'x')""", 'xx') |
| 99 | test(r"""sre.sub(r'(.)', sre.escape(s), 'x')""", s) |
| 100 | test(r"""sre.sub(r'(.)', lambda m: s, 'x')""", s) |
Fredrik Lundh | df02d0b | 2000-06-30 07:08:20 +0000 | [diff] [blame] | 101 | |
Fredrik Lundh | 03dd010 | 2000-09-03 10:43:16 +0000 | [diff] [blame] | 102 | test(r"""sre.sub(r'(?P<a>x)', '\g<a>\g<a>', 'xx')""", 'xxxx') |
| 103 | test(r"""sre.sub(r'(?P<a>x)', '\g<a>\g<1>', 'xx')""", 'xxxx') |
| 104 | test(r"""sre.sub(r'(?P<unk>x)', '\g<unk>\g<unk>', 'xx')""", 'xxxx') |
| 105 | test(r"""sre.sub(r'(?P<unk>x)', '\g<1>\g<1>', 'xx')""", 'xxxx') |
Fredrik Lundh | df02d0b | 2000-06-30 07:08:20 +0000 | [diff] [blame] | 106 | |
Fredrik Lundh | 59b6865 | 2001-09-18 20:55:24 +0000 | [diff] [blame] | 107 | # bug 449964: fails for group followed by other escape |
| 108 | test(r"""sre.sub(r'(?P<unk>x)', '\g<1>\g<1>\\b', 'xx')""", 'xx\bxx\b') |
| 109 | |
Fredrik Lundh | 03dd010 | 2000-09-03 10:43:16 +0000 | [diff] [blame] | 110 | 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') |
| 111 | test(r"""sre.sub(r'a', '\t\n\v\r\f\a', 'a')""", '\t\n\v\r\f\a') |
| 112 | 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))) |
Fredrik Lundh | df02d0b | 2000-06-30 07:08:20 +0000 | [diff] [blame] | 113 | |
Fredrik Lundh | 03dd010 | 2000-09-03 10:43:16 +0000 | [diff] [blame] | 114 | test(r"""sre.sub(r'^\s*', 'X', 'test')""", 'Xtest') |
Fredrik Lundh | df02d0b | 2000-06-30 07:08:20 +0000 | [diff] [blame] | 115 | |
Fredrik Lundh | 143328b | 2000-09-02 11:03:34 +0000 | [diff] [blame] | 116 | # qualified sub |
Fredrik Lundh | 03dd010 | 2000-09-03 10:43:16 +0000 | [diff] [blame] | 117 | test(r"""sre.sub(r'a', 'b', 'aaaaa')""", 'bbbbb') |
| 118 | test(r"""sre.sub(r'a', 'b', 'aaaaa', 1)""", 'baaaa') |
Fredrik Lundh | df02d0b | 2000-06-30 07:08:20 +0000 | [diff] [blame] | 119 | |
Fredrik Lundh | 19f977b | 2000-09-24 14:46:23 +0000 | [diff] [blame] | 120 | # bug 114660 |
| 121 | test(r"""sre.sub(r'(\S)\s+(\S)', r'\1 \2', 'hello there')""", 'hello there') |
| 122 | |
Guido van Rossum | e056e4d | 2001-08-10 14:52:48 +0000 | [diff] [blame] | 123 | # Test for sub() on escaped characters, see SF bug #449000 |
| 124 | test(r"""sre.sub(r'\r\n', r'\n', 'abc\r\ndef\r\n')""", 'abc\ndef\n') |
| 125 | test(r"""sre.sub('\r\n', r'\n', 'abc\r\ndef\r\n')""", 'abc\ndef\n') |
| 126 | test(r"""sre.sub(r'\r\n', '\n', 'abc\r\ndef\r\n')""", 'abc\ndef\n') |
| 127 | test(r"""sre.sub('\r\n', '\n', 'abc\r\ndef\r\n')""", 'abc\ndef\n') |
| 128 | |
Fredrik Lundh | 21009b9 | 2001-09-18 18:47:09 +0000 | [diff] [blame] | 129 | # Test for empty sub() behaviour, see SF bug #462270 |
| 130 | test(r"""sre.sub('x*', '-', 'abxd')""", '-a-b-d-') |
| 131 | test(r"""sre.sub('x+', '-', 'abxd')""", 'ab-d') |
| 132 | |
Fredrik Lundh | df02d0b | 2000-06-30 07:08:20 +0000 | [diff] [blame] | 133 | if verbose: |
| 134 | print 'Running tests on symbolic references' |
| 135 | |
Fredrik Lundh | 03dd010 | 2000-09-03 10:43:16 +0000 | [diff] [blame] | 136 | test(r"""sre.sub(r'(?P<a>x)', '\g<a', 'xx')""", None, sre.error) |
| 137 | test(r"""sre.sub(r'(?P<a>x)', '\g<', 'xx')""", None, sre.error) |
| 138 | test(r"""sre.sub(r'(?P<a>x)', '\g', 'xx')""", None, sre.error) |
| 139 | test(r"""sre.sub(r'(?P<a>x)', '\g<a a>', 'xx')""", None, sre.error) |
| 140 | test(r"""sre.sub(r'(?P<a>x)', '\g<1a1>', 'xx')""", None, sre.error) |
| 141 | test(r"""sre.sub(r'(?P<a>x)', '\g<ab>', 'xx')""", None, IndexError) |
| 142 | test(r"""sre.sub(r'(?P<a>x)|(?P<b>y)', '\g<b>', 'xx')""", None, sre.error) |
| 143 | test(r"""sre.sub(r'(?P<a>x)|(?P<b>y)', '\\2', 'xx')""", None, sre.error) |
Fredrik Lundh | df02d0b | 2000-06-30 07:08:20 +0000 | [diff] [blame] | 144 | |
| 145 | if verbose: |
| 146 | print 'Running tests on sre.subn' |
| 147 | |
Fredrik Lundh | 03dd010 | 2000-09-03 10:43:16 +0000 | [diff] [blame] | 148 | test(r"""sre.subn(r"(?i)b+", "x", "bbbb BBBB")""", ('x x', 2)) |
| 149 | test(r"""sre.subn(r"b+", "x", "bbbb BBBB")""", ('x BBBB', 1)) |
| 150 | test(r"""sre.subn(r"b+", "x", "xyz")""", ('xyz', 0)) |
| 151 | test(r"""sre.subn(r"b*", "x", "xyz")""", ('xxxyxzx', 4)) |
| 152 | test(r"""sre.subn(r"b*", "x", "xyz", 2)""", ('xxxyz', 2)) |
Fredrik Lundh | df02d0b | 2000-06-30 07:08:20 +0000 | [diff] [blame] | 153 | |
| 154 | if verbose: |
| 155 | print 'Running tests on sre.split' |
Fredrik Lundh | 6f01398 | 2000-07-03 18:44:21 +0000 | [diff] [blame] | 156 | |
Fredrik Lundh | 03dd010 | 2000-09-03 10:43:16 +0000 | [diff] [blame] | 157 | test(r"""sre.split(r":", ":a:b::c")""", ['', 'a', 'b', '', 'c']) |
| 158 | test(r"""sre.split(r":*", ":a:b::c")""", ['', 'a', 'b', 'c']) |
| 159 | test(r"""sre.split(r"(:*)", ":a:b::c")""", ['', ':', 'a', ':', 'b', '::', 'c']) |
| 160 | test(r"""sre.split(r"(?::*)", ":a:b::c")""", ['', 'a', 'b', 'c']) |
| 161 | test(r"""sre.split(r"(:)*", ":a:b::c")""", ['', ':', 'a', ':', 'b', ':', 'c']) |
| 162 | test(r"""sre.split(r"([b:]+)", ":a:b::c")""", ['', ':', 'a', ':b::', 'c']) |
| 163 | test(r"""sre.split(r"(b)|(:+)", ":a:b::c")""", |
Fredrik Lundh | 143328b | 2000-09-02 11:03:34 +0000 | [diff] [blame] | 164 | ['', None, ':', 'a', None, ':', '', 'b', None, '', None, '::', 'c']) |
Fredrik Lundh | 03dd010 | 2000-09-03 10:43:16 +0000 | [diff] [blame] | 165 | test(r"""sre.split(r"(?:b)|(?::+)", ":a:b::c")""", ['', 'a', '', '', 'c']) |
Fredrik Lundh | df02d0b | 2000-06-30 07:08:20 +0000 | [diff] [blame] | 166 | |
Fredrik Lundh | 03dd010 | 2000-09-03 10:43:16 +0000 | [diff] [blame] | 167 | test(r"""sre.split(r":", ":a:b::c", 2)""", ['', 'a', 'b::c']) |
| 168 | test(r"""sre.split(r':', 'a:b:c:d', 2)""", ['a', 'b', 'c:d']) |
Fredrik Lundh | df02d0b | 2000-06-30 07:08:20 +0000 | [diff] [blame] | 169 | |
Fredrik Lundh | 03dd010 | 2000-09-03 10:43:16 +0000 | [diff] [blame] | 170 | test(r"""sre.split(r"(:)", ":a:b::c", 2)""", ['', ':', 'a', ':', 'b::c']) |
| 171 | test(r"""sre.split(r"(:*)", ":a:b::c", 2)""", ['', ':', 'a', ':', 'b::c']) |
Fredrik Lundh | df02d0b | 2000-06-30 07:08:20 +0000 | [diff] [blame] | 172 | |
| 173 | if verbose: |
| 174 | print "Running tests on sre.findall" |
| 175 | |
Fredrik Lundh | 03dd010 | 2000-09-03 10:43:16 +0000 | [diff] [blame] | 176 | test(r"""sre.findall(r":+", "abc")""", []) |
| 177 | test(r"""sre.findall(r":+", "a:b::c:::d")""", [":", "::", ":::"]) |
| 178 | test(r"""sre.findall(r"(:+)", "a:b::c:::d")""", [":", "::", ":::"]) |
| 179 | test(r"""sre.findall(r"(:)(:*)", "a:b::c:::d")""", |
Fredrik Lundh | 143328b | 2000-09-02 11:03:34 +0000 | [diff] [blame] | 180 | [(":", ""), (":", ":"), (":", "::")]) |
Fredrik Lundh | 03dd010 | 2000-09-03 10:43:16 +0000 | [diff] [blame] | 181 | test(r"""sre.findall(r"(a)|(b)", "abc")""", [("a", ""), ("", "b")]) |
Fredrik Lundh | df02d0b | 2000-06-30 07:08:20 +0000 | [diff] [blame] | 182 | |
Fredrik Lundh | ebc37b2 | 2000-10-28 19:30:41 +0000 | [diff] [blame] | 183 | # bug 117612 |
| 184 | test(r"""sre.findall(r"(a|(b))", "aba")""", [("a", ""),("b", "b"),("a", "")]) |
| 185 | |
Fredrik Lundh | df02d0b | 2000-06-30 07:08:20 +0000 | [diff] [blame] | 186 | if verbose: |
| 187 | print "Running tests on sre.match" |
| 188 | |
Fredrik Lundh | 03dd010 | 2000-09-03 10:43:16 +0000 | [diff] [blame] | 189 | test(r"""sre.match(r'a', 'a').groups()""", ()) |
| 190 | test(r"""sre.match(r'(a)', 'a').groups()""", ('a',)) |
| 191 | test(r"""sre.match(r'(a)', 'a').group(0)""", 'a') |
| 192 | test(r"""sre.match(r'(a)', 'a').group(1)""", 'a') |
| 193 | test(r"""sre.match(r'(a)', 'a').group(1, 1)""", ('a', 'a')) |
Fredrik Lundh | df02d0b | 2000-06-30 07:08:20 +0000 | [diff] [blame] | 194 | |
Fredrik Lundh | 03dd010 | 2000-09-03 10:43:16 +0000 | [diff] [blame] | 195 | pat = sre.compile(r'((a)|(b))(c)?') |
Fredrik Lundh | 143328b | 2000-09-02 11:03:34 +0000 | [diff] [blame] | 196 | test(r"""pat.match('a').groups()""", ('a', 'a', None, None)) |
| 197 | test(r"""pat.match('b').groups()""", ('b', None, 'b', None)) |
| 198 | test(r"""pat.match('ac').groups()""", ('a', 'a', None, 'c')) |
| 199 | test(r"""pat.match('bc').groups()""", ('b', None, 'b', 'c')) |
| 200 | test(r"""pat.match('bc').groups("")""", ('b', "", 'b', 'c')) |
Fredrik Lundh | df02d0b | 2000-06-30 07:08:20 +0000 | [diff] [blame] | 201 | |
Fredrik Lundh | 03dd010 | 2000-09-03 10:43:16 +0000 | [diff] [blame] | 202 | pat = sre.compile(r'(?:(?P<a1>a)|(?P<b2>b))(?P<c3>c)?') |
Fredrik Lundh | 143328b | 2000-09-02 11:03:34 +0000 | [diff] [blame] | 203 | test(r"""pat.match('a').group(1, 2, 3)""", ('a', None, None)) |
| 204 | test(r"""pat.match('b').group('a1', 'b2', 'c3')""", (None, 'b', None)) |
| 205 | test(r"""pat.match('ac').group(1, 'b2', 3)""", ('a', None, 'c')) |
Fredrik Lundh | df02d0b | 2000-06-30 07:08:20 +0000 | [diff] [blame] | 206 | |
Fredrik Lundh | 397a654 | 2001-10-18 19:30:16 +0000 | [diff] [blame] | 207 | # bug 448951 (similar to 429357, but with single char match) |
| 208 | # (Also test greedy matches.) |
| 209 | for op in '','?','*': |
| 210 | test(r"""sre.match(r'((.%s):)?z', 'z').groups()"""%op, (None, None)) |
| 211 | test(r"""sre.match(r'((.%s):)?z', 'a:z').groups()"""%op, ('a:', 'a')) |
| 212 | |
Fredrik Lundh | df02d0b | 2000-06-30 07:08:20 +0000 | [diff] [blame] | 213 | if verbose: |
| 214 | print "Running tests on sre.escape" |
| 215 | |
Fredrik Lundh | 143328b | 2000-09-02 11:03:34 +0000 | [diff] [blame] | 216 | p = "" |
| 217 | for i in range(0, 256): |
| 218 | p = p + chr(i) |
Fredrik Lundh | 538f05c | 2001-01-14 15:15:37 +0000 | [diff] [blame] | 219 | test(r"""sre.match(sre.escape(chr(i)), chr(i)) is not None""", 1) |
Fredrik Lundh | 143328b | 2000-09-02 11:03:34 +0000 | [diff] [blame] | 220 | test(r"""sre.match(sre.escape(chr(i)), chr(i)).span()""", (0,1)) |
Fredrik Lundh | df02d0b | 2000-06-30 07:08:20 +0000 | [diff] [blame] | 221 | |
Fredrik Lundh | 143328b | 2000-09-02 11:03:34 +0000 | [diff] [blame] | 222 | pat = sre.compile(sre.escape(p)) |
Fredrik Lundh | 538f05c | 2001-01-14 15:15:37 +0000 | [diff] [blame] | 223 | test(r"""pat.match(p) is not None""", 1) |
Fredrik Lundh | 143328b | 2000-09-02 11:03:34 +0000 | [diff] [blame] | 224 | test(r"""pat.match(p).span()""", (0,256)) |
Fredrik Lundh | df02d0b | 2000-06-30 07:08:20 +0000 | [diff] [blame] | 225 | |
| 226 | if verbose: |
| 227 | print 'Pickling a SRE_Pattern instance' |
| 228 | |
| 229 | try: |
| 230 | import pickle |
Fredrik Lundh | 03dd010 | 2000-09-03 10:43:16 +0000 | [diff] [blame] | 231 | pat = sre.compile(r'a(?:b|(c|e){1,2}?|d)+?(.)') |
Fredrik Lundh | df02d0b | 2000-06-30 07:08:20 +0000 | [diff] [blame] | 232 | s = pickle.dumps(pat) |
| 233 | pat = pickle.loads(s) |
| 234 | except: |
| 235 | print TestFailed, 're module pickle' # expected |
| 236 | |
| 237 | try: |
| 238 | import cPickle |
Fredrik Lundh | 03dd010 | 2000-09-03 10:43:16 +0000 | [diff] [blame] | 239 | pat = sre.compile(r'a(?:b|(c|e){1,2}?|d)+?(.)') |
Fredrik Lundh | df02d0b | 2000-06-30 07:08:20 +0000 | [diff] [blame] | 240 | s = cPickle.dumps(pat) |
| 241 | pat = cPickle.loads(s) |
| 242 | except: |
| 243 | print TestFailed, 're module cPickle' # expected |
| 244 | |
Fredrik Lundh | 143328b | 2000-09-02 11:03:34 +0000 | [diff] [blame] | 245 | # constants |
| 246 | test(r"""sre.I""", sre.IGNORECASE) |
| 247 | test(r"""sre.L""", sre.LOCALE) |
| 248 | test(r"""sre.M""", sre.MULTILINE) |
| 249 | test(r"""sre.S""", sre.DOTALL) |
| 250 | test(r"""sre.X""", sre.VERBOSE) |
| 251 | test(r"""sre.T""", sre.TEMPLATE) |
| 252 | test(r"""sre.U""", sre.UNICODE) |
Fredrik Lundh | df02d0b | 2000-06-30 07:08:20 +0000 | [diff] [blame] | 253 | |
| 254 | for flags in [sre.I, sre.M, sre.X, sre.S, sre.L, sre.T, sre.U]: |
| 255 | try: |
| 256 | r = sre.compile('^pattern$', flags) |
| 257 | except: |
| 258 | print 'Exception raised on flag', flags |
| 259 | |
Fredrik Lundh | 96ab465 | 2000-08-03 16:29:50 +0000 | [diff] [blame] | 260 | if verbose: |
| 261 | print 'Test engine limitations' |
| 262 | |
| 263 | # Try nasty case that overflows the straightforward recursive |
| 264 | # implementation of repeated groups. |
Fredrik Lundh | 015415e | 2001-03-22 23:48:28 +0000 | [diff] [blame] | 265 | test("sre.match('(x)*', 50000*'x').span()", (0, 50000), RuntimeError) |
| 266 | test("sre.match(r'(x)*y', 50000*'x'+'y').span()", (0, 50001), RuntimeError) |
Fredrik Lundh | df781e6 | 2001-07-02 19:54:28 +0000 | [diff] [blame] | 267 | test("sre.match(r'(x)*?y', 50000*'x'+'y').span()", (0, 50001)) |
Fredrik Lundh | 96ab465 | 2000-08-03 16:29:50 +0000 | [diff] [blame] | 268 | |
Fredrik Lundh | df02d0b | 2000-06-30 07:08:20 +0000 | [diff] [blame] | 269 | from re_tests import * |
| 270 | |
| 271 | if verbose: |
| 272 | print 'Running re_tests test suite' |
| 273 | else: |
| 274 | # To save time, only run the first and last 10 tests |
| 275 | #tests = tests[:10] + tests[-10:] |
Fredrik Lundh | 6f01398 | 2000-07-03 18:44:21 +0000 | [diff] [blame] | 276 | pass |
Fredrik Lundh | df02d0b | 2000-06-30 07:08:20 +0000 | [diff] [blame] | 277 | |
| 278 | for t in tests: |
| 279 | sys.stdout.flush() |
| 280 | pattern=s=outcome=repl=expected=None |
| 281 | if len(t)==5: |
| 282 | pattern, s, outcome, repl, expected = t |
| 283 | elif len(t)==3: |
Fredrik Lundh | 6f01398 | 2000-07-03 18:44:21 +0000 | [diff] [blame] | 284 | pattern, s, outcome = t |
Fredrik Lundh | df02d0b | 2000-06-30 07:08:20 +0000 | [diff] [blame] | 285 | else: |
| 286 | raise ValueError, ('Test tuples should have 3 or 5 fields',t) |
| 287 | |
| 288 | try: |
| 289 | obj=sre.compile(pattern) |
| 290 | except sre.error: |
| 291 | if outcome==SYNTAX_ERROR: pass # Expected a syntax error |
Fredrik Lundh | 6f01398 | 2000-07-03 18:44:21 +0000 | [diff] [blame] | 292 | else: |
Fredrik Lundh | df02d0b | 2000-06-30 07:08:20 +0000 | [diff] [blame] | 293 | print '=== Syntax error:', t |
| 294 | except KeyboardInterrupt: raise KeyboardInterrupt |
| 295 | except: |
| 296 | print '*** Unexpected error ***', t |
| 297 | if verbose: |
| 298 | traceback.print_exc(file=sys.stdout) |
| 299 | else: |
| 300 | try: |
| 301 | result=obj.search(s) |
| 302 | except (sre.error), msg: |
| 303 | print '=== Unexpected exception', t, repr(msg) |
| 304 | if outcome==SYNTAX_ERROR: |
Fredrik Lundh | 03dd010 | 2000-09-03 10:43:16 +0000 | [diff] [blame] | 305 | print '=== Compiled incorrectly', t |
Fredrik Lundh | df02d0b | 2000-06-30 07:08:20 +0000 | [diff] [blame] | 306 | elif outcome==FAIL: |
| 307 | if result is None: pass # No match, as expected |
| 308 | else: print '=== Succeeded incorrectly', t |
| 309 | elif outcome==SUCCEED: |
| 310 | if result is not None: |
| 311 | # Matched, as expected, so now we compute the |
| 312 | # result string and compare it to our expected result. |
| 313 | start, end = result.span(0) |
| 314 | vardict={'found': result.group(0), |
| 315 | 'groups': result.group(), |
| 316 | 'flags': result.re.flags} |
| 317 | for i in range(1, 100): |
| 318 | try: |
| 319 | gi = result.group(i) |
| 320 | # Special hack because else the string concat fails: |
| 321 | if gi is None: |
| 322 | gi = "None" |
| 323 | except IndexError: |
| 324 | gi = "Error" |
| 325 | vardict['g%d' % i] = gi |
| 326 | for i in result.re.groupindex.keys(): |
| 327 | try: |
| 328 | gi = result.group(i) |
| 329 | if gi is None: |
| 330 | gi = "None" |
| 331 | except IndexError: |
| 332 | gi = "Error" |
| 333 | vardict[i] = gi |
| 334 | repl=eval(repl, vardict) |
| 335 | if repl!=expected: |
| 336 | print '=== grouping error', t, |
| 337 | print repr(repl)+' should be '+repr(expected) |
| 338 | else: |
| 339 | print '=== Failed incorrectly', t |
Fredrik Lundh | 90a0791 | 2000-06-30 07:50:59 +0000 | [diff] [blame] | 340 | continue |
Fredrik Lundh | df02d0b | 2000-06-30 07:08:20 +0000 | [diff] [blame] | 341 | |
| 342 | # Try the match on a unicode string, and check that it |
| 343 | # still succeeds. |
Fredrik Lundh | 1c5aa69 | 2001-01-16 07:37:30 +0000 | [diff] [blame] | 344 | try: |
| 345 | u = unicode(s, "latin-1") |
| 346 | except NameError: |
| 347 | pass |
Fredrik Lundh | b25e1ad | 2001-03-22 15:50:10 +0000 | [diff] [blame] | 348 | except TypeError: |
| 349 | continue # skip unicode test strings |
Fredrik Lundh | 1c5aa69 | 2001-01-16 07:37:30 +0000 | [diff] [blame] | 350 | else: |
| 351 | result=obj.search(u) |
| 352 | if result==None: |
| 353 | print '=== Fails on unicode match', t |
Fredrik Lundh | df02d0b | 2000-06-30 07:08:20 +0000 | [diff] [blame] | 354 | |
| 355 | # Try the match on a unicode pattern, and check that it |
| 356 | # still succeeds. |
Fredrik Lundh | 1c5aa69 | 2001-01-16 07:37:30 +0000 | [diff] [blame] | 357 | try: |
| 358 | u = unicode(pattern, "latin-1") |
| 359 | except NameError: |
| 360 | pass |
| 361 | else: |
| 362 | obj=sre.compile(u) |
| 363 | result=obj.search(s) |
| 364 | if result==None: |
| 365 | print '=== Fails on unicode pattern match', t |
Fredrik Lundh | df02d0b | 2000-06-30 07:08:20 +0000 | [diff] [blame] | 366 | |
| 367 | # Try the match with the search area limited to the extent |
| 368 | # of the match and see if it still succeeds. \B will |
| 369 | # break (because it won't match at the end or start of a |
| 370 | # string), so we'll ignore patterns that feature it. |
Fredrik Lundh | 6f01398 | 2000-07-03 18:44:21 +0000 | [diff] [blame] | 371 | |
Fredrik Lundh | df02d0b | 2000-06-30 07:08:20 +0000 | [diff] [blame] | 372 | if pattern[:2]!='\\B' and pattern[-2:]!='\\B': |
| 373 | obj=sre.compile(pattern) |
Fredrik Lundh | 90a0791 | 2000-06-30 07:50:59 +0000 | [diff] [blame] | 374 | result=obj.search(s, result.start(0), result.end(0)+1) |
| 375 | if result==None: |
| 376 | print '=== Failed on range-limited match', t |
Fredrik Lundh | df02d0b | 2000-06-30 07:08:20 +0000 | [diff] [blame] | 377 | |
| 378 | # Try the match with IGNORECASE enabled, and check that it |
| 379 | # still succeeds. |
| 380 | obj=sre.compile(pattern, sre.IGNORECASE) |
| 381 | result=obj.search(s) |
| 382 | if result==None: |
| 383 | print '=== Fails on case-insensitive match', t |
| 384 | |
| 385 | # Try the match with LOCALE enabled, and check that it |
| 386 | # still succeeds. |
| 387 | obj=sre.compile(pattern, sre.LOCALE) |
| 388 | result=obj.search(s) |
| 389 | if result==None: |
| 390 | print '=== Fails on locale-sensitive match', t |
| 391 | |
Fredrik Lundh | c2ed621 | 2000-08-01 13:01:43 +0000 | [diff] [blame] | 392 | # Try the match with UNICODE locale enabled, and check |
| 393 | # that it still succeeds. |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 394 | if have_unicode: |
| 395 | obj=sre.compile(pattern, sre.UNICODE) |
| 396 | result=obj.search(s) |
| 397 | if result==None: |
| 398 | print '=== Fails on unicode-sensitive match', t |