Skip to content

Commit

Permalink
adding test and st for atomic and processive
Browse files Browse the repository at this point in the history
  • Loading branch information
Cheukting committed Jul 15, 2023
1 parent 36bb4ca commit f7e2e88
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,7 @@ def recurse(codes):
# Regex 'a|b|c' (branch)
return st.one_of([recurse(branch) for branch in value[1]])

elif code in [sre.MIN_REPEAT, sre.MAX_REPEAT]:
elif code in [sre.MIN_REPEAT, sre.MAX_REPEAT, sre.POSSESSIVE_REPEAT]:
# Regexes 'a?', 'a*', 'a+' and their non-greedy variants
# (repeaters)
at_least, at_most, subregex = value
Expand All @@ -494,6 +494,8 @@ def recurse(codes):
recurse(value[1]),
recurse(value[2]) if value[2] else st.just(empty),
)
elif code == sre.ATOMIC_GROUP:
return _strategy(value, context, is_unicode)

else:
# Currently there are no known code points other than handled here.
Expand Down
30 changes: 30 additions & 0 deletions hypothesis-python/tests/nocover/test_regex.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,36 @@ def test_fuzz_stuff(data):
assert regex.search(ex)


@given(st.data())
def test_regex_atomic_point(data):
pattern = "a(?>bc|b)c"
flags = 0

try:
regex = re.compile(pattern, flags=flags)
except (re.error, FutureWarning):
# Possible nested sets, e.g. "[[", trigger a FutureWarning
reject()

ex = data.draw(st.from_regex(regex))
assert regex.search(ex)


@given(st.data())
def test_regex_processive(data):
pattern = '"[^"]*+"'
flags = 0

try:
regex = re.compile(pattern, flags=flags)
except (re.error, FutureWarning):
# Possible nested sets, e.g. "[[", trigger a FutureWarning
reject()

ex = data.draw(st.from_regex(regex))
assert regex.search(ex)


# Some preliminaries, to establish what's happening:
I_WITH_DOT = "\u0130"
assert I_WITH_DOT.swapcase() == "i\u0307" # note: string of length two!
Expand Down

0 comments on commit f7e2e88

Please sign in to comment.