Debian Codegraph @

hypothesis-python/tests/cover/test_functions.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 inspect import signature
12
13 import pytest
14
15 from hypothesis import Verbosity, assume, find, given, settings, strategies as st
16 from hypothesis.errors import InvalidArgument, InvalidState
17 from hypothesis.reporting import with_reporter
18 from hypothesis.strategies import booleans, functions, integers
19
20 from tests.common.debug import check_can_generate_examples
21
22
23 def func_a():
24 pass
25
26
27 @given(functions(like=func_a, returns=booleans()))
28 def test_functions_no_args(f):
29 assert f.__name__ == "func_a"
30 assert f is not func_a
31 assert isinstance(f(), bool)
32
33
34 def func_b(a, b, c):
35 pass
36
37
38 @given(functions(like=func_b, returns=booleans()))
39 def test_functions_with_args(f):
40 assert f.__name__ == "func_b"
41 assert f is not func_b
42 with pytest.raises(TypeError):
43 f()
44 assert isinstance(f(1, 2, 3), bool)
45
46
47 def func_c(**kwargs):
48 pass
49
50
51 @given(functions(like=func_c, returns=booleans()))
52 def test_functions_kw_args(f):
53 assert f.__name__ == "func_c"
54 assert f is not func_c
55 with pytest.raises(TypeError):
56 f(1, 2, 3)
57 assert isinstance(f(a=1, b=2, c=3), bool)
58
59
60 @given(functions(like=lambda: None, returns=booleans()))
61 def test_functions_argless_lambda(f):
62 assert f.__name__ == "<lambda>"
63 with pytest.raises(TypeError):
64 f(1)
65 assert isinstance(f(), bool)
66
67
68 @given(functions(like=lambda a: None, returns=booleans()))
69 def test_functions_lambda_with_arg(f):
70 assert f.__name__ == "<lambda>"
71 with pytest.raises(TypeError):
72 f()
73 assert isinstance(f(1), bool)
74
75
76 @pytest.mark.parametrize(
77 "like,returns,pure",
78 [
79 (None, booleans(), False),
80 (lambda: None, "not a strategy", True),
81 (lambda: None, booleans(), None),
82 ],
83 )
84 def test_invalid_arguments(like, returns, pure):
85 with pytest.raises(InvalidArgument):
86 check_can_generate_examples(functions(like=like, returns=returns, pure=pure))
87
88
89 def func_returns_str() -> str:
90 return "a string"
91
92
93 @given(functions(like=func_returns_str))
94 def test_functions_strategy_return_type_inference(f):
95 result = f()
96 assume(result != "a string")
97 assert isinstance(result, str)
98
99
100 def test_functions_valid_within_given_invalid_outside():
101 cache = None
102
103 @given(functions())
104 def t(f):
105 nonlocal cache
106 cache = f
107 assert f() is None
108
109 t()
110 with pytest.raises(InvalidState):
111 cache()
112
113
114 def test_can_call_default_like_arg():
115 # This test is somewhat silly, but coverage complains about the uncovered
116 # branch for calling it otherwise and alternative workarounds are worse.
117 like, returns, pure = signature(functions).parameters.values()
118 assert like.default() is None
119 assert returns.default is ...
120 assert pure.default is False
121
122
123 def func(arg, *, kwonly_arg):
124 pass
125
126
127 @given(functions(like=func))
128 def test_functions_strategy_with_kwonly_args(f):
129 with pytest.raises(TypeError):
130 f(1, 2)
131 f(1, kwonly_arg=2)
132 f(kwonly_arg=2, arg=1)
133
134
135 def pure_func(arg1, arg2):
136 pass
137
138
139 @given(
140 f=functions(like=pure_func, returns=integers(), pure=True),
141 arg1=integers(),
142 arg2=integers(),
143 )
144 def test_functions_pure_with_same_args(f, arg1, arg2):
145 # Same regardless of calling convention, unlike functools.lru_cache()
146 expected = f(arg1, arg2)
147 assert f(arg1, arg2) == expected
148 assert f(arg1, arg2=arg2) == expected
149 assert f(arg1=arg1, arg2=arg2) == expected
150 assert f(arg2=arg2, arg1=arg1) == expected
151
152
153 @given(
154 f=functions(like=pure_func, returns=integers(), pure=True),
155 arg1=integers(),
156 arg2=integers(),
157 )
158 def test_functions_pure_with_different_args(f, arg1, arg2):
159 r1 = f(arg1, arg2)
160 r2 = f(arg2, arg1)
161 assume(r1 != r2)
162 # If this is never true, the test will fail with Unsatisfiable
163
164
165 @given(
166 f1=functions(like=pure_func, returns=integers(), pure=True),
167 f2=functions(like=pure_func, returns=integers(), pure=True),
168 )
169 def test_functions_pure_two_functions_different_args_different_result(f1, f2):
170 r1 = f1(1, 2)
171 r2 = f2(3, 4)
172 assume(r1 != r2)
173 # If this is never true, the test will fail with Unsatisfiable
174
175
176 @given(
177 f1=functions(like=pure_func, returns=integers(), pure=True),
178 f2=functions(like=pure_func, returns=integers(), pure=True),
179 arg1=integers(),
180 arg2=integers(),
181 )
182 def test_functions_pure_two_functions_same_args_different_result(f1, f2, arg1, arg2):
183 r1 = f1(arg1, arg2)
184 r2 = f2(arg1, arg2)
185 assume(r1 != r2)
186 # If this is never true, the test will fail with Unsatisfiable
187
188
189 @settings(verbosity=Verbosity.verbose)
190 @given(functions(pure=False))
191 def test_functions_note_all_calls_to_impure_functions(f):
192 ls = []
193 with with_reporter(ls.append):
194 f()
195 f()
196 assert len(ls) == 2
197
198
199 @settings(verbosity=Verbosity.verbose)
200 @given(functions(pure=True))
201 def test_functions_note_only_first_to_pure_functions(f):
202 ls = []
203 with with_reporter(ls.append):
204 f()
205 f()
206 assert len(ls) == 1
207
208
209 def test_functions_supports_find():
210 f = find(
211 st.functions(like=pure_func, returns=st.integers(), pure=True), lambda x: True
212 )
213 with pytest.raises(InvalidState):
214 f(1, 2)
215 assert f.__name__ == pure_func.__name__