ยปCore Development>Code coverage>Lib/idlelib/idle_test/test_calltips.py

Python code coverage for Lib/idlelib/idle_test/test_calltips.py

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