1 | n/a | import unittest |
---|
2 | n/a | import idlelib.calltips as ct |
---|
3 | n/a | import textwrap |
---|
4 | n/a | import types |
---|
5 | n/a | |
---|
6 | n/a | default_tip = ct._default_callable_argspec |
---|
7 | n/a | |
---|
8 | n/a | # Test Class TC is used in multiple get_argspec test methods |
---|
9 | n/a | class TC(): |
---|
10 | n/a | 'doc' |
---|
11 | n/a | tip = "(ai=None, *b)" |
---|
12 | n/a | def __init__(self, ai=None, *b): 'doc' |
---|
13 | n/a | __init__.tip = "(self, ai=None, *b)" |
---|
14 | n/a | def t1(self): 'doc' |
---|
15 | n/a | t1.tip = "(self)" |
---|
16 | n/a | def t2(self, ai, b=None): 'doc' |
---|
17 | n/a | t2.tip = "(self, ai, b=None)" |
---|
18 | n/a | def t3(self, ai, *args): 'doc' |
---|
19 | n/a | t3.tip = "(self, ai, *args)" |
---|
20 | n/a | def t4(self, *args): 'doc' |
---|
21 | n/a | t4.tip = "(self, *args)" |
---|
22 | n/a | def t5(self, ai, b=None, *args, **kw): 'doc' |
---|
23 | n/a | t5.tip = "(self, ai, b=None, *args, **kw)" |
---|
24 | n/a | def t6(no, self): 'doc' |
---|
25 | n/a | t6.tip = "(no, self)" |
---|
26 | n/a | def __call__(self, ci): 'doc' |
---|
27 | n/a | __call__.tip = "(self, ci)" |
---|
28 | n/a | # attaching .tip to wrapped methods does not work |
---|
29 | n/a | @classmethod |
---|
30 | n/a | def cm(cls, a): 'doc' |
---|
31 | n/a | @staticmethod |
---|
32 | n/a | def sm(b): 'doc' |
---|
33 | n/a | |
---|
34 | n/a | tc = TC() |
---|
35 | n/a | |
---|
36 | n/a | signature = ct.get_argspec # 2.7 and 3.x use different functions |
---|
37 | n/a | class Get_signatureTest(unittest.TestCase): |
---|
38 | n/a | # The signature function must return a string, even if blank. |
---|
39 | n/a | # Test a variety of objects to be sure that none cause it to raise |
---|
40 | n/a | # (quite aside from getting as correct an answer as possible). |
---|
41 | n/a | # The tests of builtins may break if inspect or the docstrings change, |
---|
42 | n/a | # but a red buildbot is better than a user crash (as has happened). |
---|
43 | n/a | # For a simple mismatch, change the expected output to the actual. |
---|
44 | n/a | |
---|
45 | n/a | def test_builtins(self): |
---|
46 | n/a | |
---|
47 | n/a | # Python class that inherits builtin methods |
---|
48 | n/a | class List(list): "List() doc" |
---|
49 | n/a | # Simulate builtin with no docstring for default tip test |
---|
50 | n/a | class SB: __call__ = None |
---|
51 | n/a | |
---|
52 | n/a | def gtest(obj, out): |
---|
53 | n/a | self.assertEqual(signature(obj), out) |
---|
54 | n/a | |
---|
55 | n/a | if List.__doc__ is not None: |
---|
56 | n/a | gtest(List, List.__doc__) |
---|
57 | n/a | gtest(list.__new__, |
---|
58 | n/a | 'Create and return a new object. See help(type) for accurate signature.') |
---|
59 | n/a | gtest(list.__init__, |
---|
60 | n/a | 'Initialize self. See help(type(self)) for accurate signature.') |
---|
61 | n/a | append_doc = "L.append(object) -> None -- append object to end" |
---|
62 | n/a | gtest(list.append, append_doc) |
---|
63 | n/a | gtest([].append, append_doc) |
---|
64 | n/a | gtest(List.append, append_doc) |
---|
65 | n/a | |
---|
66 | n/a | gtest(types.MethodType, "method(function, instance)") |
---|
67 | n/a | gtest(SB(), default_tip) |
---|
68 | n/a | |
---|
69 | n/a | def test_signature_wrap(self): |
---|
70 | n/a | if textwrap.TextWrapper.__doc__ is not None: |
---|
71 | n/a | self.assertEqual(signature(textwrap.TextWrapper), '''\ |
---|
72 | n/a | (width=70, initial_indent='', subsequent_indent='', expand_tabs=True, |
---|
73 | n/a | replace_whitespace=True, fix_sentence_endings=False, break_long_words=True, |
---|
74 | n/a | drop_whitespace=True, break_on_hyphens=True, tabsize=8, *, max_lines=None, |
---|
75 | n/a | placeholder=' [...]')''') |
---|
76 | n/a | |
---|
77 | n/a | def test_docline_truncation(self): |
---|
78 | n/a | def f(): pass |
---|
79 | n/a | f.__doc__ = 'a'*300 |
---|
80 | n/a | self.assertEqual(signature(f), '()\n' + 'a' * (ct._MAX_COLS-3) + '...') |
---|
81 | n/a | |
---|
82 | n/a | def test_multiline_docstring(self): |
---|
83 | n/a | # Test fewer lines than max. |
---|
84 | n/a | self.assertEqual(signature(list), |
---|
85 | n/a | "list() -> new empty list\n" |
---|
86 | n/a | "list(iterable) -> new list initialized from iterable's items") |
---|
87 | n/a | |
---|
88 | n/a | # Test max lines |
---|
89 | n/a | self.assertEqual(signature(bytes), '''\ |
---|
90 | n/a | bytes(iterable_of_ints) -> bytes |
---|
91 | n/a | bytes(string, encoding[, errors]) -> bytes |
---|
92 | n/a | bytes(bytes_or_buffer) -> immutable copy of bytes_or_buffer |
---|
93 | n/a | bytes(int) -> bytes object of size given by the parameter initialized with null bytes |
---|
94 | n/a | bytes() -> empty bytes object''') |
---|
95 | n/a | |
---|
96 | n/a | # Test more than max lines |
---|
97 | n/a | def f(): pass |
---|
98 | n/a | f.__doc__ = 'a\n' * 15 |
---|
99 | n/a | self.assertEqual(signature(f), '()' + '\na' * ct._MAX_LINES) |
---|
100 | n/a | |
---|
101 | n/a | def test_functions(self): |
---|
102 | n/a | def t1(): 'doc' |
---|
103 | n/a | t1.tip = "()" |
---|
104 | n/a | def t2(a, b=None): 'doc' |
---|
105 | n/a | t2.tip = "(a, b=None)" |
---|
106 | n/a | def t3(a, *args): 'doc' |
---|
107 | n/a | t3.tip = "(a, *args)" |
---|
108 | n/a | def t4(*args): 'doc' |
---|
109 | n/a | t4.tip = "(*args)" |
---|
110 | n/a | def t5(a, b=None, *args, **kw): 'doc' |
---|
111 | n/a | t5.tip = "(a, b=None, *args, **kw)" |
---|
112 | n/a | |
---|
113 | n/a | doc = '\ndoc' if t1.__doc__ is not None else '' |
---|
114 | n/a | for func in (t1, t2, t3, t4, t5, TC): |
---|
115 | n/a | self.assertEqual(signature(func), func.tip + doc) |
---|
116 | n/a | |
---|
117 | n/a | def test_methods(self): |
---|
118 | n/a | doc = '\ndoc' if TC.__doc__ is not None else '' |
---|
119 | n/a | for meth in (TC.t1, TC.t2, TC.t3, TC.t4, TC.t5, TC.t6, TC.__call__): |
---|
120 | n/a | self.assertEqual(signature(meth), meth.tip + doc) |
---|
121 | n/a | self.assertEqual(signature(TC.cm), "(a)" + doc) |
---|
122 | n/a | self.assertEqual(signature(TC.sm), "(b)" + doc) |
---|
123 | n/a | |
---|
124 | n/a | def test_bound_methods(self): |
---|
125 | n/a | # test that first parameter is correctly removed from argspec |
---|
126 | n/a | doc = '\ndoc' if TC.__doc__ is not None else '' |
---|
127 | n/a | for meth, mtip in ((tc.t1, "()"), (tc.t4, "(*args)"), (tc.t6, "(self)"), |
---|
128 | n/a | (tc.__call__, '(ci)'), (tc, '(ci)'), (TC.cm, "(a)"),): |
---|
129 | n/a | self.assertEqual(signature(meth), mtip + doc) |
---|
130 | n/a | |
---|
131 | n/a | def test_starred_parameter(self): |
---|
132 | n/a | # test that starred first parameter is *not* removed from argspec |
---|
133 | n/a | class C: |
---|
134 | n/a | def m1(*args): pass |
---|
135 | n/a | def m2(**kwds): pass |
---|
136 | n/a | c = C() |
---|
137 | n/a | for meth, mtip in ((C.m1, '(*args)'), (c.m1, "(*args)"), |
---|
138 | n/a | (C.m2, "(**kwds)"), (c.m2, "(**kwds)"),): |
---|
139 | n/a | self.assertEqual(signature(meth), mtip) |
---|
140 | n/a | |
---|
141 | n/a | def test_non_ascii_name(self): |
---|
142 | n/a | # test that re works to delete a first parameter name that |
---|
143 | n/a | # includes non-ascii chars, such as various forms of A. |
---|
144 | n/a | uni = "(A\u0391\u0410\u05d0\u0627\u0905\u1e00\u3042, a)" |
---|
145 | n/a | assert ct._first_param.sub('', uni) == '(a)' |
---|
146 | n/a | |
---|
147 | n/a | def test_no_docstring(self): |
---|
148 | n/a | def nd(s): |
---|
149 | n/a | pass |
---|
150 | n/a | TC.nd = nd |
---|
151 | n/a | self.assertEqual(signature(nd), "(s)") |
---|
152 | n/a | self.assertEqual(signature(TC.nd), "(s)") |
---|
153 | n/a | self.assertEqual(signature(tc.nd), "()") |
---|
154 | n/a | |
---|
155 | n/a | def test_attribute_exception(self): |
---|
156 | n/a | class NoCall: |
---|
157 | n/a | def __getattr__(self, name): |
---|
158 | n/a | raise BaseException |
---|
159 | n/a | class Call(NoCall): |
---|
160 | n/a | def __call__(self, ci): |
---|
161 | n/a | pass |
---|
162 | n/a | for meth, mtip in ((NoCall, default_tip), (Call, default_tip), |
---|
163 | n/a | (NoCall(), ''), (Call(), '(ci)')): |
---|
164 | n/a | self.assertEqual(signature(meth), mtip) |
---|
165 | n/a | |
---|
166 | n/a | def test_non_callables(self): |
---|
167 | n/a | for obj in (0, 0.0, '0', b'0', [], {}): |
---|
168 | n/a | self.assertEqual(signature(obj), '') |
---|
169 | n/a | |
---|
170 | n/a | class Get_entityTest(unittest.TestCase): |
---|
171 | n/a | def test_bad_entity(self): |
---|
172 | n/a | self.assertIsNone(ct.get_entity('1/0')) |
---|
173 | n/a | def test_good_entity(self): |
---|
174 | n/a | self.assertIs(ct.get_entity('int'), int) |
---|
175 | n/a | |
---|
176 | n/a | if __name__ == '__main__': |
---|
177 | n/a | unittest.main(verbosity=2, exit=False) |
---|