Show More
This diff has been collapsed as it changes many lines, (688 lines changed) Show them Hide them | |||
@@ -22,12 +22,17 b' from IPython.utils.generics import complete_object' | |||
|
22 | 22 | from IPython.testing import decorators as dec |
|
23 | 23 | |
|
24 | 24 | from IPython.core.completer import ( |
|
25 | Completion, provisionalcompleter, match_dict_keys, _deduplicate_completions) | |
|
25 | Completion, | |
|
26 | provisionalcompleter, | |
|
27 | match_dict_keys, | |
|
28 | _deduplicate_completions, | |
|
29 | ) | |
|
26 | 30 | from nose.tools import assert_in, assert_not_in |
|
27 | 31 | |
|
28 | #----------------------------------------------------------------------------- | |
|
32 | # ----------------------------------------------------------------------------- | |
|
29 | 33 | # Test functions |
|
30 | #----------------------------------------------------------------------------- | |
|
34 | # ----------------------------------------------------------------------------- | |
|
35 | ||
|
31 | 36 | |
|
32 | 37 | @contextmanager |
|
33 | 38 | def greedy_completion(): |
@@ -39,39 +44,42 b' def greedy_completion():' | |||
|
39 | 44 | finally: |
|
40 | 45 | ip.Completer.greedy = greedy_original |
|
41 | 46 | |
|
47 | ||
|
42 | 48 | def test_protect_filename(): |
|
43 |
if sys.platform == |
|
|
44 |
pairs = [ |
|
|
45 |
|
|
|
46 |
|
|
|
47 |
|
|
|
48 |
|
|
|
49 |
|
|
|
49 | if sys.platform == "win32": | |
|
50 | pairs = [ | |
|
51 | ("abc", "abc"), | |
|
52 | (" abc", '" abc"'), | |
|
53 | ("a bc", '"a bc"'), | |
|
54 | ("a bc", '"a bc"'), | |
|
55 | (" bc", '" bc"'), | |
|
56 | ] | |
|
50 | 57 | else: |
|
51 |
pairs = [ |
|
|
52 |
|
|
|
53 |
|
|
|
54 |
|
|
|
55 |
|
|
|
56 | # On posix, we also protect parens and other special characters. | |
|
57 | ('a(bc',r'a\(bc'), | |
|
58 |
|
|
|
59 |
|
|
|
60 |
|
|
|
61 |
|
|
|
62 |
|
|
|
63 |
|
|
|
64 |
|
|
|
65 |
|
|
|
66 |
|
|
|
67 |
|
|
|
68 |
|
|
|
69 |
|
|
|
70 |
|
|
|
71 |
|
|
|
72 |
|
|
|
73 |
|
|
|
74 | ] | |
|
58 | pairs = [ | |
|
59 | ("abc", "abc"), | |
|
60 | (" abc", r"\ abc"), | |
|
61 | ("a bc", r"a\ bc"), | |
|
62 | ("a bc", r"a\ \ bc"), | |
|
63 | (" bc", r"\ \ bc"), | |
|
64 | # On posix, we also protect parens and other special characters. | |
|
65 | ("a(bc", r"a\(bc"), | |
|
66 | ("a)bc", r"a\)bc"), | |
|
67 | ("a( )bc", r"a\(\ \)bc"), | |
|
68 | ("a[1]bc", r"a\[1\]bc"), | |
|
69 | ("a{1}bc", r"a\{1\}bc"), | |
|
70 | ("a#bc", r"a\#bc"), | |
|
71 | ("a?bc", r"a\?bc"), | |
|
72 | ("a=bc", r"a\=bc"), | |
|
73 | ("a\\bc", r"a\\bc"), | |
|
74 | ("a|bc", r"a\|bc"), | |
|
75 | ("a;bc", r"a\;bc"), | |
|
76 | ("a:bc", r"a\:bc"), | |
|
77 | ("a'bc", r"a\'bc"), | |
|
78 | ("a*bc", r"a\*bc"), | |
|
79 | ('a"bc', r"a\"bc"), | |
|
80 | ("a^bc", r"a\^bc"), | |
|
81 | ("a&bc", r"a\&bc"), | |
|
82 | ] | |
|
75 | 83 | # run the actual tests |
|
76 | 84 | for s1, s2 in pairs: |
|
77 | 85 | s1p = completer.protect_filename(s1) |
@@ -81,7 +89,7 b' def test_protect_filename():' | |||
|
81 | 89 | def check_line_split(splitter, test_specs): |
|
82 | 90 | for part1, part2, split in test_specs: |
|
83 | 91 | cursor_pos = len(part1) |
|
84 | line = part1+part2 | |
|
92 | line = part1 + part2 | |
|
85 | 93 | out = splitter.split_line(line, cursor_pos) |
|
86 | 94 | nt.assert_equal(out, split) |
|
87 | 95 | |
@@ -93,30 +101,31 b' def test_line_split():' | |||
|
93 | 101 | # and 2 are joined into the 'line' sent to the splitter, as if the cursor |
|
94 | 102 | # was at the end of part1. So an empty part2 represents someone hitting |
|
95 | 103 | # tab at the end of the line, the most common case. |
|
96 | t = [('run some/scrip', '', 'some/scrip'), | |
|
97 | ('run scripts/er', 'ror.py foo', 'scripts/er'), | |
|
98 | ('echo $HOM', '', 'HOM'), | |
|
99 | ('print sys.pa', '', 'sys.pa'), | |
|
100 |
|
|
|
101 | ("execfile('scripts/er", '', 'scripts/er'), | |
|
102 | ('a[x.', '', 'x.'), | |
|
103 |
|
|
|
104 | ('cd "some_file/', '', 'some_file/'), | |
|
105 | ] | |
|
104 | t = [ | |
|
105 | ("run some/scrip", "", "some/scrip"), | |
|
106 | ("run scripts/er", "ror.py foo", "scripts/er"), | |
|
107 | ("echo $HOM", "", "HOM"), | |
|
108 | ("print sys.pa", "", "sys.pa"), | |
|
109 | ("print(sys.pa", "", "sys.pa"), | |
|
110 | ("execfile('scripts/er", "", "scripts/er"), | |
|
111 | ("a[x.", "", "x."), | |
|
112 | ("a[x.", "y", "x."), | |
|
113 | ('cd "some_file/', "", "some_file/"), | |
|
114 | ] | |
|
106 | 115 | check_line_split(sp, t) |
|
107 | 116 | # Ensure splitting works OK with unicode by re-running the tests with |
|
108 | 117 | # all inputs turned into unicode |
|
109 |
check_line_split(sp, [ |
|
|
110 | ||
|
118 | check_line_split(sp, [map(str, p) for p in t]) | |
|
111 | 119 | |
|
112 | 120 | |
|
113 | 121 | class NamedInstanceMetaclass(type): |
|
114 | 122 | def __getitem__(cls, item): |
|
115 | 123 | return cls.get_instance(item) |
|
116 | 124 | |
|
125 | ||
|
117 | 126 | class NamedInstanceClass(metaclass=NamedInstanceMetaclass): |
|
118 | 127 | def __init__(self, name): |
|
119 |
if not hasattr(self.__class__, |
|
|
128 | if not hasattr(self.__class__, "instances"): | |
|
120 | 129 | self.__class__.instances = {} |
|
121 | 130 | self.__class__.instances[name] = self |
|
122 | 131 | |
@@ -128,6 +137,7 b' class NamedInstanceClass(metaclass=NamedInstanceMetaclass):' | |||
|
128 | 137 | def get_instance(cls, name): |
|
129 | 138 | return cls.instances[name] |
|
130 | 139 | |
|
140 | ||
|
131 | 141 | class KeyCompletable: |
|
132 | 142 | def __init__(self, things=()): |
|
133 | 143 | self.things = things |
@@ -137,7 +147,6 b' class KeyCompletable:' | |||
|
137 | 147 | |
|
138 | 148 | |
|
139 | 149 | class TestCompleter(unittest.TestCase): |
|
140 | ||
|
141 | 150 | def setUp(self): |
|
142 | 151 | """ |
|
143 | 152 | We want to silence all PendingDeprecationWarning when testing the completer |
@@ -151,24 +160,26 b' class TestCompleter(unittest.TestCase):' | |||
|
151 | 160 | except AssertionError: |
|
152 | 161 | pass |
|
153 | 162 | |
|
154 | ||
|
155 | 163 | def test_custom_completion_error(self): |
|
156 | 164 | """Test that errors from custom attribute completers are silenced.""" |
|
157 | 165 | ip = get_ipython() |
|
158 | class A: pass | |
|
159 | ip.user_ns['x'] = A() | |
|
160 | ||
|
166 | ||
|
167 | class A: | |
|
168 | pass | |
|
169 | ||
|
170 | ip.user_ns["x"] = A() | |
|
171 | ||
|
161 | 172 | @complete_object.register(A) |
|
162 | 173 | def complete_A(a, existing_completions): |
|
163 | 174 | raise TypeError("this should be silenced") |
|
164 | ||
|
175 | ||
|
165 | 176 | ip.complete("x.") |
|
166 | 177 | |
|
167 | 178 | def test_unicode_completions(self): |
|
168 | 179 | ip = get_ipython() |
|
169 | 180 | # Some strings that trigger different types of completion. Check them both |
|
170 | 181 | # in str and unicode forms |
|
171 |
s = [ |
|
|
182 | s = ["ru", "%ru", "cd /", "floa", "float(x)/"] | |
|
172 | 183 | for t in s + list(map(str, s)): |
|
173 | 184 | # We don't need to check exact completion values (they may change |
|
174 | 185 | # depending on the state of the namespace, but at least no exceptions |
@@ -181,110 +192,100 b' class TestCompleter(unittest.TestCase):' | |||
|
181 | 192 | def test_latex_completions(self): |
|
182 | 193 | from IPython.core.latex_symbols import latex_symbols |
|
183 | 194 | import random |
|
195 | ||
|
184 | 196 | ip = get_ipython() |
|
185 | 197 | # Test some random unicode symbols |
|
186 | 198 | keys = random.sample(latex_symbols.keys(), 10) |
|
187 | 199 | for k in keys: |
|
188 | 200 | text, matches = ip.complete(k) |
|
189 | nt.assert_equal(len(matches),1) | |
|
201 | nt.assert_equal(len(matches), 1) | |
|
190 | 202 | nt.assert_equal(text, k) |
|
191 | 203 | nt.assert_equal(matches[0], latex_symbols[k]) |
|
192 | 204 | # Test a more complex line |
|
193 |
text, matches = ip.complete( |
|
|
194 |
nt.assert_equal(text, |
|
|
195 |
nt.assert_equal(matches[0], latex_symbols[ |
|
|
205 | text, matches = ip.complete("print(\\alpha") | |
|
206 | nt.assert_equal(text, "\\alpha") | |
|
207 | nt.assert_equal(matches[0], latex_symbols["\\alpha"]) | |
|
196 | 208 | # Test multiple matching latex symbols |
|
197 |
text, matches = ip.complete( |
|
|
198 |
nt.assert_in( |
|
|
199 |
nt.assert_in( |
|
|
200 | ||
|
201 | ||
|
202 | ||
|
209 | text, matches = ip.complete("\\al") | |
|
210 | nt.assert_in("\\alpha", matches) | |
|
211 | nt.assert_in("\\aleph", matches) | |
|
203 | 212 | |
|
204 | 213 | def test_back_latex_completion(self): |
|
205 | 214 | ip = get_ipython() |
|
206 | 215 | |
|
207 | 216 | # do not return more than 1 matches fro \beta, only the latex one. |
|
208 |
name, matches = ip.complete( |
|
|
217 | name, matches = ip.complete("\\Ξ²") | |
|
209 | 218 | nt.assert_equal(len(matches), 1) |
|
210 |
nt.assert_equal(matches[0], |
|
|
219 | nt.assert_equal(matches[0], "\\beta") | |
|
211 | 220 | |
|
212 | 221 | def test_back_unicode_completion(self): |
|
213 | 222 | ip = get_ipython() |
|
214 | ||
|
215 | name, matches = ip.complete('\\β €') | |
|
216 | nt.assert_equal(len(matches), 1) | |
|
217 | nt.assert_equal(matches[0], '\\ROMAN NUMERAL FIVE') | |
|
218 | 223 | |
|
224 | name, matches = ip.complete("\\β €") | |
|
225 | nt.assert_equal(len(matches), 1) | |
|
226 | nt.assert_equal(matches[0], "\\ROMAN NUMERAL FIVE") | |
|
219 | 227 | |
|
220 | 228 | def test_forward_unicode_completion(self): |
|
221 | 229 | ip = get_ipython() |
|
222 | ||
|
223 |
name, matches = ip.complete( |
|
|
230 | ||
|
231 | name, matches = ip.complete("\\ROMAN NUMERAL FIVE") | |
|
224 | 232 | nt.assert_equal(len(matches), 1) |
|
225 |
nt.assert_equal(matches[0], |
|
|
233 | nt.assert_equal(matches[0], "β €") | |
|
226 | 234 | |
|
227 | @nt.nottest # now we have a completion for \jmath | |
|
228 | @decorators.knownfailureif(sys.platform == 'win32', 'Fails if there is a C:\\j... path') | |
|
235 | @nt.nottest # now we have a completion for \jmath | |
|
236 | @decorators.knownfailureif( | |
|
237 | sys.platform == "win32", "Fails if there is a C:\\j... path" | |
|
238 | ) | |
|
229 | 239 | def test_no_ascii_back_completion(self): |
|
230 | 240 | ip = get_ipython() |
|
231 | 241 | with TemporaryWorkingDirectory(): # Avoid any filename completions |
|
232 | 242 | # single ascii letter that don't have yet completions |
|
233 |
for letter in |
|
|
234 |
name, matches = ip.complete( |
|
|
243 | for letter in "jJ": | |
|
244 | name, matches = ip.complete("\\" + letter) | |
|
235 | 245 | nt.assert_equal(matches, []) |
|
236 | 246 | |
|
237 | ||
|
238 | ||
|
239 | ||
|
240 | 247 | class CompletionSplitterTestCase(unittest.TestCase): |
|
241 | 248 | def setUp(self): |
|
242 | 249 | self.sp = completer.CompletionSplitter() |
|
243 | 250 | |
|
244 | 251 | def test_delim_setting(self): |
|
245 |
self.sp.delims = |
|
|
246 |
nt.assert_equal(self.sp.delims, |
|
|
247 |
nt.assert_equal(self.sp._delim_expr, r |
|
|
252 | self.sp.delims = " " | |
|
253 | nt.assert_equal(self.sp.delims, " ") | |
|
254 | nt.assert_equal(self.sp._delim_expr, r"[\ ]") | |
|
248 | 255 | |
|
249 | 256 | def test_spaces(self): |
|
250 | 257 | """Test with only spaces as split chars.""" |
|
251 |
self.sp.delims = |
|
|
252 | t = [('foo', '', 'foo'), | |
|
253 | ('run foo', '', 'foo'), | |
|
254 | ('run foo', 'bar', 'foo'), | |
|
255 | ] | |
|
258 | self.sp.delims = " " | |
|
259 | t = [("foo", "", "foo"), ("run foo", "", "foo"), ("run foo", "bar", "foo")] | |
|
256 | 260 | check_line_split(self.sp, t) |
|
257 | 261 | |
|
258 | ||
|
259 | 262 | def test_has_open_quotes1(self): |
|
260 | 263 | for s in ["'", "'''", "'hi' '"]: |
|
261 | 264 | nt.assert_equal(completer.has_open_quotes(s), "'") |
|
262 | 265 | |
|
263 | ||
|
264 | 266 | def test_has_open_quotes2(self): |
|
265 | 267 | for s in ['"', '"""', '"hi" "']: |
|
266 | 268 | nt.assert_equal(completer.has_open_quotes(s), '"') |
|
267 | 269 | |
|
268 | ||
|
269 | 270 | def test_has_open_quotes3(self): |
|
270 | 271 | for s in ["''", "''' '''", "'hi' 'ipython'"]: |
|
271 | 272 | nt.assert_false(completer.has_open_quotes(s)) |
|
272 | 273 | |
|
273 | ||
|
274 | 274 | def test_has_open_quotes4(self): |
|
275 | 275 | for s in ['""', '""" """', '"hi" "ipython"']: |
|
276 | 276 | nt.assert_false(completer.has_open_quotes(s)) |
|
277 | 277 | |
|
278 | ||
|
279 |
|
|
|
278 | @decorators.knownfailureif( | |
|
279 | sys.platform == "win32", "abspath completions fail on Windows" | |
|
280 | ) | |
|
280 | 281 | def test_abspath_file_completions(self): |
|
281 | 282 | ip = get_ipython() |
|
282 | 283 | with TemporaryDirectory() as tmpdir: |
|
283 |
prefix = os.path.join(tmpdir, |
|
|
284 |
suffixes = [ |
|
|
285 | names = [prefix+s for s in suffixes] | |
|
284 | prefix = os.path.join(tmpdir, "foo") | |
|
285 | suffixes = ["1", "2"] | |
|
286 | names = [prefix + s for s in suffixes] | |
|
286 | 287 | for n in names: |
|
287 |
open(n, |
|
|
288 | open(n, "w").close() | |
|
288 | 289 | |
|
289 | 290 | # Check simple completion |
|
290 | 291 | c = ip.complete(prefix)[1] |
@@ -293,18 +294,17 b' class TestCompleter(unittest.TestCase):' | |||
|
293 | 294 | # Now check with a function call |
|
294 | 295 | cmd = 'a = f("%s' % prefix |
|
295 | 296 | c = ip.complete(prefix, cmd)[1] |
|
296 | comp = [prefix+s for s in suffixes] | |
|
297 | comp = [prefix + s for s in suffixes] | |
|
297 | 298 | nt.assert_equal(c, comp) |
|
298 | 299 | |
|
299 | ||
|
300 | 300 | def test_local_file_completions(self): |
|
301 | 301 | ip = get_ipython() |
|
302 | 302 | with TemporaryWorkingDirectory(): |
|
303 |
prefix = |
|
|
304 |
suffixes = [ |
|
|
305 | names = [prefix+s for s in suffixes] | |
|
303 | prefix = "./foo" | |
|
304 | suffixes = ["1", "2"] | |
|
305 | names = [prefix + s for s in suffixes] | |
|
306 | 306 | for n in names: |
|
307 |
open(n, |
|
|
307 | open(n, "w").close() | |
|
308 | 308 | |
|
309 | 309 | # Check simple completion |
|
310 | 310 | c = ip.complete(prefix)[1] |
@@ -313,41 +313,39 b' class TestCompleter(unittest.TestCase):' | |||
|
313 | 313 | # Now check with a function call |
|
314 | 314 | cmd = 'a = f("%s' % prefix |
|
315 | 315 | c = ip.complete(prefix, cmd)[1] |
|
316 | comp = {prefix+s for s in suffixes} | |
|
316 | comp = {prefix + s for s in suffixes} | |
|
317 | 317 | nt.assert_true(comp.issubset(set(c))) |
|
318 | 318 | |
|
319 | ||
|
320 | 319 | def test_quoted_file_completions(self): |
|
321 | 320 | ip = get_ipython() |
|
322 | 321 | with TemporaryWorkingDirectory(): |
|
323 | 322 | name = "foo'bar" |
|
324 |
open(name, |
|
|
323 | open(name, "w").close() | |
|
325 | 324 | |
|
326 | 325 | # Don't escape Windows |
|
327 | 326 | escaped = name if sys.platform == "win32" else "foo\\'bar" |
|
328 | 327 | |
|
329 | 328 | # Single quote matches embedded single quote |
|
330 | 329 | text = "open('foo" |
|
331 |
c = ip.Completer._complete( |
|
|
332 | cursor_pos=len(text), | |
|
333 | full_text=text)[1] | |
|
330 | c = ip.Completer._complete( | |
|
331 | cursor_line=0, cursor_pos=len(text), full_text=text | |
|
332 | )[1] | |
|
334 | 333 | nt.assert_equal(c, [escaped]) |
|
335 | 334 | |
|
336 | 335 | # Double quote requires no escape |
|
337 | 336 | text = 'open("foo' |
|
338 |
c = ip.Completer._complete( |
|
|
339 | cursor_pos=len(text), | |
|
340 | full_text=text)[1] | |
|
337 | c = ip.Completer._complete( | |
|
338 | cursor_line=0, cursor_pos=len(text), full_text=text | |
|
339 | )[1] | |
|
341 | 340 | nt.assert_equal(c, [name]) |
|
342 | 341 | |
|
343 | 342 | # No quote requires an escape |
|
344 |
text = |
|
|
345 |
c = ip.Completer._complete( |
|
|
346 | cursor_pos=len(text), | |
|
347 | full_text=text)[1] | |
|
343 | text = "%ls foo" | |
|
344 | c = ip.Completer._complete( | |
|
345 | cursor_line=0, cursor_pos=len(text), full_text=text | |
|
346 | )[1] | |
|
348 | 347 | nt.assert_equal(c, [escaped]) |
|
349 | 348 | |
|
350 | ||
|
351 | 349 | def test_jedi(self): |
|
352 | 350 | """ |
|
353 | 351 | A couple of issue we had with Jedi |
@@ -373,14 +371,15 b' class TestCompleter(unittest.TestCase):' | |||
|
373 | 371 | assert_not_in(Completion(l, l, comp), completions, reason) |
|
374 | 372 | |
|
375 | 373 | import jedi |
|
376 | jedi_version = tuple(int(i) for i in jedi.__version__.split('.')[:3]) | |
|
374 | ||
|
375 | jedi_version = tuple(int(i) for i in jedi.__version__.split(".")[:3]) | |
|
377 | 376 | if jedi_version > (0, 10): |
|
378 |
yield _test_complete, |
|
|
379 |
yield _test_complete, |
|
|
380 |
yield _test_complete, |
|
|
381 |
yield _test_complete, |
|
|
377 | yield _test_complete, "jedi >0.9 should complete and not crash", "a=1;a.", "real" | |
|
378 | yield _test_complete, "can infer first argument", 'a=(1,"foo");a[0].', "real" | |
|
379 | yield _test_complete, "can infer second argument", 'a=(1,"foo");a[1].', "capitalize" | |
|
380 | yield _test_complete, "cover duplicate completions", "im", "import", 0, 2 | |
|
382 | 381 | |
|
383 |
yield _test_not_complete, |
|
|
382 | yield _test_not_complete, "does not mix types", 'a=(1,"foo");a[0].', "capitalize" | |
|
384 | 383 | |
|
385 | 384 | def test_completion_have_signature(self): |
|
386 | 385 | """ |
@@ -389,30 +388,36 b' class TestCompleter(unittest.TestCase):' | |||
|
389 | 388 | ip = get_ipython() |
|
390 | 389 | with provisionalcompleter(): |
|
391 | 390 | ip.Completer.use_jedi = True |
|
392 |
completions = ip.Completer.completions( |
|
|
391 | completions = ip.Completer.completions("ope", 3) | |
|
393 | 392 | c = next(completions) # should be `open` |
|
394 | 393 | ip.Completer.use_jedi = False |
|
395 |
assert |
|
|
396 | assert 'encoding' in c.signature, "Signature of function was not found by completer" | |
|
397 | ||
|
394 | assert "file" in c.signature, "Signature of function was not found by completer" | |
|
395 | assert ( | |
|
396 | "encoding" in c.signature | |
|
397 | ), "Signature of function was not found by completer" | |
|
398 | 398 | |
|
399 | 399 | def test_deduplicate_completions(self): |
|
400 | 400 | """ |
|
401 | 401 | Test that completions are correctly deduplicated (even if ranges are not the same) |
|
402 | 402 | """ |
|
403 | 403 | ip = get_ipython() |
|
404 | ip.ex(textwrap.dedent(''' | |
|
404 | ip.ex( | |
|
405 | textwrap.dedent( | |
|
406 | """ | |
|
405 | 407 |
|
|
406 | 408 |
|
|
407 |
|
|
|
409 | """ | |
|
410 | ) | |
|
411 | ) | |
|
408 | 412 | with provisionalcompleter(): |
|
409 | 413 | ip.Completer.use_jedi = True |
|
410 | l = list(_deduplicate_completions('Z.z', ip.Completer.completions('Z.z', 3))) | |
|
414 | l = list( | |
|
415 | _deduplicate_completions("Z.z", ip.Completer.completions("Z.z", 3)) | |
|
416 | ) | |
|
411 | 417 | ip.Completer.use_jedi = False |
|
412 | 418 | |
|
413 |
assert len(l) == 1, |
|
|
414 |
assert l[0].text == |
|
|
415 | ||
|
419 | assert len(l) == 1, "Completions (Z.z<tab>) correctly deduplicate: %s " % l | |
|
420 | assert l[0].text == "zoo" # and not `it.accumulate` | |
|
416 | 421 | |
|
417 | 422 | def test_greedy_completions(self): |
|
418 | 423 | """ |
@@ -425,14 +430,14 b' class TestCompleter(unittest.TestCase):' | |||
|
425 | 430 | |
|
426 | 431 | """ |
|
427 | 432 | ip = get_ipython() |
|
428 |
ip.ex( |
|
|
429 |
_,c = ip.complete( |
|
|
430 | nt.assert_false('.real' in c, | |
|
431 | "Shouldn't have completed on a[0]: %s"%c) | |
|
433 | ip.ex("a=list(range(5))") | |
|
434 | _, c = ip.complete(".", line="a[0].") | |
|
435 | nt.assert_false(".real" in c, "Shouldn't have completed on a[0]: %s" % c) | |
|
436 | ||
|
432 | 437 | def _(line, cursor_pos, expect, message, completion): |
|
433 | 438 | with greedy_completion(), provisionalcompleter(): |
|
434 | 439 | ip.Completer.use_jedi = False |
|
435 |
_,c = ip.complete( |
|
|
440 | _, c = ip.complete(".", line=line, cursor_pos=cursor_pos) | |
|
436 | 441 | nt.assert_in(expect, c, message % c) |
|
437 | 442 | |
|
438 | 443 | ip.Completer.use_jedi = True |
@@ -441,12 +446,17 b' class TestCompleter(unittest.TestCase):' | |||
|
441 | 446 | nt.assert_in(completion, completions) |
|
442 | 447 | |
|
443 | 448 | with provisionalcompleter(): |
|
444 |
yield _, |
|
|
445 | yield _, 'a[0].r', 6, 'a[0].real', "Should have completed on a[0].r: %s", Completion(5,6, 'real') | |
|
449 | yield _, "a[0].", 5, "a[0].real", "Should have completed on a[0].: %s", Completion( | |
|
450 | 5, 5, "real" | |
|
451 | ) | |
|
452 | yield _, "a[0].r", 6, "a[0].real", "Should have completed on a[0].r: %s", Completion( | |
|
453 | 5, 6, "real" | |
|
454 | ) | |
|
446 | 455 | |
|
447 | 456 | if sys.version_info > (3, 4): |
|
448 |
yield _, |
|
|
449 | ||
|
457 | yield _, "a[0].from_", 10, "a[0].from_bytes", "Should have completed on a[0].from_: %s", Completion( | |
|
458 | 5, 10, "from_bytes" | |
|
459 | ) | |
|
450 | 460 | |
|
451 | 461 | def test_omit__names(self): |
|
452 | 462 | # also happens to test IPCompleter as a configurable |
@@ -454,29 +464,28 b' class TestCompleter(unittest.TestCase):' | |||
|
454 | 464 | ip._hidden_attr = 1 |
|
455 | 465 | ip._x = {} |
|
456 | 466 | c = ip.Completer |
|
457 |
ip.ex( |
|
|
467 | ip.ex("ip=get_ipython()") | |
|
458 | 468 | cfg = Config() |
|
459 | 469 | cfg.IPCompleter.omit__names = 0 |
|
460 | 470 | c.update_config(cfg) |
|
461 | 471 | with provisionalcompleter(): |
|
462 | 472 | c.use_jedi = False |
|
463 |
s,matches = c.complete( |
|
|
464 |
nt.assert_in( |
|
|
465 |
nt.assert_in( |
|
|
473 | s, matches = c.complete("ip.") | |
|
474 | nt.assert_in("ip.__str__", matches) | |
|
475 | nt.assert_in("ip._hidden_attr", matches) | |
|
466 | 476 | |
|
467 | 477 | # c.use_jedi = True |
|
468 | 478 | # completions = set(c.completions('ip.', 3)) |
|
469 | 479 | # nt.assert_in(Completion(3, 3, '__str__'), completions) |
|
470 | 480 | # nt.assert_in(Completion(3,3, "_hidden_attr"), completions) |
|
471 | 481 | |
|
472 | ||
|
473 | 482 | cfg = Config() |
|
474 | 483 | cfg.IPCompleter.omit__names = 1 |
|
475 | 484 | c.update_config(cfg) |
|
476 | 485 | with provisionalcompleter(): |
|
477 | 486 | c.use_jedi = False |
|
478 |
s,matches = c.complete( |
|
|
479 |
nt.assert_not_in( |
|
|
487 | s, matches = c.complete("ip.") | |
|
488 | nt.assert_not_in("ip.__str__", matches) | |
|
480 | 489 | # nt.assert_in('ip._hidden_attr', matches) |
|
481 | 490 | |
|
482 | 491 | # c.use_jedi = True |
@@ -489,9 +498,9 b' class TestCompleter(unittest.TestCase):' | |||
|
489 | 498 | c.update_config(cfg) |
|
490 | 499 | with provisionalcompleter(): |
|
491 | 500 | c.use_jedi = False |
|
492 |
s,matches = c.complete( |
|
|
493 |
nt.assert_not_in( |
|
|
494 |
nt.assert_not_in( |
|
|
501 | s, matches = c.complete("ip.") | |
|
502 | nt.assert_not_in("ip.__str__", matches) | |
|
503 | nt.assert_not_in("ip._hidden_attr", matches) | |
|
495 | 504 | |
|
496 | 505 | # c.use_jedi = True |
|
497 | 506 | # completions = set(c.completions('ip.', 3)) |
@@ -500,8 +509,8 b' class TestCompleter(unittest.TestCase):' | |||
|
500 | 509 | |
|
501 | 510 | with provisionalcompleter(): |
|
502 | 511 | c.use_jedi = False |
|
503 |
s,matches = c.complete( |
|
|
504 |
nt.assert_in( |
|
|
512 | s, matches = c.complete("ip._x.") | |
|
513 | nt.assert_in("ip._x.keys", matches) | |
|
505 | 514 | |
|
506 | 515 | # c.use_jedi = True |
|
507 | 516 | # completions = set(c.completions('ip._x.', 6)) |
@@ -510,7 +519,6 b' class TestCompleter(unittest.TestCase):' | |||
|
510 | 519 | del ip._hidden_attr |
|
511 | 520 | del ip._x |
|
512 | 521 | |
|
513 | ||
|
514 | 522 | def test_limit_to__all__False_ok(self): |
|
515 | 523 | """ |
|
516 | 524 | Limit to all is deprecated, once we remove it this test can go away. |
@@ -518,69 +526,67 b' class TestCompleter(unittest.TestCase):' | |||
|
518 | 526 | ip = get_ipython() |
|
519 | 527 | c = ip.Completer |
|
520 | 528 | c.use_jedi = False |
|
521 |
ip.ex( |
|
|
522 |
ip.ex( |
|
|
529 | ip.ex("class D: x=24") | |
|
530 | ip.ex("d=D()") | |
|
523 | 531 | cfg = Config() |
|
524 | 532 | cfg.IPCompleter.limit_to__all__ = False |
|
525 | 533 | c.update_config(cfg) |
|
526 |
s, matches = c.complete( |
|
|
527 |
nt.assert_in( |
|
|
528 | ||
|
534 | s, matches = c.complete("d.") | |
|
535 | nt.assert_in("d.x", matches) | |
|
529 | 536 | |
|
530 | 537 | def test_get__all__entries_ok(self): |
|
531 | 538 | class A: |
|
532 |
__all__ = [ |
|
|
533 | words = completer.get__all__entries(A()) | |
|
534 | nt.assert_equal(words, ['x']) | |
|
539 | __all__ = ["x", 1] | |
|
535 | 540 | |
|
541 | words = completer.get__all__entries(A()) | |
|
542 | nt.assert_equal(words, ["x"]) | |
|
536 | 543 | |
|
537 | 544 | def test_get__all__entries_no__all__ok(self): |
|
538 | 545 | class A: |
|
539 | 546 | pass |
|
547 | ||
|
540 | 548 | words = completer.get__all__entries(A()) |
|
541 | 549 | nt.assert_equal(words, []) |
|
542 | 550 | |
|
543 | ||
|
544 | 551 | def test_func_kw_completions(self): |
|
545 | 552 | ip = get_ipython() |
|
546 | 553 | c = ip.Completer |
|
547 | 554 | c.use_jedi = False |
|
548 |
ip.ex( |
|
|
549 |
s, matches = c.complete(None, |
|
|
550 |
nt.assert_in( |
|
|
555 | ip.ex("def myfunc(a=1,b=2): return a+b") | |
|
556 | s, matches = c.complete(None, "myfunc(1,b") | |
|
557 | nt.assert_in("b=", matches) | |
|
551 | 558 | # Simulate completing with cursor right after b (pos==10): |
|
552 |
s, matches = c.complete(None, |
|
|
553 |
nt.assert_in( |
|
|
559 | s, matches = c.complete(None, "myfunc(1,b)", 10) | |
|
560 | nt.assert_in("b=", matches) | |
|
554 | 561 | s, matches = c.complete(None, 'myfunc(a="escaped\\")string",b') |
|
555 |
nt.assert_in( |
|
|
556 | #builtin function | |
|
557 |
s, matches = c.complete(None, |
|
|
558 |
nt.assert_in( |
|
|
559 | ||
|
562 | nt.assert_in("b=", matches) | |
|
563 | # builtin function | |
|
564 | s, matches = c.complete(None, "min(k, k") | |
|
565 | nt.assert_in("key=", matches) | |
|
560 | 566 | |
|
561 | 567 | def test_default_arguments_from_docstring(self): |
|
562 | 568 | ip = get_ipython() |
|
563 | 569 | c = ip.Completer |
|
570 | kwd = c._default_arguments_from_docstring("min(iterable[, key=func]) -> value") | |
|
571 | nt.assert_equal(kwd, ["key"]) | |
|
572 | # with cython type etc | |
|
564 | 573 | kwd = c._default_arguments_from_docstring( |
|
565 | 'min(iterable[, key=func]) -> value') | |
|
566 | nt.assert_equal(kwd, ['key']) | |
|
567 | #with cython type etc | |
|
574 | "Minuit.migrad(self, int ncall=10000, resume=True, int nsplit=1)\n" | |
|
575 | ) | |
|
576 | nt.assert_equal(kwd, ["ncall", "resume", "nsplit"]) | |
|
577 | # white spaces | |
|
568 | 578 | kwd = c._default_arguments_from_docstring( |
|
569 |
|
|
|
570 | nt.assert_equal(kwd, ['ncall', 'resume', 'nsplit']) | |
|
571 | #white spaces | |
|
572 | kwd = c._default_arguments_from_docstring( | |
|
573 | '\n Minuit.migrad(self, int ncall=10000, resume=True, int nsplit=1)\n') | |
|
574 | nt.assert_equal(kwd, ['ncall', 'resume', 'nsplit']) | |
|
579 | "\n Minuit.migrad(self, int ncall=10000, resume=True, int nsplit=1)\n" | |
|
580 | ) | |
|
581 | nt.assert_equal(kwd, ["ncall", "resume", "nsplit"]) | |
|
575 | 582 | |
|
576 | 583 | def test_line_magics(self): |
|
577 | 584 | ip = get_ipython() |
|
578 | 585 | c = ip.Completer |
|
579 |
s, matches = c.complete(None, |
|
|
580 |
nt.assert_in( |
|
|
581 |
s, matches = c.complete(None, |
|
|
582 |
nt.assert_in( |
|
|
583 | ||
|
586 | s, matches = c.complete(None, "lsmag") | |
|
587 | nt.assert_in("%lsmagic", matches) | |
|
588 | s, matches = c.complete(None, "%lsmag") | |
|
589 | nt.assert_in("%lsmagic", matches) | |
|
584 | 590 | |
|
585 | 591 | def test_cell_magics(self): |
|
586 | 592 | from IPython.core.magic import register_cell_magic |
@@ -588,15 +594,14 b' class TestCompleter(unittest.TestCase):' | |||
|
588 | 594 | @register_cell_magic |
|
589 | 595 | def _foo_cellm(line, cell): |
|
590 | 596 | pass |
|
591 | ||
|
597 | ||
|
592 | 598 | ip = get_ipython() |
|
593 | 599 | c = ip.Completer |
|
594 | 600 | |
|
595 |
s, matches = c.complete(None, |
|
|
596 |
nt.assert_in( |
|
|
597 |
s, matches = c.complete(None, |
|
|
598 |
nt.assert_in( |
|
|
599 | ||
|
601 | s, matches = c.complete(None, "_foo_ce") | |
|
602 | nt.assert_in("%%_foo_cellm", matches) | |
|
603 | s, matches = c.complete(None, "%%_foo_ce") | |
|
604 | nt.assert_in("%%_foo_cellm", matches) | |
|
600 | 605 | |
|
601 | 606 | def test_line_cell_magics(self): |
|
602 | 607 | from IPython.core.magic import register_line_cell_magic |
@@ -604,7 +609,7 b' class TestCompleter(unittest.TestCase):' | |||
|
604 | 609 | @register_line_cell_magic |
|
605 | 610 | def _bar_cellm(line, cell): |
|
606 | 611 | pass |
|
607 | ||
|
612 | ||
|
608 | 613 | ip = get_ipython() |
|
609 | 614 | c = ip.Completer |
|
610 | 615 | |
@@ -612,16 +617,15 b' class TestCompleter(unittest.TestCase):' | |||
|
612 | 617 | # returned values depend on whether the user passes %% or not explicitly, |
|
613 | 618 | # and this will show a difference if the same name is both a line and cell |
|
614 | 619 | # magic. |
|
615 |
s, matches = c.complete(None, |
|
|
616 |
nt.assert_in( |
|
|
617 |
nt.assert_in( |
|
|
618 |
s, matches = c.complete(None, |
|
|
619 |
nt.assert_in( |
|
|
620 |
nt.assert_in( |
|
|
621 |
s, matches = c.complete(None, |
|
|
622 |
nt.assert_not_in( |
|
|
623 |
nt.assert_in( |
|
|
624 | ||
|
620 | s, matches = c.complete(None, "_bar_ce") | |
|
621 | nt.assert_in("%_bar_cellm", matches) | |
|
622 | nt.assert_in("%%_bar_cellm", matches) | |
|
623 | s, matches = c.complete(None, "%_bar_ce") | |
|
624 | nt.assert_in("%_bar_cellm", matches) | |
|
625 | nt.assert_in("%%_bar_cellm", matches) | |
|
626 | s, matches = c.complete(None, "%%_bar_ce") | |
|
627 | nt.assert_not_in("%_bar_cellm", matches) | |
|
628 | nt.assert_in("%%_bar_cellm", matches) | |
|
625 | 629 | |
|
626 | 630 | def test_magic_completion_order(self): |
|
627 | 631 | ip = get_ipython() |
@@ -631,7 +635,6 b' class TestCompleter(unittest.TestCase):' | |||
|
631 | 635 | text, matches = c.complete("timeit") |
|
632 | 636 | nt.assert_equal(matches, ["%timeit", "%%timeit"]) |
|
633 | 637 | |
|
634 | ||
|
635 | 638 | def test_magic_completion_shadowing(self): |
|
636 | 639 | ip = get_ipython() |
|
637 | 640 | c = ip.Completer |
@@ -675,80 +678,76 b' class TestCompleter(unittest.TestCase):' | |||
|
675 | 678 | ip = get_ipython() |
|
676 | 679 | c = ip.Completer |
|
677 | 680 | |
|
678 |
s, matches = c.complete(None, |
|
|
679 |
nt.assert_in( |
|
|
680 |
s, matches = c.complete(None, |
|
|
681 |
nt.assert_not_in( |
|
|
682 |
s, matches = c.complete(None, |
|
|
683 |
nt.assert_in( |
|
|
684 |
s, matches = c.complete(None, |
|
|
685 |
nt.assert_in( |
|
|
686 |
s, matches = c.complete(None, |
|
|
687 |
nt.assert_list_equal([ |
|
|
688 |
s, matches = c.complete(None, |
|
|
689 |
nt.assert_list_equal([ |
|
|
690 |
s, matches = c.complete(None, |
|
|
691 |
nt.assert_list_equal([ |
|
|
692 |
s, matches = c.complete(None, |
|
|
693 |
nt.assert_list_equal([ |
|
|
694 |
s, matches = c.complete(None, |
|
|
695 |
nt.assert_in( |
|
|
696 |
s, matches = c.complete(None, |
|
|
697 |
nt.assert_in( |
|
|
698 |
s, matches = c.complete(None, |
|
|
699 |
nt.assert_list_equal([ |
|
|
700 |
s, matches = c.complete(None, |
|
|
701 |
nt.assert_list_equal([ |
|
|
702 | ||
|
681 | s, matches = c.complete(None, "conf") | |
|
682 | nt.assert_in("%config", matches) | |
|
683 | s, matches = c.complete(None, "conf") | |
|
684 | nt.assert_not_in("AliasManager", matches) | |
|
685 | s, matches = c.complete(None, "config ") | |
|
686 | nt.assert_in("AliasManager", matches) | |
|
687 | s, matches = c.complete(None, "%config ") | |
|
688 | nt.assert_in("AliasManager", matches) | |
|
689 | s, matches = c.complete(None, "config Ali") | |
|
690 | nt.assert_list_equal(["AliasManager"], matches) | |
|
691 | s, matches = c.complete(None, "%config Ali") | |
|
692 | nt.assert_list_equal(["AliasManager"], matches) | |
|
693 | s, matches = c.complete(None, "config AliasManager") | |
|
694 | nt.assert_list_equal(["AliasManager"], matches) | |
|
695 | s, matches = c.complete(None, "%config AliasManager") | |
|
696 | nt.assert_list_equal(["AliasManager"], matches) | |
|
697 | s, matches = c.complete(None, "config AliasManager.") | |
|
698 | nt.assert_in("AliasManager.default_aliases", matches) | |
|
699 | s, matches = c.complete(None, "%config AliasManager.") | |
|
700 | nt.assert_in("AliasManager.default_aliases", matches) | |
|
701 | s, matches = c.complete(None, "config AliasManager.de") | |
|
702 | nt.assert_list_equal(["AliasManager.default_aliases"], matches) | |
|
703 | s, matches = c.complete(None, "config AliasManager.de") | |
|
704 | nt.assert_list_equal(["AliasManager.default_aliases"], matches) | |
|
703 | 705 | |
|
704 | 706 | def test_magic_color(self): |
|
705 | 707 | ip = get_ipython() |
|
706 | 708 | c = ip.Completer |
|
707 | 709 | |
|
708 |
s, matches = c.complete(None, |
|
|
709 |
nt.assert_in( |
|
|
710 |
s, matches = c.complete(None, |
|
|
711 |
nt.assert_not_in( |
|
|
712 |
s, matches = c.complete(None, |
|
|
713 |
nt.assert_not_in( |
|
|
714 |
s, matches = c.complete(None, |
|
|
715 |
nt.assert_in( |
|
|
716 |
s, matches = c.complete(None, |
|
|
717 |
nt.assert_in( |
|
|
718 |
s, matches = c.complete(None, |
|
|
719 |
nt.assert_list_equal([ |
|
|
720 |
s, matches = c.complete(None, |
|
|
721 |
nt.assert_list_equal([ |
|
|
722 | ||
|
710 | s, matches = c.complete(None, "colo") | |
|
711 | nt.assert_in("%colors", matches) | |
|
712 | s, matches = c.complete(None, "colo") | |
|
713 | nt.assert_not_in("NoColor", matches) | |
|
714 | s, matches = c.complete(None, "%colors") # No trailing space | |
|
715 | nt.assert_not_in("NoColor", matches) | |
|
716 | s, matches = c.complete(None, "colors ") | |
|
717 | nt.assert_in("NoColor", matches) | |
|
718 | s, matches = c.complete(None, "%colors ") | |
|
719 | nt.assert_in("NoColor", matches) | |
|
720 | s, matches = c.complete(None, "colors NoCo") | |
|
721 | nt.assert_list_equal(["NoColor"], matches) | |
|
722 | s, matches = c.complete(None, "%colors NoCo") | |
|
723 | nt.assert_list_equal(["NoColor"], matches) | |
|
723 | 724 | |
|
724 | 725 | def test_match_dict_keys(self): |
|
725 | 726 | """ |
|
726 | 727 | Test that match_dict_keys works on a couple of use case does return what |
|
727 | 728 | expected, and does not crash |
|
728 | 729 | """ |
|
729 |
delims = |
|
|
730 | delims = " \t\n`!@#$^&*()=+[{]}\\|;:'\",<>?" | |
|
730 | 731 | |
|
732 | keys = ["foo", b"far"] | |
|
733 | assert match_dict_keys(keys, "b'", delims=delims) == ("'", 2, ["far"]) | |
|
734 | assert match_dict_keys(keys, "b'f", delims=delims) == ("'", 2, ["far"]) | |
|
735 | assert match_dict_keys(keys, 'b"', delims=delims) == ('"', 2, ["far"]) | |
|
736 | assert match_dict_keys(keys, 'b"f', delims=delims) == ('"', 2, ["far"]) | |
|
731 | 737 | |
|
732 | keys = ['foo', b'far'] | |
|
733 |
assert match_dict_keys(keys, " |
|
|
734 |
assert match_dict_keys(keys, " |
|
|
735 |
assert match_dict_keys(keys, ' |
|
|
736 | assert match_dict_keys(keys, 'b"f', delims=delims) == ('"', 2 ,['far']) | |
|
738 | assert match_dict_keys(keys, "'", delims=delims) == ("'", 1, ["foo"]) | |
|
739 | assert match_dict_keys(keys, "'f", delims=delims) == ("'", 1, ["foo"]) | |
|
740 | assert match_dict_keys(keys, '"', delims=delims) == ('"', 1, ["foo"]) | |
|
741 | assert match_dict_keys(keys, '"f', delims=delims) == ('"', 1, ["foo"]) | |
|
737 | 742 | |
|
738 | assert match_dict_keys(keys, "'", delims=delims) == ("'", 1 ,['foo']) | |
|
739 | assert match_dict_keys(keys, "'f", delims=delims) == ("'", 1 ,['foo']) | |
|
740 | assert match_dict_keys(keys, '"', delims=delims) == ('"', 1 ,['foo']) | |
|
741 | assert match_dict_keys(keys, '"f', delims=delims) == ('"', 1 ,['foo']) | |
|
742 | ||
|
743 | 743 | match_dict_keys |
|
744 | 744 | |
|
745 | ||
|
746 | 745 | def test_dict_key_completion_string(self): |
|
747 | 746 | """Test dictionary key completion for string keys""" |
|
748 | 747 | ip = get_ipython() |
|
749 | 748 | complete = ip.Completer.complete |
|
750 | 749 | |
|
751 |
ip.user_ns[ |
|
|
750 | ip.user_ns["d"] = {"abc": None} | |
|
752 | 751 | |
|
753 | 752 | # check completion at different stages |
|
754 | 753 | _, matches = complete(line_buffer="d[") |
@@ -764,13 +763,13 b' class TestCompleter(unittest.TestCase):' | |||
|
764 | 763 | nt.assert_not_in("abc']", matches) |
|
765 | 764 | |
|
766 | 765 | # check use of different quoting |
|
767 |
_, matches = complete(line_buffer=" |
|
|
766 | _, matches = complete(line_buffer='d["') | |
|
768 | 767 | nt.assert_in("abc", matches) |
|
769 |
nt.assert_not_in('abc |
|
|
768 | nt.assert_not_in('abc"]', matches) | |
|
770 | 769 | |
|
771 |
_, matches = complete(line_buffer=" |
|
|
770 | _, matches = complete(line_buffer='d["a') | |
|
772 | 771 | nt.assert_in("abc", matches) |
|
773 |
nt.assert_not_in('abc |
|
|
772 | nt.assert_not_in('abc"]', matches) | |
|
774 | 773 | |
|
775 | 774 | # check sensitivity to following context |
|
776 | 775 | _, matches = complete(line_buffer="d[]", cursor_pos=2) |
@@ -782,23 +781,28 b' class TestCompleter(unittest.TestCase):' | |||
|
782 | 781 | nt.assert_not_in("abc']", matches) |
|
783 | 782 | |
|
784 | 783 | # check multiple solutions are correctly returned and that noise is not |
|
785 | ip.user_ns['d'] = {'abc': None, 'abd': None, 'bad': None, object(): None, | |
|
786 |
|
|
|
784 | ip.user_ns["d"] = { | |
|
785 | "abc": None, | |
|
786 | "abd": None, | |
|
787 | "bad": None, | |
|
788 | object(): None, | |
|
789 | 5: None, | |
|
790 | } | |
|
787 | 791 | |
|
788 | 792 | _, matches = complete(line_buffer="d['a") |
|
789 | 793 | nt.assert_in("abc", matches) |
|
790 | 794 | nt.assert_in("abd", matches) |
|
791 | 795 | nt.assert_not_in("bad", matches) |
|
792 |
assert not any(m.endswith(( |
|
|
796 | assert not any(m.endswith(("]", '"', "'")) for m in matches), matches | |
|
793 | 797 | |
|
794 | 798 | # check escaping and whitespace |
|
795 |
ip.user_ns[ |
|
|
799 | ip.user_ns["d"] = {"a\nb": None, "a'b": None, 'a"b': None, "a word": None} | |
|
796 | 800 | _, matches = complete(line_buffer="d['a") |
|
797 | 801 | nt.assert_in("a\\nb", matches) |
|
798 | 802 | nt.assert_in("a\\'b", matches) |
|
799 |
nt.assert_in(" |
|
|
803 | nt.assert_in('a"b', matches) | |
|
800 | 804 | nt.assert_in("a word", matches) |
|
801 |
assert not any(m.endswith(( |
|
|
805 | assert not any(m.endswith(("]", '"', "'")) for m in matches), matches | |
|
802 | 806 | |
|
803 | 807 | # - can complete on non-initial word of the string |
|
804 | 808 | _, matches = complete(line_buffer="d['a w") |
@@ -810,37 +814,38 b' class TestCompleter(unittest.TestCase):' | |||
|
810 | 814 | |
|
811 | 815 | # - default quoting should work like repr |
|
812 | 816 | _, matches = complete(line_buffer="d[") |
|
813 |
nt.assert_in(" |
|
|
817 | nt.assert_in('"a\'b"', matches) | |
|
814 | 818 | |
|
815 | 819 | # - when opening quote with ", possible to match with unescaped apostrophe |
|
816 | 820 | _, matches = complete(line_buffer="d[\"a'") |
|
817 | 821 | nt.assert_in("b", matches) |
|
818 | 822 | |
|
819 | 823 | # need to not split at delims that readline won't split at |
|
820 |
if |
|
|
821 |
ip.user_ns[ |
|
|
824 | if "-" not in ip.Completer.splitter.delims: | |
|
825 | ip.user_ns["d"] = {"before-after": None} | |
|
822 | 826 | _, matches = complete(line_buffer="d['before-af") |
|
823 |
nt.assert_in( |
|
|
827 | nt.assert_in("before-after", matches) | |
|
824 | 828 | |
|
825 | 829 | def test_dict_key_completion_contexts(self): |
|
826 | 830 | """Test expression contexts in which dict key completion occurs""" |
|
827 | 831 | ip = get_ipython() |
|
828 | 832 | complete = ip.Completer.complete |
|
829 |
d = { |
|
|
830 |
ip.user_ns[ |
|
|
833 | d = {"abc": None} | |
|
834 | ip.user_ns["d"] = d | |
|
831 | 835 | |
|
832 | 836 | class C: |
|
833 | 837 | data = d |
|
834 | ip.user_ns['C'] = C | |
|
835 |
ip.user_ns[ |
|
|
838 | ||
|
839 | ip.user_ns["C"] = C | |
|
840 | ip.user_ns["get"] = lambda: d | |
|
836 | 841 | |
|
837 | 842 | def assert_no_completion(**kwargs): |
|
838 | 843 | _, matches = complete(**kwargs) |
|
839 |
nt.assert_not_in( |
|
|
840 |
nt.assert_not_in(' |
|
|
841 |
nt.assert_not_in(' |
|
|
842 |
nt.assert_not_in(' |
|
|
843 |
nt.assert_not_in(' |
|
|
844 | nt.assert_not_in("abc", matches) | |
|
845 | nt.assert_not_in("abc'", matches) | |
|
846 | nt.assert_not_in("abc']", matches) | |
|
847 | nt.assert_not_in("'abc'", matches) | |
|
848 | nt.assert_not_in("'abc']", matches) | |
|
844 | 849 | |
|
845 | 850 | def assert_completion(**kwargs): |
|
846 | 851 | _, matches = complete(**kwargs) |
@@ -849,7 +854,7 b' class TestCompleter(unittest.TestCase):' | |||
|
849 | 854 | |
|
850 | 855 | # no completion after string closed, even if reopened |
|
851 | 856 | assert_no_completion(line_buffer="d['a'") |
|
852 |
assert_no_completion(line_buffer=" |
|
|
857 | assert_no_completion(line_buffer='d["a"') | |
|
853 | 858 | assert_no_completion(line_buffer="d['a' + ") |
|
854 | 859 | assert_no_completion(line_buffer="d['a' + '") |
|
855 | 860 | |
@@ -862,7 +867,7 b' class TestCompleter(unittest.TestCase):' | |||
|
862 | 867 | def assert_completion(**kwargs): |
|
863 | 868 | _, matches = complete(**kwargs) |
|
864 | 869 | nt.assert_in("get()['abc']", matches) |
|
865 | ||
|
870 | ||
|
866 | 871 | assert_no_completion(line_buffer="get()[") |
|
867 | 872 | with greedy_completion(): |
|
868 | 873 | assert_completion(line_buffer="get()[") |
@@ -871,14 +876,12 b' class TestCompleter(unittest.TestCase):' | |||
|
871 | 876 | assert_completion(line_buffer="get()['ab") |
|
872 | 877 | assert_completion(line_buffer="get()['abc") |
|
873 | 878 | |
|
874 | ||
|
875 | ||
|
876 | 879 | def test_dict_key_completion_bytes(self): |
|
877 | 880 | """Test handling of bytes in dict key completion""" |
|
878 | 881 | ip = get_ipython() |
|
879 | 882 | complete = ip.Completer.complete |
|
880 | 883 | |
|
881 |
ip.user_ns[ |
|
|
884 | ip.user_ns["d"] = {"abc": None, b"abd": None} | |
|
882 | 885 | |
|
883 | 886 | _, matches = complete(line_buffer="d[") |
|
884 | 887 | nt.assert_in("'abc'", matches) |
@@ -901,16 +904,15 b' class TestCompleter(unittest.TestCase):' | |||
|
901 | 904 | nt.assert_in("abc", matches) |
|
902 | 905 | nt.assert_not_in("abd", matches) |
|
903 | 906 | |
|
904 | ||
|
905 | 907 | def test_dict_key_completion_unicode_py3(self): |
|
906 | 908 | """Test handling of unicode in dict key completion""" |
|
907 | 909 | ip = get_ipython() |
|
908 | 910 | complete = ip.Completer.complete |
|
909 | 911 | |
|
910 |
ip.user_ns[ |
|
|
912 | ip.user_ns["d"] = {"a\u05d0": None} | |
|
911 | 913 | |
|
912 | 914 | # query using escape |
|
913 |
if sys.platform != |
|
|
915 | if sys.platform != "win32": | |
|
914 | 916 | # Known failure on Windows |
|
915 | 917 | _, matches = complete(line_buffer="d['a\\u05d0") |
|
916 | 918 | nt.assert_in("u05d0", matches) # tokenized after \\ |
@@ -918,7 +920,7 b' class TestCompleter(unittest.TestCase):' | |||
|
918 | 920 | # query using character |
|
919 | 921 | _, matches = complete(line_buffer="d['a\u05d0") |
|
920 | 922 | nt.assert_in("a\u05d0", matches) |
|
921 | ||
|
923 | ||
|
922 | 924 | with greedy_completion(): |
|
923 | 925 | # query using escape |
|
924 | 926 | _, matches = complete(line_buffer="d['a\\u05d0") |
@@ -927,57 +929,56 b' class TestCompleter(unittest.TestCase):' | |||
|
927 | 929 | # query using character |
|
928 | 930 | _, matches = complete(line_buffer="d['a\u05d0") |
|
929 | 931 | nt.assert_in("d['a\u05d0']", matches) |
|
930 | ||
|
931 | 932 | |
|
932 | ||
|
933 | @dec.skip_without('numpy') | |
|
933 | @dec.skip_without("numpy") | |
|
934 | 934 | def test_struct_array_key_completion(self): |
|
935 | 935 | """Test dict key completion applies to numpy struct arrays""" |
|
936 | 936 | import numpy |
|
937 | ||
|
937 | 938 | ip = get_ipython() |
|
938 | 939 | complete = ip.Completer.complete |
|
939 |
ip.user_ns[ |
|
|
940 | ip.user_ns["d"] = numpy.array([], dtype=[("hello", "f"), ("world", "f")]) | |
|
940 | 941 | _, matches = complete(line_buffer="d['") |
|
941 | 942 | nt.assert_in("hello", matches) |
|
942 | 943 | nt.assert_in("world", matches) |
|
943 | 944 | # complete on the numpy struct itself |
|
944 | dt = numpy.dtype([('my_head', [('my_dt', '>u4'), ('my_df', '>u4')]), | |
|
945 | ('my_data', '>f4', 5)]) | |
|
945 | dt = numpy.dtype( | |
|
946 | [("my_head", [("my_dt", ">u4"), ("my_df", ">u4")]), ("my_data", ">f4", 5)] | |
|
947 | ) | |
|
946 | 948 | x = numpy.zeros(2, dtype=dt) |
|
947 |
ip.user_ns[ |
|
|
949 | ip.user_ns["d"] = x[1] | |
|
948 | 950 | _, matches = complete(line_buffer="d['") |
|
949 | 951 | nt.assert_in("my_head", matches) |
|
950 | 952 | nt.assert_in("my_data", matches) |
|
951 | 953 | # complete on a nested level |
|
952 | 954 | with greedy_completion(): |
|
953 |
ip.user_ns[ |
|
|
955 | ip.user_ns["d"] = numpy.zeros(2, dtype=dt) | |
|
954 | 956 | _, matches = complete(line_buffer="d[1]['my_head']['") |
|
955 | 957 | nt.assert_true(any(["my_dt" in m for m in matches])) |
|
956 | 958 | nt.assert_true(any(["my_df" in m for m in matches])) |
|
957 | 959 | |
|
958 | ||
|
959 | @dec.skip_without('pandas') | |
|
960 | @dec.skip_without("pandas") | |
|
960 | 961 | def test_dataframe_key_completion(self): |
|
961 | 962 | """Test dict key completion applies to pandas DataFrames""" |
|
962 | 963 | import pandas |
|
964 | ||
|
963 | 965 | ip = get_ipython() |
|
964 | 966 | complete = ip.Completer.complete |
|
965 |
ip.user_ns[ |
|
|
967 | ip.user_ns["d"] = pandas.DataFrame({"hello": [1], "world": [2]}) | |
|
966 | 968 | _, matches = complete(line_buffer="d['") |
|
967 | 969 | nt.assert_in("hello", matches) |
|
968 | 970 | nt.assert_in("world", matches) |
|
969 | 971 | |
|
970 | ||
|
971 | 972 | def test_dict_key_completion_invalids(self): |
|
972 | 973 | """Smoke test cases dict key completion can't handle""" |
|
973 | 974 | ip = get_ipython() |
|
974 | 975 | complete = ip.Completer.complete |
|
975 | 976 | |
|
976 |
ip.user_ns[ |
|
|
977 |
ip.user_ns[ |
|
|
978 |
ip.user_ns[ |
|
|
979 |
ip.user_ns[ |
|
|
980 |
ip.user_ns[ |
|
|
977 | ip.user_ns["no_getitem"] = None | |
|
978 | ip.user_ns["no_keys"] = [] | |
|
979 | ip.user_ns["cant_call_keys"] = dict | |
|
980 | ip.user_ns["empty"] = {} | |
|
981 | ip.user_ns["d"] = {"abc": 5} | |
|
981 | 982 | |
|
982 | 983 | _, matches = complete(line_buffer="no_getitem['") |
|
983 | 984 | _, matches = complete(line_buffer="no_keys['") |
@@ -988,71 +989,72 b' class TestCompleter(unittest.TestCase):' | |||
|
988 | 989 | |
|
989 | 990 | def test_object_key_completion(self): |
|
990 | 991 | ip = get_ipython() |
|
991 |
ip.user_ns[ |
|
|
992 | ip.user_ns["key_completable"] = KeyCompletable(["qwerty", "qwick"]) | |
|
992 | 993 | |
|
993 | 994 | _, matches = ip.Completer.complete(line_buffer="key_completable['qw") |
|
994 |
nt.assert_in( |
|
|
995 |
nt.assert_in( |
|
|
996 | ||
|
997 | ||
|
995 | nt.assert_in("qwerty", matches) | |
|
996 | nt.assert_in("qwick", matches) | |
|
998 | 997 | |
|
999 | 998 | def test_class_key_completion(self): |
|
1000 | 999 | ip = get_ipython() |
|
1001 |
NamedInstanceClass( |
|
|
1002 |
NamedInstanceClass( |
|
|
1003 |
ip.user_ns[ |
|
|
1000 | NamedInstanceClass("qwerty") | |
|
1001 | NamedInstanceClass("qwick") | |
|
1002 | ip.user_ns["named_instance_class"] = NamedInstanceClass | |
|
1004 | 1003 | |
|
1005 | 1004 | _, matches = ip.Completer.complete(line_buffer="named_instance_class['qw") |
|
1006 |
nt.assert_in( |
|
|
1007 |
nt.assert_in( |
|
|
1005 | nt.assert_in("qwerty", matches) | |
|
1006 | nt.assert_in("qwick", matches) | |
|
1008 | 1007 | |
|
1009 | 1008 | def test_tryimport(self): |
|
1010 | 1009 | """ |
|
1011 | 1010 | Test that try-import don't crash on trailing dot, and import modules before |
|
1012 | 1011 | """ |
|
1013 | 1012 | from IPython.core.completerlib import try_import |
|
1014 | assert(try_import("IPython.")) | |
|
1015 | 1013 | |
|
1014 | assert try_import("IPython.") | |
|
1016 | 1015 | |
|
1017 | 1016 | def test_aimport_module_completer(self): |
|
1018 | 1017 | ip = get_ipython() |
|
1019 |
_, matches = ip.complete( |
|
|
1020 |
nt.assert_in( |
|
|
1021 |
nt.assert_not_in( |
|
|
1018 | _, matches = ip.complete("i", "%aimport i") | |
|
1019 | nt.assert_in("io", matches) | |
|
1020 | nt.assert_not_in("int", matches) | |
|
1022 | 1021 | |
|
1023 | 1022 | def test_nested_import_module_completer(self): |
|
1024 | 1023 | ip = get_ipython() |
|
1025 |
_, matches = ip.complete(None, |
|
|
1026 |
nt.assert_in( |
|
|
1027 |
nt.assert_not_in( |
|
|
1028 |
nt.assert_not_in( |
|
|
1024 | _, matches = ip.complete(None, "import IPython.co", 17) | |
|
1025 | nt.assert_in("IPython.core", matches) | |
|
1026 | nt.assert_not_in("import IPython.core", matches) | |
|
1027 | nt.assert_not_in("IPython.display", matches) | |
|
1029 | 1028 | |
|
1030 | 1029 | def test_import_module_completer(self): |
|
1031 | 1030 | ip = get_ipython() |
|
1032 |
_, matches = ip.complete( |
|
|
1033 |
nt.assert_in( |
|
|
1034 |
nt.assert_not_in( |
|
|
1031 | _, matches = ip.complete("i", "import i") | |
|
1032 | nt.assert_in("io", matches) | |
|
1033 | nt.assert_not_in("int", matches) | |
|
1035 | 1034 | |
|
1036 | 1035 | def test_from_module_completer(self): |
|
1037 | 1036 | ip = get_ipython() |
|
1038 |
_, matches = ip.complete( |
|
|
1039 |
nt.assert_in( |
|
|
1040 |
nt.assert_not_in( |
|
|
1037 | _, matches = ip.complete("B", "from io import B", 16) | |
|
1038 | nt.assert_in("BytesIO", matches) | |
|
1039 | nt.assert_not_in("BaseException", matches) | |
|
1041 | 1040 | |
|
1042 | 1041 | def test_snake_case_completion(self): |
|
1043 | 1042 | ip = get_ipython() |
|
1044 | 1043 | ip.Completer.use_jedi = False |
|
1045 |
ip.user_ns[ |
|
|
1046 |
ip.user_ns[ |
|
|
1044 | ip.user_ns["some_three"] = 3 | |
|
1045 | ip.user_ns["some_four"] = 4 | |
|
1047 | 1046 | _, matches = ip.complete("s_", "print(s_f") |
|
1048 |
nt.assert_in( |
|
|
1049 |
nt.assert_in( |
|
|
1047 | nt.assert_in("some_three", matches) | |
|
1048 | nt.assert_in("some_four", matches) | |
|
1050 | 1049 | |
|
1051 | 1050 | def test_mix_terms(self): |
|
1052 | 1051 | ip = get_ipython() |
|
1053 | 1052 | from textwrap import dedent |
|
1053 | ||
|
1054 | 1054 | ip.Completer.use_jedi = False |
|
1055 |
ip.ex( |
|
|
1055 | ip.ex( | |
|
1056 | dedent( | |
|
1057 | """ | |
|
1056 | 1058 |
|
|
1057 | 1059 |
|
|
1058 | 1060 | print("meth") |
@@ -1063,7 +1065,9 b' class TestCompleter(unittest.TestCase):' | |||
|
1063 | 1065 |
|
|
1064 | 1066 | print("meth2") |
|
1065 | 1067 |
|
|
1066 |
|
|
|
1068 | """ | |
|
1069 | ) | |
|
1070 | ) | |
|
1067 | 1071 | _, matches = ip.complete(None, "test.meth(") |
|
1068 |
nt.assert_in( |
|
|
1069 |
nt.assert_not_in( |
|
|
1072 | nt.assert_in("meth_arg1=", matches) | |
|
1073 | nt.assert_not_in("meth2_arg1=", matches) |
General Comments 0
You need to be logged in to leave comments.
Login now