• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1try:
2    # python 2.x
3    import unittest2 as unittest
4except ImportError:
5    # python 3.x
6    import unittest
7
8import doctest
9import sys
10
11import funcsigs as inspect
12
13
14class TestFunctionSignatures(unittest.TestCase):
15
16    @staticmethod
17    def signature(func):
18        sig = inspect.signature(func)
19        return (tuple((param.name,
20                       (Ellipsis if param.default is param.empty else param.default),
21                       (Ellipsis if param.annotation is param.empty
22                                                        else param.annotation),
23                       str(param.kind).lower())
24                                    for param in sig.parameters.values()),
25                (Ellipsis if sig.return_annotation is sig.empty
26                                            else sig.return_annotation))
27
28    def test_zero_arguments(self):
29        def test():
30            pass
31        self.assertEqual(self.signature(test),
32                ((), Ellipsis))
33
34    def test_single_positional_argument(self):
35        def test(a):
36            pass
37        self.assertEqual(self.signature(test),
38                (((('a', Ellipsis, Ellipsis, "positional_or_keyword")),), Ellipsis))
39
40    def test_single_keyword_argument(self):
41        def test(a=None):
42            pass
43        self.assertEqual(self.signature(test),
44                (((('a', None, Ellipsis, "positional_or_keyword")),), Ellipsis))
45
46    def test_var_args(self):
47        def test(*args):
48            pass
49        self.assertEqual(self.signature(test),
50                (((('args', Ellipsis, Ellipsis, "var_positional")),), Ellipsis))
51
52    def test_keywords_args(self):
53        def test(**kwargs):
54            pass
55        self.assertEqual(self.signature(test),
56                (((('kwargs', Ellipsis, Ellipsis, "var_keyword")),), Ellipsis))
57
58    def test_multiple_arguments(self):
59        def test(a, b=None, *args, **kwargs):
60            pass
61        self.assertEqual(self.signature(test), ((
62            ('a', Ellipsis, Ellipsis, "positional_or_keyword"),
63            ('b', None, Ellipsis, "positional_or_keyword"),
64            ('args', Ellipsis, Ellipsis, "var_positional"),
65            ('kwargs', Ellipsis, Ellipsis, "var_keyword"),
66            ), Ellipsis))
67
68    def test_has_version(self):
69        self.assertTrue(inspect.__version__)
70
71    def test_readme(self):
72        doctest.testfile('../README.rst')
73
74    def test_unbound_method(self):
75        if sys.version_info < (3,):
76            self_kind = "positional_only"
77        else:
78            self_kind = "positional_or_keyword"
79        class Test(object):
80            def method(self):
81                pass
82            def method_with_args(self, a):
83                pass
84        self.assertEqual(self.signature(Test.method),
85                (((('self', Ellipsis, Ellipsis, self_kind)),), Ellipsis))
86        self.assertEqual(self.signature(Test.method_with_args), ((
87                ('self', Ellipsis, Ellipsis, self_kind),
88                ('a', Ellipsis, Ellipsis, "positional_or_keyword"),
89                ), Ellipsis))
90
91
92if __name__ == "__main__":
93    unittest.begin()
94