Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def teardown(*kargs, **kwargs):
saved = {}
for mock_object, expectations in FlexmockContainer.flexmock_objects.items():
saved[mock_object] = expectations[:]
for expectation in expectations:
expectation.reset()
instances = [x._object for x in saved.keys()
if not isinstance(x._object, Mock) and not inspect.isclass(x)]
classes = [x._object for x in saved.keys() if inspect.isclass(x._object)]
for obj in set(instances + classes):
for attr in Mock.UPDATED_ATTRS:
if (hasattr(obj, '__dict__') and
type(obj.__dict__) is dict and attr in obj.__dict__):
if (_get_code(getattr(obj, attr)) is _get_code(getattr(Mock, attr))):
del obj.__dict__[attr]
elif hasattr(obj, attr):
try:
if (_get_code(getattr(obj, attr)) is _get_code(getattr(Mock, attr))):
delattr(obj, attr)
def flexmock_teardown():
"""Performs lexmock-specific teardown tasks."""
saved = {}
instances = []
classes = []
for mock_object, expectations in FlexmockContainer.flexmock_objects.items():
saved[mock_object] = expectations[:]
for expectation in expectations:
_getattr(expectation, 'reset')()
for mock in saved.keys():
obj = mock._object
if not isinstance(obj, Mock) and not _isclass(obj):
instances.append(obj)
if _isclass(obj):
classes.append(obj)
for obj in instances + classes:
for attr in UPDATED_ATTRS:
try:
obj_dict = obj.__dict__
if _get_code(obj_dict[attr]) is _get_code(Mock.__dict__[attr]):
del obj_dict[attr]
except:
def _create_partial_mock(obj_or_class, **kwargs):
matches = [x for x in FlexmockContainer.flexmock_objects
if x._object is obj_or_class]
if matches:
mock = matches[0]
else:
mock = Mock()
mock._object = obj_or_class
for method, return_value in kwargs.items():
mock.should_receive(method).and_return(return_value)
if not matches:
FlexmockContainer.add_expectation(mock, Expectation(obj_or_class))
if (_attach_flexmock_methods(mock, Mock, obj_or_class) and
not inspect.isclass(mock._object)):
mock = mock._object
return mock
(not inspect.isclass(self._object) and
not inspect.ismodule(self._object))):
method = ('_%s__%s' % (self._object.__class__.__name__,
method.lstrip('_')))
if (not isinstance(self._object, Mock) and
not hasattr(self._object, method)):
raise MethodDoesNotExist('%s does not have method %s' %
(self._object, method))
if chained_methods:
return_value = Mock()
chained_expectation = return_value.should_receive(chained_methods)
if self not in FlexmockContainer.flexmock_objects:
FlexmockContainer.flexmock_objects[self] = []
expectation = self._retrieve_or_create_expectation(method, return_value)
if expectation not in FlexmockContainer.flexmock_objects[self]:
FlexmockContainer.flexmock_objects[self].append(expectation)
self._update_method(expectation, method)
if chained_methods:
return chained_expectation
else:
return expectation
def _create_partial_mock(obj_or_class, **kwargs):
matches = [x for x in FlexmockContainer.flexmock_objects
if x._object is obj_or_class]
if matches:
mock = matches[0]
else:
mock = Mock()
mock._object = obj_or_class
for name, return_value in kwargs.items():
if hasattr(return_value, '__call__'):
mock.should_receive(name).replace_with(return_value)
else:
mock.should_receive(name).and_return(return_value)
if not matches:
FlexmockContainer.add_expectation(mock, Expectation(obj_or_class))
if (_attach_flexmock_methods(mock, Mock, obj_or_class) and
not _isclass(mock._object)):
mock = mock._object
def _create_expectation(self, obj, name, return_value=None):
if self not in FlexmockContainer.flexmock_objects:
FlexmockContainer.flexmock_objects[self] = []
expectation = self._save_expectation(name, return_value)
FlexmockContainer.add_expectation(self, expectation)
if _isproperty(obj, name):
self._update_property(expectation, name, return_value)
elif (isinstance(obj, Mock) or
hasattr(getattr(obj, name), '__call__') or
_isclass(getattr(obj, name))):
self._update_method(expectation, name)
else:
self._update_attribute(expectation, name, return_value)
return expectation