Skip to content

Commit a191a57

Browse files
committed
test(pytest): check assert_trace never calls __eq__ on closure variables
1 parent 7ca652f commit a191a57

1 file changed

Lines changed: 79 additions & 0 deletions

File tree

‎tests/test_contrib/test_pytest/test_plugin_has_trace.py‎

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,46 @@ def _second_decorated_function(container_type, container_value):
3939
return container_type(container_value)
4040

4141

42+
class _NotComparable:
43+
"""Object that must only be compared by identity."""
44+
45+
def __eq__(self, other):
46+
raise AssertionError('__eq__ must not be called')
47+
48+
def __hash__(self):
49+
raise AssertionError('__hash__ must not be called')
50+
51+
def __bool__(self):
52+
raise AssertionError('__bool__ must not be called')
53+
54+
55+
def _not_comparable_decorator(function):
56+
closure_value = _NotComparable()
57+
58+
def decorator(*args, **kwargs):
59+
return function(closure_value, *args, **kwargs)
60+
61+
return decorator
62+
63+
64+
@_not_comparable_decorator
65+
def _first_not_comparable_function(
66+
closure_value,
67+
container_type,
68+
container_value,
69+
):
70+
return container_type(container_value)
71+
72+
73+
@_not_comparable_decorator
74+
def _second_not_comparable_function(
75+
closure_value,
76+
container_type,
77+
container_value,
78+
):
79+
return container_type(container_value)
80+
81+
4282
@pytest.mark.parametrize(
4383
'container_type',
4484
[ # noqa: WPS118
@@ -169,3 +209,42 @@ def test_same_decorator_failed_assert_trace(
169209
returns.assert_trace(container_type, _first_decorated_function),
170210
):
171211
_second_decorated_function(container_type, 1)
212+
213+
214+
@pytest.mark.parametrize(
215+
'container_type',
216+
[ # noqa: WPS118
217+
Success,
218+
Failure,
219+
IOSuccess,
220+
IOFailure,
221+
],
222+
)
223+
def test_identity_closure_assert_trace(
224+
container_type,
225+
returns: ReturnsAsserts,
226+
):
227+
"""Test that closure variables are compared by identity only."""
228+
with returns.assert_trace(container_type, _first_not_comparable_function):
229+
_first_not_comparable_function(container_type, 1)
230+
231+
232+
@pytest.mark.parametrize(
233+
'container_type',
234+
[ # noqa: WPS118
235+
Success,
236+
Failure,
237+
IOSuccess,
238+
IOFailure,
239+
],
240+
)
241+
def test_identity_closure_failed_assert_trace(
242+
container_type,
243+
returns: ReturnsAsserts,
244+
):
245+
"""Test that other closure variables are not compared by ``__eq__``."""
246+
with (
247+
pytest.raises(pytest.fail.Exception),
248+
returns.assert_trace(container_type, _first_not_comparable_function),
249+
):
250+
_second_not_comparable_function(container_type, 1)

0 commit comments

Comments
 (0)