Get mock coverage back to 100% (GH-18228)

* use the `: pass` and `: yield` patterns for code that isn't expected to ever be executed.

* The _Call items passed to _AnyComparer are only ever of length two, so assert instead of if/else

* fix typo

* Fix bug, where stop-without-start patching dict blows up with `TypeError: 'NoneType' object is not iterable`, highlighted by lack of coverage of an except branch.

* The fix for bpo-37972 means _Call.count and _Call.index are no longer needed.

* add coverage for calling next() on a mock_open with readline.return_value set.

* __aiter__ is defined on the Mock so the one on _AsyncIterator is never called.
This commit is contained in:
Chris Withers 2020-01-29 16:24:54 +00:00 committed by GitHub
parent a327677905
commit db5e86adbc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 35 additions and 54 deletions

View file

@ -1042,8 +1042,7 @@ class _AnyComparer(list):
the left."""
def __contains__(self, item):
for _call in self:
if len(item) != len(_call):
continue
assert len(item) == len(_call)
if all([
expected == actual
for expected, actual in zip(item, _call)
@ -1856,7 +1855,8 @@ def _unpatch_dict(self):
def __exit__(self, *args):
"""Unpatch the dict."""
self._unpatch_dict()
if self._original is not None:
self._unpatch_dict()
return False
@ -2168,7 +2168,7 @@ def __init__(self, /, *args, **kwargs):
self.__dict__['__code__'] = code_mock
async def _execute_mock_call(self, /, *args, **kwargs):
# This is nearly just like super(), except for sepcial handling
# This is nearly just like super(), except for special handling
# of coroutines
_call = self.call_args
@ -2541,12 +2541,6 @@ def __getattribute__(self, attr):
return tuple.__getattribute__(self, attr)
def count(self, /, *args, **kwargs):
return self.__getattr__('count')(*args, **kwargs)
def index(self, /, *args, **kwargs):
return self.__getattr__('index')(*args, **kwargs)
def _get_call_arguments(self):
if len(self) == 2:
args, kwargs = self
@ -2917,9 +2911,6 @@ def __init__(self, iterator):
code_mock.co_flags = inspect.CO_ITERABLE_COROUTINE
self.__dict__['__code__'] = code_mock
def __aiter__(self):
return self
async def __anext__(self):
try:
return next(self.iterator)