##// END OF EJS Templates
Some more tests for escaped commands
Thomas Kluyver -
Show More
@@ -1,82 +1,123 b''
1 1 import nose.tools as nt
2 2
3 3 from IPython.core import inputtransformer2 as ipt2
4 4 from IPython.core.inputtransformer2 import make_tokens_by_line
5 5
6 6 MULTILINE_MAGIC = ("""\
7 7 a = f()
8 8 %foo \\
9 9 bar
10 10 g()
11 11 """.splitlines(keepends=True), """\
12 12 a = f()
13 13 get_ipython().run_line_magic('foo', ' bar')
14 14 g()
15 15 """.splitlines(keepends=True))
16 16
17 INDENTED_MAGIC = ("""\
18 for a in range(5):
19 %ls
20 """.splitlines(keepends=True), """\
21 for a in range(5):
22 get_ipython().run_line_magic('ls', '')
23 """.splitlines(keepends=True))
24
17 25 MULTILINE_MAGIC_ASSIGN = ("""\
18 26 a = f()
19 27 b = %foo \\
20 28 bar
21 29 g()
22 30 """.splitlines(keepends=True), """\
23 31 a = f()
24 32 b = get_ipython().run_line_magic('foo', ' bar')
25 33 g()
26 34 """.splitlines(keepends=True))
27 35
28 36 MULTILINE_SYSTEM_ASSIGN = ("""\
29 37 a = f()
30 38 b = !foo \\
31 39 bar
32 40 g()
33 41 """.splitlines(keepends=True), """\
34 42 a = f()
35 43 b = get_ipython().getoutput('foo bar')
36 44 g()
37 45 """.splitlines(keepends=True))
38 46
47 AUTOCALL_QUOTE = (
48 [",f 1 2 3\n"],
49 ['f("1", "2", "3")\n']
50 )
51
52 AUTOCALL_QUOTE2 = (
53 [";f 1 2 3\n"],
54 ['f("1 2 3")\n']
55 )
56
57 AUTOCALL_PAREN = (
58 ["/f 1 2 3\n"],
59 ['f(1, 2, 3)\n']
60 )
61
39 62 def test_continued_line():
40 63 lines = MULTILINE_MAGIC_ASSIGN[0]
41 64 nt.assert_equal(ipt2.find_end_of_continued_line(lines, 1), 2)
42 65
43 66 nt.assert_equal(ipt2.assemble_continued_line(lines, (1, 5), 2), "foo bar")
44 67
45 68 def test_find_assign_magic():
46 69 tbl = make_tokens_by_line(MULTILINE_MAGIC_ASSIGN[0])
47 70 nt.assert_equal(ipt2.MagicAssign.find(tbl), (2, 4))
48 71
49 72 tbl = make_tokens_by_line(MULTILINE_SYSTEM_ASSIGN[0]) # Nothing to find
50 73 nt.assert_equal(ipt2.MagicAssign.find(tbl), None)
51 74
52 75 def test_transform_assign_magic():
53 76 res = ipt2.MagicAssign.transform(MULTILINE_MAGIC_ASSIGN[0], (2, 4))
54 77 nt.assert_equal(res, MULTILINE_MAGIC_ASSIGN[1])
55 78
56 79 def test_find_assign_system():
57 80 tbl = make_tokens_by_line(MULTILINE_SYSTEM_ASSIGN[0])
58 81 nt.assert_equal(ipt2.SystemAssign.find(tbl), (2, 4))
59 82
60 83 tbl = make_tokens_by_line(["a = !ls\n"])
61 84 nt.assert_equal(ipt2.SystemAssign.find(tbl), (1, 5))
62 85
63 86 tbl = make_tokens_by_line(["a=!ls\n"])
64 87 nt.assert_equal(ipt2.SystemAssign.find(tbl), (1, 2))
65 88
66 89 tbl = make_tokens_by_line(MULTILINE_MAGIC_ASSIGN[0]) # Nothing to find
67 90 nt.assert_equal(ipt2.SystemAssign.find(tbl), None)
68 91
69 92 def test_transform_assign_system():
70 93 res = ipt2.SystemAssign.transform(MULTILINE_SYSTEM_ASSIGN[0], (2, 4))
71 94 nt.assert_equal(res, MULTILINE_SYSTEM_ASSIGN[1])
72 95
73 96 def test_find_magic_escape():
74 97 tbl = make_tokens_by_line(MULTILINE_MAGIC[0])
75 98 nt.assert_equal(ipt2.EscapedCommand.find(tbl), (2, 0))
76 99
100 tbl = make_tokens_by_line(INDENTED_MAGIC[0])
101 nt.assert_equal(ipt2.EscapedCommand.find(tbl), (2, 4))
102
77 103 tbl = make_tokens_by_line(MULTILINE_MAGIC_ASSIGN[0]) # Shouldn't find a = %foo
78 104 nt.assert_equal(ipt2.EscapedCommand.find(tbl), None)
79 105
80 106 def test_transform_magic_escape():
81 107 res = ipt2.EscapedCommand.transform(MULTILINE_MAGIC[0], (2, 0))
82 108 nt.assert_equal(res, MULTILINE_MAGIC[1])
109
110 res = ipt2.EscapedCommand.transform(INDENTED_MAGIC[0], (2, 4))
111 nt.assert_equal(res, INDENTED_MAGIC[1])
112
113 def test_find_autocalls():
114 for sample, _ in [AUTOCALL_QUOTE, AUTOCALL_QUOTE2, AUTOCALL_PAREN]:
115 print("Testing %r" % sample)
116 tbl = make_tokens_by_line(sample)
117 nt.assert_equal(ipt2.EscapedCommand.find(tbl), (1, 0))
118
119 def test_transform_autocall():
120 for sample, expected in [AUTOCALL_QUOTE, AUTOCALL_QUOTE2, AUTOCALL_PAREN]:
121 print("Testing %r" % sample)
122 res = ipt2.EscapedCommand.transform(sample, (1, 0))
123 nt.assert_equal(res, expected)
General Comments 0
You need to be logged in to leave comments. Login now