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