| 1 |
|
| 2 |
import re, py |
| 3 |
from rpython.rlib.rsre.test.test_match import get_code, get_code_and_re |
| 4 |
from rpython.rlib.rsre.test import support |
| 5 |
from rpython.rlib.rsre import rsre_core, rsre_utf8, rsre_char |
| 6 |
|
| 7 |
def setup_module(mod): |
| 8 |
from rpython.rlib.unicodedata import unicodedb |
| 9 |
rsre_char.set_unicode_db(unicodedb) |
| 10 |
|
| 11 |
|
| 12 |
class BaseTestSearch: |
| 13 |
|
| 14 |
def test_code1(self): |
| 15 |
r_code1 = get_code(r'[abc][def][ghi]') |
| 16 |
res = self.search(r_code1, "fooahedixxx") |
| 17 |
assert res is None |
| 18 |
res = self.search(r_code1, "fooahcdixxx") |
| 19 |
assert res is not None |
| 20 |
P = self.P |
| 21 |
assert res.span() == (P(5), P(8)) |
| 22 |
|
| 23 |
def test_code2(self): |
| 24 |
r_code2 = get_code(r'<item>\s*<title>(.*?)</title>') |
| 25 |
res = self.search(r_code2, "foo bar <item> <title>abc</title>def") |
| 26 |
assert res is not None |
| 27 |
P = self.P |
| 28 |
assert res.span() == (P(8), P(34)) |
| 29 |
|
| 30 |
def test_pure_literal(self): |
| 31 |
r_code3 = get_code(r'foobar') |
| 32 |
res = self.search(r_code3, "foo bar foobar baz") |
| 33 |
assert res is not None |
| 34 |
P = self.P |
| 35 |
assert res.span() == (P(8), P(14)) |
| 36 |
|
| 37 |
def test_code3(self): |
| 38 |
r_code1 = get_code(r'<item>\s*<title>(.*?)</title>') |
| 39 |
res = self.match(r_code1, "<item> <title>abc</title>def") |
| 40 |
assert res is not None |
| 41 |
|
| 42 |
def test_max_until_0_65535(self): |
| 43 |
r_code2 = get_code(r'<abc>(?:xy)*xy</abc>') |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
res = self.match(r_code2, '<abc>xyxyxy</abc>def') |
| 49 |
assert res is not None |
| 50 |
res = self.match(r_code2, '<abc>' + 'xy'*1000 + '</abc>def') |
| 51 |
assert res is not None |
| 52 |
|
| 53 |
def test_max_until_3_5(self): |
| 54 |
r_code2, r = get_code_and_re(r'<abc>(?:xy){3,5}xy</abc>') |
| 55 |
for i in range(8): |
| 56 |
s = '<abc>' + 'xy'*i + '</abc>defdefdefdefdef' |
| 57 |
assert (r.match(s) is not None) is (3 <= i-1 <= 5) |
| 58 |
res = self.match(r_code2, s) |
| 59 |
assert (res is not None) is (3 <= i-1 <= 5) |
| 60 |
|
| 61 |
def test_min_until_0_65535(self): |
| 62 |
r_code2 = get_code(r'<abc>(?:xy)*?xy</abc>') |
| 63 |
res = self.match(r_code2, '<abc></abc>def') |
| 64 |
assert res is None |
| 65 |
res = self.match(r_code2, '<abc>xy</abc>def') |
| 66 |
assert res is not None |
| 67 |
res = self.match(r_code2, '<abc>xyxyxy</abc>def') |
| 68 |
assert res is not None |
| 69 |
res = self.match(r_code2, '<abc>' + 'xy'*1000 + '</abc>def') |
| 70 |
assert res is not None |
| 71 |
|
| 72 |
def test_min_until_3_5(self): |
| 73 |
r_code2, r = get_code_and_re(r'<abc>(?:xy){3,5}?xy</abc>') |
| 74 |
for i in range(8): |
| 75 |
s = '<abc>' + 'xy'*i + '</abc>defdefdefdefdef' |
| 76 |
assert (r.match(s) is not None) is (3 <= i-1 <= 5) |
| 77 |
res = self.match(r_code2, s) |
| 78 |
assert (res is not None) is (3 <= i-1 <= 5) |
| 79 |
|
| 80 |
def test_min_repeat_one(self): |
| 81 |
r_code3 = get_code(r'<abc>.{3,5}?y') |
| 82 |
for i in range(8): |
| 83 |
res = self.match(r_code3, '<abc>' + 'x'*i + 'y') |
| 84 |
assert (res is not None) is (3 <= i <= 5) |
| 85 |
|
| 86 |
def test_simple_group(self): |
| 87 |
r_code4 = get_code(r'<abc>(x.)</abc>') |
| 88 |
res = self.match(r_code4, '<abc>xa</abc>def') |
| 89 |
assert res is not None |
| 90 |
P = self.P |
| 91 |
assert res.get_mark(0) == P(5) |
| 92 |
assert res.get_mark(1) == P(7) |
| 93 |
|
| 94 |
def test_max_until_groups(self): |
| 95 |
r_code4 = get_code(r'<abc>(x.)*xy</abc>') |
| 96 |
res = self.match(r_code4, '<abc>xaxbxy</abc>def') |
| 97 |
assert res is not None |
| 98 |
P = self.P |
| 99 |
assert res.get_mark(0) == P(7) |
| 100 |
assert res.get_mark(1) == P(9) |
| 101 |
|
| 102 |
def test_group_branch(self): |
| 103 |
r_code5 = get_code(r'<abc>(ab|c)</abc>') |
| 104 |
res = self.match(r_code5, '<abc>ab</abc>def') |
| 105 |
P = self.P |
| 106 |
assert (res.get_mark(0), res.get_mark(1)) == (P(5), P(7)) |
| 107 |
res = self.match(r_code5, '<abc>c</abc>def') |
| 108 |
assert (res.get_mark(0), res.get_mark(1)) == (P(5), P(6)) |
| 109 |
res = self.match(r_code5, '<abc>de</abc>def') |
| 110 |
assert res is None |
| 111 |
|
| 112 |
def test_group_branch_max_until(self): |
| 113 |
r_code6 = get_code(r'<abc>(ab|c)*a</abc>') |
| 114 |
res = self.match(r_code6, '<abc>ccabcccaba</abc>def') |
| 115 |
P = self.P |
| 116 |
assert (res.get_mark(0), res.get_mark(1)) == (P(12), P(14)) |
| 117 |
r_code7 = get_code(r'<abc>((ab)|(c))*a</abc>') |
| 118 |
res = self.match(r_code7, '<abc>ccabcccaba</abc>def') |
| 119 |
assert (res.get_mark(0), res.get_mark(1)) == (P(12), P(14)) |
| 120 |
assert (res.get_mark(2), res.get_mark(3)) == (P(12), P(14)) |
| 121 |
assert (res.get_mark(4), res.get_mark(5)) == (P(11), P(12)) |
| 122 |
|
| 123 |
def test_group_7(self): |
| 124 |
r_code7, r7 = get_code_and_re(r'<abc>((a)?(b))*</abc>') |
| 125 |
m = r7.match('<abc>bbbabbbb</abc>') |
| 126 |
assert m.span(1) == (12, 13) |
| 127 |
assert m.span(3) == (12, 13) |
| 128 |
assert m.span(2) == (8, 9) |
| 129 |
res = self.match(r_code7, '<abc>bbbabbbb</abc>') |
| 130 |
P = self.P |
| 131 |
assert (res.get_mark(0), res.get_mark(1)) == (P(12), P(13)) |
| 132 |
assert (res.get_mark(4), res.get_mark(5)) == (P(12), P(13)) |
| 133 |
assert (res.get_mark(2), res.get_mark(3)) == (P(8), P(9)) |
| 134 |
|
| 135 |
def test_group_branch_repeat_complex_case(self): |
| 136 |
r_code8, r8 = get_code_and_re(r'<abc>((a)|(b))*</abc>') |
| 137 |
m = r8.match('<abc>ab</abc>') |
| 138 |
assert m.span(1) == (6, 7) |
| 139 |
assert m.span(3) == (6, 7) |
| 140 |
assert m.span(2) == (5, 6) |
| 141 |
res = self.match(r_code8, '<abc>ab</abc>') |
| 142 |
P = self.P |
| 143 |
assert (res.get_mark(0), res.get_mark(1)) == (P(6), P(7)) |
| 144 |
assert (res.get_mark(4), res.get_mark(5)) == (P(6), P(7)) |
| 145 |
assert (res.get_mark(2), res.get_mark(3)) == (P(5), P(6)) |
| 146 |
|
| 147 |
def test_minuntil_lastmark_restore(self): |
| 148 |
r_code9, r9 = get_code_and_re(r'(x|yz)+?(y)??c') |
| 149 |
m = r9.match('xyzxc') |
| 150 |
assert m.span(1) == (3, 4) |
| 151 |
assert m.span(2) == (-1, -1) |
| 152 |
res = self.match(r_code9, 'xyzxc') |
| 153 |
P = self.P |
| 154 |
assert (res.get_mark(0), res.get_mark(1)) == (P(3), P(4)) |
| 155 |
assert (res.get_mark(2), res.get_mark(3)) == (-1, -1) |
| 156 |
|
| 157 |
def test_minuntil_bug(self): |
| 158 |
r_code9, r9 = get_code_and_re(r'((x|yz)+?(y)??c)*') |
| 159 |
m = r9.match('xycxyzxc') |
| 160 |
assert m.span(2) == (6, 7) |
| 161 |
|
| 162 |
res = self.match(r_code9, 'xycxyzxc') |
| 163 |
P = self.P |
| 164 |
assert (res.get_mark(2), res.get_mark(3)) == (P(6), P(7)) |
| 165 |
assert (res.get_mark(4), res.get_mark(5)) == (P(1), P(2)) |
| 166 |
|
| 167 |
def test_empty_maxuntil(self): |
| 168 |
r_code, r = get_code_and_re(r'(a?)+y') |
| 169 |
assert r.match('y') |
| 170 |
assert r.match('aaayaaay').span() == (0, 4) |
| 171 |
res = self.match(r_code, 'y') |
| 172 |
assert res |
| 173 |
res = self.match(r_code, 'aaayaaay') |
| 174 |
P = self.P |
| 175 |
assert res and res.span() == (P(0), P(4)) |
| 176 |
|
| 177 |
r_code, r = get_code_and_re(r'(a?){4,6}y') |
| 178 |
assert r.match('y') |
| 179 |
res = self.match(r_code, 'y') |
| 180 |
assert res |
| 181 |
|
| 182 |
r_code, r = get_code_and_re(r'(a?)*y') |
| 183 |
assert r.match('y') |
| 184 |
res = self.match(r_code, 'y') |
| 185 |
assert res |
| 186 |
|
| 187 |
def test_empty_maxuntil_2(self): |
| 188 |
try: |
| 189 |
r_code, r = get_code_and_re(r'X(.*?)+X') |
| 190 |
except re.error as e: |
| 191 |
py.test.skip("older version of the stdlib: %s" % (e,)) |
| 192 |
assert r.match('XfooXbarX').span() == (0, 5) |
| 193 |
assert r.match('XfooXbarX').span(1) == (4, 4) |
| 194 |
res = self.match(r_code, 'XfooXbarX') |
| 195 |
P = self.P |
| 196 |
assert res.span() == (P(0), P(5)) |
| 197 |
assert res.span(1) == (P(4), P(4)) |
| 198 |
|
| 199 |
def test_empty_minuntil(self): |
| 200 |
r_code, r = get_code_and_re(r'(a?)+?y') |
| 201 |
|
| 202 |
res = self.match(r_code, 'z') |
| 203 |
assert not res |
| 204 |
|
| 205 |
r_code, r = get_code_and_re(r'(a?){4,6}?y') |
| 206 |
assert not r.match('z') |
| 207 |
res = self.match(r_code, 'z') |
| 208 |
assert not res |
| 209 |
|
| 210 |
r_code, r = get_code_and_re(r'(a?)*?y') |
| 211 |
|
| 212 |
res = self.match(r_code, 'z') |
| 213 |
assert not res |
| 214 |
|
| 215 |
def test_empty_search(self): |
| 216 |
r_code, r = get_code_and_re(r'') |
| 217 |
for j in range(-2, 6): |
| 218 |
for i in range(-2, 6): |
| 219 |
match = r.search('abc', i, j) |
| 220 |
res = self.search(r_code, 'abc', i, j) |
| 221 |
jk = min(max(j, 0), 3) |
| 222 |
ik = min(max(i, 0), 3) |
| 223 |
if ik <= jk: |
| 224 |
assert match is not None |
| 225 |
assert match.span() == (ik, ik) |
| 226 |
assert res is not None |
| 227 |
assert res.match_start == self.P(ik) |
| 228 |
assert res.match_end == self.P(ik) |
| 229 |
else: |
| 230 |
|
| 231 |
assert res is None |
| 232 |
|
| 233 |
|
| 234 |
class TestSearchCustom(BaseTestSearch): |
| 235 |
search = staticmethod(support.search) |
| 236 |
match = staticmethod(support.match) |
| 237 |
P = support.Position |
| 238 |
|
| 239 |
class TestSearchStr(BaseTestSearch): |
| 240 |
search = staticmethod(rsre_core.search) |
| 241 |
match = staticmethod(rsre_core.match) |
| 242 |
P = staticmethod(lambda n: n) |
| 243 |
|
| 244 |
class TestSearchUtf8(BaseTestSearch): |
| 245 |
search = staticmethod(rsre_utf8.utf8search) |
| 246 |
match = staticmethod(rsre_utf8.utf8match) |
| 247 |
P = staticmethod(lambda n: n) |
| 248 |
|
| 249 |
def test_groupref_unicode_bug(self): |
| 250 |
r = get_code(ur"(üü+)\1+$", re.UNICODE) ü |
| 251 |
assert not self.match(r, u"üü".encode("utf-8")) |
| 252 |
assert not self.match(r, u"üüü".encode("utf-8")) |
| 253 |
assert self.match(r, u"üüüü".encode("utf-8")) |
| 254 |
assert not self.match(r, u"üüüüü".encode("utf-8")) |
| 255 |
assert self.match(r, u"üüüüüü".encode("utf-8")) |
| 256 |
assert not self.match(r, u"üüüüüüü".encode("utf-8")) |
| 257 |
assert self.match(r, u"üüüüüüüü".encode("utf-8")) |
| 258 |
assert self.match(r, u"üüüüüüüüü".encode("utf-8")) |
| 259 |
|
| 260 |
def test_literal_uni_ignore(self): |
| 261 |
r = get_code(u"(?i)\u0135") |
| 262 |
assert self.match(r, u'\u0134'.encode('utf-8')) |
| 263 |
assert self.match(r, u'\u0134'.lower().encode('utf-8')) |
| 264 |
assert not self.match(r, "a") |
| 265 |
assert not self.match(r, "A") |
| 266 |
|
| 267 |
def test_literal_uni_ignore_repeat_one(self): |
| 268 |
r = get_code(u"(?i)ab*c") |
| 269 |
assert self.match(r, 'ac') |
| 270 |
assert self.match(r, 'abc') |
| 271 |
assert self.match(r, 'abbbbbc') |
| 272 |
assert not self.match(r, 'a') |
| 273 |
assert not self.match(r, 'axc') |
| 274 |
assert not self.match(r, 'c') |
| 275 |
|
| 276 |
def test_in_uni_ignore_repeat_one(self): |
| 277 |
r = get_code(u"(?i)[^ab]*$") |
| 278 |
assert self.match(r, u'zzstzc'.encode('utf-8')) |
| 279 |
assert self.match(r, u'c'.encode('utf-8')) |
| 280 |
assert self.match(r, u'üüü\u0134c'.encode('utf-8')) |
| 281 |
assert not self.match(r, 'cccca') |
| 282 |
assert not self.match(r, 'bc') |
| 283 |
assert not self.match(r, 'b') |
| 284 |
|