|
5 | 5 |
|
6 | 6 | from unittest.mock import ( |
7 | 7 | call, _Call, create_autospec, MagicMock, |
8 | | - Mock, ANY, _CallList, patch, PropertyMock |
| 8 | + Mock, ANY, _CallList, patch, PropertyMock, _callable |
9 | 9 | ) |
10 | 10 |
|
11 | 11 | from datetime import datetime |
@@ -1002,5 +1002,43 @@ def test_propertymock_returnvalue(self): |
1002 | 1002 | self.assertNotIsInstance(returned, PropertyMock) |
1003 | 1003 |
|
1004 | 1004 |
|
| 1005 | +class TestCallablePredicate(unittest.TestCase): |
| 1006 | + |
| 1007 | + def test_type(self): |
| 1008 | + for obj in [str, bytes, int, list, tuple, SomeClass]: |
| 1009 | + self.assertTrue(_callable(obj)) |
| 1010 | + |
| 1011 | + def test_call_magic_method(self): |
| 1012 | + class Callable: |
| 1013 | + def __call__(self): |
| 1014 | + pass |
| 1015 | + instance = Callable() |
| 1016 | + self.assertTrue(_callable(instance)) |
| 1017 | + |
| 1018 | + def test_staticmethod(self): |
| 1019 | + class WithStaticMethod: |
| 1020 | + @staticmethod |
| 1021 | + def staticfunc(): |
| 1022 | + pass |
| 1023 | + self.assertTrue(_callable(WithStaticMethod.staticfunc)) |
| 1024 | + |
| 1025 | + def test_non_callable_staticmethod(self): |
| 1026 | + class BadStaticMethod: |
| 1027 | + not_callable = staticmethod(None) |
| 1028 | + self.assertFalse(_callable(BadStaticMethod.not_callable)) |
| 1029 | + |
| 1030 | + def test_classmethod(self): |
| 1031 | + class WithClassMethod: |
| 1032 | + @classmethod |
| 1033 | + def classfunc(cls): |
| 1034 | + pass |
| 1035 | + self.assertTrue(_callable(WithClassMethod.classfunc)) |
| 1036 | + |
| 1037 | + def test_non_callable_classmethod(self): |
| 1038 | + class BadClassMethod: |
| 1039 | + not_callable = classmethod(None) |
| 1040 | + self.assertFalse(_callable(BadClassMethod.not_callable)) |
| 1041 | + |
| 1042 | + |
1005 | 1043 | if __name__ == '__main__': |
1006 | 1044 | unittest.main() |
0 commit comments