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 | 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(): | |
|
126 | ip = get_ipython() | |
|
127 | # Some strings that trigger different types of completion. Check them both | |
|
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() | |
|
113 | class NamedInstanceMetaclass(type): | |
|
114 | def __getitem__(cls, item): | |
|
115 | return cls.get_instance(item) | |
|
164 | 116 | |
|
165 | # do not return more than 1 matches fro \beta, only the latex one. | |
|
166 | name, matches = ip.complete('\\Ξ²') | |
|
167 | nt.assert_equal(len(matches), 1) | |
|
168 | nt.assert_equal(matches[0], '\\beta') | |
|
117 | class NamedInstanceClass(object, metaclass=NamedInstanceMetaclass): | |
|
118 | def __init__(self, name): | |
|
119 | if not hasattr(self.__class__, 'instances'): | |
|
120 | self.__class__.instances = {} | |
|
121 | self.__class__.instances[name] = self | |
|
169 | 122 | |
|
170 | def test_back_unicode_completion(): | |
|
171 | ip = get_ipython() | |
|
172 | ||
|
173 | name, matches = ip.complete('\\β €') | |
|
174 | nt.assert_equal(len(matches), 1) | |
|
175 | nt.assert_equal(matches[0], '\\ROMAN NUMERAL FIVE') | |
|
123 | @classmethod | |
|
124 | def _ipython_key_completions_(cls): | |
|
125 | return cls.instances.keys() | |
|
176 | 126 | |
|
127 | @classmethod | |
|
128 | def get_instance(cls, name): | |
|
129 | return cls.instances[name] | |
|
177 | 130 | |
|
178 | def test_forward_unicode_completion(): | |
|
179 | ip = get_ipython() | |
|
180 | ||
|
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, []) | |
|
131 | class KeyCompletable(object): | |
|
132 | def __init__(self, things=()): | |
|
133 | self.things = things | |
|
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 | 141 | def setUp(self): |
|
200 | self.sp = completer.CompletionSplitter() | |
|
201 | ||
|
202 | def test_delim_setting(self): | |
|
203 | self.sp.delims = ' ' | |
|
204 | nt.assert_equal(self.sp.delims, ' ') | |
|
205 | nt.assert_equal(self.sp._delim_expr, r'[\ ]') | |
|
206 | ||
|
207 | def test_spaces(self): | |
|
208 | """Test with only spaces as split chars.""" | |
|
209 | self.sp.delims = ' ' | |
|
210 | t = [('foo', '', 'foo'), | |
|
211 | ('run foo', '', 'foo'), | |
|
212 | ('run foo', 'bar', 'foo'), | |
|
213 | ] | |
|
214 | check_line_split(self.sp, t) | |
|
215 | ||
|
216 | ||
|
217 | def test_has_open_quotes1(): | |
|
218 | for s in ["'", "'''", "'hi' '"]: | |
|
219 | nt.assert_equal(completer.has_open_quotes(s), "'") | |
|
220 | ||
|
221 | ||
|
222 | def test_has_open_quotes2(): | |
|
223 | for s in ['"', '"""', '"hi" "']: | |
|
224 | nt.assert_equal(completer.has_open_quotes(s), '"') | |
|
225 | ||
|
226 | ||
|
227 | def test_has_open_quotes3(): | |
|
228 | for s in ["''", "''' '''", "'hi' 'ipython'"]: | |
|
229 | nt.assert_false(completer.has_open_quotes(s)) | |
|
230 | ||
|
231 | ||
|
232 | def test_has_open_quotes4(): | |
|
233 | for s in ['""', '""" """', '"hi" "ipython"']: | |
|
234 | nt.assert_false(completer.has_open_quotes(s)) | |
|
235 | ||
|
236 | ||
|
237 | @decorators.knownfailureif(sys.platform == 'win32', "abspath completions fail on Windows") | |
|
238 | def test_abspath_file_completions(): | |
|
239 | ip = get_ipython() | |
|
240 | with TemporaryDirectory() as tmpdir: | |
|
241 | prefix = os.path.join(tmpdir, 'foo') | |
|
242 | suffixes = ['1', '2'] | |
|
243 | names = [prefix+s for s in suffixes] | |
|
244 | for n in names: | |
|
245 | open(n, 'w').close() | |
|
246 | ||
|
247 | # Check simple completion | |
|
248 | c = ip.complete(prefix)[1] | |
|
249 |
nt.assert_equal(c, |
|
|
250 | ||
|
251 | # Now check with a function call | |
|
252 | cmd = 'a = f("%s' % prefix | |
|
253 | c = ip.complete(prefix, cmd)[1] | |
|
254 | comp = [prefix+s for s in suffixes] | |
|
255 | nt.assert_equal(c, comp) | |
|
256 | ||
|
257 | ||
|
258 | def test_local_file_completions(): | |
|
259 | ip = get_ipython() | |
|
260 | with TemporaryWorkingDirectory(): | |
|
261 | prefix = './foo' | |
|
262 | suffixes = ['1', '2'] | |
|
263 | names = [prefix+s for s in suffixes] | |
|
264 | for n in names: | |
|
265 | open(n, 'w').close() | |
|
142 | """ | |
|
143 | We want to silence all PendingDeprecationWarning when testing the completer | |
|
144 | """ | |
|
145 | self._assertwarns = self.assertWarns(PendingDeprecationWarning) | |
|
146 | self._assertwarns.__enter__() | |
|
147 | ||
|
148 | def tearDown(self): | |
|
149 | try: | |
|
150 | self._assertwarns.__exit__(None, None, None) | |
|
151 | except AssertionError: | |
|
152 | pass | |
|
153 | ||
|
154 | ||
|
155 | def test_custom_completion_error(self): | |
|
156 | """Test that errors from custom attribute completers are silenced.""" | |
|
157 | ip = get_ipython() | |
|
158 | class A(object): pass | |
|
159 | ip.user_ns['x'] = A() | |
|
160 | ||
|
161 | @complete_object.register(A) | |
|
162 | def complete_A(a, existing_completions): | |
|
163 | raise TypeError("this should be silenced") | |
|
164 | ||
|
165 | ip.complete("x.") | |
|
166 | ||
|
167 | def test_unicode_completions(self): | |
|
168 | ip = get_ipython() | |
|
169 | # Some strings that trigger different types of completion. Check them both | |
|
170 | # in str and unicode forms | |
|
171 | s = ['ru', '%ru', 'cd /', 'floa', 'float(x)/'] | |
|
172 | for t in s + list(map(str, s)): | |
|
173 | # We don't need to check exact completion values (they may change | |
|
174 | # depending on the state of the namespace, but at least no exceptions | |
|
175 | # should be thrown and the return value should be a pair of text, list | |
|
176 | # values. | |
|
177 | text, matches = ip.complete(t) | |
|
178 | nt.assert_true(isinstance(text, str)) | |
|
179 | nt.assert_true(isinstance(matches, list)) | |
|
180 | ||
|
181 | def test_latex_completions(self): | |
|
182 | from IPython.core.latex_symbols import latex_symbols | |
|
183 | import random | |
|
184 | ip = get_ipython() | |
|
185 | # Test some random unicode symbols | |
|
186 | keys = random.sample(latex_symbols.keys(), 10) | |
|
187 | for k in keys: | |
|
188 | text, matches = ip.complete(k) | |
|
189 | nt.assert_equal(len(matches),1) | |
|
190 | nt.assert_equal(text, k) | |
|
191 | nt.assert_equal(matches[0], latex_symbols[k]) | |
|
192 | # Test a more complex line | |
|
193 | text, matches = ip.complete(u'print(\\alpha') | |
|
194 | nt.assert_equal(text, u'\\alpha') | |
|
195 | nt.assert_equal(matches[0], latex_symbols['\\alpha']) | |
|
196 | # Test multiple matching latex symbols | |
|
197 | text, matches = ip.complete(u'\\al') | |
|
198 | nt.assert_in('\\alpha', matches) | |
|
199 | nt.assert_in('\\aleph', matches) | |
|
200 | ||
|
201 | ||
|
202 | ||
|
203 | ||
|
204 | def test_back_latex_completion(self): | |
|
205 | ip = get_ipython() | |
|
206 | ||
|
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 | |
|
272 | cmd = 'a = f("%s' % prefix | |
|
273 | c = ip.complete(prefix, cmd)[1] | |
|
274 | comp = set(prefix+s for s in suffixes) | |
|
275 | nt.assert_true(comp.issubset(set(c))) | |
|
220 | def test_forward_unicode_completion(self): | |
|
221 | ip = get_ipython() | |
|
222 | ||
|
223 | name, matches = ip.complete('\\ROMAN NUMERAL FIVE') | |
|
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(): | |
|
279 | ip = get_ipython() | |
|
280 | with TemporaryWorkingDirectory(): | |
|
281 | name = "foo'bar" | |
|
282 | open(name, 'w').close() | |
|
283 | ||
|
284 | # Don't escape Windows | |
|
285 | escaped = name if sys.platform == "win32" else "foo\\'bar" | |
|
286 | ||
|
287 | # Single quote matches embedded single quote | |
|
288 | text = "open('foo" | |
|
289 | c = ip.Completer._complete(cursor_line=0, | |
|
290 | cursor_pos=len(text), | |
|
291 | full_text=text)[1] | |
|
292 | nt.assert_equal(c, [escaped]) | |
|
293 | ||
|
294 | # Double quote requires no escape | |
|
295 | text = 'open("foo' | |
|
296 | c = ip.Completer._complete(cursor_line=0, | |
|
297 | cursor_pos=len(text), | |
|
298 | full_text=text)[1] | |
|
299 | nt.assert_equal(c, [name]) | |
|
300 | ||
|
301 | # No quote requires an escape | |
|
302 | text = '%ls foo' | |
|
303 | c = ip.Completer._complete(cursor_line=0, | |
|
304 | cursor_pos=len(text), | |
|
305 | full_text=text)[1] | |
|
306 | nt.assert_equal(c, [escaped]) | |
|
307 | ||
|
308 | ||
|
309 | def test_jedi(): | |
|
310 | """ | |
|
311 | A couple of issue we had with Jedi | |
|
312 | """ | |
|
313 | ip = get_ipython() | |
|
259 | def test_has_open_quotes1(self): | |
|
260 | for s in ["'", "'''", "'hi' '"]: | |
|
261 | nt.assert_equal(completer.has_open_quotes(s), "'") | |
|
262 | ||
|
263 | ||
|
264 | def test_has_open_quotes2(self): | |
|
265 | for s in ['"', '"""', '"hi" "']: | |
|
266 | nt.assert_equal(completer.has_open_quotes(s), '"') | |
|
267 | ||
|
268 | ||
|
269 | def test_has_open_quotes3(self): | |
|
270 | for s in ["''", "''' '''", "'hi' 'ipython'"]: | |
|
271 | nt.assert_false(completer.has_open_quotes(s)) | |
|
272 | ||
|
273 | ||
|
274 | def test_has_open_quotes4(self): | |
|
275 | for s in ['""', '""" """', '"hi" "ipython"']: | |
|
276 | nt.assert_false(completer.has_open_quotes(s)) | |
|
277 | ||
|
278 | ||
|
279 | @decorators.knownfailureif(sys.platform == 'win32', "abspath completions fail on Windows") | |
|
280 | def test_abspath_file_completions(self): | |
|
281 | ip = get_ipython() | |
|
282 | with TemporaryDirectory() as tmpdir: | |
|
283 | prefix = os.path.join(tmpdir, 'foo') | |
|
284 | suffixes = ['1', '2'] | |
|
285 | names = [prefix+s for s in suffixes] | |
|
286 | for n in names: | |
|
287 | open(n, 'w').close() | |
|
288 | ||
|
289 | # Check simple completion | |
|
290 | c = ip.complete(prefix)[1] | |
|
291 | nt.assert_equal(c, names) | |
|
292 | ||
|
293 | # Now check with a function call | |
|
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 |
|
|
316 | l = len(s) | |
|
317 | start = start if start is not None else l | |
|
318 | end = end if end is not None else l | |
|
367 | def _test_not_complete(reason, s, comp): | |
|
368 | l = len(s) | |
|
369 | with provisionalcompleter(): | |
|
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 | 390 | with provisionalcompleter(): |
|
320 | 391 | ip.Completer.use_jedi = True |
|
321 |
completions = |
|
|
392 | completions = ip.Completer.completions('ope', 3) | |
|
393 | c = next(completions) # should be `open` | |
|
322 | 394 | ip.Completer.use_jedi = False |
|
323 | assert_in(Completion(start, end, comp), completions, reason) | |
|
324 | ||
|
325 | def _test_not_complete(reason, s, comp): | |
|
326 | l = len(s) | |
|
395 | assert 'file' in c.signature, "Signature of function was not found by completer" | |
|
396 | assert 'encoding' in c.signature, "Signature of function was not found by completer" | |
|
397 | ||
|
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 | 408 | with provisionalcompleter(): |
|
328 | 409 | ip.Completer.use_jedi = True |
|
329 |
|
|
|
410 | l = list(_deduplicate_completions('Z.z', ip.Completer.completions('Z.z', 3))) | |
|
330 | 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 | |
|
372 | assert l[0].text == 'zoo' # and not `it.accumulate` | |
|
417 | def test_greedy_completions(self): | |
|
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(): | |
|
376 | """ | |
|
377 | Test the capability of the Greedy completer. | |
|
424 | See the :any:`test_dict_key_completion_contexts` | |
|
378 | 425 | |
|
379 | Most of the test here does not really show off the greedy completer, for proof | |
|
380 | each of the text below now pass with Jedi. The greedy completer is capable of more. | |
|
426 | """ | |
|
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 | """ | |
|
385 | ip = get_ipython() | |
|
386 | ip.ex('a=list(range(5))') | |
|
387 | _,c = ip.complete('.',line='a[0].') | |
|
388 | nt.assert_false('.real' in c, | |
|
389 | "Shouldn't have completed on a[0]: %s"%c) | |
|
390 | def _(line, cursor_pos, expect, message, completion): | |
|
391 | with greedy_completion(), provisionalcompleter(): | |
|
392 | ip.Completer.use_jedi = False | |
|
393 | _,c = ip.complete('.', line=line, cursor_pos=cursor_pos) | |
|
394 | nt.assert_in(expect, c, message % c) | |
|
395 | ||
|
396 | ip.Completer.use_jedi = True | |
|
397 |
|
|
|
398 | completions = ip.Completer.completions(line, cursor_pos) | |
|
399 | nt.assert_in(completion, completions) | |
|
443 | with provisionalcompleter(): | |
|
444 | yield _, 'a[0].', 5, 'a[0].real', "Should have completed on a[0].: %s", Completion(5,5, 'real') | |
|
445 | yield _, 'a[0].r', 6, 'a[0].real', "Should have completed on a[0].r: %s", Completion(5,6, 'real') | |
|
446 | ||
|
447 | if sys.version_info > (3, 4): | |
|
448 | yield _, 'a[0].from_', 10, 'a[0].from_bytes', "Should have completed on a[0].from_: %s", Completion(5, 10, 'from_bytes') | |
|
449 | ||
|
450 | ||
|
451 | def test_omit__names(self): | |
|
452 | # also happens to test IPCompleter as a configurable | |
|
453 | ip = get_ipython() | |
|
454 | ip._hidden_attr = 1 | |
|
455 | ip._x = {} | |
|
456 | c = ip.Completer | |
|
457 | ip.ex('ip=get_ipython()') | |
|
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(): | |
|
402 | yield _, 'a[0].', 5, 'a[0].real', "Should have completed on a[0].: %s", Completion(5,5, 'real') | |
|
403 | yield _, 'a[0].r', 6, 'a[0].real', "Should have completed on a[0].r: %s", Completion(5,6, 'real') | |
|
467 | # c.use_jedi = True | |
|
468 | # completions = set(c.completions('ip.', 3)) | |
|
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(): | |
|
410 | # also happens to test IPCompleter as a configurable | |
|
411 | ip = get_ipython() | |
|
412 | ip._hidden_attr = 1 | |
|
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) | |
|
496 | # c.use_jedi = True | |
|
497 | # completions = set(c.completions('ip.', 3)) | |
|
498 | # nt.assert_not_in(Completion(3,3,'__str__'), completions) | |
|
499 | # nt.assert_not_in(Completion(3,3, "_hidden_attr"), completions) | |
|
424 | 500 | |
|
425 | # c.use_jedi = True | |
|
426 | # completions = set(c.completions('ip.', 3)) | |
|
427 | # nt.assert_in(Completion(3, 3, '__str__'), completions) | |
|
428 | # nt.assert_in(Completion(3,3, "_hidden_attr"), completions) | |
|
501 | with provisionalcompleter(): | |
|
502 | c.use_jedi = False | |
|
503 | s,matches = c.complete('ip._x.') | |
|
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() | |
|
432 | cfg.IPCompleter.omit__names = 1 | |
|
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) | |
|
510 | del ip._hidden_attr | |
|
511 | del ip._x | |
|
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 | 520 | c.use_jedi = False |
|
461 | s,matches = c.complete('ip._x.') | |
|
462 | nt.assert_in('ip._x.keys', matches) | |
|
521 | ip.ex('class D: x=24') | |
|
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 | |
|
469 | del ip._x | |
|
530 | def test_get__all__entries_ok(self): | |
|
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(): | |
|
473 | """ | |
|
474 | Limit to all is deprecated, once we remove it this test can go away. | |
|
475 | """ | |
|
476 | ip = get_ipython() | |
|
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) | |
|
537 | def test_get__all__entries_no__all__ok(self): | |
|
538 | class A(object): | |
|
539 | pass | |
|
540 | words = completer.get__all__entries(A()) | |
|
541 | nt.assert_equal(words, []) | |
|
486 | 542 | |
|
487 | 543 | |
|
488 | def test_get__all__entries_ok(): | |
|
489 | class A(object): | |
|
490 | __all__ = ['x', 1] | |
|
491 | words = completer.get__all__entries(A()) | |
|
492 | nt.assert_equal(words, ['x']) | |
|
544 | def test_func_kw_completions(self): | |
|
545 | ip = get_ipython() | |
|
546 | c = ip.Completer | |
|
547 | c.use_jedi = False | |
|
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(): | |
|
503 | ip = get_ipython() | |
|
504 | c = ip.Completer | |
|
505 | c.use_jedi = False | |
|
506 | ip.ex('def myfunc(a=1,b=2): return a+b') | |
|
507 | s, matches = c.complete(None, 'myfunc(1,b') | |
|
508 | nt.assert_in('b=', matches) | |
|
509 | # Simulate completing with cursor right after b (pos==10): | |
|
510 | s, matches = c.complete(None, 'myfunc(1,b)', 10) | |
|
511 | nt.assert_in('b=', matches) | |
|
512 | s, matches = c.complete(None, 'myfunc(a="escaped\\")string",b') | |
|
513 | nt.assert_in('b=', matches) | |
|
514 | #builtin function | |
|
515 | s, matches = c.complete(None, 'min(k, k') | |
|
516 | nt.assert_in('key=', matches) | |
|
517 | ||
|
518 | ||
|
519 | def test_default_arguments_from_docstring(): | |
|
520 | ip = get_ipython() | |
|
521 | c = ip.Completer | |
|
522 | kwd = c._default_arguments_from_docstring( | |
|
523 | 'min(iterable[, key=func]) -> value') | |
|
524 | nt.assert_equal(kwd, ['key']) | |
|
525 | #with cython type etc | |
|
526 | kwd = c._default_arguments_from_docstring( | |
|
527 | 'Minuit.migrad(self, int ncall=10000, resume=True, int nsplit=1)\n') | |
|
528 | nt.assert_equal(kwd, ['ncall', 'resume', 'nsplit']) | |
|
529 | #white spaces | |
|
530 | kwd = c._default_arguments_from_docstring( | |
|
531 | '\n Minuit.migrad(self, int ncall=10000, resume=True, int nsplit=1)\n') | |
|
532 | nt.assert_equal(kwd, ['ncall', 'resume', 'nsplit']) | |
|
533 | ||
|
534 | def test_line_magics(): | |
|
535 | ip = get_ipython() | |
|
536 | c = ip.Completer | |
|
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) | |
|
604 | @register_line_cell_magic | |
|
605 | def _bar_cellm(line, cell): | |
|
606 | pass | |
|
607 | ||
|
608 | ip = get_ipython() | |
|
609 | c = ip.Completer | |
|
610 | ||
|
611 | # The policy here is trickier, see comments in completion code. The | |
|
612 | # returned values depend on whether the user passes %% or not explicitly, | |
|
613 | # and this will show a difference if the same name is both a line and cell | |
|
614 | # magic. | |
|
615 | s, matches = c.complete(None, '_bar_ce') | |
|
616 | nt.assert_in('%_bar_cellm', matches) | |
|
617 | nt.assert_in('%%_bar_cellm', matches) | |
|
618 | s, matches = c.complete(None, '%_bar_ce') | |
|
619 | nt.assert_in('%_bar_cellm', matches) | |
|
620 | nt.assert_in('%%_bar_cellm', matches) | |
|
621 | s, matches = c.complete(None, '%%_bar_ce') | |
|
622 | nt.assert_not_in('%_bar_cellm', matches) | |
|
623 | nt.assert_in('%%_bar_cellm', matches) | |
|
624 | ||
|
625 | ||
|
626 | def test_magic_completion_order(self): | |
|
627 | ip = get_ipython() | |
|
628 | c = ip.Completer | |
|
629 | ||
|
630 | # Test ordering of line and cell magics. | |
|
631 | text, matches = c.complete("timeit") | |
|
632 | nt.assert_equal(matches, ["%timeit", "%%timeit"]) | |
|
633 | ||
|
634 | ||
|
635 | def test_magic_completion_shadowing(self): | |
|
636 | ip = get_ipython() | |
|
637 | c = ip.Completer | |
|
638 | c.use_jedi = False | |
|
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 | |
|
547 | def _foo_cellm(line, cell): | |
|
548 | pass | |
|
549 | ||
|
550 | ip = get_ipython() | |
|
551 | c = ip.Completer | |
|
746 | def test_dict_key_completion_string(self): | |
|
747 | """Test dictionary key completion for string keys""" | |
|
748 | ip = get_ipython() | |
|
749 | complete = ip.Completer.complete | |
|
552 | 750 | |
|
553 | s, matches = c.complete(None, '_foo_ce') | |
|
554 | nt.assert_in('%%_foo_cellm', matches) | |
|
555 | s, matches = c.complete(None, '%%_foo_ce') | |
|
556 | nt.assert_in('%%_foo_cellm', matches) | |
|
751 | ip.user_ns['d'] = {'abc': None} | |
|
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(): | |
|
560 | from IPython.core.magic import register_line_cell_magic | |
|
758 | _, matches = complete(line_buffer="d['") | |
|
759 | nt.assert_in("abc", matches) | |
|
760 | nt.assert_not_in("abc']", matches) | |
|
561 | 761 | |
|
562 | @register_line_cell_magic | |
|
563 | def _bar_cellm(line, cell): | |
|
564 | pass | |
|
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 | |
|
762 | _, matches = complete(line_buffer="d['a") | |
|
763 | nt.assert_in("abc", matches) | |
|
764 | nt.assert_not_in("abc']", matches) | |
|
587 | 765 | |
|
588 | # Test ordering of line and cell magics. | |
|
589 |
|
|
|
590 | nt.assert_equal(matches, ["%timeit", "%%timeit"]) | |
|
766 | # check use of different quoting | |
|
767 | _, matches = complete(line_buffer="d[\"") | |
|
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(): | |
|
594 | ip = get_ipython() | |
|
595 | c = ip.Completer | |
|
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 | |
|
775 | # check sensitivity to following context | |
|
776 | _, matches = complete(line_buffer="d[]", cursor_pos=2) | |
|
777 | nt.assert_in("'abc'", matches) | |
|
620 | 778 | |
|
621 | # Before importing matplotlib, %matplotlib magic should be the only option. | |
|
622 | text, matches = c.complete("%mat") | |
|
623 | nt.assert_equal(matches, ["%matplotlib"]) | |
|
779 | _, matches = complete(line_buffer="d['']", cursor_pos=3) | |
|
780 | nt.assert_in("abc", matches) | |
|
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 | |
|
628 | # the only option. | |
|
629 | text, matches = c.complete("%mat") | |
|
630 | nt.assert_equal(matches, ["%matplotlib"]) | |
|
788 | _, matches = complete(line_buffer="d['a") | |
|
789 | nt.assert_in("abc", matches) | |
|
790 | nt.assert_in("abd", matches) | |
|
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[") | |
|
842 | nt.assert_in("'abc'", matches) | |
|
843 | nt.assert_in("b'abd'", matches) | |
|
881 | ip.user_ns['d'] = {'abc': None, b'abd': None} | |
|
844 | 882 | |
|
845 | if False: # not currently implemented | |
|
846 | _, matches = complete(line_buffer="d[b") | |
|
883 | _, matches = complete(line_buffer="d[") | |
|
884 | nt.assert_in("'abc'", matches) | |
|
847 | 885 | nt.assert_in("b'abd'", matches) |
|
848 | nt.assert_not_in("b'abc'", matches) | |
|
849 | 886 | |
|
850 | _, matches = complete(line_buffer="d[b'") | |
|
851 | nt.assert_in("abd", matches) | |
|
852 |
nt.assert_ |
|
|
887 | if False: # not currently implemented | |
|
888 | _, matches = complete(line_buffer="d[b") | |
|
889 | nt.assert_in("b'abd'", matches) | |
|
890 | nt.assert_not_in("b'abc'", matches) | |
|
853 | 891 | |
|
854 |
_, matches = complete(line_buffer="d[ |
|
|
855 | nt.assert_in("abd", matches) | |
|
856 | nt.assert_not_in("abc", matches) | |
|
892 | _, matches = complete(line_buffer="d[b'") | |
|
893 | nt.assert_in("abd", matches) | |
|
894 | nt.assert_not_in("abc", matches) | |
|
857 | 895 | |
|
858 | _, matches = complete(line_buffer="d['") | |
|
859 |
nt.assert_in("ab |
|
|
860 |
nt.assert_not_in("ab |
|
|
896 | _, matches = complete(line_buffer="d[B'") | |
|
897 | nt.assert_in("abd", matches) | |
|
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 | |
|
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 \\ | |
|
910 | ip.user_ns['d'] = {u'a\u05d0': None} | |
|
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 | 912 | # query using escape |
|
882 | _, matches = complete(line_buffer="d['a\\u05d0") | |
|
883 | nt.assert_in("d['a\\u05d0']", matches) # tokenized after \\ | |
|
913 | if sys.platform != 'win32': | |
|
914 | # Known failure on Windows | |
|
915 | _, matches = complete(line_buffer="d['a\\u05d0") | |
|
916 | nt.assert_in("u05d0", matches) # tokenized after \\ | |
|
884 | 917 | |
|
885 | 918 | # query using character |
|
886 | 919 | _, matches = complete(line_buffer="d['a\u05d0") |
|
887 |
nt.assert_in(u" |
|
|
920 | nt.assert_in(u"a\u05d0", matches) | |
|
888 | 921 | |
|
889 | ||
|
890 | ||
|
891 | @dec.skip_without('numpy') | |
|
892 | def test_struct_array_key_completion(): | |
|
893 | """Test dict key completion applies to numpy struct arrays""" | |
|
894 | import numpy | |
|
895 | ip = get_ipython() | |
|
896 | complete = ip.Completer.complete | |
|
897 | ip.user_ns['d'] = numpy.array([], dtype=[('hello', 'f'), ('world', 'f')]) | |
|
898 | _, matches = complete(line_buffer="d['") | |
|
899 | nt.assert_in("hello", matches) | |
|
900 | nt.assert_in("world", matches) | |
|
901 | # complete on the numpy struct itself | |
|
902 | dt = numpy.dtype([('my_head', [('my_dt', '>u4'), ('my_df', '>u4')]), | |
|
903 | ('my_data', '>f4', 5)]) | |
|
904 | x = numpy.zeros(2, dtype=dt) | |
|
905 | ip.user_ns['d'] = x[1] | |
|
906 | _, matches = complete(line_buffer="d['") | |
|
907 | nt.assert_in("my_head", matches) | |
|
908 |
nt.assert_in(" |
|
|
909 | # complete on a nested level | |
|
910 | with greedy_completion(): | |
|
911 | ip.user_ns['d'] = numpy.zeros(2, dtype=dt) | |
|
912 | _, matches = complete(line_buffer="d[1]['my_head']['") | |
|
913 | nt.assert_true(any(["my_dt" in m for m in matches])) | |
|
914 | nt.assert_true(any(["my_df" in m for m in matches])) | |
|
915 | ||
|
916 | ||
|
917 | @dec.skip_without('pandas') | |
|
918 | def test_dataframe_key_completion(): | |
|
919 | """Test dict key completion applies to pandas DataFrames""" | |
|
920 | import pandas | |
|
921 | ip = get_ipython() | |
|
922 | complete = ip.Completer.complete | |
|
923 | ip.user_ns['d'] = pandas.DataFrame({'hello': [1], 'world': [2]}) | |
|
924 | _, matches = complete(line_buffer="d['") | |
|
925 | nt.assert_in("hello", matches) | |
|
926 | nt.assert_in("world", matches) | |
|
927 | ||
|
928 | ||
|
929 | def test_dict_key_completion_invalids(): | |
|
930 | """Smoke test cases dict key completion can't handle""" | |
|
931 | ip = get_ipython() | |
|
932 | complete = ip.Completer.complete | |
|
933 | ||
|
934 | ip.user_ns['no_getitem'] = None | |
|
935 | ip.user_ns['no_keys'] = [] | |
|
936 | ip.user_ns['cant_call_keys'] = dict | |
|
937 | ip.user_ns['empty'] = {} | |
|
938 | ip.user_ns['d'] = {'abc': 5} | |
|
939 | ||
|
940 | _, matches = complete(line_buffer="no_getitem['") | |
|
941 | _, matches = complete(line_buffer="no_keys['") | |
|
942 | _, matches = complete(line_buffer="cant_call_keys['") | |
|
943 | _, matches = complete(line_buffer="empty['") | |
|
944 | _, matches = complete(line_buffer="name_error['") | |
|
945 | _, matches = complete(line_buffer="d['\\") # incomplete escape | |
|
946 | ||
|
947 | class KeyCompletable(object): | |
|
948 | def __init__(self, things=()): | |
|
949 | self.things = things | |
|
950 | ||
|
951 | def _ipython_key_completions_(self): | |
|
952 | return list(self.things) | |
|
953 | ||
|
954 | def test_object_key_completion(): | |
|
955 | ip = get_ipython() | |
|
956 | ip.user_ns['key_completable'] = KeyCompletable(['qwerty', 'qwick']) | |
|
957 | ||
|
958 | _, matches = ip.Completer.complete(line_buffer="key_completable['qw") | |
|
959 | nt.assert_in('qwerty', matches) | |
|
960 | nt.assert_in('qwick', matches) | |
|
961 | ||
|
962 | ||
|
963 | class NamedInstanceMetaclass(type): | |
|
964 | def __getitem__(cls, item): | |
|
965 | return cls.get_instance(item) | |
|
966 | ||
|
967 | class NamedInstanceClass(object, metaclass=NamedInstanceMetaclass): | |
|
968 | def __init__(self, name): | |
|
969 | if not hasattr(self.__class__, 'instances'): | |
|
970 | self.__class__.instances = {} | |
|
971 | self.__class__.instances[name] = self | |
|
972 | ||
|
973 | @classmethod | |
|
974 | def _ipython_key_completions_(cls): | |
|
975 | return cls.instances.keys() | |
|
976 | ||
|
977 | @classmethod | |
|
978 | def get_instance(cls, name): | |
|
979 | return cls.instances[name] | |
|
980 | ||
|
981 | def test_class_key_completion(): | |
|
982 | ip = get_ipython() | |
|
983 | NamedInstanceClass('qwerty') | |
|
984 | NamedInstanceClass('qwick') | |
|
985 | ip.user_ns['named_instance_class'] = NamedInstanceClass | |
|
986 | ||
|
987 | _, matches = ip.Completer.complete(line_buffer="named_instance_class['qw") | |
|
988 |
nt.assert_in(' |
|
|
989 | nt.assert_in('qwick', matches) | |
|
990 | ||
|
991 | def test_tryimport(): | |
|
992 | """ | |
|
993 | Test that try-import don't crash on trailing dot, and import modules before | |
|
994 | """ | |
|
995 | from IPython.core.completerlib import try_import | |
|
996 | assert(try_import("IPython.")) | |
|
997 | ||
|
998 | ||
|
999 | def test_aimport_module_completer(): | |
|
1000 | ip = get_ipython() | |
|
1001 | _, matches = ip.complete('i', '%aimport i') | |
|
1002 | nt.assert_in('io', matches) | |
|
1003 | nt.assert_not_in('int', matches) | |
|
1004 | ||
|
1005 | def test_nested_import_module_completer(): | |
|
1006 | ip = get_ipython() | |
|
1007 | _, matches = ip.complete(None, 'import IPython.co', 17) | |
|
1008 | nt.assert_in('IPython.core', matches) | |
|
1009 | nt.assert_not_in('import IPython.core', matches) | |
|
1010 | nt.assert_not_in('IPython.display', matches) | |
|
1011 | ||
|
1012 | def test_import_module_completer(): | |
|
1013 | ip = get_ipython() | |
|
1014 |
_, matches = ip.complete( |
|
|
1015 |
nt.assert_in(' |
|
|
1016 |
nt.assert_ |
|
|
1017 | ||
|
1018 | def test_from_module_completer(): | |
|
1019 | ip = get_ipython() | |
|
1020 | _, matches = ip.complete('B', 'from io import B', 16) | |
|
1021 | nt.assert_in('BytesIO', matches) | |
|
1022 | nt.assert_not_in('BaseException', matches) | |
|
1023 | ||
|
1024 | def test_snake_case_completion(): | |
|
1025 | ip = get_ipython() | |
|
1026 | ip.Completer.use_jedi = False | |
|
1027 | ip.user_ns['some_three'] = 3 | |
|
1028 | ip.user_ns['some_four'] = 4 | |
|
1029 | _, matches = ip.complete("s_", "print(s_f") | |
|
1030 | nt.assert_in('some_three', matches) | |
|
1031 | nt.assert_in('some_four', matches) | |
|
1032 | ||
|
1033 | def test_mix_terms(): | |
|
1034 | ip = get_ipython() | |
|
1035 | from textwrap import dedent | |
|
1036 | ip.Completer.use_jedi = False | |
|
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) | |
|
922 | with greedy_completion(): | |
|
923 | # query using escape | |
|
924 | _, matches = complete(line_buffer="d['a\\u05d0") | |
|
925 | nt.assert_in("d['a\\u05d0']", matches) # tokenized after \\ | |
|
926 | ||
|
927 | # query using character | |
|
928 | _, matches = complete(line_buffer="d['a\u05d0") | |
|
929 | nt.assert_in(u"d['a\u05d0']", matches) | |
|
930 | ||
|
931 | ||
|
932 | ||
|
933 | @dec.skip_without('numpy') | |
|
934 | def test_struct_array_key_completion(self): | |
|
935 | """Test dict key completion applies to numpy struct arrays""" | |
|
936 | import numpy | |
|
937 | ip = get_ipython() | |
|
938 | complete = ip.Completer.complete | |
|
939 | ip.user_ns['d'] = numpy.array([], dtype=[('hello', 'f'), ('world', 'f')]) | |
|
940 | _, matches = complete(line_buffer="d['") | |
|
941 | nt.assert_in("hello", matches) | |
|
942 | nt.assert_in("world", matches) | |
|
943 | # complete on the numpy struct itself | |
|
944 | dt = numpy.dtype([('my_head', [('my_dt', '>u4'), ('my_df', '>u4')]), | |
|
945 | ('my_data', '>f4', 5)]) | |
|
946 | x = numpy.zeros(2, dtype=dt) | |
|
947 | ip.user_ns['d'] = x[1] | |
|
948 | _, matches = complete(line_buffer="d['") | |
|
949 | nt.assert_in("my_head", matches) | |
|
950 | nt.assert_in("my_data", matches) | |
|
951 | # complete on a nested level | |
|
952 | with greedy_completion(): | |
|
953 | ip.user_ns['d'] = numpy.zeros(2, dtype=dt) | |
|
954 | _, matches = complete(line_buffer="d[1]['my_head']['") | |
|
955 | nt.assert_true(any(["my_dt" in m for m in matches])) | |
|
956 | nt.assert_true(any(["my_df" in m for m in matches])) | |
|
957 | ||
|
958 | ||
|
959 | @dec.skip_without('pandas') | |
|
960 | def test_dataframe_key_completion(self): | |
|
961 | """Test dict key completion applies to pandas DataFrames""" | |
|
962 | import pandas | |
|
963 | ip = get_ipython() | |
|
964 | complete = ip.Completer.complete | |
|
965 | ip.user_ns['d'] = pandas.DataFrame({'hello': [1], 'world': [2]}) | |
|
966 | _, matches = complete(line_buffer="d['") | |
|
967 | nt.assert_in("hello", matches) | |
|
968 | nt.assert_in("world", matches) | |
|
969 | ||
|
970 | ||
|
971 | def test_dict_key_completion_invalids(self): | |
|
972 | """Smoke test cases dict key completion can't handle""" | |
|
973 | ip = get_ipython() | |
|
974 | complete = ip.Completer.complete | |
|
975 | ||
|
976 | ip.user_ns['no_getitem'] = None | |
|
977 | ip.user_ns['no_keys'] = [] | |
|
978 | ip.user_ns['cant_call_keys'] = dict | |
|
979 | ip.user_ns['empty'] = {} | |
|
980 | ip.user_ns['d'] = {'abc': 5} | |
|
981 | ||
|
982 | _, matches = complete(line_buffer="no_getitem['") | |
|
983 | _, matches = complete(line_buffer="no_keys['") | |
|
984 | _, matches = complete(line_buffer="cant_call_keys['") | |
|
985 | _, matches = complete(line_buffer="empty['") | |
|
986 | _, matches = complete(line_buffer="name_error['") | |
|
987 | _, matches = complete(line_buffer="d['\\") # incomplete escape | |
|
988 | ||
|
989 | def test_object_key_completion(self): | |
|
990 | ip = get_ipython() | |
|
991 | ip.user_ns['key_completable'] = KeyCompletable(['qwerty', 'qwick']) | |
|
992 | ||
|
993 | _, matches = ip.Completer.complete(line_buffer="key_completable['qw") | |
|
994 | nt.assert_in('qwerty', matches) | |
|
995 | nt.assert_in('qwick', matches) | |
|
996 | ||
|
997 | ||
|
998 | ||
|
999 | def test_class_key_completion(self): | |
|
1000 | ip = get_ipython() | |
|
1001 | NamedInstanceClass('qwerty') | |
|
1002 | NamedInstanceClass('qwick') | |
|
1003 | ip.user_ns['named_instance_class'] = NamedInstanceClass | |
|
1004 | ||
|
1005 | _, matches = ip.Completer.complete(line_buffer="named_instance_class['qw") | |
|
1006 | nt.assert_in('qwerty', matches) | |
|
1007 | nt.assert_in('qwick', matches) | |
|
1008 | ||
|
1009 | def test_tryimport(self): | |
|
1010 | """ | |
|
1011 | Test that try-import don't crash on trailing dot, and import modules before | |
|
1012 | """ | |
|
1013 | from IPython.core.completerlib import try_import | |
|
1014 | assert(try_import("IPython.")) | |
|
1015 | ||
|
1016 | ||
|
1017 | def test_aimport_module_completer(self): | |
|
1018 | ip = get_ipython() | |
|
1019 | _, matches = ip.complete('i', '%aimport i') | |
|
1020 | nt.assert_in('io', matches) | |
|
1021 | nt.assert_not_in('int', matches) | |
|
1022 | ||
|
1023 | def test_nested_import_module_completer(self): | |
|
1024 | ip = get_ipython() | |
|
1025 | _, matches = ip.complete(None, 'import IPython.co', 17) | |
|
1026 | nt.assert_in('IPython.core', matches) | |
|
1027 | nt.assert_not_in('import IPython.core', matches) | |
|
1028 | nt.assert_not_in('IPython.display', matches) | |
|
1029 | ||
|
1030 | def test_import_module_completer(self): | |
|
1031 | ip = get_ipython() | |
|
1032 | _, matches = ip.complete('i', 'import i') | |
|
1033 | nt.assert_in('io', matches) | |
|
1034 | nt.assert_not_in('int', matches) | |
|
1035 | ||
|
1036 | def test_from_module_completer(self): | |
|
1037 | ip = get_ipython() | |
|
1038 | _, matches = ip.complete('B', 'from io import B', 16) | |
|
1039 | nt.assert_in('BytesIO', matches) | |
|
1040 | nt.assert_not_in('BaseException', matches) | |
|
1041 | ||
|
1042 | def test_snake_case_completion(self): | |
|
1043 | ip = get_ipython() | |
|
1044 | ip.Completer.use_jedi = False | |
|
1045 | ip.user_ns['some_three'] = 3 | |
|
1046 | ip.user_ns['some_four'] = 4 | |
|
1047 | _, matches = ip.complete("s_", "print(s_f") | |
|
1048 | nt.assert_in('some_three', matches) | |
|
1049 | nt.assert_in('some_four', matches) | |
|
1050 | ||
|
1051 | def test_mix_terms(self): | |
|
1052 | ip = get_ipython() | |
|
1053 | from textwrap import dedent | |
|
1054 | ip.Completer.use_jedi = False | |
|
1055 | ip.ex(dedent(""" | |
|
1056 | class Test: | |
|
1057 | def meth(self, meth_arg1): | |
|
1058 | print("meth") | |
|
1059 | ||
|
1060 | def meth_1(self, meth1_arg1, meth1_arg2): | |
|
1061 | print("meth1") | |
|
1062 | ||
|
1063 | def meth_2(self, meth2_arg1, meth2_arg2): | |
|
1064 | print("meth2") | |
|
1065 | test = Test() | |
|
1066 | """)) | |
|
1067 | _, matches = ip.complete(None, "test.meth(") | |
|
1068 | nt.assert_in('meth_arg1=', matches) | |
|
1069 | nt.assert_not_in('meth2_arg1=', matches) |
General Comments 0
You need to be logged in to leave comments.
Login now