##// END OF EJS Templates
Fix cell magic transformation
Thomas Kluyver -
Show More
@@ -0,0 +1,21 b''
1 """Tests for the line-based transformers in IPython.core.inputtransformer2
2
3 Line-based transformers are the simpler ones; token-based transformers are
4 more complex.
5 """
6 import nose.tools as nt
7
8 from IPython.core import inputtransformer2 as ipt2
9
10 SIMPLE = ("""\
11 %%foo arg
12 body 1
13 body 2
14 """, """\
15 get_ipython().run_cell_magic('foo', 'arg', 'body 1\\nbody 2\\n')
16 """)
17
18 def test_cell_magic():
19 for sample, expected in [SIMPLE]:
20 nt.assert_equal(ipt2.cell_magic(sample.splitlines(keepends=True)),
21 expected.splitlines(keepends=True))
@@ -61,9 +61,10 b' def cell_magic(lines):'
61 if re.match('%%\w+\?', lines[0]):
61 if re.match('%%\w+\?', lines[0]):
62 # This case will be handled by help_end
62 # This case will be handled by help_end
63 return lines
63 return lines
64 magic_name, first_line = lines[0][2:].partition(' ')
64 magic_name, _, first_line = lines[0][2:-1].partition(' ')
65 body = '\n'.join(lines[1:])
65 body = ''.join(lines[1:])
66 return ['get_ipython().run_cell_magic(%r, %r, %r)' % (magic_name, first_line, body)]
66 return ['get_ipython().run_cell_magic(%r, %r, %r)\n'
67 % (magic_name, first_line, body)]
67
68
68 line_transforms = [
69 line_transforms = [
69 leading_indent,
70 leading_indent,
@@ -430,5 +431,4 b' def transform_cell(cell):'
430 lines = transform(lines)
431 lines = transform(lines)
431
432
432 lines = TokenTransformers()(lines)
433 lines = TokenTransformers()(lines)
433 for line in lines:
434 return ''.join(lines)
434 print('~~', line)
@@ -700,8 +700,8 b' class CellMagicTestCase(TestCase):'
700 out = _ip.run_cell_magic(magic, 'a', 'b')
700 out = _ip.run_cell_magic(magic, 'a', 'b')
701 nt.assert_equal(out, ('a','b'))
701 nt.assert_equal(out, ('a','b'))
702 # Via run_cell, it goes into the user's namespace via displayhook
702 # Via run_cell, it goes into the user's namespace via displayhook
703 _ip.run_cell('%%' + magic +' c\nd')
703 _ip.run_cell('%%' + magic +' c\nd\n')
704 nt.assert_equal(_ip.user_ns['_'], ('c','d'))
704 nt.assert_equal(_ip.user_ns['_'], ('c','d\n'))
705
705
706 def test_cell_magic_func_deco(self):
706 def test_cell_magic_func_deco(self):
707 "Cell magic using simple decorator"
707 "Cell magic using simple decorator"
General Comments 0
You need to be logged in to leave comments. Login now