##// END OF EJS Templates
Some more tests for escaped commands
Thomas Kluyver -
Show More
@@ -1,82 +1,123 b''
1 import nose.tools as nt
1 import nose.tools as nt
2
2
3 from IPython.core import inputtransformer2 as ipt2
3 from IPython.core import inputtransformer2 as ipt2
4 from IPython.core.inputtransformer2 import make_tokens_by_line
4 from IPython.core.inputtransformer2 import make_tokens_by_line
5
5
6 MULTILINE_MAGIC = ("""\
6 MULTILINE_MAGIC = ("""\
7 a = f()
7 a = f()
8 %foo \\
8 %foo \\
9 bar
9 bar
10 g()
10 g()
11 """.splitlines(keepends=True), """\
11 """.splitlines(keepends=True), """\
12 a = f()
12 a = f()
13 get_ipython().run_line_magic('foo', ' bar')
13 get_ipython().run_line_magic('foo', ' bar')
14 g()
14 g()
15 """.splitlines(keepends=True))
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 MULTILINE_MAGIC_ASSIGN = ("""\
25 MULTILINE_MAGIC_ASSIGN = ("""\
18 a = f()
26 a = f()
19 b = %foo \\
27 b = %foo \\
20 bar
28 bar
21 g()
29 g()
22 """.splitlines(keepends=True), """\
30 """.splitlines(keepends=True), """\
23 a = f()
31 a = f()
24 b = get_ipython().run_line_magic('foo', ' bar')
32 b = get_ipython().run_line_magic('foo', ' bar')
25 g()
33 g()
26 """.splitlines(keepends=True))
34 """.splitlines(keepends=True))
27
35
28 MULTILINE_SYSTEM_ASSIGN = ("""\
36 MULTILINE_SYSTEM_ASSIGN = ("""\
29 a = f()
37 a = f()
30 b = !foo \\
38 b = !foo \\
31 bar
39 bar
32 g()
40 g()
33 """.splitlines(keepends=True), """\
41 """.splitlines(keepends=True), """\
34 a = f()
42 a = f()
35 b = get_ipython().getoutput('foo bar')
43 b = get_ipython().getoutput('foo bar')
36 g()
44 g()
37 """.splitlines(keepends=True))
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 def test_continued_line():
62 def test_continued_line():
40 lines = MULTILINE_MAGIC_ASSIGN[0]
63 lines = MULTILINE_MAGIC_ASSIGN[0]
41 nt.assert_equal(ipt2.find_end_of_continued_line(lines, 1), 2)
64 nt.assert_equal(ipt2.find_end_of_continued_line(lines, 1), 2)
42
65
43 nt.assert_equal(ipt2.assemble_continued_line(lines, (1, 5), 2), "foo bar")
66 nt.assert_equal(ipt2.assemble_continued_line(lines, (1, 5), 2), "foo bar")
44
67
45 def test_find_assign_magic():
68 def test_find_assign_magic():
46 tbl = make_tokens_by_line(MULTILINE_MAGIC_ASSIGN[0])
69 tbl = make_tokens_by_line(MULTILINE_MAGIC_ASSIGN[0])
47 nt.assert_equal(ipt2.MagicAssign.find(tbl), (2, 4))
70 nt.assert_equal(ipt2.MagicAssign.find(tbl), (2, 4))
48
71
49 tbl = make_tokens_by_line(MULTILINE_SYSTEM_ASSIGN[0]) # Nothing to find
72 tbl = make_tokens_by_line(MULTILINE_SYSTEM_ASSIGN[0]) # Nothing to find
50 nt.assert_equal(ipt2.MagicAssign.find(tbl), None)
73 nt.assert_equal(ipt2.MagicAssign.find(tbl), None)
51
74
52 def test_transform_assign_magic():
75 def test_transform_assign_magic():
53 res = ipt2.MagicAssign.transform(MULTILINE_MAGIC_ASSIGN[0], (2, 4))
76 res = ipt2.MagicAssign.transform(MULTILINE_MAGIC_ASSIGN[0], (2, 4))
54 nt.assert_equal(res, MULTILINE_MAGIC_ASSIGN[1])
77 nt.assert_equal(res, MULTILINE_MAGIC_ASSIGN[1])
55
78
56 def test_find_assign_system():
79 def test_find_assign_system():
57 tbl = make_tokens_by_line(MULTILINE_SYSTEM_ASSIGN[0])
80 tbl = make_tokens_by_line(MULTILINE_SYSTEM_ASSIGN[0])
58 nt.assert_equal(ipt2.SystemAssign.find(tbl), (2, 4))
81 nt.assert_equal(ipt2.SystemAssign.find(tbl), (2, 4))
59
82
60 tbl = make_tokens_by_line(["a = !ls\n"])
83 tbl = make_tokens_by_line(["a = !ls\n"])
61 nt.assert_equal(ipt2.SystemAssign.find(tbl), (1, 5))
84 nt.assert_equal(ipt2.SystemAssign.find(tbl), (1, 5))
62
85
63 tbl = make_tokens_by_line(["a=!ls\n"])
86 tbl = make_tokens_by_line(["a=!ls\n"])
64 nt.assert_equal(ipt2.SystemAssign.find(tbl), (1, 2))
87 nt.assert_equal(ipt2.SystemAssign.find(tbl), (1, 2))
65
88
66 tbl = make_tokens_by_line(MULTILINE_MAGIC_ASSIGN[0]) # Nothing to find
89 tbl = make_tokens_by_line(MULTILINE_MAGIC_ASSIGN[0]) # Nothing to find
67 nt.assert_equal(ipt2.SystemAssign.find(tbl), None)
90 nt.assert_equal(ipt2.SystemAssign.find(tbl), None)
68
91
69 def test_transform_assign_system():
92 def test_transform_assign_system():
70 res = ipt2.SystemAssign.transform(MULTILINE_SYSTEM_ASSIGN[0], (2, 4))
93 res = ipt2.SystemAssign.transform(MULTILINE_SYSTEM_ASSIGN[0], (2, 4))
71 nt.assert_equal(res, MULTILINE_SYSTEM_ASSIGN[1])
94 nt.assert_equal(res, MULTILINE_SYSTEM_ASSIGN[1])
72
95
73 def test_find_magic_escape():
96 def test_find_magic_escape():
74 tbl = make_tokens_by_line(MULTILINE_MAGIC[0])
97 tbl = make_tokens_by_line(MULTILINE_MAGIC[0])
75 nt.assert_equal(ipt2.EscapedCommand.find(tbl), (2, 0))
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 tbl = make_tokens_by_line(MULTILINE_MAGIC_ASSIGN[0]) # Shouldn't find a = %foo
103 tbl = make_tokens_by_line(MULTILINE_MAGIC_ASSIGN[0]) # Shouldn't find a = %foo
78 nt.assert_equal(ipt2.EscapedCommand.find(tbl), None)
104 nt.assert_equal(ipt2.EscapedCommand.find(tbl), None)
79
105
80 def test_transform_magic_escape():
106 def test_transform_magic_escape():
81 res = ipt2.EscapedCommand.transform(MULTILINE_MAGIC[0], (2, 0))
107 res = ipt2.EscapedCommand.transform(MULTILINE_MAGIC[0], (2, 0))
82 nt.assert_equal(res, MULTILINE_MAGIC[1])
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