Skip to content

Commit

Permalink
tests + release
Browse files Browse the repository at this point in the history
  • Loading branch information
reaganjlee committed Dec 28, 2023
1 parent 19e129f commit ba9ee3c
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
3 changes: 3 additions & 0 deletions hypothesis-python/RELEASE.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
RELEASE_TYPE: minor

This adds support for filter-rewriting for length filters. You can now re-write text strategies with length constraints with :func:`partial` and :func:`lambda` functions
71 changes: 71 additions & 0 deletions hypothesis-python/tests/cover/test_filter_rewriting.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@

from hypothesis import given, strategies as st
from hypothesis.errors import HypothesisWarning, Unsatisfiable
from hypothesis.internal.filtering import max_len, min_len
from hypothesis.internal.floats import next_down, next_up
from hypothesis.internal.reflection import get_pretty_function_description
from hypothesis.strategies._internal.core import data
from hypothesis.strategies._internal.lazy import LazyStrategy, unwrap_strategies
from hypothesis.strategies._internal.numbers import FloatStrategy, IntegersStrategy
from hypothesis.strategies._internal.strategies import FilteredStrategy
from hypothesis.strategies._internal.strings import TextStrategy

from tests.common.utils import fails_with

Expand Down Expand Up @@ -383,3 +385,72 @@ def test_isidentifer_filter_unsatisfiable(al):
def test_filter_floats_can_skip_subnormals(op, attr, value, expected):
base = st.floats(allow_subnormal=False).filter(partial(op, value))
assert getattr(base.wrapped_strategy, attr) == expected


@pytest.mark.parametrize(
"strategy, predicate, start, end",
[
# text with integer bounds
(st.text(min_size=1, max_size=5), partial(min_len, 3), 3, 5),
(st.text(min_size=1, max_size=5), partial(max_len, 3), 1, 3),
# text with only one bound
(st.text(min_size=1), partial(min_len, 3), 3, math.inf),
(st.text(min_size=1), partial(max_len, 3), 1, 3),
(st.text(max_size=5), partial(min_len, 3), 3, 5),
(st.text(max_size=5), partial(max_len, 3), 0, 3),
# Unbounded text
(st.text(), partial(min_len, 3), 3, math.inf),
(st.text(), partial(max_len, 3), 0, 3),
],
)
@given(data=st.data())
def test_filter_rewriting_text_partial_len(data, strategy, predicate, start, end):
s = strategy.filter(predicate)

assert isinstance(s, LazyStrategy)
assert isinstance(s.wrapped_strategy, TextStrategy)
assert s.wrapped_strategy.min_size == start
assert s.wrapped_strategy.max_size == end
value = data.draw(s)
assert predicate(value)


@pytest.mark.parametrize(
"strategy, predicate, start, end",
[
# Simple lambdas
(st.text(), lambda x: len(x) < 3, 0, 2),
(st.text(), lambda x: len(x) <= 3, 0, 3),
(st.text(), lambda x: len(x) == 3, 3, 3),
(st.text(), lambda x: len(x) >= 3, 3, math.inf),
(st.text(), lambda x: len(x) > 3, 4, math.inf),
# Simple lambdas, reverse comparison
(st.text(), lambda x: 3 > len(x), 0, 2),
(st.text(), lambda x: 3 >= len(x), 0, 3),
(st.text(), lambda x: 3 == len(x), 3, 3),
(st.text(), lambda x: 3 <= len(x), 3, math.inf),
(st.text(), lambda x: 3 < len(x), 4, math.inf),
# More complicated lambdas
(st.text(), lambda x: 0 < len(x) < 5, 1, 4),
(st.text(), lambda x: 0 < len(x) >= 1, 1, math.inf),
(st.text(), lambda x: 1 > len(x) <= 0, 0, 0),
(st.text(), lambda x: len(x) > 0 and len(x) > 0, 1, math.inf),
(st.text(), lambda x: len(x) < 1 and len(x) < 1, 0, 0),
(st.text(), lambda x: len(x) > 1 and len(x) > 0, 2, math.inf),
(st.text(), lambda x: len(x) < 1 and len(x) < 2, 0, 0),
],
ids=get_pretty_function_description,
)
@given(data=st.data())
def test_filter_rewriting_text_lambda_len(data, strategy, predicate, start, end):
s = strategy.filter(predicate)
unwrapped = s.wrapped_strategy
assert isinstance(unwrapped, FilteredStrategy)
assert isinstance(unwrapped.filtered_strategy, TextStrategy)
for pred in unwrapped.flat_conditions:
assert pred.__name__ == "<lambda>"

assert unwrapped.filtered_strategy.min_size == start
assert unwrapped.filtered_strategy.max_size == end
value = data.draw(s)
assert predicate(value)

0 comments on commit ba9ee3c

Please sign in to comment.