| 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 pytest |
| 12 |
|
| 13 |
from hypothesis import given, note, settings, strategies as st |
| 14 |
from hypothesis.errors import HypothesisWarning, InvalidArgument |
| 15 |
|
| 16 |
from tests.common.debug import ( |
| 17 |
assert_all_examples, |
| 18 |
assert_no_examples, |
| 19 |
check_can_generate_examples, |
| 20 |
find_any, |
| 21 |
minimal, |
| 22 |
) |
| 23 |
from tests.common.utils import checks_deprecated_behaviour |
| 24 |
|
| 25 |
|
| 26 |
@given(st.recursive(st.booleans(), st.lists, max_leaves=10)) |
| 27 |
def test_respects_leaf_limit(xs): |
| 28 |
def flatten(x): |
| 29 |
if isinstance(x, list): |
| 30 |
return sum(map(flatten, x), []) |
| 31 |
else: |
| 32 |
return [x] |
| 33 |
|
| 34 |
assert len(flatten(xs)) <= 10 |
| 35 |
|
| 36 |
|
| 37 |
def test_can_find_nested(): |
| 38 |
x = minimal( |
| 39 |
st.recursive(st.booleans(), lambda x: st.tuples(x, x)), |
| 40 |
lambda x: isinstance(x, tuple) and isinstance(x[0], tuple), |
| 41 |
) |
| 42 |
|
| 43 |
assert x == ((False, False), False) |
| 44 |
|
| 45 |
|
| 46 |
def test_recursive_call_validates_expand_returns_strategies(): |
| 47 |
with pytest.raises(InvalidArgument): |
| 48 |
check_can_generate_examples(st.recursive(st.booleans(), lambda x: 1)) |
| 49 |
|
| 50 |
|
| 51 |
def test_recursive_call_validates_base_is_strategy(): |
| 52 |
x = st.recursive(1, lambda x: st.none()) |
| 53 |
with pytest.raises(InvalidArgument): |
| 54 |
check_can_generate_examples(x) |
| 55 |
|
| 56 |
|
| 57 |
def test_can_find_exactly_max_leaves(): |
| 58 |
strat = st.recursive(st.none(), lambda x: st.tuples(x, x), max_leaves=5) |
| 59 |
|
| 60 |
def enough_leaves(t): |
| 61 |
print(t) |
| 62 |
count = 0 |
| 63 |
stack = [t] |
| 64 |
while stack: |
| 65 |
s = stack.pop() |
| 66 |
if s is None: |
| 67 |
count += 1 |
| 68 |
else: |
| 69 |
stack.extend(s) |
| 70 |
return count >= 5 |
| 71 |
|
| 72 |
find_any(strat, enough_leaves) |
| 73 |
|
| 74 |
|
| 75 |
@given(st.recursive(st.none(), lambda x: st.tuples(x, x), max_leaves=1)) |
| 76 |
def test_can_exclude_branching_with_max_leaves(t): |
| 77 |
assert t is None |
| 78 |
|
| 79 |
|
| 80 |
@given(st.recursive(st.none(), lambda x: st.one_of(x, x))) |
| 81 |
def test_issue_1502_regression(s): |
| 82 |
pass |
| 83 |
|
| 84 |
|
| 85 |
def test_recursive_can_generate_varied_structures(): |
| 86 |
values = st.recursive(st.none(), st.lists) |
| 87 |
|
| 88 |
find_any(values, lambda x: x is None) |
| 89 |
find_any(values, lambda x: isinstance(x, list)) |
| 90 |
find_any( |
| 91 |
values, lambda x: isinstance(x, list) and any(isinstance(y, list) for y in x) |
| 92 |
) |
| 93 |
|
| 94 |
|
| 95 |
@checks_deprecated_behaviour |
| 96 |
def test_recursive_can_generate_varied_structures_without_using_leaves(): |
| 97 |
values = st.recursive(st.none(), lambda _: st.lists(st.none())) |
| 98 |
|
| 99 |
find_any(values, lambda x: x is None) |
| 100 |
find_any(values, lambda x: isinstance(x, list)) |
| 101 |
# The bad `extend` function means we can't actually recurse! |
| 102 |
assert_no_examples( |
| 103 |
values, lambda x: isinstance(x, list) and any(isinstance(y, list) for y in x) |
| 104 |
) |
| 105 |
|
| 106 |
|
| 107 |
@pytest.mark.parametrize( |
| 108 |
"s", |
| 109 |
[ |
| 110 |
st.recursive(None, st.lists), |
| 111 |
st.recursive(st.none(), lambda x: None), |
| 112 |
st.recursive(st.none(), st.lists, max_leaves=-1), |
| 113 |
st.recursive(st.none(), st.lists, max_leaves=0), |
| 114 |
st.recursive(st.none(), st.lists, max_leaves=1.0), |
| 115 |
st.recursive(st.none(), st.lists, min_leaves=-1), |
| 116 |
st.recursive(st.none(), st.lists, min_leaves=0), |
| 117 |
st.recursive(st.none(), st.lists, min_leaves=1.0), |
| 118 |
st.recursive(st.none(), st.lists, min_leaves=10, max_leaves=5), |
| 119 |
st.recursive(st.none(), lambda _: st.lists(st.none()), min_leaves=1), |
| 120 |
], |
| 121 |
) |
| 122 |
def test_invalid_args(s): |
| 123 |
with pytest.raises(InvalidArgument): |
| 124 |
check_can_generate_examples(s) |
| 125 |
|
| 126 |
|
| 127 |
def _count_leaves(tree): |
| 128 |
if isinstance(tree, tuple): |
| 129 |
return sum(_count_leaves(child) for child in tree) |
| 130 |
return 1 |
| 131 |
|
| 132 |
|
| 133 |
@given(st.data()) |
| 134 |
@settings(max_examples=5, suppress_health_check=["filter_too_much"]) |
| 135 |
def test_respects_min_leaves(data): |
| 136 |
min_leaves = data.draw(st.integers(1, 20)) |
| 137 |
max_leaves = data.draw(st.integers(min_leaves, 40)) |
| 138 |
note(f"{min_leaves=}") |
| 139 |
note(f"{max_leaves=}") |
| 140 |
s = st.recursive( |
| 141 |
st.none(), |
| 142 |
lambda x: st.tuples(x, x), |
| 143 |
min_leaves=min_leaves, |
| 144 |
max_leaves=max_leaves, |
| 145 |
) |
| 146 |
assert_all_examples(s, lambda tree: min_leaves <= _count_leaves(tree) <= max_leaves) |
| 147 |
|
| 148 |
|
| 149 |
@given(st.recursive(st.none(), lambda x: st.tuples(x, x), min_leaves=5, max_leaves=5)) |
| 150 |
@settings(suppress_health_check=["filter_too_much"]) |
| 151 |
def test_can_set_exact_leaf_count(tree): |
| 152 |
assert _count_leaves(tree) == 5 |
| 153 |
|
| 154 |
|
| 155 |
def test_identity_extend_warns(): |
| 156 |
with pytest.warns(HypothesisWarning, match="extend=lambda x: x is a no-op"): |
| 157 |
st.recursive(st.none(), lambda x: x).validate() |