##// END OF EJS Templates
Add some inputtransformer test cases
Matthias Bussonnier -
Show More
@@ -1,195 +1,217 b''
1 """Tests for the token-based transformers in IPython.core.inputtransformer2
1 """Tests for the token-based transformers in IPython.core.inputtransformer2
2
2
3 Line-based transformers are the simpler ones; token-based transformers are
3 Line-based transformers are the simpler ones; token-based transformers are
4 more complex. See test_inputtransformer2_line for tests for line-based
4 more complex. See test_inputtransformer2_line for tests for line-based
5 transformations.
5 transformations.
6 """
6 """
7 import nose.tools as nt
7 import nose.tools as nt
8 import string
8
9
9 from IPython.core import inputtransformer2 as ipt2
10 from IPython.core import inputtransformer2 as ipt2
10 from IPython.core.inputtransformer2 import make_tokens_by_line
11 from IPython.core.inputtransformer2 import make_tokens_by_line
11
12
12 MULTILINE_MAGIC = ("""\
13 MULTILINE_MAGIC = ("""\
13 a = f()
14 a = f()
14 %foo \\
15 %foo \\
15 bar
16 bar
16 g()
17 g()
17 """.splitlines(keepends=True), (2, 0), """\
18 """.splitlines(keepends=True), (2, 0), """\
18 a = f()
19 a = f()
19 get_ipython().run_line_magic('foo', ' bar')
20 get_ipython().run_line_magic('foo', ' bar')
20 g()
21 g()
21 """.splitlines(keepends=True))
22 """.splitlines(keepends=True))
22
23
23 INDENTED_MAGIC = ("""\
24 INDENTED_MAGIC = ("""\
24 for a in range(5):
25 for a in range(5):
25 %ls
26 %ls
26 """.splitlines(keepends=True), (2, 4), """\
27 """.splitlines(keepends=True), (2, 4), """\
27 for a in range(5):
28 for a in range(5):
28 get_ipython().run_line_magic('ls', '')
29 get_ipython().run_line_magic('ls', '')
29 """.splitlines(keepends=True))
30 """.splitlines(keepends=True))
30
31
31 MULTILINE_MAGIC_ASSIGN = ("""\
32 MULTILINE_MAGIC_ASSIGN = ("""\
32 a = f()
33 a = f()
33 b = %foo \\
34 b = %foo \\
34 bar
35 bar
35 g()
36 g()
36 """.splitlines(keepends=True), (2, 4), """\
37 """.splitlines(keepends=True), (2, 4), """\
37 a = f()
38 a = f()
38 b = get_ipython().run_line_magic('foo', ' bar')
39 b = get_ipython().run_line_magic('foo', ' bar')
39 g()
40 g()
40 """.splitlines(keepends=True))
41 """.splitlines(keepends=True))
41
42
42 MULTILINE_SYSTEM_ASSIGN = ("""\
43 MULTILINE_SYSTEM_ASSIGN = ("""\
43 a = f()
44 a = f()
44 b = !foo \\
45 b = !foo \\
45 bar
46 bar
46 g()
47 g()
47 """.splitlines(keepends=True), (2, 4), """\
48 """.splitlines(keepends=True), (2, 4), """\
48 a = f()
49 a = f()
49 b = get_ipython().getoutput('foo bar')
50 b = get_ipython().getoutput('foo bar')
50 g()
51 g()
51 """.splitlines(keepends=True))
52 """.splitlines(keepends=True))
52
53
53 AUTOCALL_QUOTE = (
54 AUTOCALL_QUOTE = (
54 [",f 1 2 3\n"], (1, 0),
55 [",f 1 2 3\n"], (1, 0),
55 ['f("1", "2", "3")\n']
56 ['f("1", "2", "3")\n']
56 )
57 )
57
58
58 AUTOCALL_QUOTE2 = (
59 AUTOCALL_QUOTE2 = (
59 [";f 1 2 3\n"], (1, 0),
60 [";f 1 2 3\n"], (1, 0),
60 ['f("1 2 3")\n']
61 ['f("1 2 3")\n']
61 )
62 )
62
63
63 AUTOCALL_PAREN = (
64 AUTOCALL_PAREN = (
64 ["/f 1 2 3\n"], (1, 0),
65 ["/f 1 2 3\n"], (1, 0),
65 ['f(1, 2, 3)\n']
66 ['f(1, 2, 3)\n']
66 )
67 )
67
68
68 SIMPLE_HELP = (
69 SIMPLE_HELP = (
69 ["foo?\n"], (1, 0),
70 ["foo?\n"], (1, 0),
70 ["get_ipython().run_line_magic('pinfo', 'foo')\n"]
71 ["get_ipython().run_line_magic('pinfo', 'foo')\n"]
71 )
72 )
72
73
73 DETAILED_HELP = (
74 DETAILED_HELP = (
74 ["foo??\n"], (1, 0),
75 ["foo??\n"], (1, 0),
75 ["get_ipython().run_line_magic('pinfo2', 'foo')\n"]
76 ["get_ipython().run_line_magic('pinfo2', 'foo')\n"]
76 )
77 )
77
78
78 MAGIC_HELP = (
79 MAGIC_HELP = (
79 ["%foo?\n"], (1, 0),
80 ["%foo?\n"], (1, 0),
80 ["get_ipython().run_line_magic('pinfo', '%foo')\n"]
81 ["get_ipython().run_line_magic('pinfo', '%foo')\n"]
81 )
82 )
82
83
83 HELP_IN_EXPR = (
84 HELP_IN_EXPR = (
84 ["a = b + c?\n"], (1, 0),
85 ["a = b + c?\n"], (1, 0),
85 ["get_ipython().set_next_input('a = b + c');"
86 ["get_ipython().set_next_input('a = b + c');"
86 "get_ipython().run_line_magic('pinfo', 'c')\n"]
87 "get_ipython().run_line_magic('pinfo', 'c')\n"]
87 )
88 )
88
89
89 HELP_CONTINUED_LINE = ("""\
90 HELP_CONTINUED_LINE = ("""\
90 a = \\
91 a = \\
91 zip?
92 zip?
92 """.splitlines(keepends=True), (1, 0),
93 """.splitlines(keepends=True), (1, 0),
93 [r"get_ipython().set_next_input('a = \\\nzip');get_ipython().run_line_magic('pinfo', 'zip')" + "\n"]
94 [r"get_ipython().set_next_input('a = \\\nzip');get_ipython().run_line_magic('pinfo', 'zip')" + "\n"]
94 )
95 )
95
96
96 HELP_MULTILINE = ("""\
97 HELP_MULTILINE = ("""\
97 (a,
98 (a,
98 b) = zip?
99 b) = zip?
99 """.splitlines(keepends=True), (1, 0),
100 """.splitlines(keepends=True), (1, 0),
100 [r"get_ipython().set_next_input('(a,\nb) = zip');get_ipython().run_line_magic('pinfo', 'zip')" + "\n"]
101 [r"get_ipython().set_next_input('(a,\nb) = zip');get_ipython().run_line_magic('pinfo', 'zip')" + "\n"]
101 )
102 )
102
103
104 def check_make_token_by_line_never_ends_empty():
105 """
106 Check that not sequence of single or double characters ends up leading to en empty list of tokens
107 """
108 from string import printable
109 for c in printable:
110 nt.assert_not_equal(make_tokens_by_line(c)[-1], [])
111 for k in printable:
112 nt.assert_not_equal(make_tokens_by_line(c+k)[-1], [])
113
103 def check_find(transformer, case, match=True):
114 def check_find(transformer, case, match=True):
104 sample, expected_start, _ = case
115 sample, expected_start, _ = case
105 tbl = make_tokens_by_line(sample)
116 tbl = make_tokens_by_line(sample)
106 res = transformer.find(tbl)
117 res = transformer.find(tbl)
107 if match:
118 if match:
108 # start_line is stored 0-indexed, expected values are 1-indexed
119 # start_line is stored 0-indexed, expected values are 1-indexed
109 nt.assert_equal((res.start_line+1, res.start_col), expected_start)
120 nt.assert_equal((res.start_line+1, res.start_col), expected_start)
110 return res
121 return res
111 else:
122 else:
112 nt.assert_is(res, None)
123 nt.assert_is(res, None)
113
124
114 def check_transform(transformer_cls, case):
125 def check_transform(transformer_cls, case):
115 lines, start, expected = case
126 lines, start, expected = case
116 transformer = transformer_cls(start)
127 transformer = transformer_cls(start)
117 nt.assert_equal(transformer.transform(lines), expected)
128 nt.assert_equal(transformer.transform(lines), expected)
118
129
119 def test_continued_line():
130 def test_continued_line():
120 lines = MULTILINE_MAGIC_ASSIGN[0]
131 lines = MULTILINE_MAGIC_ASSIGN[0]
121 nt.assert_equal(ipt2.find_end_of_continued_line(lines, 1), 2)
132 nt.assert_equal(ipt2.find_end_of_continued_line(lines, 1), 2)
122
133
123 nt.assert_equal(ipt2.assemble_continued_line(lines, (1, 5), 2), "foo bar")
134 nt.assert_equal(ipt2.assemble_continued_line(lines, (1, 5), 2), "foo bar")
124
135
125 def test_find_assign_magic():
136 def test_find_assign_magic():
126 check_find(ipt2.MagicAssign, MULTILINE_MAGIC_ASSIGN)
137 check_find(ipt2.MagicAssign, MULTILINE_MAGIC_ASSIGN)
127 check_find(ipt2.MagicAssign, MULTILINE_SYSTEM_ASSIGN, match=False)
138 check_find(ipt2.MagicAssign, MULTILINE_SYSTEM_ASSIGN, match=False)
128
139
129 def test_transform_assign_magic():
140 def test_transform_assign_magic():
130 check_transform(ipt2.MagicAssign, MULTILINE_MAGIC_ASSIGN)
141 check_transform(ipt2.MagicAssign, MULTILINE_MAGIC_ASSIGN)
131
142
132 def test_find_assign_system():
143 def test_find_assign_system():
133 check_find(ipt2.SystemAssign, MULTILINE_SYSTEM_ASSIGN)
144 check_find(ipt2.SystemAssign, MULTILINE_SYSTEM_ASSIGN)
134 check_find(ipt2.SystemAssign, (["a = !ls\n"], (1, 5), None))
145 check_find(ipt2.SystemAssign, (["a = !ls\n"], (1, 5), None))
135 check_find(ipt2.SystemAssign, (["a=!ls\n"], (1, 2), None))
146 check_find(ipt2.SystemAssign, (["a=!ls\n"], (1, 2), None))
136 check_find(ipt2.SystemAssign, MULTILINE_MAGIC_ASSIGN, match=False)
147 check_find(ipt2.SystemAssign, MULTILINE_MAGIC_ASSIGN, match=False)
137
148
138 def test_transform_assign_system():
149 def test_transform_assign_system():
139 check_transform(ipt2.SystemAssign, MULTILINE_SYSTEM_ASSIGN)
150 check_transform(ipt2.SystemAssign, MULTILINE_SYSTEM_ASSIGN)
140
151
141 def test_find_magic_escape():
152 def test_find_magic_escape():
142 check_find(ipt2.EscapedCommand, MULTILINE_MAGIC)
153 check_find(ipt2.EscapedCommand, MULTILINE_MAGIC)
143 check_find(ipt2.EscapedCommand, INDENTED_MAGIC)
154 check_find(ipt2.EscapedCommand, INDENTED_MAGIC)
144 check_find(ipt2.EscapedCommand, MULTILINE_MAGIC_ASSIGN, match=False)
155 check_find(ipt2.EscapedCommand, MULTILINE_MAGIC_ASSIGN, match=False)
145
156
146 def test_transform_magic_escape():
157 def test_transform_magic_escape():
147 check_transform(ipt2.EscapedCommand, MULTILINE_MAGIC)
158 check_transform(ipt2.EscapedCommand, MULTILINE_MAGIC)
148 check_transform(ipt2.EscapedCommand, INDENTED_MAGIC)
159 check_transform(ipt2.EscapedCommand, INDENTED_MAGIC)
149
160
150 def test_find_autocalls():
161 def test_find_autocalls():
151 for case in [AUTOCALL_QUOTE, AUTOCALL_QUOTE2, AUTOCALL_PAREN]:
162 for case in [AUTOCALL_QUOTE, AUTOCALL_QUOTE2, AUTOCALL_PAREN]:
152 print("Testing %r" % case[0])
163 print("Testing %r" % case[0])
153 check_find(ipt2.EscapedCommand, case)
164 check_find(ipt2.EscapedCommand, case)
154
165
155 def test_transform_autocall():
166 def test_transform_autocall():
156 for case in [AUTOCALL_QUOTE, AUTOCALL_QUOTE2, AUTOCALL_PAREN]:
167 for case in [AUTOCALL_QUOTE, AUTOCALL_QUOTE2, AUTOCALL_PAREN]:
157 print("Testing %r" % case[0])
168 print("Testing %r" % case[0])
158 check_transform(ipt2.EscapedCommand, case)
169 check_transform(ipt2.EscapedCommand, case)
159
170
160 def test_find_help():
171 def test_find_help():
161 for case in [SIMPLE_HELP, DETAILED_HELP, MAGIC_HELP, HELP_IN_EXPR]:
172 for case in [SIMPLE_HELP, DETAILED_HELP, MAGIC_HELP, HELP_IN_EXPR]:
162 check_find(ipt2.HelpEnd, case)
173 check_find(ipt2.HelpEnd, case)
163
174
164 tf = check_find(ipt2.HelpEnd, HELP_CONTINUED_LINE)
175 tf = check_find(ipt2.HelpEnd, HELP_CONTINUED_LINE)
165 nt.assert_equal(tf.q_line, 1)
176 nt.assert_equal(tf.q_line, 1)
166 nt.assert_equal(tf.q_col, 3)
177 nt.assert_equal(tf.q_col, 3)
167
178
168 tf = check_find(ipt2.HelpEnd, HELP_MULTILINE)
179 tf = check_find(ipt2.HelpEnd, HELP_MULTILINE)
169 nt.assert_equal(tf.q_line, 1)
180 nt.assert_equal(tf.q_line, 1)
170 nt.assert_equal(tf.q_col, 8)
181 nt.assert_equal(tf.q_col, 8)
171
182
172 # ? in a comment does not trigger help
183 # ? in a comment does not trigger help
173 check_find(ipt2.HelpEnd, (["foo # bar?\n"], None, None), match=False)
184 check_find(ipt2.HelpEnd, (["foo # bar?\n"], None, None), match=False)
174 # Nor in a string
185 # Nor in a string
175 check_find(ipt2.HelpEnd, (["foo = '''bar?\n"], None, None), match=False)
186 check_find(ipt2.HelpEnd, (["foo = '''bar?\n"], None, None), match=False)
176
187
177 def test_transform_help():
188 def test_transform_help():
178 tf = ipt2.HelpEnd((1, 0), (1, 9))
189 tf = ipt2.HelpEnd((1, 0), (1, 9))
179 nt.assert_equal(tf.transform(HELP_IN_EXPR[0]), HELP_IN_EXPR[2])
190 nt.assert_equal(tf.transform(HELP_IN_EXPR[0]), HELP_IN_EXPR[2])
180
191
181 tf = ipt2.HelpEnd((1, 0), (2, 3))
192 tf = ipt2.HelpEnd((1, 0), (2, 3))
182 nt.assert_equal(tf.transform(HELP_CONTINUED_LINE[0]), HELP_CONTINUED_LINE[2])
193 nt.assert_equal(tf.transform(HELP_CONTINUED_LINE[0]), HELP_CONTINUED_LINE[2])
183
194
184 tf = ipt2.HelpEnd((1, 0), (2, 8))
195 tf = ipt2.HelpEnd((1, 0), (2, 8))
185 nt.assert_equal(tf.transform(HELP_MULTILINE[0]), HELP_MULTILINE[2])
196 nt.assert_equal(tf.transform(HELP_MULTILINE[0]), HELP_MULTILINE[2])
186
197
187 def test_check_complete():
198 def test_check_complete():
188 cc = ipt2.TransformerManager().check_complete
199 cc = ipt2.TransformerManager().check_complete
189 nt.assert_equal(cc("a = 1"), ('complete', None))
200 nt.assert_equal(cc("a = 1"), ('complete', None))
190 nt.assert_equal(cc("for a in range(5):"), ('incomplete', 4))
201 nt.assert_equal(cc("for a in range(5):"), ('incomplete', 4))
191 nt.assert_equal(cc("raise = 2"), ('invalid', None))
202 nt.assert_equal(cc("raise = 2"), ('invalid', None))
192 nt.assert_equal(cc("a = [1,\n2,"), ('incomplete', 0))
203 nt.assert_equal(cc("a = [1,\n2,"), ('incomplete', 0))
204 nt.assert_equal(cc(")"), ('incomplete', 0))
205 nt.assert_equal(cc("\\\r\n"), ('incomplete', 0))
193 nt.assert_equal(cc("a = '''\n hi"), ('incomplete', 3))
206 nt.assert_equal(cc("a = '''\n hi"), ('incomplete', 3))
194 nt.assert_equal(cc("def a():\n x=1\n global x"), ('invalid', None))
207 nt.assert_equal(cc("def a():\n x=1\n global x"), ('invalid', None))
195 nt.assert_equal(cc("a \\ "), ('invalid', None)) # Nothing allowed after backslash
208 nt.assert_equal(cc("a \\ "), ('invalid', None)) # Nothing allowed after backslash
209
210 # no need to loop on all the letters/numbers.
211 short = '12abAB'+string.printable[62:]
212 for c in short:
213 # test does not raise:
214 cc(c)
215 for k in short:
216 cc(c+k)
217
General Comments 0
You need to be logged in to leave comments. Login now