diff options
author | Xtreak <tir.karthi@gmail.com> | 2019-04-22 08:00:23 +0530 |
---|---|---|
committer | Chris Withers <chris@withers.org> | 2019-04-30 08:39:55 +0100 |
commit | 6039ff3979a1bed5a5737d71dde8d0e91f4a46e1 (patch) | |
tree | f67c4ed617db652dafd4f9179385796ca1edcae8 /mock/tests | |
parent | 0e50d0d1e6d90d7297548aceae6a5ff9ee62a812 (diff) | |
download | mock-6039ff3979a1bed5a5737d71dde8d0e91f4a46e1.tar.gz |
bpo-23078: Add support for {class,static}method to mock.create_autospec() (GH-11613)
Co-authored-by: Felipe <felipe.nospam.ochoa@gmail.com>
Backports: 9b21856b0fcda949de239edc7aa6cf3f2f4f77a3
Signed-off-by: Chris Withers <chris@withers.org>
Diffstat (limited to 'mock/tests')
-rw-r--r-- | mock/tests/testhelpers.py | 40 | ||||
-rw-r--r-- | mock/tests/testmock.py | 17 | ||||
-rw-r--r-- | mock/tests/testpatch.py | 20 |
3 files changed, 76 insertions, 1 deletions
diff --git a/mock/tests/testhelpers.py b/mock/tests/testhelpers.py index 8ff276e..e44e4ee 100644 --- a/mock/tests/testhelpers.py +++ b/mock/tests/testhelpers.py @@ -13,7 +13,7 @@ from mock import ( call, create_autospec, MagicMock, Mock, ANY, patch, PropertyMock ) -from mock.mock import _Call, _CallList +from mock.mock import _Call, _CallList, _callable from datetime import datetime from functools import partial @@ -1107,5 +1107,43 @@ class TestCallList(unittest.TestCase): self.assertNotIsInstance(returned, PropertyMock) +class TestCallablePredicate(unittest.TestCase): + + def test_type(self): + for obj in [str, bytes, int, list, tuple, SomeClass]: + self.assertTrue(_callable(obj)) + + def test_call_magic_method(self): + class Callable: + def __call__(self): + pass + instance = Callable() + self.assertTrue(_callable(instance)) + + def test_staticmethod(self): + class WithStaticMethod: + @staticmethod + def staticfunc(): + pass + self.assertTrue(_callable(WithStaticMethod.staticfunc)) + + def test_non_callable_staticmethod(self): + class BadStaticMethod: + not_callable = staticmethod(None) + self.assertFalse(_callable(BadStaticMethod.not_callable)) + + def test_classmethod(self): + class WithClassMethod: + @classmethod + def classfunc(cls): + pass + self.assertTrue(_callable(WithClassMethod.classfunc)) + + def test_non_callable_classmethod(self): + class BadClassMethod: + not_callable = classmethod(None) + self.assertFalse(_callable(BadClassMethod.not_callable)) + + if __name__ == '__main__': unittest.main() diff --git a/mock/tests/testmock.py b/mock/tests/testmock.py index dd6bfbd..ae37637 100644 --- a/mock/tests/testmock.py +++ b/mock/tests/testmock.py @@ -1482,6 +1482,23 @@ class MockTest(unittest.TestCase): m = mock.create_autospec(object(), name='sweet_func') self.assertIn('sweet_func', repr(m)) + #Issue23078 + def test_create_autospec_classmethod_and_staticmethod(self): + class TestClass: + @classmethod + def class_method(cls): + pass + + @staticmethod + def static_method(): + pass + for method in ('class_method', 'static_method'): + with self.subTest(method=method): + mock_method = mock.create_autospec(getattr(TestClass, method)) + mock_method() + mock_method.assert_called_once_with() + self.assertRaises(TypeError, mock_method, 'extra_arg') + #Issue21238 def test_mock_unsafe(self): m = Mock() diff --git a/mock/tests/testpatch.py b/mock/tests/testpatch.py index 586b1d3..bbd81fb 100644 --- a/mock/tests/testpatch.py +++ b/mock/tests/testpatch.py @@ -55,6 +55,14 @@ class Foo(object): pass foo = 'bar' + @staticmethod + def static_method(): + return 24 + + @classmethod + def class_method(cls): + return 42 + class Bar(object): def a(self): pass @@ -1025,6 +1033,18 @@ class PatchTest(unittest.TestCase): self.assertEqual(result, 3) + def test_autospec_staticmethod(self): + with patch('%s.Foo.static_method' % __name__, autospec=True) as method: + Foo.static_method() + method.assert_called_once_with() + + + def test_autospec_classmethod(self): + with patch('%s.Foo.class_method' % __name__, autospec=True) as method: + Foo.class_method() + method.assert_called_once_with() + + def test_autospec_with_new(self): patcher = patch('%s.function' % __name__, new=3, autospec=True) self.assertRaises(TypeError, patcher.start) |