-
Notifications
You must be signed in to change notification settings - Fork 575
/
test_regex.py
482 lines (364 loc) · 13.1 KB
/
test_regex.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
# coding=utf-8
#
# This file is part of Hypothesis, which may be found at
# https://github.com/HypothesisWorks/hypothesis/
#
# Most of this work is copyright (C) 2013-2019 David R. MacIver
# (david@drmaciver.com), but it contains contributions by others. See
# CONTRIBUTING.rst for a full list of people who may hold copyright, and
# consult the git log if you need to determine who owns an individual
# contribution.
#
# This Source Code Form is subject to the terms of the Mozilla Public License,
# v. 2.0. If a copy of the MPL was not distributed with this file, You can
# obtain one at https://mozilla.org/MPL/2.0/.
#
# END HEADER
from __future__ import absolute_import, division, print_function
import re
import sys
import unicodedata
import pytest
import hypothesis.strategies as st
from hypothesis import assume, given, settings
from hypothesis.errors import InvalidArgument
from hypothesis.internal.compat import PY2, PY3, PYPY, hrange, hunichr
from hypothesis.searchstrategy.regex import (
SPACE_CHARS,
UNICODE_DIGIT_CATEGORIES,
UNICODE_SPACE_CATEGORIES,
UNICODE_SPACE_CHARS,
UNICODE_WEIRD_NONWORD_CHARS,
UNICODE_WORD_CATEGORIES,
base_regex_strategy,
)
from tests.common.debug import assert_all_examples, assert_no_examples, find_any
def is_ascii(s):
return all(ord(c) < 128 for c in s)
def is_digit(s):
return all(unicodedata.category(c) in UNICODE_DIGIT_CATEGORIES for c in s)
def is_space(s):
return all(c in SPACE_CHARS for c in s)
def is_unicode_space(s):
return all(
unicodedata.category(c) in UNICODE_SPACE_CATEGORIES or c in UNICODE_SPACE_CHARS
for c in s
)
def is_word(s):
return all(
c == "_"
or (
(PY3 or c not in UNICODE_WEIRD_NONWORD_CHARS)
and unicodedata.category(c) in UNICODE_WORD_CATEGORIES
)
for c in s
)
def ascii_regex(pattern):
flags = re.ASCII if PY3 else 0
return re.compile(pattern, flags)
def unicode_regex(pattern):
return re.compile(pattern, re.UNICODE)
def _test_matching_pattern(pattern, isvalidchar, is_unicode=False):
r = unicode_regex(pattern) if is_unicode else ascii_regex(pattern)
codepoints = hrange(0, sys.maxunicode + 1) if is_unicode else hrange(1, 128)
for c in [hunichr(x) for x in codepoints]:
if isvalidchar(c):
assert r.search(c), (
'"%s" supposed to match "%s" (%r, category "%s"), '
"but it doesn't" % (pattern, c, c, unicodedata.category(c))
)
else:
assert not r.search(c), (
'"%s" supposed not to match "%s" (%r, category "%s"), '
"but it does" % (pattern, c, c, unicodedata.category(c))
)
@pytest.mark.parametrize(
"category,predicate", [(r"\w", is_word), (r"\d", is_digit), (r"\s", None)]
)
@pytest.mark.parametrize("invert", [False, True])
@pytest.mark.parametrize("is_unicode", [False, True])
def test_matching(category, predicate, invert, is_unicode):
if predicate is None:
# Special behaviour due to \x1c, INFORMATION SEPARATOR FOUR
predicate = is_unicode_space if is_unicode else is_space
if invert:
category = category.swapcase()
def pred(s):
return not predicate(s)
else:
pred = predicate
_test_matching_pattern(category, pred, is_unicode)
@pytest.mark.parametrize(
"pattern",
[
u".", # anything
u"a",
u"abc",
u"[a][b][c]",
u"[^a][^b][^c]", # literals
u"[a-z0-9_]",
u"[^a-z0-9_]", # range and negative range
u"ab?",
u"ab*",
u"ab+", # quantifiers
u"ab{5}",
u"ab{5,10}",
u"ab{,10}",
u"ab{5,}", # repeaters
u"ab|cd|ef", # branch
u"(foo)+",
u"(['\"])[a-z]+\\1",
u"(?:[a-z])(['\"])[a-z]+\\1",
u"(?P<foo>['\"])[a-z]+(?P=foo)", # groups
u"^abc", # beginning
u"\\d",
u"[\\d]",
u"[^\\D]",
u"\\w",
u"[\\w]",
u"[^\\W]",
u"\\s",
u"[\\s]",
u"[^\\S]", # categories
],
)
@pytest.mark.parametrize("encode", [False, True])
def test_can_generate(pattern, encode):
if encode:
pattern = pattern.encode("ascii")
assert_all_examples(st.from_regex(pattern), re.compile(pattern).search)
@pytest.mark.parametrize(
"pattern",
[
re.compile(u"\\Aa\\Z", re.IGNORECASE),
u"(?i)\\Aa\\Z",
re.compile(u"\\A[ab]\\Z", re.IGNORECASE),
u"(?i)\\A[ab]\\Z",
],
)
def test_literals_with_ignorecase(pattern):
strategy = st.from_regex(pattern)
find_any(strategy, lambda s: s == u"a")
find_any(strategy, lambda s: s == u"A")
@pytest.mark.parametrize(
"pattern", [re.compile(u"\\A[^a][^b]\\Z", re.IGNORECASE), u"(?i)\\A[^a][^b]\\Z"]
)
def test_not_literal_with_ignorecase(pattern):
assert_all_examples(
st.from_regex(pattern),
lambda s: s[0] not in (u"a", u"A") and s[1] not in (u"b", u"B"),
)
def test_any_doesnt_generate_newline():
assert_all_examples(st.from_regex(u"\\A.\\Z"), lambda s: s != u"\n")
@pytest.mark.parametrize("pattern", [re.compile(u"\\A.\\Z", re.DOTALL), u"(?s)\\A.\\Z"])
def test_any_with_dotall_generate_newline(pattern):
find_any(
st.from_regex(pattern), lambda s: s == u"\n", settings(max_examples=10 ** 6)
)
@pytest.mark.parametrize("pattern", [re.compile(b"\\A.\\Z", re.DOTALL), b"(?s)\\A.\\Z"])
def test_any_with_dotall_generate_newline_binary(pattern):
find_any(
st.from_regex(pattern), lambda s: s == b"\n", settings(max_examples=10 ** 6)
)
@pytest.mark.parametrize(
"pattern",
[
u"\\d",
u"[\\d]",
u"[^\\D]",
u"\\w",
u"[\\w]",
u"[^\\W]",
u"\\s",
u"[\\s]",
u"[^\\S]",
],
)
@pytest.mark.parametrize("is_unicode", [False, True])
@pytest.mark.parametrize("invert", [False, True])
def test_groups(pattern, is_unicode, invert):
if u"d" in pattern.lower():
group_pred = is_digit
elif u"w" in pattern.lower():
group_pred = is_word
else:
# Special behaviour due to \x1c, INFORMATION SEPARATOR FOUR
group_pred = is_unicode_space if is_unicode else is_space
if invert:
pattern = pattern.swapcase()
_p = group_pred
def group_pred(s): # pylint:disable=function-redefined
return not _p(s)
pattern = u"^%s\\Z" % (pattern,)
compiler = unicode_regex if is_unicode else ascii_regex
strategy = st.from_regex(compiler(pattern))
find_any(strategy.filter(group_pred), is_ascii)
if is_unicode:
find_any(strategy, lambda s: group_pred(s) and not is_ascii(s))
assert_all_examples(strategy, group_pred)
def test_caret_in_the_middle_does_not_generate_anything():
r = re.compile(u"a^b")
assert_no_examples(st.from_regex(r))
def test_end_with_terminator_does_not_pad():
assert_all_examples(st.from_regex(u"abc\\Z"), lambda x: x[-3:] == u"abc")
def test_end():
strategy = st.from_regex(u"\\Aabc$")
find_any(strategy, lambda s: s == u"abc")
find_any(strategy, lambda s: s == u"abc\n")
def test_groupref_exists():
assert_all_examples(
st.from_regex(u"^(<)?a(?(1)>)$"),
lambda s: s in (u"a", u"a\n", u"<a>", u"<a>\n"),
)
assert_all_examples(
st.from_regex(u"^(a)?(?(1)b|c)$"), lambda s: s in (u"ab", u"ab\n", u"c", u"c\n")
)
def test_impossible_negative_lookahead():
assert_no_examples(st.from_regex(u"(?!foo)foo"))
@given(st.from_regex(u"(\\Afoo\\Z)"))
def test_can_handle_boundaries_nested(s):
assert s == u"foo"
def test_groupref_not_shared_between_regex():
# If group references are (incorrectly!) shared between regex, this would
# fail as the would only be one reference.
st.tuples(st.from_regex("(a)\\1"), st.from_regex("(b)\\1")).example()
@pytest.mark.skipif(
PYPY or PY2, # Skip for now so we can test the rest
reason=r"Triggers bugs in poor handling of unicode in re for these implementations",
)
@given(st.data())
def test_group_ref_is_not_shared_between_identical_regex(data):
pattern = re.compile(u"^(.+)\\1\\Z", re.UNICODE)
x = data.draw(base_regex_strategy(pattern))
y = data.draw(base_regex_strategy(pattern))
assume(x != y)
assert pattern.match(x).end() == len(x)
assert pattern.match(y).end() == len(y)
@given(st.data())
def test_does_not_leak_groups(data):
a = data.draw(base_regex_strategy(re.compile(u"^(a)\\Z")))
assert a == "a"
b = data.draw(base_regex_strategy(re.compile(u"^(?(1)a|b)(.)\\Z")))
assert b[0] == "b"
def test_positive_lookbehind():
find_any(st.from_regex(u".*(?<=ab)c"), lambda s: s.endswith(u"abc"))
def test_positive_lookahead():
st.from_regex(u"a(?=bc).*").filter(lambda s: s.startswith(u"abc")).example()
def test_negative_lookbehind():
# no efficient support
strategy = st.from_regex(u"[abc]*(?<!abc)d")
assert_all_examples(strategy, lambda s: not s.endswith(u"abcd"))
assert_no_examples(strategy, lambda s: s.endswith(u"abcd"))
def test_negative_lookahead():
# no efficient support
strategy = st.from_regex(u"^ab(?!cd)[abcd]*")
assert_all_examples(strategy, lambda s: not s.startswith(u"abcd"))
assert_no_examples(strategy, lambda s: s.startswith(u"abcd"))
@given(st.from_regex(u"^a+\\Z"))
def test_generates_only_the_provided_characters_given_boundaries(xs):
assert set(xs) == {u"a"}
@given(st.from_regex(u"^(.)?\\1\\Z"))
def test_group_backref_may_not_be_present(s):
assert len(s) == 2
assert s[0] == s[1]
@pytest.mark.skipif(sys.version_info[:2] < (3, 6), reason="requires Python 3.6")
def test_subpattern_flags():
strategy = st.from_regex(u"(?i)\\Aa(?-i:b)\\Z")
# "a" is case insensitive
find_any(strategy, lambda s: s[0] == u"a")
find_any(strategy, lambda s: s[0] == u"A")
# "b" is case sensitive
find_any(strategy, lambda s: s[1] == u"b")
assert_no_examples(strategy, lambda s: s[1] == u"B")
def test_can_handle_binary_regex_which_is_not_ascii():
bad = b"\xad"
assert_all_examples(st.from_regex(bad), lambda x: bad in x)
@pytest.mark.parametrize("pattern", [b".", u"."])
def test_regex_have_same_type_as_pattern(pattern):
@given(st.from_regex(pattern))
def test_result_type(s):
assert type(s) == type(pattern)
test_result_type()
def test_can_pad_strings_arbitrarily():
find_any(st.from_regex(u"a"), lambda x: x[0] != u"a")
find_any(st.from_regex(u"a"), lambda x: x[-1] != u"a")
def test_can_pad_empty_strings():
find_any(st.from_regex(u""), bool)
find_any(st.from_regex(b""), bool)
def test_can_pad_strings_with_newlines():
find_any(st.from_regex(u"^$"), bool)
find_any(st.from_regex(b"^$"), bool)
def test_given_multiline_regex_can_insert_after_dollar():
find_any(
st.from_regex(re.compile(u"\\Ahi$", re.MULTILINE)),
lambda x: "\n" in x and x.split(u"\n")[1],
)
def test_given_multiline_regex_can_insert_before_caret():
find_any(
st.from_regex(re.compile(u"^hi\\Z", re.MULTILINE)),
lambda x: "\n" in x and x.split(u"\n")[0],
)
def test_does_not_left_pad_beginning_of_string_marker():
assert_all_examples(st.from_regex(u"\\Afoo"), lambda x: x.startswith(u"foo"))
def test_bare_caret_can_produce():
find_any(st.from_regex(u"^"), bool)
def test_bare_dollar_can_produce():
find_any(st.from_regex(u"$"), bool)
def test_shared_union():
# This gets parsed as [(ANY, None), (BRANCH, (None, [[], []]))], the
# interesting feature of which is that it contains empty sub-expressions
# in the branch.
find_any(st.from_regex(".|."))
@given(st.data())
def test_issue_992_regression(data):
strat = st.from_regex(
re.compile(
r"""\d + # the integral part
\. # the decimal point
\d * # some fractional digits""",
re.VERBOSE,
)
)
data.draw(strat)
@pytest.mark.parametrize(
"pattern,matching_str",
[
(u"a", u"a"),
(u"[Aa]", u"A"),
(u"[ab]*", u"abb"),
(b"[Aa]", b"A"),
(b"[ab]*", b"abb"),
(re.compile(u"[ab]*", re.IGNORECASE), u"aBb"),
(re.compile(b"[ab]", re.IGNORECASE), b"A"),
],
)
def test_fullmatch_generates_example(pattern, matching_str):
find_any(
st.from_regex(pattern, fullmatch=True),
lambda s: s == matching_str,
settings(max_examples=10 ** 6),
)
@pytest.mark.parametrize(
"pattern,eqiv_pattern",
[
(u"a", u"\\Aa\\Z"),
(u"[Aa]", u"\\A[Aa]\\Z"),
(u"[ab]*", u"\\A[ab]*\\Z"),
(b"[Aa]", br"\A[Aa]\Z"),
(b"[ab]*", br"\A[ab]*\Z"),
(
re.compile(u"[ab]*", re.IGNORECASE),
re.compile(u"\\A[ab]*\\Z", re.IGNORECASE),
),
(re.compile(br"[ab]", re.IGNORECASE), re.compile(br"\A[ab]\Z", re.IGNORECASE)),
],
)
def test_fullmatch_matches(pattern, eqiv_pattern):
assert_all_examples(
st.from_regex(pattern, fullmatch=True), lambda s: re.match(eqiv_pattern, s)
)
def test_fullmatch_must_be_bool():
with pytest.raises(InvalidArgument):
st.from_regex("a", fullmatch=None).validate()
def test_issue_1786_regression():
st.from_regex(re.compile("\\\\", flags=re.IGNORECASE)).validate()