test_handlers.py
97 lines
| 3.2 KiB
| text/x-python
|
PythonLexer
Fernando Perez
|
r2659 | """Tests for input handlers. | ||
""" | ||||
#----------------------------------------------------------------------------- | ||||
# Module imports | ||||
#----------------------------------------------------------------------------- | ||||
dan.milstein
|
r657 | |||
Fernando Perez
|
r2659 | # our own packages | ||
from IPython.core import autocall | ||||
Thomas Kluyver
|
r4744 | from IPython.testing import tools as tt | ||
Fernando Perez
|
r2659 | |||
#----------------------------------------------------------------------------- | ||||
# Globals | ||||
#----------------------------------------------------------------------------- | ||||
# Get the public instance of IPython | ||||
dan.milstein
|
r657 | |||
failures = [] | ||||
num_tests = 0 | ||||
Fernando Perez
|
r2659 | #----------------------------------------------------------------------------- | ||
# Test functions | ||||
#----------------------------------------------------------------------------- | ||||
class CallableIndexable(object): | ||||
def __getitem__(self, idx): return True | ||||
def __call__(self, *args, **kws): return True | ||||
class Autocallable(autocall.IPyAutocall): | ||||
def __call__(self): | ||||
return "called" | ||||
dan.milstein
|
r657 | def run(tests): | ||
"""Loop through a list of (pre, post) inputs, where pre is the string | ||||
handed to ipython, and post is how that string looks after it's been | ||||
transformed (i.e. ipython's notion of _i)""" | ||||
Thomas Kluyver
|
r4744 | tt.check_pairs(ip.prefilter_manager.prefilter_lines, tests) | ||
dan.milstein
|
r657 | |||
Fernando Perez
|
r2659 | def test_handlers(): | ||
dan.milstein
|
r657 | call_idx = CallableIndexable() | ||
Fernando Perez
|
r2659 | ip.user_ns['call_idx'] = call_idx | ||
dan.milstein
|
r657 | |||
# For many of the below, we're also checking that leading whitespace | ||||
# turns off the esc char, which it should unless there is a continuation | ||||
# line. | ||||
Matthias Bussonnier
|
r25285 | run( | ||
Thomas Kluyver
|
r4895 | [('"no change"', '"no change"'), # normal | ||
adityausathe
|
r23764 | (u"lsmagic", "get_ipython().run_line_magic('lsmagic', '')"), # magic | ||
Fernando Perez
|
r2659 | #("a = b # PYTHON-MODE", '_i'), # emacs -- avoids _in cache | ||
Matthias Bussonnier
|
r25285 | ]) | ||
dan.milstein
|
r657 | |||
# Objects which are instances of IPyAutocall are *always* autocalled | ||||
autocallable = Autocallable() | ||||
Fernando Perez
|
r2659 | ip.user_ns['autocallable'] = autocallable | ||
dan.milstein
|
r657 | |||
Bernardo B. Marques
|
r4872 | # auto | ||
Matthias Bussonnier
|
r27549 | ip.run_line_magic("autocall", "0") | ||
dan.milstein
|
r657 | # Only explicit escapes or instances of IPyAutocallable should get | ||
# expanded | ||||
Matthias Bussonnier
|
r27549 | run( | ||
[ | ||||
('len "abc"', 'len "abc"'), | ||||
("autocallable", "autocallable()"), | ||||
# Don't add extra brackets (gh-1117) | ||||
("autocallable()", "autocallable()"), | ||||
] | ||||
) | ||||
ip.run_line_magic("autocall", "1") | ||||
run( | ||||
[ | ||||
('len "abc"', 'len("abc")'), | ||||
('len "abc";', 'len("abc");'), # ; is special -- moves out of parens | ||||
# Autocall is turned off if first arg is [] and the object | ||||
# is both callable and indexable. Like so: | ||||
("len [1,2]", "len([1,2])"), # len doesn't support __getitem__... | ||||
("call_idx [1]", "call_idx [1]"), # call_idx *does*.. | ||||
("call_idx 1", "call_idx(1)"), | ||||
("len", "len"), # only at 2 does it auto-call on single args | ||||
] | ||||
) | ||||
ip.run_line_magic("autocall", "2") | ||||
run( | ||||
[ | ||||
('len "abc"', 'len("abc")'), | ||||
('len "abc";', 'len("abc");'), | ||||
("len [1,2]", "len([1,2])"), | ||||
("call_idx [1]", "call_idx [1]"), | ||||
("call_idx 1", "call_idx(1)"), | ||||
# This is what's different: | ||||
("len", "len()"), # only at 2 does it auto-call on single args | ||||
] | ||||
) | ||||
ip.run_line_magic("autocall", "1") | ||||
dan.milstein
|
r657 | |||
Samuel Gaist
|
r26895 | assert failures == [] | ||