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