Debian Codegraph @

hypothesis-python/tests/snapshots/test_always_failing.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 hashlib
12 from ipaddress import IPv4Address
13
14 import pytest
15 from pytest import param
16
17 from hypothesis import given, strategies as st
18
19 from tests.common.utils import run_test_for_falsifying_example
20 from tests.snapshots.conftest import EXPLAIN_SETTINGS, SNAPSHOT_SETTINGS
21
22
23 class Opaque:
24 """Object with no useful repr, forcing call-style output."""
25
26
27 class Pair:
28 def __init__(self, x, y):
29 self.x = x
30 self.y = y
31
32
33 @pytest.mark.parametrize(
34 "given_args",
35 [
36 # Primitives
37 param([st.integers()], id="integers"),
38 param([st.floats()], id="floats"),
39 param([st.booleans()], id="booleans"),
40 param([st.text()], id="text"),
41 param([st.binary()], id="binary"),
42 param([st.characters()], id="characters"),
43 param([st.none()], id="none"),
44 param([st.complex_numbers()], id="complex_numbers"),
45 param([st.decimals()], id="decimals"),
46 param([st.fractions()], id="fractions"),
47 # Date/time
48 param([st.dates()], id="dates"),
49 param([st.datetimes()], id="datetimes"),
50 param([st.times()], id="times"),
51 param([st.timedeltas()], id="timedeltas"),
52 # Collections
53 param([st.lists(st.integers())], id="lists"),
54 param([st.sets(st.integers())], id="sets"),
55 param([st.frozensets(st.integers())], id="frozensets"),
56 param([st.tuples(st.integers(), st.text(), st.booleans())], id="tuples"),
57 param([st.dictionaries(st.text(max_size=3), st.integers())], id="dictionaries"),
58 param(
59 [
60 st.fixed_dictionaries(
61 {"name": st.text(max_size=5), "age": st.integers()}
62 )
63 ],
64 id="fixed_dictionaries",
65 ),
66 param([st.iterables(st.integers())], id="iterables"),
67 param([st.permutations(list(range(5)))], id="permutations"),
68 # Combinators
69 param([st.just(42)], id="just"),
70 param([st.sampled_from(["alice", "bob", "charlie"])], id="sampled_from"),
71 param([st.one_of(st.integers(), st.text())], id="one_of"),
72 param([st.from_regex(r"[a-z]{3,5}", fullmatch=True)], id="from_regex"),
73 param([st.from_type(IPv4Address)], id="from_type"),
74 param(
75 [st.recursive(st.integers(), lambda s: st.lists(s, max_size=3))],
76 id="recursive",
77 ),
78 param([st.deferred(st.integers)], id="deferred"),
79 param([st.shared(st.integers(), key="test")], id="shared"),
80 param([st.integers().map(str)], id="map_to_str"),
81 param(
82 [st.binary().map(lambda b: hashlib.sha256(b).digest())], id="map_to_bytes"
83 ),
84 param([st.integers().filter(lambda n: n % 2 == 0)], id="filter"),
85 param([st.builds(Pair, x=st.integers(), y=st.text(max_size=3))], id="builds"),
86 param([st.from_type(list[int])], id="builds_from_type"),
87 param([st.functions(like=lambda x: x, returns=st.booleans())], id="functions"),
88 # Special types
89 param([st.uuids()], id="uuids"),
90 param([st.emails()], id="emails"),
91 param([st.ip_addresses()], id="ip_addresses"),
92 param([st.slices(10)], id="slices"),
93 param([st.randoms()], id="randoms"),
94 # Multi-arg
95 param([st.integers(), st.text()], id="two_args"),
96 param(
97 [st.integers(), st.floats(), st.text(), st.booleans(), st.none()],
98 id="many_args",
99 ),
100 param(
101 [
102 st.lists(st.integers()),
103 st.dictionaries(st.text(max_size=3), st.booleans()),
104 st.sampled_from([10, 20, 30]),
105 ],
106 id="mixed_strategies",
107 ),
108 # Lambda formatting
109 param([st.builds(lambda: Opaque())], id="builds_no_arg_lambda"),
110 param(
111 [st.builds(lambda n: Opaque(), n=st.integers())],
112 id="builds_single_arg_lambda",
113 ),
114 param(
115 [st.builds(lambda x, y: Opaque(), x=st.integers(), y=st.text())],
116 id="builds_multi_arg_lambda",
117 ),
118 param(
119 [st.builds(lambda x, y: Opaque(), st.integers(), st.text())],
120 id="builds_lambda_positional_args",
121 ),
122 param(
123 [
124 st.builds(
125 lambda x, y, z: Opaque(),
126 st.integers(),
127 y=st.text(),
128 z=st.booleans(),
129 )
130 ],
131 id="builds_lambda_mixed_args",
132 ),
133 param(
134 [st.builds(lambda x, y: Pair(x, y), x=st.integers(), y=st.text())],
135 id="builds_lambda_returning_object",
136 ),
137 param([st.integers().map(lambda n: Opaque())], id="map_lambda_opaque_result"),
138 param(
139 [st.integers().map(lambda n: n * 2).map(lambda n: Opaque())],
140 id="map_chained_lambdas_opaque",
141 ),
142 param(
143 [st.builds(lambda a, b="hello": Opaque(), a=st.integers())],
144 id="builds_lambda_with_defaults",
145 ),
146 param(
147 [
148 st.integers(min_value=0, max_value=3).flatmap(
149 lambda n: st.text(min_size=n, max_size=n)
150 )
151 ],
152 id="flatmap_lambda",
153 ),
154 ],
155 )
156 def test_always_failing(given_args, snapshot):
157 given_kwargs = {f"v{i}": v for i, v in enumerate(given_args)}
158
159 @SNAPSHOT_SETTINGS
160 @given(**given_kwargs)
161 def inner(**kwargs):
162 raise AssertionError
163
164 assert run_test_for_falsifying_example(inner) == snapshot
165
166
167 @pytest.mark.parametrize(
168 "given_args",
169 [
170 param(
171 [st.integers().map(lambda n: n + 1)],
172 id="map_lambda_explain_forces_call_style",
173 ),
174 param([st.from_regex(r"..", fullmatch=True)], id="explain_from_regex"),
175 param([st.integers().map(str)], id="explain_map_to_str"),
176 ],
177 )
178 def test_always_failing_explain(given_args, snapshot):
179 given_kwargs = {f"v{i}": v for i, v in enumerate(given_args)}
180
181 @EXPLAIN_SETTINGS
182 @given(**given_kwargs)
183 def inner(**kwargs):
184 raise AssertionError
185
186 assert run_test_for_falsifying_example(inner) == snapshot