|
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 |
@@ -1011,5 +1011,43 @@ def test_propertymock_returnvalue(self): |
1011 | 1011 | self.assertNotIsInstance(returned, PropertyMock) |
1012 | 1012 |
|
1013 | 1013 |
|
| 1014 | +class TestCallablePredicate(unittest.TestCase): |
| 1015 | + |
| 1016 | + def test_type(self): |
| 1017 | + for obj in [str, bytes, int, list, tuple, SomeClass]: |
| 1018 | + self.assertTrue(_callable(obj)) |
| 1019 | + |
| 1020 | + def test_call_magic_method(self): |
| 1021 | + class Callable: |
| 1022 | + def __call__(self): |
| 1023 | + pass |
| 1024 | + instance = Callable() |
| 1025 | + self.assertTrue(_callable(instance)) |
| 1026 | + |
| 1027 | + def test_staticmethod(self): |
| 1028 | + class WithStaticMethod: |
| 1029 | + @staticmethod |
| 1030 | + def staticfunc(): |
| 1031 | + pass |
| 1032 | + self.assertTrue(_callable(WithStaticMethod.staticfunc)) |
| 1033 | + |
| 1034 | + def test_non_callable_staticmethod(self): |
| 1035 | + class BadStaticMethod: |
| 1036 | + not_callable = staticmethod(None) |
| 1037 | + self.assertFalse(_callable(BadStaticMethod.not_callable)) |
| 1038 | + |
| 1039 | + def test_classmethod(self): |
| 1040 | + class WithClassMethod: |
| 1041 | + @classmethod |
| 1042 | + def classfunc(cls): |
| 1043 | + pass |
| 1044 | + self.assertTrue(_callable(WithClassMethod.classfunc)) |
| 1045 | + |
| 1046 | + def test_non_callable_classmethod(self): |
| 1047 | + class BadClassMethod: |
| 1048 | + not_callable = classmethod(None) |
| 1049 | + self.assertFalse(_callable(BadClassMethod.not_callable)) |
| 1050 | + |
| 1051 | + |
1014 | 1052 | if __name__ == '__main__': |
1015 | 1053 | unittest.main() |
0 commit comments