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