diff options
author | Michael Foord <michael@voidspace.org.uk> | 2012-03-25 17:16:07 (GMT) |
---|---|---|
committer | Michael Foord <michael@voidspace.org.uk> | 2012-03-25 17:16:07 (GMT) |
commit | 313f85f0ca1adb198e1cf7c012f7f0d1692339ae (patch) | |
tree | 15e37f08c1a05ba14ec5233af56f4693d6f00441 /Lib/unittest | |
parent | 1d5617958fa4e3e1039c62cbe15565e642a9e428 (diff) | |
download | cpython-313f85f0ca1adb198e1cf7c012f7f0d1692339ae.zip cpython-313f85f0ca1adb198e1cf7c012f7f0d1692339ae.tar.gz cpython-313f85f0ca1adb198e1cf7c012f7f0d1692339ae.tar.bz2 |
unittest.mock.MagicMock objects are now unorderable by default
Diffstat (limited to 'Lib/unittest')
-rw-r--r-- | Lib/unittest/mock.py | 22 | ||||
-rw-r--r-- | Lib/unittest/test/testmock/testmagicmethods.py | 13 |
2 files changed, 26 insertions, 9 deletions
diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py index e6b103d..6371fc8 100644 --- a/Lib/unittest/mock.py +++ b/Lib/unittest/mock.py @@ -167,16 +167,15 @@ def _set_signature(mock, original, instance=False): signature, func = result src = "lambda %s: None" % signature - context = {'_mock_': mock} - checksig = eval(src, context) + checksig = eval(src, {}) _copy_func_details(func, checksig) name = original.__name__ if not name.isidentifier(): name = 'funcopy' - context = {'checksig': checksig, 'mock': mock} + context = {'_checksig_': checksig, 'mock': mock} src = """def %s(*args, **kwargs): - checksig(*args, **kwargs) + _checksig_(*args, **kwargs) return mock(*args, **kwargs)""" % name exec (src, context) funcopy = context[name] @@ -620,14 +619,16 @@ class NonCallableMock(Base): def __dir__(self): """Filter the output of `dir(mock)` to only useful members.""" + if not FILTER_DIR: + return object.__dir__(self) + extras = self._mock_methods or [] from_type = dir(type(self)) from_dict = list(self.__dict__) - if FILTER_DIR: - from_type = [e for e in from_type if not e.startswith('_')] - from_dict = [e for e in from_dict if not e.startswith('_') or - _is_magic(e)] + from_type = [e for e in from_type if not e.startswith('_')] + from_dict = [e for e in from_dict if not e.startswith('_') or + _is_magic(e)] return sorted(set(extras + from_type + from_dict + list(self._mock_children))) @@ -1582,6 +1583,10 @@ _calculate_return_value = { } _return_values = { + '__lt__': NotImplemented, + '__gt__': NotImplemented, + '__le__': NotImplemented, + '__ge__': NotImplemented, '__int__': 1, '__contains__': False, '__len__': 0, @@ -2000,7 +2005,6 @@ def create_autospec(spec, spec_set=False, instance=False, _parent=None, _parent._mock_children[_name] = mock if is_type and not instance and 'return_value' not in kwargs: - # XXXX could give a name to the return_value mock? mock.return_value = create_autospec(spec, spec_set, instance=True, _name='()', _parent=mock) diff --git a/Lib/unittest/test/testmock/testmagicmethods.py b/Lib/unittest/test/testmock/testmagicmethods.py index 65d2711..bd52e25 100644 --- a/Lib/unittest/test/testmock/testmagicmethods.py +++ b/Lib/unittest/test/testmock/testmagicmethods.py @@ -156,6 +156,19 @@ class TestMockingMagicMethods(unittest.TestCase): self. assertTrue(mock <= 3) self. assertTrue(mock >= 3) + self.assertRaises(TypeError, lambda: MagicMock() < object()) + self.assertRaises(TypeError, lambda: object() < MagicMock()) + self.assertRaises(TypeError, lambda: MagicMock() < MagicMock()) + self.assertRaises(TypeError, lambda: MagicMock() > object()) + self.assertRaises(TypeError, lambda: object() > MagicMock()) + self.assertRaises(TypeError, lambda: MagicMock() > MagicMock()) + self.assertRaises(TypeError, lambda: MagicMock() <= object()) + self.assertRaises(TypeError, lambda: object() <= MagicMock()) + self.assertRaises(TypeError, lambda: MagicMock() <= MagicMock()) + self.assertRaises(TypeError, lambda: MagicMock() >= object()) + self.assertRaises(TypeError, lambda: object() >= MagicMock()) + self.assertRaises(TypeError, lambda: MagicMock() >= MagicMock()) + def test_equality(self): for mock in Mock(), MagicMock(): |