##// END OF EJS Templates
forgotten import (#13961)...
forgotten import (#13961) Fixes #13967

File last commit:

r26750:7b2546d2
r28161:a80ee46c merge
Show More
test_prefilter.py
127 lines | 4.1 KiB | text/x-python | PythonLexer
Fernando Perez
Add transformers to understand code pasted with >>> or IPython prompts....
r2426 """Tests for input manipulation machinery."""
#-----------------------------------------------------------------------------
# Imports
#-----------------------------------------------------------------------------
Tomasz Kłoczko
nose2pytest migration batch 1...
r26749 import pytest
Fernando Perez
Add transformers to understand code pasted with >>> or IPython prompts....
r2426
Bradley M. Froehle
Test exclude_regexp in AutocallChecker.
r6737 from IPython.core.prefilter import AutocallChecker
Fernando Perez
Add transformers to understand code pasted with >>> or IPython prompts....
r2426
#-----------------------------------------------------------------------------
# Tests
#-----------------------------------------------------------------------------
Fernando Perez
Add some extra tests for prefiltering.
r2551
Fernando Perez
Add transformers to understand code pasted with >>> or IPython prompts....
r2426 def test_prefilter():
"""Test user input conversions"""
# pairs of (raw, expected correct) input
pairs = [ ('2+2','2+2'),
]
for raw, correct in pairs:
Tomasz Kłoczko
nose2pytest migration batch 1...
r26749 assert ip.prefilter(raw) == correct
Fernando Perez
Add some extra tests for prefiltering.
r2551
Thomas Kluyver
Keywords should shadow magic functions
r12760 def test_prefilter_shadowed():
def dummy_magic(line): pass
prev_automagic_state = ip.automagic
ip.automagic = True
Thomas Kluyver
Disable autocall for prefilter test
r12761 ip.autocall = 0
Thomas Kluyver
Keywords should shadow magic functions
r12760
try:
# These should not be transformed - they are shadowed by other names
for name in ['if', 'zip', 'get_ipython']: # keyword, builtin, global
ip.register_magic_function(dummy_magic, magic_name=name)
Blazej Michalik
Darker
r26750 res = ip.prefilter(name + " foo")
assert res == name + " foo"
del ip.magics_manager.magics["line"][name]
Thomas Kluyver
Keywords should shadow magic functions
r12760
# These should be transformed
for name in ['fi', 'piz', 'nohtypi_teg']:
ip.register_magic_function(dummy_magic, magic_name=name)
Blazej Michalik
Darker
r26750 res = ip.prefilter(name + " foo")
assert res != name + " foo"
del ip.magics_manager.magics["line"][name]
Thomas Kluyver
Keywords should shadow magic functions
r12760
finally:
ip.automagic = prev_automagic_state
Fernando Perez
Catch errors raised by user objects when accessing attributes....
r5298
Fernando Perez
Add some extra tests for prefiltering.
r2551 def test_autocall_binops():
Thomas Kluyver
Replace links to launchpad bugs in comments/docstrings with equivalent github links.
r3917 """See https://github.com/ipython/ipython/issues/81"""
Fernando Perez
Add some extra tests for prefiltering.
r2551 ip.magic('autocall 2')
f = lambda x: x
ip.user_ns['f'] = f
try:
Blazej Michalik
Darker
r26750 assert ip.prefilter("f 1") == "f(1)"
for t in ["f +1", "f -1"]:
Tomasz Kłoczko
nose2pytest migration batch 1...
r26749 assert ip.prefilter(t) == t
Bradley M. Froehle
Test exclude_regexp in AutocallChecker.
r6737
# Run tests again with a more permissive exclude_regexp, which will
# allow transformation of binary operations ('f -1' -> 'f(-1)').
pm = ip.prefilter_manager
ac = AutocallChecker(shell=pm.shell, prefilter_manager=pm,
config=pm.config)
try:
ac.priority = 1
ac.exclude_regexp = r'^[,&^\|\*/]|^is |^not |^in |^and |^or '
pm.sort_checkers()
Blazej Michalik
Darker
r26750 assert ip.prefilter("f -1") == "f(-1)"
assert ip.prefilter("f +1") == "f(+1)"
Bradley M. Froehle
Test exclude_regexp in AutocallChecker.
r6737 finally:
pm.unregister_checker(ac)
Fernando Perez
Add some extra tests for prefiltering.
r2551 finally:
ip.magic('autocall 0')
del ip.user_ns['f']
Paul Ivanov
check that multiline string literals don't expand as magic...
r2618
Fernando Perez
Catch errors raised by user objects when accessing attributes....
r5298
def test_issue_114():
Paul Ivanov
check that multiline string literals don't expand as magic...
r2618 """Check that multiline string literals don't expand as magic
Fernando Perez
Catch errors raised by user objects when accessing attributes....
r5298 see http://github.com/ipython/ipython/issues/114"""
Fernando Perez
Fix test for issue #114, in this case the test had a small error....
r2648
Paul Ivanov
check that multiline string literals don't expand as magic...
r2618 template = '"""\n%s\n"""'
Fernando Perez
Fix test for issue #114, in this case the test had a small error....
r2648 # Store the current value of multi_line_specials and turn it off before
# running test, since it could be true (case in which the test doesn't make
# sense, as multiline string literals *will* expand as magic in that case).
msp = ip.prefilter_manager.multi_line_specials
ip.prefilter_manager.multi_line_specials = False
try:
Fernando Perez
Fix various test failures from then new magics API.
r6942 for mgk in ip.magics_manager.lsmagic()['line']:
Fernando Perez
Fix test for issue #114, in this case the test had a small error....
r2648 raw = template % mgk
Tomasz Kłoczko
nose2pytest migration batch 1...
r26749 assert ip.prefilter(raw) == raw
Fernando Perez
Fix test for issue #114, in this case the test had a small error....
r2648 finally:
ip.prefilter_manager.multi_line_specials = msp
Fernando Perez
Catch errors raised by user objects when accessing attributes....
r5298
def test_prefilter_attribute_errors():
"""Capture exceptions thrown by user objects on attribute access.
See http://github.com/ipython/ipython/issues/988."""
class X(object):
def __getattr__(self, k):
raise ValueError('broken object')
def __call__(self, x):
return x
# Create a callable broken object
ip.user_ns['x'] = X()
ip.magic('autocall 2')
try:
# Even if x throws an attribute error when looking at its rewrite
# attribute, we should not crash. So the test here is simply making
# the prefilter call and not having an exception.
ip.prefilter('x 1')
finally:
del ip.user_ns['x']
ip.magic('autocall 0')
Markus Wageringel
support for unicode identifiers...
r25595
def test_autocall_should_support_unicode():
ip.magic('autocall 2')
ip.user_ns['π'] = lambda x: x
try:
Blazej Michalik
Darker
r26750 assert ip.prefilter("π 3") == "π(3)"
Markus Wageringel
support for unicode identifiers...
r25595 finally:
ip.magic('autocall 0')
del ip.user_ns['π']