| 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, strategies as st |
| 14 |
from hypothesis.errors import InvalidArgument |
| 15 |
|
| 16 |
from tests.common.debug import assert_no_examples, check_can_generate_examples, minimal |
| 17 |
|
| 18 |
|
| 19 |
def test_binary_tree(): |
| 20 |
tree = st.deferred(lambda: st.integers() | st.tuples(tree, tree)) |
| 21 |
|
| 22 |
assert minimal(tree) == 0 |
| 23 |
assert minimal(tree, lambda x: isinstance(x, tuple)) == (0, 0) |
| 24 |
|
| 25 |
|
| 26 |
def test_mutual_recursion(): |
| 27 |
t = st.deferred(lambda: a | b) |
| 28 |
a = st.deferred(lambda: st.none() | st.tuples(st.just("a"), b)) |
| 29 |
b = st.deferred(lambda: st.none() | st.tuples(st.just("b"), a)) |
| 30 |
|
| 31 |
for c in ("a", "b"): |
| 32 |
assert minimal(t, lambda x: x is not None and x[0] == c) == (c, None) # noqa |
| 33 |
|
| 34 |
|
| 35 |
def test_errors_on_non_function_define(): |
| 36 |
x = st.deferred(1) |
| 37 |
with pytest.raises(InvalidArgument): |
| 38 |
check_can_generate_examples(x) |
| 39 |
|
| 40 |
|
| 41 |
def test_errors_if_define_does_not_return_search_strategy(): |
| 42 |
x = st.deferred(lambda: 1) |
| 43 |
with pytest.raises(InvalidArgument): |
| 44 |
check_can_generate_examples(x) |
| 45 |
|
| 46 |
|
| 47 |
def test_errors_on_definition_as_self(): |
| 48 |
x = st.deferred(lambda: x) |
| 49 |
with pytest.raises(InvalidArgument): |
| 50 |
check_can_generate_examples(x) |
| 51 |
|
| 52 |
|
| 53 |
def test_branches_pass_through_deferred(): |
| 54 |
x = st.one_of(st.booleans(), st.integers()) |
| 55 |
y = st.deferred(lambda: x) |
| 56 |
assert x.branches == y.branches |
| 57 |
|
| 58 |
|
| 59 |
def test_can_draw_one_of_self(): |
| 60 |
x = st.deferred(lambda: st.one_of(st.booleans(), x)) |
| 61 |
assert minimal(x) is False |
| 62 |
assert len(x.branches) == 1 |
| 63 |
|
| 64 |
|
| 65 |
def test_hidden_self_references_just_result_in_no_example(): |
| 66 |
bad = st.deferred(lambda: st.none().flatmap(lambda _: bad)) |
| 67 |
assert_no_examples(bad) |
| 68 |
|
| 69 |
|
| 70 |
def test_self_recursive_flatmap(): |
| 71 |
bad = st.deferred(lambda: bad.flatmap(lambda x: st.none())) |
| 72 |
assert_no_examples(bad) |
| 73 |
|
| 74 |
|
| 75 |
def test_self_reference_through_one_of_can_detect_emptiness(): |
| 76 |
bad = st.deferred(lambda: st.one_of(bad, bad)) |
| 77 |
assert bad.is_empty |
| 78 |
|
| 79 |
|
| 80 |
def test_self_tuple_draws_nothing(): |
| 81 |
x = st.deferred(lambda: st.tuples(x)) |
| 82 |
assert_no_examples(x) |
| 83 |
|
| 84 |
|
| 85 |
def test_mutually_recursive_tuples_draw_nothing(): |
| 86 |
x = st.deferred(lambda: st.tuples(y)) |
| 87 |
y = st.tuples(x) |
| 88 |
|
| 89 |
assert_no_examples(x) |
| 90 |
assert_no_examples(y) |
| 91 |
|
| 92 |
|
| 93 |
def test_literals_strategy_is_valid(): |
| 94 |
literals = st.deferred( |
| 95 |
lambda: st.one_of( |
| 96 |
st.booleans(), st.tuples(literals, literals), literals.map(lambda x: [x]) |
| 97 |
) |
| 98 |
) |
| 99 |
|
| 100 |
@given(literals) |
| 101 |
def test(e): |
| 102 |
pass |
| 103 |
|
| 104 |
test() |
| 105 |
|
| 106 |
assert not literals.has_reusable_values |
| 107 |
|
| 108 |
|
| 109 |
def test_impossible_self_recursion(): |
| 110 |
x = st.deferred(lambda: st.tuples(st.none(), x)) |
| 111 |
assert x.is_empty |
| 112 |
assert x.has_reusable_values |
| 113 |
|
| 114 |
|
| 115 |
def test_very_deep_deferral(): |
| 116 |
# This test is designed so that the recursive properties take a very long |
| 117 |
# time to converge: Although we can rapidly determine them for the original |
| 118 |
# value, each round in the fixed point calculation only manages to update |
| 119 |
# a single value in the related strategies, so it takes 100 rounds to |
| 120 |
# update everything. Most importantly this triggers our infinite loop |
| 121 |
# detection heuristic and we start tracking duplicates, but we shouldn't |
| 122 |
# see any because this loop isn't infinite, just long. |
| 123 |
def strat(i): |
| 124 |
if i == 0: |
| 125 |
return st.deferred(lambda: st.one_of([*strategies, st.none()])) |
| 126 |
else: |
| 127 |
return st.deferred(lambda: st.tuples(strategies[(i + 1) % len(strategies)])) |
| 128 |
|
| 129 |
strategies = list(map(strat, range(100))) |
| 130 |
|
| 131 |
assert strategies[0].has_reusable_values |
| 132 |
assert not strategies[0].is_empty |
| 133 |
|
| 134 |
|
| 135 |
def test_recursion_in_middle(): |
| 136 |
# This test is significant because the integers().map(abs) is not checked |
| 137 |
# in the initial pass - when we recurse into x initially we decide that |
| 138 |
# x is empty, so the tuple is empty, and don't need to check the third |
| 139 |
# argument. Then when we do the more refined test we've discovered that x |
| 140 |
# is non-empty, so we need to check the non-emptiness of the last component |
| 141 |
# to determine the non-emptiness of the tuples. |
| 142 |
x = st.deferred(lambda: st.tuples(st.none(), x, st.integers().map(abs)) | st.none()) |
| 143 |
assert not x.is_empty |