|
1 | 1 | from collections.abc import Callable, Sequence |
2 | | -from contextlib import ExitStack |
3 | 2 | from typing import Any, TypeVar |
4 | 3 |
|
5 | 4 | import pytest |
|
17 | 16 | RequiresContextResultE, |
18 | 17 | ) |
19 | 18 | from returns.contrib.hypothesis.laws import ( |
20 | | - _enter_hypothesis_context, # noqa: PLC2701 |
21 | 19 | _Settings, # noqa: PLC2701 |
| 20 | + _types_to_strategies, # noqa: PLC2701 |
22 | 21 | ) |
23 | 22 | from returns.contrib.hypothesis.type_resolver import ( |
24 | 23 | StrategyFactory, |
@@ -116,134 +115,95 @@ def test_custom_readerresult_types_resolve( |
116 | 115 | _ValueType = TypeVar('_ValueType') |
117 | 116 |
|
118 | 117 |
|
119 | | -def test_hypothesis_state_outside_context() -> None: # noqa: WPS210 |
120 | | - """Check values of strategies before we register them. |
121 | | -
|
122 | | - This is mostly useful as a baseline to compare the the values when we do |
123 | | - register them. |
124 | | - """ |
| 118 | +def test_types_to_strategies_default() -> None: # noqa: WPS210 |
| 119 | + """Check the default strategies for types.""" |
125 | 120 | container_type = test_custom_type_applicative._Wrapper # noqa: SLF001 |
126 | 121 | # NOTE: There is a type error because `Callable` is a |
127 | 122 | # special form, not a type. |
128 | 123 | callable_type: type[object] = Callable # type: ignore[assignment] |
129 | 124 |
|
130 | | - container_strategy_outside = look_up_strategy(container_type) |
131 | | - interface_strategies_outside = _interface_factories(container_type) |
132 | | - pure_functions_strategy_outside = look_up_strategy(callable_type) |
133 | | - type_var_strategy_outside = look_up_strategy(TypeVar) |
134 | | - |
135 | | - assert ( |
136 | | - _strategy_string(container_strategy_outside, container_type) == 'None' |
137 | | - ) |
138 | | - assert _strategy_strings(interface_strategies_outside, container_type) == [ |
139 | | - 'None', |
140 | | - 'None', |
141 | | - ] |
142 | | - assert ( |
143 | | - _strategy_string( |
144 | | - pure_functions_strategy_outside, Callable[[int, str], bool] |
145 | | - ) |
146 | | - == 'functions(like=lambda *a, **k: None, returns=booleans())' |
147 | | - ) |
148 | | - assert ( |
149 | | - _strategy_string(type_var_strategy_outside, _ValueType) |
150 | | - == "shared(sampled_from([<class 'NoneType'>, <class 'bool'>," |
151 | | - " <class 'int'>, <class 'float'>, <class 'str'>, <class 'bytes'>])," |
152 | | - " key='typevar=~_ValueType').flatmap(from_type)" |
| 125 | + result = _types_to_strategies( |
| 126 | + container_type, |
| 127 | + _Settings( |
| 128 | + settings_kwargs={}, |
| 129 | + use_init=False, |
| 130 | + container_strategy=None, |
| 131 | + ), |
153 | 132 | ) |
154 | 133 |
|
155 | | - |
156 | | -def test_hypothesis_state_inside_context() -> None: # noqa: WPS210 |
157 | | - """Check that strategies are registered correctly.""" |
158 | | - container_type = test_custom_type_applicative._Wrapper # noqa: SLF001 |
159 | | - # NOTE: There is a type error because `Callable` is a |
160 | | - # special form, not a type. |
161 | | - callable_type: type[object] = Callable # type: ignore[assignment] |
162 | | - |
163 | | - with ExitStack() as stack: |
164 | | - _enter_hypothesis_context( |
165 | | - stack, |
166 | | - container_type, |
167 | | - settings=_Settings( |
168 | | - settings_kwargs={}, use_init=False, container_strategy=None |
169 | | - ), |
170 | | - ) |
171 | | - container_strategy = look_up_strategy(container_type) |
172 | | - interface_strategies = _interface_factories(container_type) |
173 | | - pure_functions_strategy = look_up_strategy(callable_type) |
174 | | - type_var_strategy = look_up_strategy(TypeVar) |
175 | | - |
176 | 134 | wrapper_strategy = ( |
177 | 135 | "builds(from_value, shared(sampled_from([<class 'NoneType'>," |
178 | 136 | " <class 'bool'>, <class 'int'>, <class 'float'>, <class 'str'>," |
179 | 137 | " <class 'bytes'>]), key='typevar=~_FirstType').flatmap(from_type))" |
180 | 138 | ) |
181 | 139 | assert ( |
182 | | - _strategy_string(container_strategy, container_type) == wrapper_strategy |
| 140 | + _strategy_string(result[container_type], container_type) |
| 141 | + == wrapper_strategy |
183 | 142 | ) |
184 | | - assert _strategy_strings(interface_strategies, container_type) == [ |
| 143 | + assert _strategy_strings( |
| 144 | + [result[interface] for interface in container_type.laws()], |
| 145 | + container_type, |
| 146 | + ) == [ |
185 | 147 | wrapper_strategy, |
186 | 148 | wrapper_strategy, |
187 | 149 | ] |
188 | 150 | assert ( |
189 | | - _strategy_string(pure_functions_strategy, Callable[[int, str], bool]) |
| 151 | + _strategy_string(result[callable_type], Callable[[int, str], bool]) |
190 | 152 | == 'functions(like=lambda *args, **kwargs: <unknown>,' |
191 | 153 | ' returns=booleans(), pure=True)' |
192 | 154 | ) |
193 | 155 | assert ( |
194 | | - _strategy_string(pure_functions_strategy, Callable[[], None]) |
| 156 | + _strategy_string(result[callable_type], Callable[[], None]) |
195 | 157 | == 'functions(like=lambda: None, returns=none(), pure=True)' |
196 | 158 | ) |
197 | 159 | assert ( |
198 | | - _strategy_string(type_var_strategy, _ValueType) |
| 160 | + _strategy_string(result[TypeVar], _ValueType) |
199 | 161 | == "shared(sampled_from([<class 'NoneType'>, <class 'bool'>," |
200 | 162 | " <class 'int'>, <class 'float'>, <class 'str'>, <class 'bytes'>])," |
201 | 163 | " key='typevar=~_ValueType').flatmap(from_type).filter(lambda" |
202 | 164 | ' inner: inner == inner)' |
203 | 165 | ) |
204 | 166 |
|
205 | 167 |
|
206 | | -def test_hypothesis_state_with_setting() -> None: # noqa: WPS210 |
| 168 | +def test_types_to_strategies_overrides() -> None: # noqa: WPS210 |
207 | 169 | """Check that we prefer the strategies in settings.""" |
208 | 170 | container_type = test_custom_type_applicative._Wrapper # noqa: SLF001 |
209 | 171 | # NOTE: There is a type error because `Callable` is a |
210 | 172 | # special form, not a type. |
211 | 173 | callable_type: type[object] = Callable # type: ignore[assignment] |
212 | 174 |
|
213 | | - with ExitStack() as stack: |
214 | | - _enter_hypothesis_context( |
215 | | - stack, |
216 | | - container_type, |
217 | | - settings=_Settings( |
218 | | - settings_kwargs={}, |
219 | | - use_init=False, |
220 | | - container_strategy=st.builds(container_type, st.integers()), |
221 | | - ), |
222 | | - ) |
223 | | - container_strategy = look_up_strategy(container_type) |
224 | | - interface_strategies = _interface_factories(container_type) |
225 | | - pure_functions_strategy = look_up_strategy(callable_type) |
226 | | - type_var_strategy = look_up_strategy(TypeVar) |
| 175 | + result = _types_to_strategies( |
| 176 | + container_type, |
| 177 | + _Settings( |
| 178 | + settings_kwargs={}, |
| 179 | + use_init=False, |
| 180 | + container_strategy=st.builds(container_type, st.integers()), |
| 181 | + ), |
| 182 | + ) |
227 | 183 |
|
228 | 184 | wrapper_strategy = 'builds(_Wrapper, integers())' |
229 | 185 | assert ( |
230 | | - _strategy_string(container_strategy, container_type) == wrapper_strategy |
| 186 | + _strategy_string(result[container_type], container_type) |
| 187 | + == wrapper_strategy |
231 | 188 | ) |
232 | | - assert _strategy_strings(interface_strategies, container_type) == [ |
| 189 | + assert _strategy_strings( |
| 190 | + [result[interface] for interface in container_type.laws()], |
| 191 | + container_type, |
| 192 | + ) == [ |
233 | 193 | wrapper_strategy, |
234 | 194 | wrapper_strategy, |
235 | 195 | ] |
236 | 196 | assert ( |
237 | | - _strategy_string(pure_functions_strategy, Callable[[int, str], bool]) |
| 197 | + _strategy_string(result[callable_type], Callable[[int, str], bool]) |
238 | 198 | == 'functions(like=lambda *args, **kwargs: <unknown>,' |
239 | 199 | ' returns=booleans(), pure=True)' |
240 | 200 | ) |
241 | 201 | assert ( |
242 | | - _strategy_string(pure_functions_strategy, Callable[[], None]) |
| 202 | + _strategy_string(result[callable_type], Callable[[], None]) |
243 | 203 | == 'functions(like=lambda: None, returns=none(), pure=True)' |
244 | 204 | ) |
245 | 205 | assert ( |
246 | | - _strategy_string(type_var_strategy, _ValueType) |
| 206 | + _strategy_string(result[TypeVar], _ValueType) |
247 | 207 | == "shared(sampled_from([<class 'NoneType'>, <class 'bool'>," |
248 | 208 | " <class 'int'>, <class 'float'>, <class 'str'>, <class 'bytes'>])," |
249 | 209 | " key='typevar=~_ValueType').flatmap(from_type).filter(lambda" |
|
0 commit comments