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