test_completer.py
948 lines
| 31.0 KiB
| text/x-python
|
PythonLexer
MinRK
|
r16564 | # encoding: utf-8 | |
"""Tests for the IPython tab-completion machinery.""" | |||
# Copyright (c) IPython Development Team. | |||
# Distributed under the terms of the Modified BSD License. | |||
Fernando Perez
|
r2365 | ||
Fernando Perez
|
r3184 | import os | |
Fernando Perez
|
r2365 | import sys | |
Matthias Bussonnier
|
r23358 | import textwrap | |
Fernando Perez
|
r2855 | import unittest | |
Fernando Perez
|
r2365 | ||
MinRK
|
r16564 | from contextlib import contextmanager | |
Fernando Perez
|
r2365 | import nose.tools as nt | |
Min RK
|
r21253 | from traitlets.config.loader import Config | |
Min RK
|
r21718 | from IPython import get_ipython | |
Fernando Perez
|
r2365 | from IPython.core import completer | |
Min RK
|
r4105 | from IPython.external.decorators import knownfailureif | |
Thomas Kluyver
|
r16767 | from IPython.utils.tempdir import TemporaryDirectory, TemporaryWorkingDirectory | |
Thomas Kluyver
|
r5155 | from IPython.utils.generics import complete_object | |
Joel Nothman
|
r15766 | from IPython.testing import decorators as dec | |
Fernando Perez
|
r2365 | ||
Matthias Bussonnier
|
r23358 | from IPython.core.completer import ( | |
Completion, provisionalcompleter, match_dict_keys, _deduplicate_completions) | |||
Matthias Bussonnier
|
r23284 | from nose.tools import assert_in, assert_not_in | |
Fernando Perez
|
r2365 | #----------------------------------------------------------------------------- | |
# Test functions | |||
#----------------------------------------------------------------------------- | |||
MinRK
|
r16564 | ||
@contextmanager | |||
def greedy_completion(): | |||
ip = get_ipython() | |||
greedy_original = ip.Completer.greedy | |||
try: | |||
ip.Completer.greedy = True | |||
yield | |||
finally: | |||
ip.Completer.greedy = greedy_original | |||
Fernando Perez
|
r2365 | def test_protect_filename(): | |
Antony Lee
|
r22418 | if sys.platform == 'win32': | |
Antony Lee
|
r22566 | pairs = [('abc','abc'), | |
(' abc','" abc"'), | |||
('a bc','"a bc"'), | |||
('a bc','"a bc"'), | |||
(' bc','" bc"'), | |||
] | |||
Antony Lee
|
r22418 | else: | |
Antony Lee
|
r22566 | pairs = [('abc','abc'), | |
(' abc',r'\ abc'), | |||
('a bc',r'a\ bc'), | |||
('a bc',r'a\ \ bc'), | |||
(' bc',r'\ \ bc'), | |||
# On posix, we also protect parens and other special characters. | |||
('a(bc',r'a\(bc'), | |||
('a)bc',r'a\)bc'), | |||
('a( )bc',r'a\(\ \)bc'), | |||
('a[1]bc', r'a\[1\]bc'), | |||
('a{1}bc', r'a\{1\}bc'), | |||
('a#bc', r'a\#bc'), | |||
('a?bc', r'a\?bc'), | |||
('a=bc', r'a\=bc'), | |||
('a\\bc', r'a\\bc'), | |||
('a|bc', r'a\|bc'), | |||
('a;bc', r'a\;bc'), | |||
('a:bc', r'a\:bc'), | |||
("a'bc", r"a\'bc"), | |||
('a*bc', r'a\*bc'), | |||
('a"bc', r'a\"bc'), | |||
('a^bc', r'a\^bc'), | |||
('a&bc', r'a\&bc'), | |||
] | |||
Fernando Perez
|
r2365 | # run the actual tests | |
for s1, s2 in pairs: | |||
s1p = completer.protect_filename(s1) | |||
Bradley M. Froehle
|
r7875 | nt.assert_equal(s1p, s2) | |
Fernando Perez
|
r2855 | ||
def check_line_split(splitter, test_specs): | |||
for part1, part2, split in test_specs: | |||
cursor_pos = len(part1) | |||
line = part1+part2 | |||
out = splitter.split_line(line, cursor_pos) | |||
nt.assert_equal(out, split) | |||
def test_line_split(): | |||
Thomas Kluyver
|
r13375 | """Basic line splitter test with default specs.""" | |
Fernando Perez
|
r2855 | sp = completer.CompletionSplitter() | |
# The format of the test specs is: part1, part2, expected answer. Parts 1 | |||
# and 2 are joined into the 'line' sent to the splitter, as if the cursor | |||
# was at the end of part1. So an empty part2 represents someone hitting | |||
# tab at the end of the line, the most common case. | |||
t = [('run some/scrip', '', 'some/scrip'), | |||
('run scripts/er', 'ror.py foo', 'scripts/er'), | |||
('echo $HOM', '', 'HOM'), | |||
('print sys.pa', '', 'sys.pa'), | |||
('print(sys.pa', '', 'sys.pa'), | |||
("execfile('scripts/er", '', 'scripts/er'), | |||
('a[x.', '', 'x.'), | |||
('a[x.', 'y', 'x.'), | |||
('cd "some_file/', '', 'some_file/'), | |||
] | |||
check_line_split(sp, t) | |||
Fernando Perez
|
r3074 | # Ensure splitting works OK with unicode by re-running the tests with | |
# all inputs turned into unicode | |||
Srinivas Reddy Thatiparthy
|
r23044 | check_line_split(sp, [ map(str, p) for p in t] ) | |
Fernando Perez
|
r3074 | ||
Piti Ongmongkolkul
|
r6511 | ||
Thomas Kluyver
|
r5155 | def test_custom_completion_error(): | |
"""Test that errors from custom attribute completers are silenced.""" | |||
ip = get_ipython() | |||
class A(object): pass | |||
ip.user_ns['a'] = A() | |||
@complete_object.when_type(A) | |||
def complete_A(a, existing_completions): | |||
raise TypeError("this should be silenced") | |||
ip.complete("a.") | |||
Fernando Perez
|
r3074 | ||
def test_unicode_completions(): | |||
ip = get_ipython() | |||
# Some strings that trigger different types of completion. Check them both | |||
# in str and unicode forms | |||
s = ['ru', '%ru', 'cd /', 'floa', 'float(x)/'] | |||
Srinivas Reddy Thatiparthy
|
r23044 | for t in s + list(map(str, s)): | |
Fernando Perez
|
r3074 | # We don't need to check exact completion values (they may change | |
# depending on the state of the namespace, but at least no exceptions | |||
# should be thrown and the return value should be a pair of text, list | |||
# values. | |||
text, matches = ip.complete(t) | |||
Srinivas Reddy Thatiparthy
|
r23037 | nt.assert_true(isinstance(text, str)) | |
Fernando Perez
|
r3074 | nt.assert_true(isinstance(matches, list)) | |
Fernando Perez
|
r2855 | ||
Brian E. Granger
|
r17742 | def test_latex_completions(): | |
from IPython.core.latex_symbols import latex_symbols | |||
import random | |||
ip = get_ipython() | |||
# Test some random unicode symbols | |||
keys = random.sample(latex_symbols.keys(), 10) | |||
for k in keys: | |||
text, matches = ip.complete(k) | |||
nt.assert_equal(len(matches),1) | |||
nt.assert_equal(text, k) | |||
Thomas Kluyver
|
r17806 | nt.assert_equal(matches[0], latex_symbols[k]) | |
Brian E. Granger
|
r17742 | # Test a more complex line | |
text, matches = ip.complete(u'print(\\alpha') | |||
Srinivas Reddy Thatiparthy
|
r23050 | nt.assert_equal(text, u'\\alpha') | |
nt.assert_equal(matches[0], latex_symbols['\\alpha']) | |||
Brian E. Granger
|
r17742 | # Test multiple matching latex symbols | |
text, matches = ip.complete(u'\\al') | |||
nt.assert_in('\\alpha', matches) | |||
nt.assert_in('\\aleph', matches) | |||
Fernando Perez
|
r2855 | ||
Matthias Bussonnier
|
r21103 | ||
def test_back_latex_completion(): | |||
ip = get_ipython() | |||
# do not return more than 1 matches fro \beta, only the latex one. | |||
name, matches = ip.complete('\\β') | |||
nt.assert_equal(len(matches), 1) | |||
nt.assert_equal(matches[0], '\\beta') | |||
def test_back_unicode_completion(): | |||
ip = get_ipython() | |||
name, matches = ip.complete('\\â…¤') | |||
nt.assert_equal(len(matches), 1) | |||
nt.assert_equal(matches[0], '\\ROMAN NUMERAL FIVE') | |||
def test_forward_unicode_completion(): | |||
ip = get_ipython() | |||
name, matches = ip.complete('\\ROMAN NUMERAL FIVE') | |||
nt.assert_equal(len(matches), 1) | |||
nt.assert_equal(matches[0], 'â…¤') | |||
Thomas Kluyver
|
r22761 | @dec.knownfailureif(sys.platform == 'win32', 'Fails if there is a C:\\j... path') | |
Matthias Bussonnier
|
r21103 | def test_no_ascii_back_completion(): | |
ip = get_ipython() | |||
Thomas Kluyver
|
r21275 | with TemporaryWorkingDirectory(): # Avoid any filename completions | |
# single ascii letter that don't have yet completions | |||
Kelly Liu
|
r22292 | for letter in 'jJ' : | |
Thomas Kluyver
|
r21275 | name, matches = ip.complete('\\'+letter) | |
nt.assert_equal(matches, []) | |||
Matthias Bussonnier
|
r21103 | ||
Fernando Perez
|
r2855 | class CompletionSplitterTestCase(unittest.TestCase): | |
def setUp(self): | |||
self.sp = completer.CompletionSplitter() | |||
def test_delim_setting(self): | |||
Fernando Perez
|
r6945 | self.sp.delims = ' ' | |
nt.assert_equal(self.sp.delims, ' ') | |||
Fernando Perez
|
r2857 | nt.assert_equal(self.sp._delim_expr, '[\ ]') | |
Fernando Perez
|
r2855 | ||
def test_spaces(self): | |||
"""Test with only spaces as split chars.""" | |||
self.sp.delims = ' ' | |||
t = [('foo', '', 'foo'), | |||
('run foo', '', 'foo'), | |||
('run foo', 'bar', 'foo'), | |||
] | |||
check_line_split(self.sp, t) | |||
Fernando Perez
|
r3184 | ||
def test_has_open_quotes1(): | |||
for s in ["'", "'''", "'hi' '"]: | |||
nt.assert_equal(completer.has_open_quotes(s), "'") | |||
def test_has_open_quotes2(): | |||
for s in ['"', '"""', '"hi" "']: | |||
nt.assert_equal(completer.has_open_quotes(s), '"') | |||
def test_has_open_quotes3(): | |||
for s in ["''", "''' '''", "'hi' 'ipython'"]: | |||
nt.assert_false(completer.has_open_quotes(s)) | |||
def test_has_open_quotes4(): | |||
for s in ['""', '""" """', '"hi" "ipython"']: | |||
nt.assert_false(completer.has_open_quotes(s)) | |||
Piti Ongmongkolkul
|
r6511 | ||
Min RK
|
r4105 | @knownfailureif(sys.platform == 'win32', "abspath completions fail on Windows") | |
def test_abspath_file_completions(): | |||
Fernando Perez
|
r3184 | ip = get_ipython() | |
with TemporaryDirectory() as tmpdir: | |||
prefix = os.path.join(tmpdir, 'foo') | |||
Thomas Kluyver
|
r13375 | suffixes = ['1', '2'] | |
Fernando Perez
|
r3184 | names = [prefix+s for s in suffixes] | |
for n in names: | |||
open(n, 'w').close() | |||
# Check simple completion | |||
c = ip.complete(prefix)[1] | |||
nt.assert_equal(c, names) | |||
# Now check with a function call | |||
cmd = 'a = f("%s' % prefix | |||
c = ip.complete(prefix, cmd)[1] | |||
comp = [prefix+s for s in suffixes] | |||
nt.assert_equal(c, comp) | |||
Min RK
|
r4105 | ||
Piti Ongmongkolkul
|
r6511 | ||
Min RK
|
r4105 | def test_local_file_completions(): | |
ip = get_ipython() | |||
Thomas Kluyver
|
r16767 | with TemporaryWorkingDirectory(): | |
prefix = './foo' | |||
suffixes = ['1', '2'] | |||
names = [prefix+s for s in suffixes] | |||
for n in names: | |||
open(n, 'w').close() | |||
# Check simple completion | |||
c = ip.complete(prefix)[1] | |||
nt.assert_equal(c, names) | |||
# Now check with a function call | |||
cmd = 'a = f("%s' % prefix | |||
c = ip.complete(prefix, cmd)[1] | |||
Kelly Liu
|
r22292 | comp = set(prefix+s for s in suffixes) | |
nt.assert_true(comp.issubset(set(c))) | |||
MinRK
|
r4825 | ||
Piti Ongmongkolkul
|
r6511 | ||
Christopher C. Aycock
|
r23613 | def test_quoted_file_completions(): | |
ip = get_ipython() | |||
with TemporaryWorkingDirectory(): | |||
name = "foo'bar" | |||
open(name, 'w').close() | |||
# Don't escape Windows | |||
escaped = name if sys.platform == "win32" else "foo\\'bar" | |||
# Single quote matches embedded single quote | |||
text = "open('foo" | |||
c = ip.Completer._complete(cursor_line=0, | |||
cursor_pos=len(text), | |||
full_text=text)[1] | |||
nt.assert_equal(c, [escaped]) | |||
# Double quote requires no escape | |||
text = 'open("foo' | |||
c = ip.Completer._complete(cursor_line=0, | |||
cursor_pos=len(text), | |||
full_text=text)[1] | |||
nt.assert_equal(c, [name]) | |||
# No quote requires an escape | |||
text = '%ls foo' | |||
c = ip.Completer._complete(cursor_line=0, | |||
cursor_pos=len(text), | |||
full_text=text)[1] | |||
nt.assert_equal(c, [escaped]) | |||
Matthias Bussonnier
|
r23284 | def test_jedi(): | |
""" | |||
A couple of issue we had with Jedi | |||
""" | |||
ip = get_ipython() | |||
def _test_complete(reason, s, comp, start=None, end=None): | |||
l = len(s) | |||
start = start if start is not None else l | |||
end = end if end is not None else l | |||
with provisionalcompleter(): | |||
completions = set(ip.Completer.completions(s, l)) | |||
assert_in(Completion(start, end, comp), completions, reason) | |||
def _test_not_complete(reason, s, comp): | |||
l = len(s) | |||
with provisionalcompleter(): | |||
completions = set(ip.Completer.completions(s, l)) | |||
assert_not_in(Completion(l, l, comp), completions, reason) | |||
Matthias Bussonnier
|
r23285 | import jedi | |
jedi_version = tuple(int(i) for i in jedi.__version__.split('.')[:3]) | |||
Matthias Bussonnier
|
r23358 | if jedi_version > (0, 10): | |
Matthias Bussonnier
|
r23285 | yield _test_complete, 'jedi >0.9 should complete and not crash', 'a=1;a.', 'real' | |
Matthias Bussonnier
|
r23284 | yield _test_complete, 'can infer first argument', 'a=(1,"foo");a[0].', 'real' | |
yield _test_complete, 'can infer second argument', 'a=(1,"foo");a[1].', 'capitalize' | |||
yield _test_complete, 'cover duplicate completions', 'im', 'import', 0, 2 | |||
yield _test_not_complete, 'does not mix types', 'a=(1,"foo");a[0].', 'capitalize' | |||
Matthias Bussonnier
|
r23358 | def test_deduplicate_completions(): | |
""" | |||
Test that completions are correctly deduplicated (even if ranges are not the same) | |||
""" | |||
ip = get_ipython() | |||
ip.ex(textwrap.dedent(''' | |||
class Z: | |||
zoo = 1 | |||
''')) | |||
with provisionalcompleter(): | |||
l = list(_deduplicate_completions('Z.z', ip.Completer.completions('Z.z', 3))) | |||
assert len(l) == 1, 'Completions (Z.z<tab>) correctly deduplicate: %s ' % l | |||
assert l[0].text == 'zoo' # and not `it.accumulate` | |||
Matthias Bussonnier
|
r23284 | ||
MinRK
|
r4825 | def test_greedy_completions(): | |
Matthias Bussonnier
|
r23284 | """ | |
Test the capability of the Greedy completer. | |||
Most of the test here do not really show off the greedy completer, for proof | |||
each of the text bellow now pass with Jedi. The greedy completer is capable of more. | |||
See the :any:`test_dict_key_completion_contexts` | |||
""" | |||
MinRK
|
r4825 | ip = get_ipython() | |
MinRK
|
r16564 | ip.ex('a=list(range(5))') | |
_,c = ip.complete('.',line='a[0].') | |||
Kelly Liu
|
r22292 | nt.assert_false('.real' in c, | |
MinRK
|
r16564 | "Shouldn't have completed on a[0]: %s"%c) | |
Matthias Bussonnier
|
r23284 | with greedy_completion(), provisionalcompleter(): | |
def _(line, cursor_pos, expect, message, completion): | |||
Kelly Liu
|
r22292 | _,c = ip.complete('.', line=line, cursor_pos=cursor_pos) | |
Matthias Bussonnier
|
r23284 | with provisionalcompleter(): | |
completions = ip.Completer.completions(line, cursor_pos) | |||
Kelly Liu
|
r22292 | nt.assert_in(expect, c, message%c) | |
Matthias Bussonnier
|
r23284 | nt.assert_in(completion, completions) | |
Kelly Liu
|
r22292 | ||
Matthias Bussonnier
|
r23284 | yield _, 'a[0].', 5, 'a[0].real', "Should have completed on a[0].: %s", Completion(5,5, 'real') | |
yield _, 'a[0].r', 6, 'a[0].real', "Should have completed on a[0].r: %s", Completion(5,6, 'real') | |||
Kelly Liu
|
r22292 | ||
Matthias Bussonnier
|
r23284 | if sys.version_info > (3, 4): | |
yield _, 'a[0].from_', 10, 'a[0].from_bytes', "Should have completed on a[0].from_: %s", Completion(5, 10, 'from_bytes') | |||
MinRK
|
r4825 | ||
Piti Ongmongkolkul
|
r6511 | ||
MinRK
|
r5231 | def test_omit__names(): | |
# also happens to test IPCompleter as a configurable | |||
ip = get_ipython() | |||
ip._hidden_attr = 1 | |||
Paul Ivanov
|
r17734 | ip._x = {} | |
MinRK
|
r5231 | c = ip.Completer | |
ip.ex('ip=get_ipython()') | |||
cfg = Config() | |||
cfg.IPCompleter.omit__names = 0 | |||
c.update_config(cfg) | |||
Matthias Bussonnier
|
r23284 | with provisionalcompleter(): | |
s,matches = c.complete('ip.') | |||
completions = set(c.completions('ip.', 3)) | |||
nt.assert_in('ip.__str__', matches) | |||
nt.assert_in(Completion(3, 3, '__str__'), completions) | |||
nt.assert_in('ip._hidden_attr', matches) | |||
nt.assert_in(Completion(3,3, "_hidden_attr"), completions) | |||
Min RK
|
r21509 | cfg = Config() | |
MinRK
|
r5231 | cfg.IPCompleter.omit__names = 1 | |
c.update_config(cfg) | |||
Matthias Bussonnier
|
r23284 | with provisionalcompleter(): | |
s,matches = c.complete('ip.') | |||
completions = set(c.completions('ip.', 3)) | |||
nt.assert_not_in('ip.__str__', matches) | |||
nt.assert_not_in(Completion(3,3,'__str__'), completions) | |||
# nt.assert_in('ip._hidden_attr', matches) | |||
nt.assert_in(Completion(3,3, "_hidden_attr"), completions) | |||
Min RK
|
r21509 | cfg = Config() | |
MinRK
|
r5231 | cfg.IPCompleter.omit__names = 2 | |
c.update_config(cfg) | |||
Matthias Bussonnier
|
r23284 | with provisionalcompleter(): | |
s,matches = c.complete('ip.') | |||
completions = set(c.completions('ip.', 3)) | |||
nt.assert_not_in('ip.__str__', matches) | |||
nt.assert_not_in(Completion(3,3,'__str__'), completions) | |||
nt.assert_not_in('ip._hidden_attr', matches) | |||
nt.assert_not_in(Completion(3,3, "_hidden_attr"), completions) | |||
with provisionalcompleter(): | |||
s,matches = c.complete('ip._x.') | |||
completions = set(c.completions('ip._x.', 6)) | |||
nt.assert_in('ip._x.keys', matches) | |||
nt.assert_in(Completion(6,6, "keys"), completions) | |||
MinRK
|
r5231 | del ip._hidden_attr | |
Matthias Bussonnier
|
r23284 | del ip._x | |
Tim Couper
|
r6308 | ||
Tim Couper
|
r6312 | ||
def test_limit_to__all__False_ok(): | |||
Matthias Bussonnier
|
r23284 | """ | |
Limit to all is deprecated, once we remove it this test can go away. | |||
""" | |||
Tim Couper
|
r6312 | ip = get_ipython() | |
c = ip.Completer | |||
ip.ex('class D: x=24') | |||
ip.ex('d=D()') | |||
cfg = Config() | |||
cfg.IPCompleter.limit_to__all__ = False | |||
c.update_config(cfg) | |||
s, matches = c.complete('d.') | |||
Thomas Kluyver
|
r13375 | nt.assert_in('d.x', matches) | |
Tim Couper
|
r6312 | ||
Piti Ongmongkolkul
|
r6511 | ||
Tim Couper
|
r6308 | def test_get__all__entries_ok(): | |
Piti Ongmongkolkul
|
r6511 | class A(object): | |
__all__ = ['x', 1] | |||
words = completer.get__all__entries(A()) | |||
nt.assert_equal(words, ['x']) | |||
Tim Couper
|
r6308 | ||
def test_get__all__entries_no__all__ok(): | |||
Piti Ongmongkolkul
|
r6511 | class A(object): | |
pass | |||
words = completer.get__all__entries(A()) | |||
nt.assert_equal(words, []) | |||
def test_func_kw_completions(): | |||
ip = get_ipython() | |||
c = ip.Completer | |||
ip.ex('def myfunc(a=1,b=2): return a+b') | |||
Fernando Perez
|
r6945 | s, matches = c.complete(None, 'myfunc(1,b') | |
nt.assert_in('b=', matches) | |||
# Simulate completing with cursor right after b (pos==10): | |||
Piti Ongmongkolkul
|
r9165 | s, matches = c.complete(None, 'myfunc(1,b)', 10) | |
Fernando Perez
|
r6945 | nt.assert_in('b=', matches) | |
Piti Ongmongkolkul
|
r9165 | s, matches = c.complete(None, 'myfunc(a="escaped\\")string",b') | |
Jez Ng
|
r7507 | nt.assert_in('b=', matches) | |
Piti Ongmongkolkul
|
r9165 | #builtin function | |
s, matches = c.complete(None, 'min(k, k') | |||
nt.assert_in('key=', matches) | |||
Fernando Perez
|
r6991 | ||
Piti Ongmongkolkul
|
r9167 | def test_default_arguments_from_docstring(): | |
ip = get_ipython() | |||
c = ip.Completer | |||
kwd = c._default_arguments_from_docstring( | |||
'min(iterable[, key=func]) -> value') | |||
Piti Ongmongkolkul
|
r9170 | nt.assert_equal(kwd, ['key']) | |
Piti Ongmongkolkul
|
r9167 | #with cython type etc | |
kwd = c._default_arguments_from_docstring( | |||
'Minuit.migrad(self, int ncall=10000, resume=True, int nsplit=1)\n') | |||
Piti Ongmongkolkul
|
r9170 | nt.assert_equal(kwd, ['ncall', 'resume', 'nsplit']) | |
Piti Ongmongkolkul
|
r9167 | #white spaces | |
kwd = c._default_arguments_from_docstring( | |||
'\n Minuit.migrad(self, int ncall=10000, resume=True, int nsplit=1)\n') | |||
Piti Ongmongkolkul
|
r9170 | nt.assert_equal(kwd, ['ncall', 'resume', 'nsplit']) | |
Piti Ongmongkolkul
|
r9167 | ||
Fernando Perez
|
r6991 | def test_line_magics(): | |
ip = get_ipython() | |||
c = ip.Completer | |||
s, matches = c.complete(None, 'lsmag') | |||
nt.assert_in('%lsmagic', matches) | |||
s, matches = c.complete(None, '%lsmag') | |||
nt.assert_in('%lsmagic', matches) | |||
def test_cell_magics(): | |||
from IPython.core.magic import register_cell_magic | |||
@register_cell_magic | |||
def _foo_cellm(line, cell): | |||
pass | |||
ip = get_ipython() | |||
c = ip.Completer | |||
s, matches = c.complete(None, '_foo_ce') | |||
nt.assert_in('%%_foo_cellm', matches) | |||
s, matches = c.complete(None, '%%_foo_ce') | |||
nt.assert_in('%%_foo_cellm', matches) | |||
def test_line_cell_magics(): | |||
from IPython.core.magic import register_line_cell_magic | |||
@register_line_cell_magic | |||
def _bar_cellm(line, cell): | |||
pass | |||
ip = get_ipython() | |||
c = ip.Completer | |||
# The policy here is trickier, see comments in completion code. The | |||
# returned values depend on whether the user passes %% or not explicitly, | |||
# and this will show a difference if the same name is both a line and cell | |||
# magic. | |||
s, matches = c.complete(None, '_bar_ce') | |||
nt.assert_in('%_bar_cellm', matches) | |||
nt.assert_in('%%_bar_cellm', matches) | |||
s, matches = c.complete(None, '%_bar_ce') | |||
nt.assert_in('%_bar_cellm', matches) | |||
nt.assert_in('%%_bar_cellm', matches) | |||
s, matches = c.complete(None, '%%_bar_ce') | |||
nt.assert_not_in('%_bar_cellm', matches) | |||
nt.assert_in('%%_bar_cellm', matches) | |||
David P. Sanders
|
r13344 | ||
def test_magic_completion_order(): | |||
ip = get_ipython() | |||
c = ip.Completer | |||
Alex Alekseyev
|
r23642 | # Test ordering of line and cell magics. | |
text, matches = c.complete("timeit") | |||
nt.assert_equal(matches, ["%timeit", "%%timeit"]) | |||
David P. Sanders
|
r13344 | ||
Alex Alekseyev
|
r23642 | def test_magic_completion_shadowing(): | |
ip = get_ipython() | |||
c = ip.Completer | |||
David P. Sanders
|
r13344 | ||
Alex Alekseyev
|
r23642 | # Before importing matplotlib, %matplotlib magic should be the only option. | |
text, matches = c.complete("mat") | |||
nt.assert_equal(matches, ["%matplotlib"]) | |||
David P. Sanders
|
r13344 | ||
Alex Alekseyev
|
r23642 | # The newly introduced name should shadow the magic. | |
ip.run_cell("matplotlib = 1") | |||
text, matches = c.complete("mat") | |||
nt.assert_equal(matches, ["matplotlib"]) | |||
David P. Sanders
|
r13344 | ||
Alex Alekseyev
|
r23642 | # After removing matplotlib from namespace, the magic should again be | |
# the only option. | |||
del ip.user_ns["matplotlib"] | |||
text, matches = c.complete("mat") | |||
nt.assert_equal(matches, ["%matplotlib"]) | |||
David P. Sanders
|
r13344 | ||
Matthias Bussonnier
|
r23322 | ||
Sang Min Park
|
r23641 | def test_magic_config(): | |
ip = get_ipython() | |||
c = ip.Completer | |||
s, matches = c.complete(None, 'conf') | |||
nt.assert_in('%config', matches) | |||
Sang Min Park
|
r23685 | s, matches = c.complete(None, 'conf') | |
nt.assert_not_in('AliasManager', matches) | |||
Sang Min Park
|
r23641 | s, matches = c.complete(None, 'config ') | |
nt.assert_in('AliasManager', matches) | |||
s, matches = c.complete(None, '%config ') | |||
nt.assert_in('AliasManager', matches) | |||
s, matches = c.complete(None, 'config Ali') | |||
Sang Min Park
|
r23685 | nt.assert_list_equal(['AliasManager'], matches) | |
Sang Min Park
|
r23641 | s, matches = c.complete(None, '%config Ali') | |
Sang Min Park
|
r23685 | nt.assert_list_equal(['AliasManager'], matches) | |
Sang Min Park
|
r23641 | s, matches = c.complete(None, 'config AliasManager') | |
nt.assert_list_equal(['AliasManager'], matches) | |||
s, matches = c.complete(None, '%config AliasManager') | |||
nt.assert_list_equal(['AliasManager'], matches) | |||
s, matches = c.complete(None, 'config AliasManager.') | |||
nt.assert_in('AliasManager.default_aliases', matches) | |||
s, matches = c.complete(None, '%config AliasManager.') | |||
nt.assert_in('AliasManager.default_aliases', matches) | |||
s, matches = c.complete(None, 'config AliasManager.de') | |||
Sang Min Park
|
r23685 | nt.assert_list_equal(['AliasManager.default_aliases'], matches) | |
Sang Min Park
|
r23641 | s, matches = c.complete(None, 'config AliasManager.de') | |
Sang Min Park
|
r23685 | nt.assert_list_equal(['AliasManager.default_aliases'], matches) | |
def test_magic_color(): | |||
ip = get_ipython() | |||
c = ip.Completer | |||
s, matches = c.complete(None, 'colo') | |||
nt.assert_in('%colors', matches) | |||
s, matches = c.complete(None, 'colo') | |||
nt.assert_not_in('NoColor', matches) | |||
s, matches = c.complete(None, 'colors ') | |||
nt.assert_in('NoColor', matches) | |||
s, matches = c.complete(None, '%colors ') | |||
nt.assert_in('NoColor', matches) | |||
s, matches = c.complete(None, 'colors NoCo') | |||
nt.assert_list_equal(['NoColor'], matches) | |||
s, matches = c.complete(None, '%colors NoCo') | |||
nt.assert_list_equal(['NoColor'], matches) | |||
Sang Min Park
|
r23641 | ||
Matthias Bussonnier
|
r23322 | def test_match_dict_keys(): | |
""" | |||
Test that match_dict_keys works on a couple of use case does return what | |||
expected, and does not crash | |||
""" | |||
delims = ' \t\n`!@#$^&*()=+[{]}\\|;:\'",<>?' | |||
keys = ['foo', b'far'] | |||
assert match_dict_keys(keys, "b'", delims=delims) == ("'", 2 ,['far']) | |||
assert match_dict_keys(keys, "b'f", delims=delims) == ("'", 2 ,['far']) | |||
assert match_dict_keys(keys, 'b"', delims=delims) == ('"', 2 ,['far']) | |||
assert match_dict_keys(keys, 'b"f', delims=delims) == ('"', 2 ,['far']) | |||
assert match_dict_keys(keys, "'", delims=delims) == ("'", 1 ,['foo']) | |||
assert match_dict_keys(keys, "'f", delims=delims) == ("'", 1 ,['foo']) | |||
assert match_dict_keys(keys, '"', delims=delims) == ('"', 1 ,['foo']) | |||
assert match_dict_keys(keys, '"f', delims=delims) == ('"', 1 ,['foo']) | |||
match_dict_keys | |||
David P. Sanders
|
r13344 | ||
Joel Nothman
|
r15766 | ||
def test_dict_key_completion_string(): | |||
"""Test dictionary key completion for string keys""" | |||
ip = get_ipython() | |||
complete = ip.Completer.complete | |||
ip.user_ns['d'] = {'abc': None} | |||
# check completion at different stages | |||
_, matches = complete(line_buffer="d[") | |||
MinRK
|
r16564 | nt.assert_in("'abc'", matches) | |
nt.assert_not_in("'abc']", matches) | |||
Joel Nothman
|
r15766 | ||
_, matches = complete(line_buffer="d['") | |||
MinRK
|
r16564 | nt.assert_in("abc", matches) | |
nt.assert_not_in("abc']", matches) | |||
Joel Nothman
|
r15766 | ||
_, matches = complete(line_buffer="d['a") | |||
MinRK
|
r16564 | nt.assert_in("abc", matches) | |
nt.assert_not_in("abc']", matches) | |||
Joel Nothman
|
r15766 | ||
# check use of different quoting | |||
_, matches = complete(line_buffer="d[\"") | |||
MinRK
|
r16564 | nt.assert_in("abc", matches) | |
nt.assert_not_in('abc\"]', matches) | |||
Joel Nothman
|
r15766 | ||
_, matches = complete(line_buffer="d[\"a") | |||
MinRK
|
r16564 | nt.assert_in("abc", matches) | |
nt.assert_not_in('abc\"]', matches) | |||
Joel Nothman
|
r15766 | ||
# check sensitivity to following context | |||
_, matches = complete(line_buffer="d[]", cursor_pos=2) | |||
nt.assert_in("'abc'", matches) | |||
_, matches = complete(line_buffer="d['']", cursor_pos=3) | |||
nt.assert_in("abc", matches) | |||
MinRK
|
r16564 | nt.assert_not_in("abc'", matches) | |
nt.assert_not_in("abc']", matches) | |||
Joel Nothman
|
r15766 | ||
# check multiple solutions are correctly returned and that noise is not | |||
ip.user_ns['d'] = {'abc': None, 'abd': None, 'bad': None, object(): None, | |||
5: None} | |||
_, matches = complete(line_buffer="d['a") | |||
MinRK
|
r16564 | nt.assert_in("abc", matches) | |
nt.assert_in("abd", matches) | |||
nt.assert_not_in("bad", matches) | |||
assert not any(m.endswith((']', '"', "'")) for m in matches), matches | |||
Joel Nothman
|
r15766 | ||
# check escaping and whitespace | |||
ip.user_ns['d'] = {'a\nb': None, 'a\'b': None, 'a"b': None, 'a word': None} | |||
_, matches = complete(line_buffer="d['a") | |||
MinRK
|
r16564 | nt.assert_in("a\\nb", matches) | |
nt.assert_in("a\\'b", matches) | |||
nt.assert_in("a\"b", matches) | |||
nt.assert_in("a word", matches) | |||
assert not any(m.endswith((']', '"', "'")) for m in matches), matches | |||
Joel Nothman
|
r15766 | ||
# - can complete on non-initial word of the string | |||
_, matches = complete(line_buffer="d['a w") | |||
MinRK
|
r16564 | nt.assert_in("word", matches) | |
Joel Nothman
|
r15766 | ||
# - understands quote escaping | |||
_, matches = complete(line_buffer="d['a\\'") | |||
MinRK
|
r16564 | nt.assert_in("b", matches) | |
Joel Nothman
|
r15766 | ||
# - default quoting should work like repr | |||
_, matches = complete(line_buffer="d[") | |||
MinRK
|
r16564 | nt.assert_in("\"a'b\"", matches) | |
Joel Nothman
|
r15766 | ||
# - when opening quote with ", possible to match with unescaped apostrophe | |||
_, matches = complete(line_buffer="d[\"a'") | |||
MinRK
|
r16564 | nt.assert_in("b", matches) | |
Joel Nothman
|
r15766 | ||
Jeff Hussmann
|
r21294 | # need to not split at delims that readline won't split at | |
if '-' not in ip.Completer.splitter.delims: | |||
ip.user_ns['d'] = {'before-after': None} | |||
_, matches = complete(line_buffer="d['before-af") | |||
nt.assert_in('before-after', matches) | |||
Joel Nothman
|
r15766 | ||
def test_dict_key_completion_contexts(): | |||
"""Test expression contexts in which dict key completion occurs""" | |||
ip = get_ipython() | |||
complete = ip.Completer.complete | |||
d = {'abc': None} | |||
ip.user_ns['d'] = d | |||
class C: | |||
data = d | |||
ip.user_ns['C'] = C | |||
ip.user_ns['get'] = lambda: d | |||
def assert_no_completion(**kwargs): | |||
_, matches = complete(**kwargs) | |||
nt.assert_not_in('abc', matches) | |||
nt.assert_not_in('abc\'', matches) | |||
nt.assert_not_in('abc\']', matches) | |||
nt.assert_not_in('\'abc\'', matches) | |||
nt.assert_not_in('\'abc\']', matches) | |||
def assert_completion(**kwargs): | |||
_, matches = complete(**kwargs) | |||
MinRK
|
r16564 | nt.assert_in("'abc'", matches) | |
nt.assert_not_in("'abc']", matches) | |||
Joel Nothman
|
r15766 | ||
# no completion after string closed, even if reopened | |||
assert_no_completion(line_buffer="d['a'") | |||
assert_no_completion(line_buffer="d[\"a\"") | |||
assert_no_completion(line_buffer="d['a' + ") | |||
assert_no_completion(line_buffer="d['a' + '") | |||
# completion in non-trivial expressions | |||
assert_completion(line_buffer="+ d[") | |||
assert_completion(line_buffer="(d[") | |||
assert_completion(line_buffer="C.data[") | |||
# greedy flag | |||
MinRK
|
r16564 | def assert_completion(**kwargs): | |
_, matches = complete(**kwargs) | |||
nt.assert_in("get()['abc']", matches) | |||
Joel Nothman
|
r15766 | assert_no_completion(line_buffer="get()[") | |
MinRK
|
r16564 | with greedy_completion(): | |
assert_completion(line_buffer="get()[") | |||
assert_completion(line_buffer="get()['") | |||
assert_completion(line_buffer="get()['a") | |||
assert_completion(line_buffer="get()['ab") | |||
assert_completion(line_buffer="get()['abc") | |||
Joel Nothman
|
r15766 | ||
def test_dict_key_completion_bytes(): | |||
"""Test handling of bytes in dict key completion""" | |||
ip = get_ipython() | |||
complete = ip.Completer.complete | |||
ip.user_ns['d'] = {'abc': None, b'abd': None} | |||
_, matches = complete(line_buffer="d[") | |||
MinRK
|
r16564 | nt.assert_in("'abc'", matches) | |
nt.assert_in("b'abd'", matches) | |||
Joel Nothman
|
r15766 | ||
if False: # not currently implemented | |||
_, matches = complete(line_buffer="d[b") | |||
MinRK
|
r16564 | nt.assert_in("b'abd'", matches) | |
nt.assert_not_in("b'abc'", matches) | |||
Joel Nothman
|
r15766 | ||
_, matches = complete(line_buffer="d[b'") | |||
MinRK
|
r16564 | nt.assert_in("abd", matches) | |
nt.assert_not_in("abc", matches) | |||
Joel Nothman
|
r15766 | ||
_, matches = complete(line_buffer="d[B'") | |||
MinRK
|
r16564 | nt.assert_in("abd", matches) | |
nt.assert_not_in("abc", matches) | |||
Joel Nothman
|
r15766 | ||
_, matches = complete(line_buffer="d['") | |||
MinRK
|
r16564 | nt.assert_in("abc", matches) | |
nt.assert_not_in("abd", matches) | |||
Joel Nothman
|
r15766 | ||
Joel Nothman
|
r15773 | def test_dict_key_completion_unicode_py3(): | |
"""Test handling of unicode in dict key completion""" | |||
ip = get_ipython() | |||
complete = ip.Completer.complete | |||
MinRK
|
r16564 | ip.user_ns['d'] = {u'a\u05d0': None} | |
Joel Nothman
|
r15773 | ||
# query using escape | |||
Thomas Kluyver
|
r22761 | if sys.platform != 'win32': | |
# Known failure on Windows | |||
_, matches = complete(line_buffer="d['a\\u05d0") | |||
nt.assert_in("u05d0", matches) # tokenized after \\ | |||
Joel Nothman
|
r15773 | ||
# query using character | |||
MinRK
|
r16564 | _, matches = complete(line_buffer="d['a\u05d0") | |
nt.assert_in(u"a\u05d0", matches) | |||
with greedy_completion(): | |||
# query using escape | |||
_, matches = complete(line_buffer="d['a\\u05d0") | |||
nt.assert_in("d['a\\u05d0']", matches) # tokenized after \\ | |||
# query using character | |||
_, matches = complete(line_buffer="d['a\u05d0") | |||
nt.assert_in(u"d['a\u05d0']", matches) | |||
Joel Nothman
|
r15773 | ||
Joel Nothman
|
r15766 | ||
@dec.skip_without('numpy') | |||
def test_struct_array_key_completion(): | |||
"""Test dict key completion applies to numpy struct arrays""" | |||
import numpy | |||
ip = get_ipython() | |||
complete = ip.Completer.complete | |||
ip.user_ns['d'] = numpy.array([], dtype=[('hello', 'f'), ('world', 'f')]) | |||
_, matches = complete(line_buffer="d['") | |||
MinRK
|
r16564 | nt.assert_in("hello", matches) | |
nt.assert_in("world", matches) | |||
mbyt
|
r19790 | # complete on the numpy struct itself | |
dt = numpy.dtype([('my_head', [('my_dt', '>u4'), ('my_df', '>u4')]), | |||
('my_data', '>f4', 5)]) | |||
x = numpy.zeros(2, dtype=dt) | |||
ip.user_ns['d'] = x[1] | |||
_, matches = complete(line_buffer="d['") | |||
nt.assert_in("my_head", matches) | |||
nt.assert_in("my_data", matches) | |||
# complete on a nested level | |||
with greedy_completion(): | |||
ip.user_ns['d'] = numpy.zeros(2, dtype=dt) | |||
_, matches = complete(line_buffer="d[1]['my_head']['") | |||
nt.assert_true(any(["my_dt" in m for m in matches])) | |||
nt.assert_true(any(["my_df" in m for m in matches])) | |||
Joel Nothman
|
r15766 | ||
@dec.skip_without('pandas') | |||
def test_dataframe_key_completion(): | |||
"""Test dict key completion applies to pandas DataFrames""" | |||
import pandas | |||
ip = get_ipython() | |||
complete = ip.Completer.complete | |||
ip.user_ns['d'] = pandas.DataFrame({'hello': [1], 'world': [2]}) | |||
_, matches = complete(line_buffer="d['") | |||
MinRK
|
r16564 | nt.assert_in("hello", matches) | |
nt.assert_in("world", matches) | |||
Joel Nothman
|
r15773 | ||
def test_dict_key_completion_invalids(): | |||
"""Smoke test cases dict key completion can't handle""" | |||
ip = get_ipython() | |||
complete = ip.Completer.complete | |||
ip.user_ns['no_getitem'] = None | |||
ip.user_ns['no_keys'] = [] | |||
ip.user_ns['cant_call_keys'] = dict | |||
ip.user_ns['empty'] = {} | |||
ip.user_ns['d'] = {'abc': 5} | |||
_, matches = complete(line_buffer="no_getitem['") | |||
_, matches = complete(line_buffer="no_keys['") | |||
_, matches = complete(line_buffer="cant_call_keys['") | |||
_, matches = complete(line_buffer="empty['") | |||
_, matches = complete(line_buffer="name_error['") | |||
_, matches = complete(line_buffer="d['\\") # incomplete escape | |||
Min RK
|
r21718 | ||
Thomas Kluyver
|
r22145 | class KeyCompletable(object): | |
def __init__(self, things=()): | |||
self.things = things | |||
Thomas Kluyver
|
r22146 | def _ipython_key_completions_(self): | |
Thomas Kluyver
|
r22145 | return list(self.things) | |
def test_object_key_completion(): | |||
ip = get_ipython() | |||
ip.user_ns['key_completable'] = KeyCompletable(['qwerty', 'qwick']) | |||
_, matches = ip.Completer.complete(line_buffer="key_completable['qw") | |||
nt.assert_in('qwerty', matches) | |||
nt.assert_in('qwick', matches) | |||
Matthias Bussonnier
|
r23214 | def test_tryimport(): | |
""" | |||
Test that try-import don't crash on trailing dot, and import modules before | |||
""" | |||
from IPython.core.completerlib import try_import | |||
assert(try_import("IPython.")) | |||
Min RK
|
r21718 | def test_aimport_module_completer(): | |
ip = get_ipython() | |||
_, matches = ip.complete('i', '%aimport i') | |||
nt.assert_in('io', matches) | |||
nt.assert_not_in('int', matches) | |||
Kelly Liu
|
r22292 | def test_nested_import_module_completer(): | |
ip = get_ipython() | |||
_, matches = ip.complete(None, 'import IPython.co', 17) | |||
nt.assert_in('IPython.core', matches) | |||
nt.assert_not_in('import IPython.core', matches) | |||
Matthias Bussonnier
|
r22237 | nt.assert_not_in('IPython.display', matches) | |
Kelly Liu
|
r22292 | ||
Min RK
|
r21718 | def test_import_module_completer(): | |
ip = get_ipython() | |||
_, matches = ip.complete('i', 'import i') | |||
nt.assert_in('io', matches) | |||
nt.assert_not_in('int', matches) | |||
def test_from_module_completer(): | |||
ip = get_ipython() | |||
Matthias Bussonnier
|
r22303 | _, matches = ip.complete('B', 'from io import B', 16) | |
Min RK
|
r21718 | nt.assert_in('BytesIO', matches) | |
nt.assert_not_in('BaseException', matches) | |||
sagnak
|
r23614 | ||
def test_snake_case_completion(): | |||
ip = get_ipython() | |||
ip.user_ns['some_three'] = 3 | |||
ip.user_ns['some_four'] = 4 | |||
_, matches = ip.complete("s_", "print(s_f") | |||
nt.assert_in('some_three', matches) | |||
nt.assert_in('some_four', matches) |