Debian Codegraph @

hypothesis-python/tests/snapshots/test_explain.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 from hypothesis import given, strategies as st
12
13 from tests.common.utils import run_test_for_falsifying_example
14 from tests.snapshots.conftest import EXPLAIN_SETTINGS
15
16
17 def test_explain_comments_basic_fail_if_either(snapshot):
18 @EXPLAIN_SETTINGS
19 @given(
20 st.booleans(),
21 st.booleans(),
22 st.lists(st.none()),
23 st.booleans(),
24 st.booleans(),
25 )
26 def inner(a, b, c, d, e):
27 assert not (b and d)
28
29 assert run_test_for_falsifying_example(inner) == snapshot
30
31
32 def test_explain_comments_basic_fail_if_not_all(snapshot):
33 @EXPLAIN_SETTINGS
34 @given(st.text(), st.text(), st.text())
35 def inner(a, b, c):
36 condition = a and b and c
37 assert condition
38
39 assert run_test_for_falsifying_example(inner) == snapshot
40
41
42 def test_explain_no_together_comment_if_single_argument(snapshot):
43 @EXPLAIN_SETTINGS
44 @given(st.text(), st.text())
45 def inner(a, b):
46 assert a
47
48 assert run_test_for_falsifying_example(inner) == snapshot
49
50
51 class MyClass:
52 def __init__(self, x, y):
53 self.x = x
54 self.y = y
55
56
57 def test_explain_builds_subargs(snapshot):
58 @EXPLAIN_SETTINGS
59 @given(st.builds(MyClass, st.integers(), st.booleans()))
60 def inner(obj):
61 assert not obj.y
62
63 assert run_test_for_falsifying_example(inner) == snapshot
64
65
66 def test_explain_builds_kwargs_subargs(snapshot):
67 @EXPLAIN_SETTINGS
68 @given(st.builds(MyClass, x=st.integers(), y=st.booleans()))
69 def inner(obj):
70 assert not obj.y
71
72 assert run_test_for_falsifying_example(inner) == snapshot
73
74
75 def test_explain_tuple_subargs(snapshot):
76 @EXPLAIN_SETTINGS
77 @given(st.tuples(st.integers(), st.booleans()))
78 def inner(t):
79 assert not t[1]
80
81 assert run_test_for_falsifying_example(inner) == snapshot
82
83
84 def test_explain_fixeddict_subargs(snapshot):
85 @EXPLAIN_SETTINGS
86 @given(st.fixed_dictionaries({"x": st.integers(), "y": st.booleans()}))
87 def inner(d):
88 assert not d["y"]
89
90 assert run_test_for_falsifying_example(inner) == snapshot
91
92
93 def test_explain_tuple_multiple_varying(snapshot):
94 @EXPLAIN_SETTINGS
95 @given(st.tuples(st.integers(), st.text(), st.booleans()))
96 def inner(t):
97 assert not t[2]
98
99 assert run_test_for_falsifying_example(inner) == snapshot
100
101
102 def test_explain_skip_subset_slices(snapshot):
103 @EXPLAIN_SETTINGS
104 @given(st.builds(MyClass, st.tuples(st.integers(), st.booleans()), y=st.booleans()))
105 def inner(obj):
106 assert obj.y
107
108 assert run_test_for_falsifying_example(inner) == snapshot
109
110
111 def test_explain_duplicate_param_names(snapshot):
112 @EXPLAIN_SETTINGS
113 @given(
114 kw=st.integers(),
115 b=st.fixed_dictionaries({"kw": st.text(), "c": st.booleans()}),
116 )
117 def inner(kw, b):
118 assert not b["c"]
119
120 assert run_test_for_falsifying_example(inner) == snapshot
121
122
123 class Outer:
124 def __init__(self, inner, value):
125 self.inner = inner
126 self.value = value
127
128
129 class Inner:
130 def __init__(self, x):
131 self.x = x
132
133
134 def test_explain_multi_level_nesting(snapshot):
135 @EXPLAIN_SETTINGS
136 @given(
137 bare=st.integers(),
138 outer=st.builds(
139 Outer, inner=st.builds(Inner, x=st.integers()), value=st.booleans()
140 ),
141 )
142 def inner(bare, outer):
143 assert not outer.value
144
145 assert run_test_for_falsifying_example(inner) == snapshot
146
147
148 # Regression for https://github.com/HypothesisWorks/hypothesis/issues/4708.
149 # The composite reconstructs ``st.one_of(st.just(W(None)), ...)`` per call,
150 # so the OneOfStrategy's label is unstable across runs. Previously this
151 # tripped an assertion in the explain phase.
152 class W:
153 def __init__(self, a):
154 self.a = a
155
156
157 @st.composite
158 def _wrapped_4708(draw):
159 inner = st.one_of(st.integers(), st.tuples(st.integers(), st.integers()))
160 elements = st.tuples(inner, st.just(0))
161 structured = st.lists(elements, min_size=1, max_size=3)
162 return draw(st.one_of(st.just(W(None)), structured.map(W)))
163
164
165 def test_explain_unstable_one_of_labels(snapshot):
166 @EXPLAIN_SETTINGS
167 @given(_wrapped_4708())
168 def inner(w):
169 assert not isinstance(w.a, list)
170
171 assert run_test_for_falsifying_example(inner) == snapshot