ยปCore Development>Code coverage>Lib/test/test_rlcompleter.py

Python code coverage for Lib/test/test_rlcompleter.py

#countcontent
1n/aimport unittest
2n/afrom unittest.mock import patch
3n/aimport builtins
4n/aimport rlcompleter
5n/a
6n/aclass CompleteMe:
7n/a """ Trivial class used in testing rlcompleter.Completer. """
8n/a spam = 1
9n/a _ham = 2
10n/a
11n/a
12n/aclass TestRlcompleter(unittest.TestCase):
13n/a def setUp(self):
14n/a self.stdcompleter = rlcompleter.Completer()
15n/a self.completer = rlcompleter.Completer(dict(spam=int,
16n/a egg=str,
17n/a CompleteMe=CompleteMe))
18n/a
19n/a # forces stdcompleter to bind builtins namespace
20n/a self.stdcompleter.complete('', 0)
21n/a
22n/a def test_namespace(self):
23n/a class A(dict):
24n/a pass
25n/a class B(list):
26n/a pass
27n/a
28n/a self.assertTrue(self.stdcompleter.use_main_ns)
29n/a self.assertFalse(self.completer.use_main_ns)
30n/a self.assertFalse(rlcompleter.Completer(A()).use_main_ns)
31n/a self.assertRaises(TypeError, rlcompleter.Completer, B((1,)))
32n/a
33n/a def test_global_matches(self):
34n/a # test with builtins namespace
35n/a self.assertEqual(sorted(self.stdcompleter.global_matches('di')),
36n/a [x+'(' for x in dir(builtins) if x.startswith('di')])
37n/a self.assertEqual(sorted(self.stdcompleter.global_matches('st')),
38n/a [x+'(' for x in dir(builtins) if x.startswith('st')])
39n/a self.assertEqual(self.stdcompleter.global_matches('akaksajadhak'), [])
40n/a
41n/a # test with a customized namespace
42n/a self.assertEqual(self.completer.global_matches('CompleteM'),
43n/a ['CompleteMe('])
44n/a self.assertEqual(self.completer.global_matches('eg'),
45n/a ['egg('])
46n/a # XXX: see issue5256
47n/a self.assertEqual(self.completer.global_matches('CompleteM'),
48n/a ['CompleteMe('])
49n/a
50n/a def test_attr_matches(self):
51n/a # test with builtins namespace
52n/a self.assertEqual(self.stdcompleter.attr_matches('str.s'),
53n/a ['str.{}('.format(x) for x in dir(str)
54n/a if x.startswith('s')])
55n/a self.assertEqual(self.stdcompleter.attr_matches('tuple.foospamegg'), [])
56n/a expected = sorted({'None.%s%s' % (x, '(' if x != '__doc__' else '')
57n/a for x in dir(None)})
58n/a self.assertEqual(self.stdcompleter.attr_matches('None.'), expected)
59n/a self.assertEqual(self.stdcompleter.attr_matches('None._'), expected)
60n/a self.assertEqual(self.stdcompleter.attr_matches('None.__'), expected)
61n/a
62n/a # test with a customized namespace
63n/a self.assertEqual(self.completer.attr_matches('CompleteMe.sp'),
64n/a ['CompleteMe.spam'])
65n/a self.assertEqual(self.completer.attr_matches('Completeme.egg'), [])
66n/a self.assertEqual(self.completer.attr_matches('CompleteMe.'),
67n/a ['CompleteMe.mro(', 'CompleteMe.spam'])
68n/a self.assertEqual(self.completer.attr_matches('CompleteMe._'),
69n/a ['CompleteMe._ham'])
70n/a matches = self.completer.attr_matches('CompleteMe.__')
71n/a for x in matches:
72n/a self.assertTrue(x.startswith('CompleteMe.__'), x)
73n/a self.assertIn('CompleteMe.__name__', matches)
74n/a self.assertIn('CompleteMe.__new__(', matches)
75n/a
76n/a with patch.object(CompleteMe, "me", CompleteMe, create=True):
77n/a self.assertEqual(self.completer.attr_matches('CompleteMe.me.me.sp'),
78n/a ['CompleteMe.me.me.spam'])
79n/a self.assertEqual(self.completer.attr_matches('egg.s'),
80n/a ['egg.{}('.format(x) for x in dir(str)
81n/a if x.startswith('s')])
82n/a
83n/a def test_excessive_getattr(self):
84n/a # Ensure getattr() is invoked no more than once per attribute
85n/a class Foo:
86n/a calls = 0
87n/a @property
88n/a def bar(self):
89n/a self.calls += 1
90n/a return None
91n/a f = Foo()
92n/a completer = rlcompleter.Completer(dict(f=f))
93n/a self.assertEqual(completer.complete('f.b', 0), 'f.bar')
94n/a self.assertEqual(f.calls, 1)
95n/a
96n/a def test_uncreated_attr(self):
97n/a # Attributes like properties and slots should be completed even when
98n/a # they haven't been created on an instance
99n/a class Foo:
100n/a __slots__ = ("bar",)
101n/a completer = rlcompleter.Completer(dict(f=Foo()))
102n/a self.assertEqual(completer.complete('f.', 0), 'f.bar')
103n/a
104n/a @unittest.mock.patch('rlcompleter._readline_available', False)
105n/a def test_complete(self):
106n/a completer = rlcompleter.Completer()
107n/a self.assertEqual(completer.complete('', 0), '\t')
108n/a self.assertEqual(completer.complete('a', 0), 'and ')
109n/a self.assertEqual(completer.complete('a', 1), 'as ')
110n/a self.assertEqual(completer.complete('as', 2), 'assert ')
111n/a self.assertEqual(completer.complete('an', 0), 'and ')
112n/a self.assertEqual(completer.complete('pa', 0), 'pass')
113n/a self.assertEqual(completer.complete('Fa', 0), 'False')
114n/a self.assertEqual(completer.complete('el', 0), 'elif ')
115n/a self.assertEqual(completer.complete('el', 1), 'else')
116n/a self.assertEqual(completer.complete('tr', 0), 'try:')
117n/a
118n/a def test_duplicate_globals(self):
119n/a namespace = {
120n/a 'False': None, # Keyword vs builtin vs namespace
121n/a 'assert': None, # Keyword vs namespace
122n/a 'try': lambda: None, # Keyword vs callable
123n/a 'memoryview': None, # Callable builtin vs non-callable
124n/a 'Ellipsis': lambda: None, # Non-callable builtin vs callable
125n/a }
126n/a completer = rlcompleter.Completer(namespace)
127n/a self.assertEqual(completer.complete('False', 0), 'False')
128n/a self.assertIsNone(completer.complete('False', 1)) # No duplicates
129n/a # Space or colon added due to being a reserved keyword
130n/a self.assertEqual(completer.complete('assert', 0), 'assert ')
131n/a self.assertIsNone(completer.complete('assert', 1))
132n/a self.assertEqual(completer.complete('try', 0), 'try:')
133n/a self.assertIsNone(completer.complete('try', 1))
134n/a # No opening bracket "(" because we overrode the built-in class
135n/a self.assertEqual(completer.complete('memoryview', 0), 'memoryview')
136n/a self.assertIsNone(completer.complete('memoryview', 1))
137n/a self.assertEqual(completer.complete('Ellipsis', 0), 'Ellipsis(')
138n/a self.assertIsNone(completer.complete('Ellipsis', 1))
139n/a
140n/aif __name__ == '__main__':
141n/a unittest.main()