| 1 |
# This file is part of Hypothesis, which may be found at |
| 2 |
# https://github.com/HypothesisWorks/hypothesis/ |
| 3 |
# |
| 4 |
# Copyright the Hypothesis Authors. |
| 5 |
# Individual contributors are listed in AUTHORS.rst and the git log. |
| 6 |
# |
| 7 |
# This Source Code Form is subject to the terms of the Mozilla Public License, |
| 8 |
# v. 2.0. If a copy of the MPL was not distributed with this file, You can |
| 9 |
# obtain one at https://mozilla.org/MPL/2.0/. |
| 10 |
|
| 11 |
import datetime as dt |
| 12 |
import decimal |
| 13 |
import math |
| 14 |
import operator |
| 15 |
import re |
| 16 |
import sys |
| 17 |
from fractions import Fraction |
| 18 |
from functools import partial |
| 19 |
from sys import float_info |
| 20 |
|
| 21 |
import pytest |
| 22 |
|
| 23 |
from hypothesis import HealthCheck, given, settings, strategies as st |
| 24 |
from hypothesis.errors import HypothesisWarning, Unsatisfiable |
| 25 |
from hypothesis.internal.conjecture.providers import COLLECTION_DEFAULT_MAX_SIZE |
| 26 |
from hypothesis.internal.filtering import max_len, min_len |
| 27 |
from hypothesis.internal.floats import next_down, next_up |
| 28 |
from hypothesis.internal.reflection import get_pretty_function_description |
| 29 |
from hypothesis.strategies._internal.core import data |
| 30 |
from hypothesis.strategies._internal.lazy import LazyStrategy, unwrap_strategies |
| 31 |
from hypothesis.strategies._internal.numbers import FloatStrategy, IntegersStrategy |
| 32 |
from hypothesis.strategies._internal.strategies import FilteredStrategy, MappedStrategy |
| 33 |
from hypothesis.strategies._internal.strings import BytesStrategy, TextStrategy |
| 34 |
|
| 35 |
from tests.common.debug import check_can_generate_examples |
| 36 |
from tests.common.utils import fails_with |
| 37 |
|
| 38 |
A_FEW = 15 # speed up massively-parametrized tests |
| 39 |
|
| 40 |
|
| 41 |
@pytest.mark.parametrize( |
| 42 |
"strategy, predicate, start, end", |
| 43 |
[ |
| 44 |
# Finitude check |
| 45 |
(st.integers(1, 5), math.isfinite, 1, 5), |
| 46 |
# Integers with integer bounds |
| 47 |
(st.integers(1, 5), partial(operator.lt, 3), 4, 5), # lambda x: 3 < x |
| 48 |
(st.integers(1, 5), partial(operator.le, 3), 3, 5), # lambda x: 3 <= x |
| 49 |
(st.integers(1, 5), partial(operator.eq, 3), 3, 3), # lambda x: 3 == x |
| 50 |
(st.integers(1, 5), partial(operator.ge, 3), 1, 3), # lambda x: 3 >= x |
| 51 |
(st.integers(1, 5), partial(operator.gt, 3), 1, 2), # lambda x: 3 > x |
| 52 |
# Integers with non-integer bounds |
| 53 |
(st.integers(1, 5), partial(operator.lt, 3.5), 4, 5), |
| 54 |
(st.integers(1, 5), partial(operator.le, 3.5), 4, 5), |
| 55 |
(st.integers(1, 5), partial(operator.ge, 3.5), 1, 3), |
| 56 |
(st.integers(1, 5), partial(operator.gt, 3.5), 1, 3), |
| 57 |
(st.integers(1, 5), partial(operator.lt, -math.inf), 1, 5), |
| 58 |
(st.integers(1, 5), partial(operator.gt, math.inf), 1, 5), |
| 59 |
# Integers with only one bound |
| 60 |
(st.integers(min_value=1), partial(operator.lt, 3), 4, None), |
| 61 |
(st.integers(min_value=1), partial(operator.le, 3), 3, None), |
| 62 |
(st.integers(max_value=5), partial(operator.ge, 3), None, 3), |
| 63 |
(st.integers(max_value=5), partial(operator.gt, 3), None, 2), |
| 64 |
# Unbounded integers |
| 65 |
(st.integers(), partial(operator.lt, 3), 4, None), |
| 66 |
(st.integers(), partial(operator.le, 3), 3, None), |
| 67 |
(st.integers(), partial(operator.eq, 3), 3, 3), |
| 68 |
(st.integers(), partial(operator.ge, 3), None, 3), |
| 69 |
(st.integers(), partial(operator.gt, 3), None, 2), |
| 70 |
# Simple lambdas |
| 71 |
(st.integers(), lambda x: x < 3, None, 2), |
| 72 |
(st.integers(), lambda x: x <= 3, None, 3), |
| 73 |
(st.integers(), lambda x: x == 3, 3, 3), |
| 74 |
(st.integers(), lambda x: x >= 3, 3, None), |
| 75 |
(st.integers(), lambda x: x > 3, 4, None), |
| 76 |
# Simple lambdas, reverse comparison |
| 77 |
(st.integers(), lambda x: 3 > x, None, 2), |
| 78 |
(st.integers(), lambda x: 3 >= x, None, 3), |
| 79 |
(st.integers(), lambda x: 3 == x, 3, 3), |
| 80 |
(st.integers(), lambda x: 3 <= x, 3, None), |
| 81 |
(st.integers(), lambda x: 3 < x, 4, None), |
| 82 |
# More complicated lambdas |
| 83 |
(st.integers(), lambda x: 0 < x < 5, 1, 4), |
| 84 |
(st.integers(), lambda x: 0 < x >= 1, 1, None), |
| 85 |
(st.integers(), lambda x: 1 > x <= 0, None, 0), |
| 86 |
(st.integers(), lambda x: x > 0 and x > 0, 1, None), |
| 87 |
(st.integers(), lambda x: x < 1 and x < 1, None, 0), |
| 88 |
(st.integers(), lambda x: x > 1 and x > 0, 2, None), |
| 89 |
(st.integers(), lambda x: x < 1 and x < 2, None, 0), |
| 90 |
], |
| 91 |
ids=get_pretty_function_description, |
| 92 |
) |
| 93 |
@settings(max_examples=A_FEW) |
| 94 |
@given(data=st.data()) |
| 95 |
def test_filter_rewriting_ints(data, strategy, predicate, start, end): |
| 96 |
s = strategy.filter(predicate) |
| 97 |
assert isinstance(s, LazyStrategy) |
| 98 |
assert isinstance(s.wrapped_strategy, IntegersStrategy) |
| 99 |
assert s.wrapped_strategy.start == start |
| 100 |
assert s.wrapped_strategy.end == end |
| 101 |
value = data.draw(s) |
| 102 |
assert predicate(value) |
| 103 |
|
| 104 |
|
| 105 |
@pytest.mark.parametrize( |
| 106 |
"strategy, predicate, min_value, max_value", |
| 107 |
[ |
| 108 |
# Floats with integer bounds |
| 109 |
(st.floats(1, 5), partial(operator.lt, 3), next_up(3.0), 5), # 3 < x |
| 110 |
(st.floats(1, 5), partial(operator.le, 3), 3, 5), # lambda x: 3 <= x |
| 111 |
(st.floats(1, 5), partial(operator.eq, 3), 3, 3), # lambda x: 3 == x |
| 112 |
(st.floats(1, 5), partial(operator.ge, 3), 1, 3), # lambda x: 3 >= x |
| 113 |
(st.floats(1, 5), partial(operator.gt, 3), 1, next_down(3.0)), # 3 > x |
| 114 |
# Floats with non-integer bounds |
| 115 |
(st.floats(1, 5), partial(operator.lt, 3.5), next_up(3.5), 5), |
| 116 |
(st.floats(1, 5), partial(operator.le, 3.5), 3.5, 5), |
| 117 |
(st.floats(1, 5), partial(operator.ge, 3.5), 1, 3.5), |
| 118 |
(st.floats(1, 5), partial(operator.gt, 3.5), 1, next_down(3.5)), |
| 119 |
(st.floats(1, 5), partial(operator.lt, -math.inf), 1, 5), |
| 120 |
(st.floats(1, 5), partial(operator.gt, math.inf), 1, 5), |
| 121 |
# Floats with only one bound |
| 122 |
(st.floats(min_value=1), partial(operator.lt, 3), next_up(3.0), math.inf), |
| 123 |
(st.floats(min_value=1), partial(operator.le, 3), 3, math.inf), |
| 124 |
(st.floats(max_value=5), partial(operator.ge, 3), -math.inf, 3), |
| 125 |
(st.floats(max_value=5), partial(operator.gt, 3), -math.inf, next_down(3.0)), |
| 126 |
# Unbounded floats |
| 127 |
(st.floats(), partial(operator.lt, 3), next_up(3.0), math.inf), |
| 128 |
(st.floats(), partial(operator.le, 3), 3, math.inf), |
| 129 |
(st.floats(), partial(operator.eq, 3), 3, 3), |
| 130 |
(st.floats(), partial(operator.ge, 3), -math.inf, 3), |
| 131 |
(st.floats(), partial(operator.gt, 3), -math.inf, next_down(3.0)), |
| 132 |
# Simple lambdas |
| 133 |
(st.floats(), lambda x: x < 3, -math.inf, next_down(3.0)), |
| 134 |
(st.floats(), lambda x: x <= 3, -math.inf, 3), |
| 135 |
(st.floats(), lambda x: x == 3, 3, 3), |
| 136 |
(st.floats(), lambda x: x >= 3, 3, math.inf), |
| 137 |
(st.floats(), lambda x: x > 3, next_up(3.0), math.inf), |
| 138 |
# Simple lambdas, reverse comparison |
| 139 |
(st.floats(), lambda x: 3 > x, -math.inf, next_down(3.0)), |
| 140 |
(st.floats(), lambda x: 3 >= x, -math.inf, 3), |
| 141 |
(st.floats(), lambda x: 3 == x, 3, 3), |
| 142 |
(st.floats(), lambda x: 3 <= x, 3, math.inf), |
| 143 |
(st.floats(), lambda x: 3 < x, next_up(3.0), math.inf), |
| 144 |
# More complicated lambdas |
| 145 |
(st.floats(), lambda x: 0 < x < 5, next_up(0.0), next_down(5.0)), |
| 146 |
(st.floats(), lambda x: 0 < x >= 1, 1, math.inf), |
| 147 |
(st.floats(), lambda x: 1 > x <= 0, -math.inf, 0), |
| 148 |
(st.floats(), lambda x: x > 0 and x > 0, next_up(0.0), math.inf), |
| 149 |
(st.floats(), lambda x: x < 1 and x < 1, -math.inf, next_down(1.0)), |
| 150 |
(st.floats(), lambda x: x > 1 and x > 0, next_up(1.0), math.inf), |
| 151 |
(st.floats(), lambda x: x < 1 and x < 2, -math.inf, next_down(1.0)), |
| 152 |
# Specific named functions |
| 153 |
(st.floats(), math.isfinite, next_up(-math.inf), next_down(math.inf)), |
| 154 |
], |
| 155 |
ids=get_pretty_function_description, |
| 156 |
) |
| 157 |
@settings(max_examples=A_FEW) |
| 158 |
@given(data=st.data()) |
| 159 |
def test_filter_rewriting_floats(data, strategy, predicate, min_value, max_value): |
| 160 |
s = strategy.filter(predicate) |
| 161 |
assert isinstance(s, LazyStrategy) |
| 162 |
assert isinstance(s.wrapped_strategy, FloatStrategy) |
| 163 |
assert s.wrapped_strategy.min_value == min_value |
| 164 |
assert s.wrapped_strategy.max_value == max_value |
| 165 |
value = data.draw(s) |
| 166 |
assert predicate(value) |
| 167 |
|
| 168 |
|
| 169 |
@pytest.mark.parametrize( |
| 170 |
"pred", |
| 171 |
[ |
| 172 |
math.isinf, |
| 173 |
math.isnan, |
| 174 |
partial(operator.lt, 6), |
| 175 |
partial(operator.eq, Fraction(10, 3)), |
| 176 |
partial(operator.ge, 0), |
| 177 |
partial(operator.lt, math.inf), |
| 178 |
partial(operator.gt, -math.inf), |
| 179 |
], |
| 180 |
) |
| 181 |
@pytest.mark.parametrize("s", [st.integers(1, 5), st.floats(1, 5)]) |
| 182 |
def test_rewrite_unsatisfiable_filter(s, pred): |
| 183 |
assert s.filter(pred).is_empty |
| 184 |
|
| 185 |
|
| 186 |
@pytest.mark.parametrize( |
| 187 |
"pred", |
| 188 |
[ |
| 189 |
partial(operator.eq, "numbers are never equal to strings"), |
| 190 |
], |
| 191 |
) |
| 192 |
@pytest.mark.parametrize("s", [st.integers(1, 5), st.floats(1, 5)]) |
| 193 |
@fails_with(Unsatisfiable) |
| 194 |
def test_erroring_rewrite_unsatisfiable_filter(s, pred): |
| 195 |
check_can_generate_examples(s.filter(pred)) |
| 196 |
|
| 197 |
|
| 198 |
@pytest.mark.parametrize( |
| 199 |
"strategy, predicate", |
| 200 |
[ |
| 201 |
(st.floats(), math.isinf), |
| 202 |
(st.floats(0, math.inf), math.isinf), |
| 203 |
(st.floats(), math.isnan), |
| 204 |
], |
| 205 |
) |
| 206 |
@given(data=st.data()) |
| 207 |
def test_misc_sat_filter_rewrites(data, strategy, predicate): |
| 208 |
s = strategy.filter(predicate).wrapped_strategy |
| 209 |
assert not isinstance(s, FloatStrategy) |
| 210 |
value = data.draw(s) |
| 211 |
assert predicate(value) |
| 212 |
|
| 213 |
|
| 214 |
@pytest.mark.parametrize( |
| 215 |
"strategy, predicate", |
| 216 |
[ |
| 217 |
(st.floats(allow_infinity=False), math.isinf), |
| 218 |
(st.floats(0, math.inf), math.isnan), |
| 219 |
(st.floats(allow_nan=False), math.isnan), |
| 220 |
], |
| 221 |
) |
| 222 |
@given(data=st.data()) |
| 223 |
def test_misc_unsat_filter_rewrites(data, strategy, predicate): |
| 224 |
assert strategy.filter(predicate).is_empty |
| 225 |
|
| 226 |
|
| 227 |
@given(st.integers(0, 2).filter(partial(operator.ne, 1))) |
| 228 |
def test_unhandled_operator(x): |
| 229 |
assert x in (0, 2) |
| 230 |
|
| 231 |
|
| 232 |
def test_rewriting_does_not_compare_decimal_snan(): |
| 233 |
s = st.integers(1, 5).filter(partial(operator.eq, decimal.Decimal("snan"))) |
| 234 |
s.wrapped_strategy |
| 235 |
with pytest.raises(decimal.InvalidOperation): |
| 236 |
check_can_generate_examples(s) |
| 237 |
|
| 238 |
|
| 239 |
@pytest.mark.parametrize("strategy", [st.integers(0, 1), st.floats(0, 1)], ids=repr) |
| 240 |
def test_applying_noop_filter_returns_self(strategy): |
| 241 |
s = strategy.wrapped_strategy |
| 242 |
s2 = s.filter(partial(operator.le, -1)).filter(partial(operator.ge, 2)) |
| 243 |
assert s is s2 |
| 244 |
|
| 245 |
|
| 246 |
def mod2(x): |
| 247 |
return x % 2 |
| 248 |
|
| 249 |
|
| 250 |
Y = 2**20 |
| 251 |
|
| 252 |
|
| 253 |
@pytest.mark.parametrize("s", [st.integers(1, 5), st.floats(1, 5)]) |
| 254 |
@given( |
| 255 |
data=st.data(), |
| 256 |
predicates=st.permutations( |
| 257 |
[ |
| 258 |
partial(operator.lt, 1), |
| 259 |
partial(operator.le, 2), |
| 260 |
partial(operator.ge, 4), |
| 261 |
partial(operator.gt, 5), |
| 262 |
mod2, |
| 263 |
lambda x: x > 2 or x % 7, |
| 264 |
lambda x: 0 < x <= Y, |
| 265 |
] |
| 266 |
), |
| 267 |
) |
| 268 |
@settings(suppress_health_check=[HealthCheck.too_slow]) |
| 269 |
def test_rewrite_filter_chains_with_some_unhandled(data, predicates, s): |
| 270 |
# Set up our strategy |
| 271 |
for p in predicates: |
| 272 |
s = s.filter(p) |
| 273 |
|
| 274 |
# Whatever value we draw is in fact valid for these strategies |
| 275 |
value = data.draw(s) |
| 276 |
for p in predicates: |
| 277 |
assert p(value), f"{p=}, value={value}" |
| 278 |
|
| 279 |
# No matter the order of the filters, we get the same resulting structure |
| 280 |
unwrapped = s.wrapped_strategy |
| 281 |
assert isinstance(unwrapped, FilteredStrategy) |
| 282 |
assert isinstance(unwrapped.filtered_strategy, (IntegersStrategy, FloatStrategy)) |
| 283 |
for pred in unwrapped.flat_conditions: |
| 284 |
assert pred is mod2 or pred.__name__ == "<lambda>" |
| 285 |
|
| 286 |
|
| 287 |
class NotAFunction: |
| 288 |
def __call__(self, bar): |
| 289 |
return True |
| 290 |
|
| 291 |
|
| 292 |
lambda_without_source = eval("lambda x: x > 2", {}, {}) |
| 293 |
assert get_pretty_function_description(lambda_without_source) == "lambda x: <unknown>" |
| 294 |
|
| 295 |
|
| 296 |
@pytest.mark.parametrize( |
| 297 |
"start, end, predicate", |
| 298 |
[ |
| 299 |
(1, 4, lambda x: 0 < x < 5 and x % 7), |
| 300 |
(0, 9, lambda x: 0 <= x < 10 and x % 3), |
| 301 |
(1, None, lambda x: 0 < x <= Y), |
| 302 |
(None, None, lambda x: x == x), |
| 303 |
(None, None, lambda x: 1 == 1), |
| 304 |
(None, None, lambda x: 1 <= 2), |
| 305 |
(None, None, lambda x: x != 0), |
| 306 |
(None, None, NotAFunction()), |
| 307 |
(None, None, lambda_without_source), |
| 308 |
(None, None, lambda x, y=2: x >= 0), |
| 309 |
], |
| 310 |
) |
| 311 |
@given(data=st.data()) |
| 312 |
def test_rewriting_partially_understood_filters(data, start, end, predicate): |
| 313 |
s = st.integers().filter(predicate).wrapped_strategy |
| 314 |
|
| 315 |
assert isinstance(s, FilteredStrategy) |
| 316 |
assert isinstance(s.filtered_strategy, IntegersStrategy) |
| 317 |
assert s.filtered_strategy.start == start |
| 318 |
assert s.filtered_strategy.end == end |
| 319 |
assert s.flat_conditions == (predicate,) |
| 320 |
|
| 321 |
value = data.draw(s) |
| 322 |
assert predicate(value) |
| 323 |
|
| 324 |
|
| 325 |
@pytest.mark.parametrize( |
| 326 |
"strategy", |
| 327 |
[ |
| 328 |
st.text(), |
| 329 |
st.text(min_size=2), |
| 330 |
st.lists(st.none()), |
| 331 |
st.lists(st.none(), min_size=2), |
| 332 |
], |
| 333 |
) |
| 334 |
@pytest.mark.parametrize( |
| 335 |
"predicate", |
| 336 |
[bool, len, tuple, list, lambda x: x], |
| 337 |
ids=get_pretty_function_description, |
| 338 |
) |
| 339 |
def test_sequence_filter_rewriting(strategy, predicate): |
| 340 |
s = unwrap_strategies(strategy) |
| 341 |
fs = s.filter(predicate) |
| 342 |
assert not isinstance(fs, FilteredStrategy) |
| 343 |
if s.min_size > 0: |
| 344 |
assert fs is s |
| 345 |
else: |
| 346 |
assert fs.min_size == 1 |
| 347 |
|
| 348 |
|
| 349 |
@pytest.mark.parametrize("method", [str.lower, str.title, str.upper]) |
| 350 |
def test_warns_on_suspicious_string_methods(method): |
| 351 |
s = unwrap_strategies(st.text()) |
| 352 |
with pytest.warns( |
| 353 |
HypothesisWarning, match="this allows all nonempty strings! Did you mean" |
| 354 |
): |
| 355 |
fs = s.filter(method) |
| 356 |
assert fs.min_size == 1 |
| 357 |
|
| 358 |
|
| 359 |
@pytest.mark.parametrize("method", [str.isalnum]) |
| 360 |
def test_bumps_min_size_and_filters_for_content_str_methods(method): |
| 361 |
s = unwrap_strategies(st.text()) |
| 362 |
fs = s.filter(method) |
| 363 |
assert fs.filtered_strategy.min_size == 1 |
| 364 |
assert fs.flat_conditions == (method,) |
| 365 |
|
| 366 |
|
| 367 |
# Should we deterministically check whether ascii or not or st.characters fine? |
| 368 |
@pytest.mark.parametrize("al", [None, "cdef123", "cd12¥¦§©"]) |
| 369 |
@given(data()) |
| 370 |
def test_isidentifier_filter_properly_rewritten(al, data): |
| 371 |
if al is None: |
| 372 |
example = data.draw(st.text().filter(str.isidentifier)) |
| 373 |
else: |
| 374 |
example = data.draw(st.text(alphabet=al).filter(str.isidentifier)) |
| 375 |
assert set(example).issubset(al) |
| 376 |
assert example.isidentifier() |
| 377 |
|
| 378 |
|
| 379 |
def test_isidentifer_filter_unsatisfiable(): |
| 380 |
alphabet = "¥¦§©" |
| 381 |
assert not any(f"_{c}".isidentifier() for c in alphabet) |
| 382 |
fs = st.text(alphabet=alphabet).filter(str.isidentifier) |
| 383 |
with pytest.raises(Unsatisfiable): |
| 384 |
check_can_generate_examples(fs) |
| 385 |
|
| 386 |
|
| 387 |
@pytest.mark.parametrize( |
| 388 |
"op, attr, value, expected", |
| 389 |
[ |
| 390 |
(operator.lt, "min_value", -float_info.min / 2, 0), |
| 391 |
(operator.lt, "min_value", float_info.min / 2, float_info.min), |
| 392 |
(operator.gt, "max_value", float_info.min / 2, 0), |
| 393 |
(operator.gt, "max_value", -float_info.min / 2, -float_info.min), |
| 394 |
], |
| 395 |
) |
| 396 |
def test_filter_floats_can_skip_subnormals(op, attr, value, expected): |
| 397 |
base = st.floats(allow_subnormal=False).filter(partial(op, value)) |
| 398 |
assert getattr(base.wrapped_strategy, attr) == expected |
| 399 |
|
| 400 |
|
| 401 |
@pytest.mark.parametrize( |
| 402 |
"strategy, predicate, start, end", |
| 403 |
[ |
| 404 |
# text with integer bounds |
| 405 |
(st.text(min_size=1, max_size=5), partial(min_len, 3), 3, 5), |
| 406 |
(st.text(min_size=1, max_size=5), partial(max_len, 3), 1, 3), |
| 407 |
# text with only one bound |
| 408 |
(st.text(min_size=1), partial(min_len, 3), 3, math.inf), |
| 409 |
(st.text(min_size=1), partial(max_len, 3), 1, 3), |
| 410 |
(st.text(max_size=5), partial(min_len, 3), 3, 5), |
| 411 |
(st.text(max_size=5), partial(max_len, 3), 0, 3), |
| 412 |
# Unbounded text |
| 413 |
(st.text(), partial(min_len, 3), 3, math.inf), |
| 414 |
(st.text(), partial(max_len, 3), 0, 3), |
| 415 |
], |
| 416 |
ids=get_pretty_function_description, |
| 417 |
) |
| 418 |
@settings(max_examples=A_FEW) |
| 419 |
@given(data=st.data()) |
| 420 |
def test_filter_rewriting_text_partial_len(data, strategy, predicate, start, end): |
| 421 |
s = strategy.filter(predicate) |
| 422 |
|
| 423 |
assert isinstance(s, LazyStrategy) |
| 424 |
inner = unwrap_strategies(s) |
| 425 |
assert isinstance(inner, TextStrategy) |
| 426 |
assert inner.min_size == start |
| 427 |
assert inner.max_size == end |
| 428 |
value = data.draw(s) |
| 429 |
assert predicate(value) |
| 430 |
|
| 431 |
|
| 432 |
@given(data=st.data()) |
| 433 |
def test_can_rewrite_multiple_length_filters_if_not_lambdas(data): |
| 434 |
# This is a key capability for efficient rewriting using the `annotated-types` |
| 435 |
# package, although unfortunately we can't do it for lambdas. |
| 436 |
s = ( |
| 437 |
st.text(min_size=1, max_size=5) |
| 438 |
.filter(partial(min_len, 2)) |
| 439 |
.filter(partial(max_len, 4)) |
| 440 |
) |
| 441 |
assert isinstance(s, LazyStrategy) |
| 442 |
inner = unwrap_strategies(s) |
| 443 |
assert isinstance(inner, TextStrategy) |
| 444 |
assert inner.min_size == 2 |
| 445 |
assert inner.max_size == 4 |
| 446 |
value = data.draw(s) |
| 447 |
assert 2 <= len(value) <= 4 |
| 448 |
|
| 449 |
|
| 450 |
@pytest.mark.parametrize( |
| 451 |
"predicate, start, end", |
| 452 |
[ |
| 453 |
# Simple lambdas |
| 454 |
(lambda x: len(x) < 3, 0, 2), |
| 455 |
(lambda x: len(x) <= 3, 0, 3), |
| 456 |
(lambda x: len(x) == 3, 3, 3), |
| 457 |
(lambda x: len(x) >= 3, 3, math.inf), |
| 458 |
(lambda x: len(x) > 3, 4, math.inf), |
| 459 |
# Simple lambdas, reverse comparison |
| 460 |
(lambda x: 3 > len(x), 0, 2), |
| 461 |
(lambda x: 3 >= len(x), 0, 3), |
| 462 |
(lambda x: 3 == len(x), 3, 3), |
| 463 |
(lambda x: 3 <= len(x), 3, math.inf), |
| 464 |
(lambda x: 3 < len(x), 4, math.inf), |
| 465 |
# More complicated lambdas |
| 466 |
(lambda x: 0 < len(x) < 5, 1, 4), |
| 467 |
(lambda x: 0 < len(x) >= 1, 1, math.inf), |
| 468 |
(lambda x: 1 > len(x) <= 0, 0, 0), |
| 469 |
(lambda x: len(x) > 0 and len(x) > 0, 1, math.inf), |
| 470 |
(lambda x: len(x) < 1 and len(x) < 1, 0, 0), |
| 471 |
(lambda x: len(x) > 1 and len(x) > 0, 2, math.inf), |
| 472 |
(lambda x: len(x) < 1 and len(x) < 2, 0, 0), |
| 473 |
], |
| 474 |
ids=get_pretty_function_description, |
| 475 |
) |
| 476 |
@pytest.mark.parametrize( |
| 477 |
"strategy", |
| 478 |
[ |
| 479 |
st.text(), |
| 480 |
st.lists(st.integers()), |
| 481 |
st.lists(st.integers(), unique=True), |
| 482 |
st.lists(st.sampled_from([1, 2, 3])), |
| 483 |
st.binary(), |
| 484 |
st.sets(st.integers()), |
| 485 |
st.frozensets(st.integers()), |
| 486 |
st.dictionaries(st.integers(), st.none()), |
| 487 |
st.lists(st.integers(), unique_by=lambda x: x % 17).map(tuple), |
| 488 |
], |
| 489 |
ids=get_pretty_function_description, |
| 490 |
) |
| 491 |
@settings(max_examples=A_FEW) |
| 492 |
@given(data=st.data()) |
| 493 |
def test_filter_rewriting_text_lambda_len(data, strategy, predicate, start, end): |
| 494 |
s = strategy.filter(predicate) |
| 495 |
unwrapped_nofilter = unwrap_strategies(strategy) |
| 496 |
unwrapped = unwrap_strategies(s) |
| 497 |
|
| 498 |
if was_mapped := isinstance(unwrapped, MappedStrategy): |
| 499 |
unwrapped = unwrapped.mapped_strategy |
| 500 |
|
| 501 |
assert isinstance(unwrapped, FilteredStrategy), f"{unwrapped=} {type(unwrapped)=}" |
| 502 |
assert isinstance( |
| 503 |
unwrapped.filtered_strategy, |
| 504 |
type(unwrapped_nofilter.mapped_strategy if was_mapped else unwrapped_nofilter), |
| 505 |
) |
| 506 |
for pred in unwrapped.flat_conditions: |
| 507 |
assert pred.__name__ == "<lambda>" |
| 508 |
|
| 509 |
if isinstance(unwrapped.filtered_strategy, MappedStrategy): |
| 510 |
unwrapped = unwrapped.filtered_strategy.mapped_strategy |
| 511 |
|
| 512 |
# binary() has a finite-but-effectively-infinite cap instead. |
| 513 |
if isinstance(unwrapped_nofilter, BytesStrategy) and end == math.inf: |
| 514 |
end = COLLECTION_DEFAULT_MAX_SIZE |
| 515 |
|
| 516 |
assert unwrapped.filtered_strategy.min_size == start |
| 517 |
assert unwrapped.filtered_strategy.max_size == end |
| 518 |
value = data.draw(s) |
| 519 |
assert predicate(value) |
| 520 |
|
| 521 |
|
| 522 |
two = 2 |
| 523 |
|
| 524 |
|
| 525 |
@pytest.mark.parametrize( |
| 526 |
"predicate, start, end", |
| 527 |
[ |
| 528 |
# Simple lambdas |
| 529 |
(lambda x: len(x) < 3, 0, 2), |
| 530 |
(lambda x: len(x) <= 3, 0, 3), |
| 531 |
(lambda x: len(x) == 3, 3, 3), |
| 532 |
(lambda x: len(x) >= 3, 3, 3), # input max element_count=3 |
| 533 |
# Simple lambdas, reverse comparison |
| 534 |
(lambda x: 3 > len(x), 0, 2), |
| 535 |
(lambda x: 3 >= len(x), 0, 3), |
| 536 |
(lambda x: 3 == len(x), 3, 3), |
| 537 |
(lambda x: 3 <= len(x), 3, 3), # input max element_count=3 |
| 538 |
# More complicated lambdas |
| 539 |
(lambda x: 0 < len(x) < 5, 1, 3), # input max element_count=3 |
| 540 |
(lambda x: 0 < len(x) >= 1, 1, 3), # input max element_count=3 |
| 541 |
(lambda x: 1 > len(x) <= 0, 0, 0), |
| 542 |
(lambda x: len(x) > 0 and len(x) > 0, 1, 3), # input max element_count=3 |
| 543 |
(lambda x: len(x) < 1 and len(x) < 1, 0, 0), |
| 544 |
(lambda x: len(x) > 1 and len(x) > 0, 2, 3), # input max element_count=3 |
| 545 |
(lambda x: len(x) < 1 and len(x) < 2, 0, 0), |
| 546 |
# Comparisons involving one literal and one variable |
| 547 |
(lambda x: 1 <= len(x) <= two, 1, 3), |
| 548 |
(lambda x: two <= len(x) <= 4, 0, 3), |
| 549 |
], |
| 550 |
ids=get_pretty_function_description, |
| 551 |
) |
| 552 |
@pytest.mark.parametrize( |
| 553 |
"strategy", |
| 554 |
[ |
| 555 |
st.lists(st.sampled_from([1, 2, 3]), unique=True), |
| 556 |
], |
| 557 |
ids=get_pretty_function_description, |
| 558 |
) |
| 559 |
@settings(max_examples=A_FEW) |
| 560 |
@given(data=st.data()) |
| 561 |
def test_filter_rewriting_lambda_len_unique_elements( |
| 562 |
data, strategy, predicate, start, end |
| 563 |
): |
| 564 |
s = strategy.filter(predicate) |
| 565 |
unwrapped = unwrap_strategies(s) |
| 566 |
assert isinstance(unwrapped, FilteredStrategy) |
| 567 |
assert isinstance(unwrapped.filtered_strategy, type(unwrap_strategies(strategy))) |
| 568 |
for pred in unwrapped.flat_conditions: |
| 569 |
assert pred.__name__ == "<lambda>" |
| 570 |
|
| 571 |
assert unwrapped.filtered_strategy.min_size == start |
| 572 |
assert unwrapped.filtered_strategy.max_size == end |
| 573 |
value = data.draw(s) |
| 574 |
assert predicate(value) |
| 575 |
|
| 576 |
|
| 577 |
@pytest.mark.parametrize( |
| 578 |
"predicate", |
| 579 |
[ |
| 580 |
(lambda x: len(x) < 3), |
| 581 |
(lambda x: len(x) > 5), |
| 582 |
], |
| 583 |
ids=get_pretty_function_description, |
| 584 |
) |
| 585 |
def test_does_not_rewrite_unsatisfiable_len_filter(predicate): |
| 586 |
strategy = st.lists(st.none(), min_size=4, max_size=4).filter(predicate) |
| 587 |
with pytest.raises(Unsatisfiable): |
| 588 |
check_can_generate_examples(strategy) |
| 589 |
# Rewriting to nothing() would correctly express the constraint. However |
| 590 |
# we don't want _only rewritable strategies_ to work in e.g. one_of, so: |
| 591 |
assert not strategy.is_empty |
| 592 |
|
| 593 |
|
| 594 |
@pytest.mark.parametrize( |
| 595 |
"method", ["match", "search", "findall", "fullmatch", "finditer", "split"] |
| 596 |
) |
| 597 |
@pytest.mark.parametrize( |
| 598 |
"strategy, pattern", |
| 599 |
[ |
| 600 |
(st.text(), "ab+c"), |
| 601 |
(st.text(), "a|b"), |
| 602 |
(st.text(alphabet="abcdef"), "ab+c"), |
| 603 |
(st.text(min_size=5, max_size=10), "ab+c"), |
| 604 |
(st.binary(), b"ab+c"), |
| 605 |
(st.binary(), b"a|b"), |
| 606 |
(st.binary(min_size=5, max_size=10), b"ab+c"), |
| 607 |
], |
| 608 |
ids=repr, |
| 609 |
) |
| 610 |
@settings(max_examples=A_FEW) |
| 611 |
@given(data=st.data()) |
| 612 |
def test_regex_filter_rewriting(data, strategy, pattern, method): |
| 613 |
# This would raise a HealthCheck without rewriting, so checking that |
| 614 |
# we can draw a valid value is sufficient. |
| 615 |
predicate = getattr(re.compile(pattern), method) |
| 616 |
s = strategy.filter(predicate) |
| 617 |
if method in ("finditer", "split"): |
| 618 |
msg = r"You applied re.compile\(.+?\).\w+ as a filter, but this allows" |
| 619 |
with pytest.warns(HypothesisWarning, match=msg): |
| 620 |
value = data.draw(s) |
| 621 |
else: |
| 622 |
value = data.draw(s) |
| 623 |
assert predicate(value) |
| 624 |
|
| 625 |
|
| 626 |
@fails_with(TypeError) |
| 627 |
@given(st.text().filter(re.compile("abc").sub)) |
| 628 |
def test_error_on_method_which_requires_multiple_args(_): |
| 629 |
pass |
| 630 |
|
| 631 |
|
| 632 |
def test_dates_filter_rewriting(): |
| 633 |
today = dt.date.today() |
| 634 |
|
| 635 |
assert st.dates().filter(partial(operator.lt, dt.date.max)).is_empty |
| 636 |
assert st.dates().filter(partial(operator.gt, dt.date.min)).is_empty |
| 637 |
assert st.dates(min_value=today).filter(partial(operator.gt, today)).is_empty |
| 638 |
assert st.dates(max_value=today).filter(partial(operator.lt, today)).is_empty |
| 639 |
|
| 640 |
bare = unwrap_strategies(st.dates()) |
| 641 |
assert bare.filter(partial(operator.ge, dt.date.max)) is bare |
| 642 |
assert bare.filter(partial(operator.le, dt.date.min)) is bare |
| 643 |
|
| 644 |
new = bare.filter(partial(operator.le, today)) |
| 645 |
assert not new.is_empty |
| 646 |
assert new is not bare |
| 647 |
|
| 648 |
|
| 649 |
@pytest.mark.skipif( |
| 650 |
sys.version_info[:2] < (3, 14), reason="functools.Placeholder is new in 3.14" |
| 651 |
) |
| 652 |
def test_partial_placeholder(): |
| 653 |
from functools import Placeholder |
| 654 |
|
| 655 |
assert st.integers(0, 5).filter(partial(operator.gt, Placeholder, 5)).is_empty |
| 656 |
|
| 657 |
s = unwrap_strategies( |
| 658 |
st.integers(-5, 5).filter(partial(operator.lt, Placeholder, 3)) |
| 659 |
) |
| 660 |
assert (s.start, s.end) == (-5, 2) |
| 661 |
|
| 662 |
s = unwrap_strategies( |
| 663 |
st.integers(-5, 5).filter(partial(operator.le, Placeholder, 3)) |
| 664 |
) |
| 665 |
assert (s.start, s.end) == (-5, 3) |
| 666 |
|
| 667 |
s = unwrap_strategies( |
| 668 |
st.integers(-5, 5).filter(partial(operator.gt, Placeholder, 3)) |
| 669 |
) |
| 670 |
assert (s.start, s.end) == (4, 5) |
| 671 |
|
| 672 |
s = unwrap_strategies( |
| 673 |
st.integers(-5, 5).filter(partial(operator.ge, Placeholder, 3)) |
| 674 |
) |
| 675 |
assert (s.start, s.end) == (3, 5) |
| 676 |
|
| 677 |
s = unwrap_strategies( |
| 678 |
st.integers(-5, 5).filter(partial(operator.eq, Placeholder, 3)) |
| 679 |
) |
| 680 |
assert (s.start, s.end) == (3, 3) |