##// END OF EJS Templates
Transformers for assignment from %magic and \!system calls
Transformers for assignment from %magic and \!system calls

File last commit:

r10092:b1060f64
r10092:b1060f64
Show More
test_inputtransformer.py
84 lines | 2.9 KiB | text/x-python | PythonLexer
/ IPython / core / tests / test_inputtransformer.py
Thomas Kluyver
First go at new input transformation system
r10090 import unittest
import nose.tools as nt
from IPython.testing import tools as tt
from IPython.utils import py3compat
from IPython.core import inputtransformer
from IPython.core.tests.test_inputsplitter import syntax
def wrap_transform(transformer):
def transform(inp):
Thomas Kluyver
More input transformers
r10091 results = []
Thomas Kluyver
First go at new input transformation system
r10090 for line in inp:
res = transformer.push(line)
if res is not None:
Thomas Kluyver
More input transformers
r10091 results.append(res)
transformer.reset()
return results
Thomas Kluyver
First go at new input transformation system
r10090
return transform
cellmagic_tests = [
Thomas Kluyver
More input transformers
r10091 (['%%foo a', None], ["get_ipython().run_cell_magic('foo', 'a', '')"]),
(['%%bar 123', 'hello', ''], ["get_ipython().run_cell_magic('bar', '123', 'hello')"]),
Thomas Kluyver
First go at new input transformation system
r10090 ]
def test_transform_cellmagic():
tt.check_pairs(wrap_transform(inputtransformer.cellmagic), cellmagic_tests)
Thomas Kluyver
More input transformers
r10091 esctransform_tests = [(i, [py3compat.u_format(ol) for ol in o]) for i,o in [
(['%pdef zip'], ["get_ipython().magic({u}'pdef zip')"]),
(['%abc def \\', 'ghi'], ["get_ipython().magic({u}'abc def ghi')"]),
(['%abc def \\', 'ghi\\', None], ["get_ipython().magic({u}'abc def ghi')"]),
Thomas Kluyver
First go at new input transformation system
r10090 ]]
def test_transform_escaped():
tt.check_pairs(wrap_transform(inputtransformer.escaped_transformer), esctransform_tests)
def endhelp_test():
tt.check_pairs(inputtransformer.transform_help_end.push, syntax['end_help'])
Thomas Kluyver
More input transformers
r10091
classic_prompt_tests = [
(['>>> a=1'], ['a=1']),
(['>>> a="""','... 123"""'], ['a="""', '123"""']),
(['a="""','... 123"""'], ['a="""', '... 123"""']),
]
def test_classic_prompt():
tt.check_pairs(wrap_transform(inputtransformer.classic_prompt), classic_prompt_tests)
ipy_prompt_tests = [
(['In [1]: a=1'], ['a=1']),
(['In [2]: a="""',' ...: 123"""'], ['a="""', '123"""']),
(['a="""',' ...: 123"""'], ['a="""', ' ...: 123"""']),
]
def test_ipy_prompt():
tt.check_pairs(wrap_transform(inputtransformer.ipy_prompt), ipy_prompt_tests)
leading_indent_tests = [
([' print "hi"'], ['print "hi"']),
([' for a in range(5):', ' a*2'], ['for a in range(5):', ' a*2']),
([' a="""',' 123"""'], ['a="""', '123"""']),
(['a="""',' 123"""'], ['a="""', ' 123"""']),
]
def test_leading_indent():
tt.check_pairs(wrap_transform(inputtransformer.leading_indent), leading_indent_tests)
Thomas Kluyver
Transformers for assignment from %magic and \!system calls
r10092
assign_magic_tests = [(i, [py3compat.u_format(ol) for ol in o]) for i,o in [
(['a = %bc de \\', 'fg'], ["a = get_ipython().magic('bc de fg')"]),
(['a = %bc de \\', 'fg\\', None], ["a = get_ipython().magic('bc de fg')"]),
]]
def test_assign_magic():
tt.check_pairs(wrap_transform(inputtransformer.assign_from_magic), assign_magic_tests)
assign_system_tests = [(i, [py3compat.u_format(ol) for ol in o]) for i,o in [
(['a = !bc de \\', 'fg'], ["a = get_ipython().getoutput('bc de fg')"]),
(['a = !bc de \\', 'fg\\', None], ["a = get_ipython().getoutput('bc de fg')"]),
]]
def test_assign_system():
tt.check_pairs(wrap_transform(inputtransformer.assign_from_system), assign_system_tests)