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