Skip to content

Commit

Permalink
Remove AnyCompare and use call objects everywhere.
Browse files Browse the repository at this point in the history
  • Loading branch information
tirkarthi committed Aug 19, 2019
1 parent 7a89117 commit bc09989
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 21 deletions.
25 changes: 4 additions & 21 deletions Lib/unittest/mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -869,7 +869,7 @@ def assert_called_with(self, /, *args, **kwargs):
def _error_message():
msg = self._format_mock_failure_message(args, kwargs)
return msg
expected = self._call_matcher(_Call((args, kwargs)))
expected = self._call_matcher(_Call((args, kwargs), two=True))
actual = self._call_matcher(self.call_args)
if actual != expected:
cause = expected if isinstance(expected, Exception) else None
Expand Down Expand Up @@ -932,9 +932,9 @@ def assert_any_call(self, /, *args, **kwargs):
`assert_called_with` and `assert_called_once_with` that only pass if
the call is the most recent one."""
expected = self._call_matcher(_Call((args, kwargs), two=True))
cause = expected if isinstance(expected, Exception) else None
actual = [self._call_matcher(c) for c in self.call_args_list]
if cause or expected not in _AnyComparer(actual):
if expected not in actual:
cause = expected if isinstance(expected, Exception) else None
expected_string = self._format_mock_call_signature(args, kwargs)
raise AssertionError(
'%s call not found' % expected_string
Expand Down Expand Up @@ -987,23 +987,6 @@ def _calls_repr(self, prefix="Calls"):
return f"\n{prefix}: {safe_repr(self.mock_calls)}."


class _AnyComparer(list):
"""A list which checks if it contains a call which may have an
argument of ANY, flipping the components of item and self from
their traditional locations so that ANY is guaranteed to be on
the left."""
def __contains__(self, item):
for _call in self:
if len(item) != len(_call):
continue
if all([
expected == actual
for expected, actual in zip(item, _call)
]):
return True
return False


def _try_iter(obj):
if obj is None:
return obj
Expand Down Expand Up @@ -2177,7 +2160,7 @@ def assert_any_await(self, /, *args, **kwargs):
"""
expected = self._call_matcher(_Call((args, kwargs), two=True))
actual = [self._call_matcher(c) for c in self.await_args_list]
if expected not in _AnyComparer(actual):
if expected not in actual:
cause = expected if isinstance(expected, Exception) else None
expected_string = self._format_mock_call_signature(args, kwargs)
raise AssertionError(
Expand Down
4 changes: 4 additions & 0 deletions Lib/unittest/test/testmock/testasync.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,10 @@ async def main():
spec.assert_awaited_with(1, 2, c=3)
spec.assert_awaited()

with self.assertRaises(AssertionError):
spec.assert_any_await(e=1)


def test_patch_with_autospec(self):

async def test_async():
Expand Down

0 comments on commit bc09989

Please sign in to comment.