Debian Codegraph @

hypothesis-python/tests/nocover/test_reusable_values.py

raw
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 example, given, strategies as st
14 from hypothesis.errors import InvalidArgument
15
16 # Be aware that tests in this file pass strategies as arguments to @example.
17 # That's normally a mistake, but for these tests it's intended.
18 # If one of these tests fails, Hypothesis will complain about the
19 # @example/strategy interaction, but it should be safe to ignore that
20 # error message and focus on the underlying failure instead.
21
22 base_reusable_strategies = (
23 st.text(),
24 st.binary(),
25 st.dates(),
26 st.times(),
27 st.timedeltas(),
28 st.booleans(),
29 st.complex_numbers(),
30 st.floats(),
31 st.floats(-1.0, 1.0),
32 st.integers(),
33 st.integers(1, 10),
34 st.integers(1),
35 # Note that `just` and `sampled_from` count as "reusable" even if their
36 # values are mutable, because the user has implicitly promised that they
37 # don't care about the same mutable value being returned by separate draws.
38 st.just([]),
39 st.sampled_from([[]]),
40 st.tuples(st.integers()),
41 )
42
43
44 @st.deferred
45 def reusable():
46 """Meta-strategy that produces strategies that should have
47 ``.has_reusable_values == True``."""
48 return st.one_of(
49 # This looks like it should be `one_of`, but `sampled_from` is correct
50 # because we want this meta-strategy to yield strategies as its values.
51 st.sampled_from(base_reusable_strategies),
52 # This sometimes produces invalid combinations of arguments, which
53 # we filter out further down with an explicit validation check.
54 st.builds(
55 st.floats,
56 min_value=st.none() | st.floats(allow_nan=False),
57 max_value=st.none() | st.floats(allow_nan=False),
58 allow_infinity=st.booleans(),
59 allow_nan=st.booleans(),
60 ),
61 st.builds(st.just, st.builds(list)),
62 st.builds(st.sampled_from, st.lists(st.builds(list), min_size=1)),
63 st.lists(reusable).map(st.one_of),
64 st.lists(reusable).map(lambda ls: st.tuples(*ls)),
65 )
66
67
68 def is_valid(s):
69 try:
70 s.validate()
71 return True
72 except InvalidArgument:
73 return False
74
75
76 reusable = reusable.filter(is_valid)
77
78 assert not reusable.is_empty
79
80
81 def many_examples(examples):
82 """Helper decorator to apply the ``@example`` decorator multiple times,
83 once for each given example."""
84
85 def accept(f):
86 for e in examples:
87 f = example(e)(f)
88 return f
89
90 return accept
91
92
93 @many_examples(base_reusable_strategies)
94 @many_examples(st.tuples(s) for s in base_reusable_strategies)
95 @given(reusable)
96 def test_reusable_strategies_are_all_reusable(s):
97 assert s.has_reusable_values
98
99
100 @many_examples(base_reusable_strategies)
101 @given(reusable)
102 def test_filter_breaks_reusability(s):
103 cond = True
104
105 def nontrivial_filter(x):
106 """Non-trivial filtering function, intended to remain opaque even if
107 some strategies introspect their filters."""
108 return cond
109
110 assert s.has_reusable_values
111 assert not s.filter(nontrivial_filter).has_reusable_values
112
113
114 @many_examples(base_reusable_strategies)
115 @given(reusable)
116 def test_map_breaks_reusability(s):
117 cond = True
118
119 def nontrivial_map(x):
120 """Non-trivial mapping function, intended to remain opaque even if
121 some strategies introspect their mappings."""
122 if cond:
123 return x
124 else:
125 return None
126
127 assert s.has_reusable_values
128 assert not s.map(nontrivial_map).has_reusable_values
129
130
131 @many_examples(base_reusable_strategies)
132 @given(reusable)
133 def test_flatmap_breaks_reusability(s):
134 cond = True
135
136 def nontrivial_flatmap(x):
137 """Non-trivial flat-mapping function, intended to remain opaque even
138 if some strategies introspect their flat-mappings."""
139 if cond:
140 return st.just(x)
141 else:
142 return st.none()
143
144 assert s.has_reusable_values
145 assert not s.flatmap(nontrivial_flatmap).has_reusable_values
146
147
148 @pytest.mark.parametrize(
149 "strat",
150 [
151 st.lists(st.booleans()),
152 st.sets(st.booleans()),
153 st.dictionaries(st.booleans(), st.booleans()),
154 ],
155 )
156 def test_mutable_collections_do_not_have_reusable_values(strat):
157 assert not strat.has_reusable_values
158
159
160 def test_recursion_does_not_break_reusability():
161 x = st.deferred(lambda: st.none() | st.tuples(x))
162 assert x.has_reusable_values