|
| 1 | +import inspect |
1 | 2 | import time |
2 | 3 | import types |
3 | 4 | import unittest |
@@ -901,6 +902,35 @@ def __getattr__(self, attribute):
|
901 | 902 | self.assertFalse(hasattr(autospec, '__name__')) |
902 | 903 | |
903 | 904 | |
| 905 | +def test_spec_inspect_signature(self): |
| 906 | + |
| 907 | +def myfunc(x, y): |
| 908 | +pass |
| 909 | + |
| 910 | +mock = create_autospec(myfunc) |
| 911 | +mock(1, 2) |
| 912 | +mock(x=1, y=2) |
| 913 | + |
| 914 | +self.assertEqual(inspect.getfullargspec(mock), inspect.getfullargspec(myfunc)) |
| 915 | +self.assertEqual(mock.mock_calls, [call(1, 2), call(x=1, y=2)]) |
| 916 | +self.assertRaises(TypeError, mock, 1) |
| 917 | + |
| 918 | + |
| 919 | +def test_spec_inspect_signature_annotations(self): |
| 920 | + |
| 921 | +def foo(a: int, b: int=10, *, c:int) -> int: |
| 922 | +return a + b + c |
| 923 | + |
| 924 | +mock = create_autospec(foo) |
| 925 | +mock(1, 2, c=3) |
| 926 | +mock(1, c=3) |
| 927 | + |
| 928 | +self.assertEqual(inspect.getfullargspec(mock), inspect.getfullargspec(foo)) |
| 929 | +self.assertEqual(mock.mock_calls, [call(1, 2, c=3), call(1, c=3)]) |
| 930 | +self.assertRaises(TypeError, mock, 1) |
| 931 | +self.assertRaises(TypeError, mock, 1, 2, 3, c=4) |
| 932 | + |
| 933 | + |
904 | 934 | class TestCallList(unittest.TestCase): |
905 | 935 | |
906 | 936 | def test_args_list_contains_call_list(self): |
|