test_handlers.py
170 lines
| 6.4 KiB
| text/x-python
|
PythonLexer
|
r2659 | """Tests for input handlers. | ||
""" | ||||
#----------------------------------------------------------------------------- | ||||
# Module imports | ||||
#----------------------------------------------------------------------------- | ||||
|
r657 | |||
|
r2659 | # third party | ||
import nose.tools as nt | ||||
# our own packages | ||||
from IPython.core import autocall | ||||
from IPython.testing import decorators as dec | ||||
|
r4744 | from IPython.testing import tools as tt | ||
|
r2659 | from IPython.testing.globalipapp import get_ipython | ||
|
r4896 | from IPython.utils import py3compat | ||
|
r2659 | |||
#----------------------------------------------------------------------------- | ||||
# Globals | ||||
#----------------------------------------------------------------------------- | ||||
# Get the public instance of IPython | ||||
ip = get_ipython() | ||||
|
r657 | |||
failures = [] | ||||
num_tests = 0 | ||||
|
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" | ||||
|
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)""" | ||||
|
r4744 | tt.check_pairs(ip.prefilter_manager.prefilter_lines, tests) | ||
|
r657 | |||
|
r2659 | def test_handlers(): | ||
|
r657 | # alias expansion | ||
|
r2659 | |||
|
r657 | # We're using 'true' as our syscall of choice because it doesn't | ||
# write anything to stdout. | ||||
# Turn off actual execution of aliases, because it's noisy | ||||
|
r707 | old_system_cmd = ip.system | ||
ip.system = lambda cmd: None | ||||
|
r2659 | |||
ip.alias_manager.alias_table['an_alias'] = (0, 'true') | ||||
|
r657 | # These are useful for checking a particular recursive alias issue | ||
|
r2659 | ip.alias_manager.alias_table['top'] = (0, 'd:/cygwin/top') | ||
ip.alias_manager.alias_table['d'] = (0, 'true') | ||||
|
r4895 | run([(i,py3compat.u_format(o)) for i,o in \ | ||
|
r5350 | [("an_alias", "get_ipython().system({u}'true ')"), # alias | ||
|
r657 | # Below: recursive aliases should expand whitespace-surrounded | ||
# chars, *not* initial chars which happen to be aliases: | ||||
|
r5350 | ("top", "get_ipython().system({u}'d:/cygwin/top ')"), | ||
|
r4895 | ]]) | ||
|
r707 | ip.system = old_system_cmd | ||
|
r657 | |||
call_idx = CallableIndexable() | ||||
|
r2659 | ip.user_ns['call_idx'] = call_idx | ||
|
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. | ||||
|
r4895 | run([(i,py3compat.u_format(o)) for i,o in \ | ||
[('"no change"', '"no change"'), # normal | ||||
|
r5350 | (u"!true", "get_ipython().system({u}'true')"), # shell_escapes | ||
(u"!! true", "get_ipython().magic({u}'sx true')"), # shell_escapes + magic | ||||
(u"!!true", "get_ipython().magic({u}'sx true')"), # shell_escapes + magic | ||||
(u"%lsmagic", "get_ipython().magic({u}'lsmagic ')"), # magic | ||||
(u"lsmagic", "get_ipython().magic({u}'lsmagic ')"), # magic | ||||
|
r2659 | #("a = b # PYTHON-MODE", '_i'), # emacs -- avoids _in cache | ||
|
r657 | |||
# post-esc-char whitespace goes inside | ||||
|
r5350 | (u"! true", "get_ipython().system({u}' true')"), | ||
|
r657 | |||
# handle_help | ||||
# These are weak tests -- just looking at what the help handlers | ||||
# logs, which is not how it really does its work. But it still | ||||
# lets us check the key paths through the handler. | ||||
|
r2659 | |||
|
r657 | ("x=1 # what?", "x=1 # what?"), # no help if valid python | ||
|
r4895 | ]]) | ||
|
r657 | |||
# multi_line_specials | ||||
|
r2659 | ip.prefilter_manager.multi_line_specials = False | ||
|
r657 | # W/ multi_line_specials off, leading ws kills esc chars/autoexpansion | ||
run([ | ||||
|
r5350 | (u'if 1:\n !true', u'if 1:\n !true'), | ||
(u'if 1:\n lsmagic', u'if 1:\n lsmagic'), | ||||
(u'if 1:\n an_alias', u'if 1:\n an_alias'), | ||||
|
r657 | ]) | ||
|
r2659 | ip.prefilter_manager.multi_line_specials = True | ||
|
r657 | # initial indents must be preserved. | ||
|
r4895 | run([(i,py3compat.u_format(o)) for i,o in \ | ||
|
r5350 | [(u'if 1:\n !true', "if 1:\n get_ipython().system({u}'true')"), | ||
(u'if 2:\n lsmagic', "if 2:\n get_ipython().magic({u}'lsmagic ')"), | ||||
(u'if 1:\n an_alias', "if 1:\n get_ipython().system({u}'true ')"), | ||||
|
r657 | # Weird one | ||
|
r5350 | (u'if 1:\n !!true', "if 1:\n get_ipython().magic({u}'sx true')"), | ||
|
r657 | |||
|
r2659 | # Even with m_l_s on, autocall is off even with special chars | ||
|
r4872 | ('if 1:\n /fun 1 2', 'if 1:\n /fun 1 2'), | ||
|
r657 | ('if 1:\n ;fun 1 2', 'if 1:\n ;fun 1 2'), | ||
('if 1:\n ,fun 1 2', 'if 1:\n ,fun 1 2'), | ||||
('if 1:\n ?fun 1 2', 'if 1:\n ?fun 1 2'), | ||||
|
r4872 | # What about !! | ||
|
r4895 | ]]) | ||
|
r657 | |||
# Objects which are instances of IPyAutocall are *always* autocalled | ||||
autocallable = Autocallable() | ||||
|
r2659 | ip.user_ns['autocallable'] = autocallable | ||
|
r657 | |||
|
r4872 | # auto | ||
|
r2659 | ip.magic('autocall 0') | ||
|
r657 | # Only explicit escapes or instances of IPyAutocallable should get | ||
# expanded | ||||
run([ | ||||
|
r4872 | ('len "abc"', 'len "abc"'), | ||
('autocallable', 'autocallable()'), | ||||
|
r5651 | # Don't add extra brackets (gh-1117) | ||
|
r5654 | ('autocallable()', 'autocallable()'), | ||
|
r657 | (",list 1 2 3", 'list("1", "2", "3")'), | ||
|
r4872 | (";list 1 2 3", 'list("1 2 3")'), | ||
|
r657 | ("/len range(1,4)", 'len(range(1,4))'), | ||
]) | ||||
|
r2659 | ip.magic('autocall 1') | ||
|
r657 | run([ | ||
(",list 1 2 3", 'list("1", "2", "3")'), | ||||
|
r4872 | (";list 1 2 3", 'list("1 2 3")'), | ||
|
r657 | ("/len range(1,4)", 'len(range(1,4))'), | ||
('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)'), | ||||
|
r5654 | ('len', 'len'), # only at 2 does it auto-call on single args | ||
|
r657 | ]) | ||
|
r2659 | ip.magic('autocall 2') | ||
|
r657 | run([ | ||
(",list 1 2 3", 'list("1", "2", "3")'), | ||||
|
r4872 | (";list 1 2 3", 'list("1 2 3")'), | ||
|
r657 | ("/len range(1,4)", 'len(range(1,4))'), | ||
('len "abc"', 'len("abc")'), | ||||
('len "abc";', 'len("abc");'), | ||||
|
r4872 | ('len [1,2]', 'len([1,2])'), | ||
|
r657 | ('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 | ||||
]) | ||||
|
r2659 | ip.magic('autocall 1') | ||
|
r657 | |||
|
r2659 | nt.assert_equals(failures, []) | ||