##// END OF EJS Templates
Add failing test for issue gh-4728
Thomas Kluyver -
Show More
@@ -1,470 +1,475 b''
1 import tokenize
1 import tokenize
2 import nose.tools as nt
2 import nose.tools as nt
3
3
4 from IPython.testing import tools as tt
4 from IPython.testing import tools as tt
5 from IPython.utils import py3compat
5 from IPython.utils import py3compat
6 u_fmt = py3compat.u_format
6 u_fmt = py3compat.u_format
7
7
8 from IPython.core import inputtransformer as ipt
8 from IPython.core import inputtransformer as ipt
9
9
10 def transform_and_reset(transformer):
10 def transform_and_reset(transformer):
11 transformer = transformer()
11 transformer = transformer()
12 def transform(inp):
12 def transform(inp):
13 try:
13 try:
14 return transformer.push(inp)
14 return transformer.push(inp)
15 finally:
15 finally:
16 transformer.reset()
16 transformer.reset()
17
17
18 return transform
18 return transform
19
19
20 # Transformer tests
20 # Transformer tests
21 def transform_checker(tests, transformer, **kwargs):
21 def transform_checker(tests, transformer, **kwargs):
22 """Utility to loop over test inputs"""
22 """Utility to loop over test inputs"""
23 transformer = transformer(**kwargs)
23 transformer = transformer(**kwargs)
24 try:
24 try:
25 for inp, tr in tests:
25 for inp, tr in tests:
26 if inp is None:
26 if inp is None:
27 out = transformer.reset()
27 out = transformer.reset()
28 else:
28 else:
29 out = transformer.push(inp)
29 out = transformer.push(inp)
30 nt.assert_equal(out, tr)
30 nt.assert_equal(out, tr)
31 finally:
31 finally:
32 transformer.reset()
32 transformer.reset()
33
33
34 # Data for all the syntax tests in the form of lists of pairs of
34 # Data for all the syntax tests in the form of lists of pairs of
35 # raw/transformed input. We store it here as a global dict so that we can use
35 # raw/transformed input. We store it here as a global dict so that we can use
36 # it both within single-function tests and also to validate the behavior of the
36 # it both within single-function tests and also to validate the behavior of the
37 # larger objects
37 # larger objects
38
38
39 syntax = \
39 syntax = \
40 dict(assign_system =
40 dict(assign_system =
41 [(i,py3compat.u_format(o)) for i,o in \
41 [(i,py3compat.u_format(o)) for i,o in \
42 [(u'a =! ls', "a = get_ipython().getoutput({u}'ls')"),
42 [(u'a =! ls', "a = get_ipython().getoutput({u}'ls')"),
43 (u'b = !ls', "b = get_ipython().getoutput({u}'ls')"),
43 (u'b = !ls', "b = get_ipython().getoutput({u}'ls')"),
44 ('x=1', 'x=1'), # normal input is unmodified
44 ('x=1', 'x=1'), # normal input is unmodified
45 (' ',' '), # blank lines are kept intact
45 (' ',' '), # blank lines are kept intact
46 ]],
46 ]],
47
47
48 assign_magic =
48 assign_magic =
49 [(i,py3compat.u_format(o)) for i,o in \
49 [(i,py3compat.u_format(o)) for i,o in \
50 [(u'a =% who', "a = get_ipython().magic({u}'who')"),
50 [(u'a =% who', "a = get_ipython().magic({u}'who')"),
51 (u'b = %who', "b = get_ipython().magic({u}'who')"),
51 (u'b = %who', "b = get_ipython().magic({u}'who')"),
52 ('x=1', 'x=1'), # normal input is unmodified
52 ('x=1', 'x=1'), # normal input is unmodified
53 (' ',' '), # blank lines are kept intact
53 (' ',' '), # blank lines are kept intact
54 ]],
54 ]],
55
55
56 classic_prompt =
56 classic_prompt =
57 [('>>> x=1', 'x=1'),
57 [('>>> x=1', 'x=1'),
58 ('x=1', 'x=1'), # normal input is unmodified
58 ('x=1', 'x=1'), # normal input is unmodified
59 (' ', ' '), # blank lines are kept intact
59 (' ', ' '), # blank lines are kept intact
60 ],
60 ],
61
61
62 ipy_prompt =
62 ipy_prompt =
63 [('In [1]: x=1', 'x=1'),
63 [('In [1]: x=1', 'x=1'),
64 ('x=1', 'x=1'), # normal input is unmodified
64 ('x=1', 'x=1'), # normal input is unmodified
65 (' ',' '), # blank lines are kept intact
65 (' ',' '), # blank lines are kept intact
66 ],
66 ],
67
67
68 strip_encoding_cookie =
68 strip_encoding_cookie =
69 [
69 [
70 ('# -*- encoding: utf-8 -*-', ''),
70 ('# -*- encoding: utf-8 -*-', ''),
71 ('# coding: latin-1', ''),
71 ('# coding: latin-1', ''),
72 ],
72 ],
73
73
74
74
75 # Tests for the escape transformer to leave normal code alone
75 # Tests for the escape transformer to leave normal code alone
76 escaped_noesc =
76 escaped_noesc =
77 [ (' ', ' '),
77 [ (' ', ' '),
78 ('x=1', 'x=1'),
78 ('x=1', 'x=1'),
79 ],
79 ],
80
80
81 # System calls
81 # System calls
82 escaped_shell =
82 escaped_shell =
83 [(i,py3compat.u_format(o)) for i,o in \
83 [(i,py3compat.u_format(o)) for i,o in \
84 [ (u'!ls', "get_ipython().system({u}'ls')"),
84 [ (u'!ls', "get_ipython().system({u}'ls')"),
85 # Double-escape shell, this means to capture the output of the
85 # Double-escape shell, this means to capture the output of the
86 # subprocess and return it
86 # subprocess and return it
87 (u'!!ls', "get_ipython().getoutput({u}'ls')"),
87 (u'!!ls', "get_ipython().getoutput({u}'ls')"),
88 ]],
88 ]],
89
89
90 # Help/object info
90 # Help/object info
91 escaped_help =
91 escaped_help =
92 [(i,py3compat.u_format(o)) for i,o in \
92 [(i,py3compat.u_format(o)) for i,o in \
93 [ (u'?', 'get_ipython().show_usage()'),
93 [ (u'?', 'get_ipython().show_usage()'),
94 (u'?x1', "get_ipython().magic({u}'pinfo x1')"),
94 (u'?x1', "get_ipython().magic({u}'pinfo x1')"),
95 (u'??x2', "get_ipython().magic({u}'pinfo2 x2')"),
95 (u'??x2', "get_ipython().magic({u}'pinfo2 x2')"),
96 (u'?a.*s', "get_ipython().magic({u}'psearch a.*s')"),
96 (u'?a.*s', "get_ipython().magic({u}'psearch a.*s')"),
97 (u'?%hist1', "get_ipython().magic({u}'pinfo %hist1')"),
97 (u'?%hist1', "get_ipython().magic({u}'pinfo %hist1')"),
98 (u'?%%hist2', "get_ipython().magic({u}'pinfo %%hist2')"),
98 (u'?%%hist2', "get_ipython().magic({u}'pinfo %%hist2')"),
99 (u'?abc = qwe', "get_ipython().magic({u}'pinfo abc')"),
99 (u'?abc = qwe', "get_ipython().magic({u}'pinfo abc')"),
100 ]],
100 ]],
101
101
102 end_help =
102 end_help =
103 [(i,py3compat.u_format(o)) for i,o in \
103 [(i,py3compat.u_format(o)) for i,o in \
104 [ (u'x3?', "get_ipython().magic({u}'pinfo x3')"),
104 [ (u'x3?', "get_ipython().magic({u}'pinfo x3')"),
105 (u'x4??', "get_ipython().magic({u}'pinfo2 x4')"),
105 (u'x4??', "get_ipython().magic({u}'pinfo2 x4')"),
106 (u'%hist1?', "get_ipython().magic({u}'pinfo %hist1')"),
106 (u'%hist1?', "get_ipython().magic({u}'pinfo %hist1')"),
107 (u'%hist2??', "get_ipython().magic({u}'pinfo2 %hist2')"),
107 (u'%hist2??', "get_ipython().magic({u}'pinfo2 %hist2')"),
108 (u'%%hist3?', "get_ipython().magic({u}'pinfo %%hist3')"),
108 (u'%%hist3?', "get_ipython().magic({u}'pinfo %%hist3')"),
109 (u'%%hist4??', "get_ipython().magic({u}'pinfo2 %%hist4')"),
109 (u'%%hist4??', "get_ipython().magic({u}'pinfo2 %%hist4')"),
110 (u'f*?', "get_ipython().magic({u}'psearch f*')"),
110 (u'f*?', "get_ipython().magic({u}'psearch f*')"),
111 (u'ax.*aspe*?', "get_ipython().magic({u}'psearch ax.*aspe*')"),
111 (u'ax.*aspe*?', "get_ipython().magic({u}'psearch ax.*aspe*')"),
112 (u'a = abc?', "get_ipython().set_next_input({u}'a = abc');"
112 (u'a = abc?', "get_ipython().set_next_input({u}'a = abc');"
113 "get_ipython().magic({u}'pinfo abc')"),
113 "get_ipython().magic({u}'pinfo abc')"),
114 (u'a = abc.qe??', "get_ipython().set_next_input({u}'a = abc.qe');"
114 (u'a = abc.qe??', "get_ipython().set_next_input({u}'a = abc.qe');"
115 "get_ipython().magic({u}'pinfo2 abc.qe')"),
115 "get_ipython().magic({u}'pinfo2 abc.qe')"),
116 (u'a = *.items?', "get_ipython().set_next_input({u}'a = *.items');"
116 (u'a = *.items?', "get_ipython().set_next_input({u}'a = *.items');"
117 "get_ipython().magic({u}'psearch *.items')"),
117 "get_ipython().magic({u}'psearch *.items')"),
118 (u'plot(a?', "get_ipython().set_next_input({u}'plot(a');"
118 (u'plot(a?', "get_ipython().set_next_input({u}'plot(a');"
119 "get_ipython().magic({u}'pinfo a')"),
119 "get_ipython().magic({u}'pinfo a')"),
120 (u'a*2 #comment?', 'a*2 #comment?'),
120 (u'a*2 #comment?', 'a*2 #comment?'),
121 ]],
121 ]],
122
122
123 # Explicit magic calls
123 # Explicit magic calls
124 escaped_magic =
124 escaped_magic =
125 [(i,py3compat.u_format(o)) for i,o in \
125 [(i,py3compat.u_format(o)) for i,o in \
126 [ (u'%cd', "get_ipython().magic({u}'cd')"),
126 [ (u'%cd', "get_ipython().magic({u}'cd')"),
127 (u'%cd /home', "get_ipython().magic({u}'cd /home')"),
127 (u'%cd /home', "get_ipython().magic({u}'cd /home')"),
128 # Backslashes need to be escaped.
128 # Backslashes need to be escaped.
129 (u'%cd C:\\User', "get_ipython().magic({u}'cd C:\\\\User')"),
129 (u'%cd C:\\User', "get_ipython().magic({u}'cd C:\\\\User')"),
130 (u' %magic', " get_ipython().magic({u}'magic')"),
130 (u' %magic', " get_ipython().magic({u}'magic')"),
131 ]],
131 ]],
132
132
133 # Quoting with separate arguments
133 # Quoting with separate arguments
134 escaped_quote =
134 escaped_quote =
135 [ (',f', 'f("")'),
135 [ (',f', 'f("")'),
136 (',f x', 'f("x")'),
136 (',f x', 'f("x")'),
137 (' ,f y', ' f("y")'),
137 (' ,f y', ' f("y")'),
138 (',f a b', 'f("a", "b")'),
138 (',f a b', 'f("a", "b")'),
139 ],
139 ],
140
140
141 # Quoting with single argument
141 # Quoting with single argument
142 escaped_quote2 =
142 escaped_quote2 =
143 [ (';f', 'f("")'),
143 [ (';f', 'f("")'),
144 (';f x', 'f("x")'),
144 (';f x', 'f("x")'),
145 (' ;f y', ' f("y")'),
145 (' ;f y', ' f("y")'),
146 (';f a b', 'f("a b")'),
146 (';f a b', 'f("a b")'),
147 ],
147 ],
148
148
149 # Simply apply parens
149 # Simply apply parens
150 escaped_paren =
150 escaped_paren =
151 [ ('/f', 'f()'),
151 [ ('/f', 'f()'),
152 ('/f x', 'f(x)'),
152 ('/f x', 'f(x)'),
153 (' /f y', ' f(y)'),
153 (' /f y', ' f(y)'),
154 ('/f a b', 'f(a, b)'),
154 ('/f a b', 'f(a, b)'),
155 ],
155 ],
156
156
157 # Check that we transform prompts before other transforms
157 # Check that we transform prompts before other transforms
158 mixed =
158 mixed =
159 [(i,py3compat.u_format(o)) for i,o in \
159 [(i,py3compat.u_format(o)) for i,o in \
160 [ (u'In [1]: %lsmagic', "get_ipython().magic({u}'lsmagic')"),
160 [ (u'In [1]: %lsmagic', "get_ipython().magic({u}'lsmagic')"),
161 (u'>>> %lsmagic', "get_ipython().magic({u}'lsmagic')"),
161 (u'>>> %lsmagic', "get_ipython().magic({u}'lsmagic')"),
162 (u'In [2]: !ls', "get_ipython().system({u}'ls')"),
162 (u'In [2]: !ls', "get_ipython().system({u}'ls')"),
163 (u'In [3]: abs?', "get_ipython().magic({u}'pinfo abs')"),
163 (u'In [3]: abs?', "get_ipython().magic({u}'pinfo abs')"),
164 (u'In [4]: b = %who', "b = get_ipython().magic({u}'who')"),
164 (u'In [4]: b = %who', "b = get_ipython().magic({u}'who')"),
165 ]],
165 ]],
166 )
166 )
167
167
168 # multiline syntax examples. Each of these should be a list of lists, with
168 # multiline syntax examples. Each of these should be a list of lists, with
169 # each entry itself having pairs of raw/transformed input. The union (with
169 # each entry itself having pairs of raw/transformed input. The union (with
170 # '\n'.join() of the transformed inputs is what the splitter should produce
170 # '\n'.join() of the transformed inputs is what the splitter should produce
171 # when fed the raw lines one at a time via push.
171 # when fed the raw lines one at a time via push.
172 syntax_ml = \
172 syntax_ml = \
173 dict(classic_prompt =
173 dict(classic_prompt =
174 [ [('>>> for i in range(10):','for i in range(10):'),
174 [ [('>>> for i in range(10):','for i in range(10):'),
175 ('... print i',' print i'),
175 ('... print i',' print i'),
176 ('... ', ''),
176 ('... ', ''),
177 ],
177 ],
178 [('>>> a="""','a="""'),
178 [('>>> a="""','a="""'),
179 ('... 123"""','123"""'),
179 ('... 123"""','123"""'),
180 ],
180 ],
181 [('a="""','a="""'),
181 [('a="""','a="""'),
182 ('... 123','123'),
182 ('... 123','123'),
183 ('... 456"""','456"""'),
183 ('... 456"""','456"""'),
184 ],
184 ],
185 [('a="""','a="""'),
185 [('a="""','a="""'),
186 ('>>> 123','123'),
186 ('>>> 123','123'),
187 ('... 456"""','456"""'),
187 ('... 456"""','456"""'),
188 ],
188 ],
189 [('a="""','a="""'),
189 [('a="""','a="""'),
190 ('123','123'),
190 ('123','123'),
191 ('... 456"""','... 456"""'),
191 ('... 456"""','... 456"""'),
192 ],
192 ],
193 [('....__class__','....__class__'),
193 [('....__class__','....__class__'),
194 ],
194 ],
195 [('a=5', 'a=5'),
195 [('a=5', 'a=5'),
196 ('...', ''),
196 ('...', ''),
197 ],
197 ],
198 [('>>> def f(x):', 'def f(x):'),
198 [('>>> def f(x):', 'def f(x):'),
199 ('...', ''),
199 ('...', ''),
200 ('... return x', ' return x'),
200 ('... return x', ' return x'),
201 ],
201 ],
202 ],
202 ],
203
203
204 ipy_prompt =
204 ipy_prompt =
205 [ [('In [24]: for i in range(10):','for i in range(10):'),
205 [ [('In [24]: for i in range(10):','for i in range(10):'),
206 (' ....: print i',' print i'),
206 (' ....: print i',' print i'),
207 (' ....: ', ''),
207 (' ....: ', ''),
208 ],
208 ],
209 [('In [24]: for i in range(10):','for i in range(10):'),
210 # Qt console prompts expand with spaces, not dots
211 (' ...: print i',' print i'),
212 (' ...: ', ''),
213 ],
209 [('In [2]: a="""','a="""'),
214 [('In [2]: a="""','a="""'),
210 (' ...: 123"""','123"""'),
215 (' ...: 123"""','123"""'),
211 ],
216 ],
212 [('a="""','a="""'),
217 [('a="""','a="""'),
213 (' ...: 123','123'),
218 (' ...: 123','123'),
214 (' ...: 456"""','456"""'),
219 (' ...: 456"""','456"""'),
215 ],
220 ],
216 [('a="""','a="""'),
221 [('a="""','a="""'),
217 ('In [1]: 123','123'),
222 ('In [1]: 123','123'),
218 (' ...: 456"""','456"""'),
223 (' ...: 456"""','456"""'),
219 ],
224 ],
220 [('a="""','a="""'),
225 [('a="""','a="""'),
221 ('123','123'),
226 ('123','123'),
222 (' ...: 456"""',' ...: 456"""'),
227 (' ...: 456"""',' ...: 456"""'),
223 ],
228 ],
224 ],
229 ],
225
230
226 strip_encoding_cookie =
231 strip_encoding_cookie =
227 [
232 [
228 [
233 [
229 ('# -*- coding: utf-8 -*-', ''),
234 ('# -*- coding: utf-8 -*-', ''),
230 ('foo', 'foo'),
235 ('foo', 'foo'),
231 ],
236 ],
232 [
237 [
233 ('#!/usr/bin/env python', '#!/usr/bin/env python'),
238 ('#!/usr/bin/env python', '#!/usr/bin/env python'),
234 ('# -*- coding: latin-1 -*-', ''),
239 ('# -*- coding: latin-1 -*-', ''),
235 # only the first-two lines
240 # only the first-two lines
236 ('# -*- coding: latin-1 -*-', '# -*- coding: latin-1 -*-'),
241 ('# -*- coding: latin-1 -*-', '# -*- coding: latin-1 -*-'),
237 ],
242 ],
238 ],
243 ],
239
244
240 multiline_datastructure_prompt =
245 multiline_datastructure_prompt =
241 [ [('>>> a = [1,','a = [1,'),
246 [ [('>>> a = [1,','a = [1,'),
242 ('... 2]','2]'),
247 ('... 2]','2]'),
243 ],
248 ],
244 ],
249 ],
245
250
246 multiline_datastructure =
251 multiline_datastructure =
247 [ [('b = ("%s"', None),
252 [ [('b = ("%s"', None),
248 ('# comment', None),
253 ('# comment', None),
249 ('%foo )', 'b = ("%s"\n# comment\n%foo )'),
254 ('%foo )', 'b = ("%s"\n# comment\n%foo )'),
250 ],
255 ],
251 ],
256 ],
252
257
253 multiline_string =
258 multiline_string =
254 [ [("'''foo?", None),
259 [ [("'''foo?", None),
255 ("bar'''", "'''foo?\nbar'''"),
260 ("bar'''", "'''foo?\nbar'''"),
256 ],
261 ],
257 ],
262 ],
258
263
259 leading_indent =
264 leading_indent =
260 [ [(' print "hi"','print "hi"'),
265 [ [(' print "hi"','print "hi"'),
261 ],
266 ],
262 [(' for a in range(5):','for a in range(5):'),
267 [(' for a in range(5):','for a in range(5):'),
263 (' a*2',' a*2'),
268 (' a*2',' a*2'),
264 ],
269 ],
265 [(' a="""','a="""'),
270 [(' a="""','a="""'),
266 (' 123"""','123"""'),
271 (' 123"""','123"""'),
267 ],
272 ],
268 [('a="""','a="""'),
273 [('a="""','a="""'),
269 (' 123"""',' 123"""'),
274 (' 123"""',' 123"""'),
270 ],
275 ],
271 ],
276 ],
272
277
273 cellmagic =
278 cellmagic =
274 [ [(u'%%foo a', None),
279 [ [(u'%%foo a', None),
275 (None, u_fmt("get_ipython().run_cell_magic({u}'foo', {u}'a', {u}'')")),
280 (None, u_fmt("get_ipython().run_cell_magic({u}'foo', {u}'a', {u}'')")),
276 ],
281 ],
277 [(u'%%bar 123', None),
282 [(u'%%bar 123', None),
278 (u'hello', None),
283 (u'hello', None),
279 (None , u_fmt("get_ipython().run_cell_magic({u}'bar', {u}'123', {u}'hello')")),
284 (None , u_fmt("get_ipython().run_cell_magic({u}'bar', {u}'123', {u}'hello')")),
280 ],
285 ],
281 [(u'a=5', 'a=5'),
286 [(u'a=5', 'a=5'),
282 (u'%%cellmagic', '%%cellmagic'),
287 (u'%%cellmagic', '%%cellmagic'),
283 ],
288 ],
284 ],
289 ],
285
290
286 escaped =
291 escaped =
287 [ [('%abc def \\', None),
292 [ [('%abc def \\', None),
288 ('ghi', u_fmt("get_ipython().magic({u}'abc def ghi')")),
293 ('ghi', u_fmt("get_ipython().magic({u}'abc def ghi')")),
289 ],
294 ],
290 [('%abc def \\', None),
295 [('%abc def \\', None),
291 ('ghi\\', None),
296 ('ghi\\', None),
292 (None, u_fmt("get_ipython().magic({u}'abc def ghi')")),
297 (None, u_fmt("get_ipython().magic({u}'abc def ghi')")),
293 ],
298 ],
294 ],
299 ],
295
300
296 assign_magic =
301 assign_magic =
297 [ [(u'a = %bc de \\', None),
302 [ [(u'a = %bc de \\', None),
298 (u'fg', u_fmt("a = get_ipython().magic({u}'bc de fg')")),
303 (u'fg', u_fmt("a = get_ipython().magic({u}'bc de fg')")),
299 ],
304 ],
300 [(u'a = %bc de \\', None),
305 [(u'a = %bc de \\', None),
301 (u'fg\\', None),
306 (u'fg\\', None),
302 (None, u_fmt("a = get_ipython().magic({u}'bc de fg')")),
307 (None, u_fmt("a = get_ipython().magic({u}'bc de fg')")),
303 ],
308 ],
304 ],
309 ],
305
310
306 assign_system =
311 assign_system =
307 [ [(u'a = !bc de \\', None),
312 [ [(u'a = !bc de \\', None),
308 (u'fg', u_fmt("a = get_ipython().getoutput({u}'bc de fg')")),
313 (u'fg', u_fmt("a = get_ipython().getoutput({u}'bc de fg')")),
309 ],
314 ],
310 [(u'a = !bc de \\', None),
315 [(u'a = !bc de \\', None),
311 (u'fg\\', None),
316 (u'fg\\', None),
312 (None, u_fmt("a = get_ipython().getoutput({u}'bc de fg')")),
317 (None, u_fmt("a = get_ipython().getoutput({u}'bc de fg')")),
313 ],
318 ],
314 ],
319 ],
315 )
320 )
316
321
317
322
318 def test_assign_system():
323 def test_assign_system():
319 tt.check_pairs(transform_and_reset(ipt.assign_from_system), syntax['assign_system'])
324 tt.check_pairs(transform_and_reset(ipt.assign_from_system), syntax['assign_system'])
320
325
321 def test_assign_magic():
326 def test_assign_magic():
322 tt.check_pairs(transform_and_reset(ipt.assign_from_magic), syntax['assign_magic'])
327 tt.check_pairs(transform_and_reset(ipt.assign_from_magic), syntax['assign_magic'])
323
328
324 def test_classic_prompt():
329 def test_classic_prompt():
325 tt.check_pairs(transform_and_reset(ipt.classic_prompt), syntax['classic_prompt'])
330 tt.check_pairs(transform_and_reset(ipt.classic_prompt), syntax['classic_prompt'])
326 for example in syntax_ml['classic_prompt']:
331 for example in syntax_ml['classic_prompt']:
327 transform_checker(example, ipt.classic_prompt)
332 transform_checker(example, ipt.classic_prompt)
328 for example in syntax_ml['multiline_datastructure_prompt']:
333 for example in syntax_ml['multiline_datastructure_prompt']:
329 transform_checker(example, ipt.classic_prompt)
334 transform_checker(example, ipt.classic_prompt)
330
335
331
336
332 def test_ipy_prompt():
337 def test_ipy_prompt():
333 tt.check_pairs(transform_and_reset(ipt.ipy_prompt), syntax['ipy_prompt'])
338 tt.check_pairs(transform_and_reset(ipt.ipy_prompt), syntax['ipy_prompt'])
334 for example in syntax_ml['ipy_prompt']:
339 for example in syntax_ml['ipy_prompt']:
335 transform_checker(example, ipt.ipy_prompt)
340 transform_checker(example, ipt.ipy_prompt)
336
341
337 def test_coding_cookie():
342 def test_coding_cookie():
338 tt.check_pairs(transform_and_reset(ipt.strip_encoding_cookie), syntax['strip_encoding_cookie'])
343 tt.check_pairs(transform_and_reset(ipt.strip_encoding_cookie), syntax['strip_encoding_cookie'])
339 for example in syntax_ml['strip_encoding_cookie']:
344 for example in syntax_ml['strip_encoding_cookie']:
340 transform_checker(example, ipt.strip_encoding_cookie)
345 transform_checker(example, ipt.strip_encoding_cookie)
341
346
342 def test_assemble_logical_lines():
347 def test_assemble_logical_lines():
343 tests = \
348 tests = \
344 [ [(u"a = \\", None),
349 [ [(u"a = \\", None),
345 (u"123", u"a = 123"),
350 (u"123", u"a = 123"),
346 ],
351 ],
347 [(u"a = \\", None), # Test resetting when within a multi-line string
352 [(u"a = \\", None), # Test resetting when within a multi-line string
348 (u"12 *\\", None),
353 (u"12 *\\", None),
349 (None, u"a = 12 *"),
354 (None, u"a = 12 *"),
350 ],
355 ],
351 [(u"# foo\\", u"# foo\\"), # Comments can't be continued like this
356 [(u"# foo\\", u"# foo\\"), # Comments can't be continued like this
352 ],
357 ],
353 ]
358 ]
354 for example in tests:
359 for example in tests:
355 transform_checker(example, ipt.assemble_logical_lines)
360 transform_checker(example, ipt.assemble_logical_lines)
356
361
357 def test_assemble_python_lines():
362 def test_assemble_python_lines():
358 tests = \
363 tests = \
359 [ [(u"a = '''", None),
364 [ [(u"a = '''", None),
360 (u"abc'''", u"a = '''\nabc'''"),
365 (u"abc'''", u"a = '''\nabc'''"),
361 ],
366 ],
362 [(u"a = '''", None), # Test resetting when within a multi-line string
367 [(u"a = '''", None), # Test resetting when within a multi-line string
363 (u"def", None),
368 (u"def", None),
364 (None, u"a = '''\ndef"),
369 (None, u"a = '''\ndef"),
365 ],
370 ],
366 [(u"a = [1,", None),
371 [(u"a = [1,", None),
367 (u"2]", u"a = [1,\n2]"),
372 (u"2]", u"a = [1,\n2]"),
368 ],
373 ],
369 [(u"a = [1,", None), # Test resetting when within a multi-line string
374 [(u"a = [1,", None), # Test resetting when within a multi-line string
370 (u"2,", None),
375 (u"2,", None),
371 (None, u"a = [1,\n2,"),
376 (None, u"a = [1,\n2,"),
372 ],
377 ],
373 ] + syntax_ml['multiline_datastructure']
378 ] + syntax_ml['multiline_datastructure']
374 for example in tests:
379 for example in tests:
375 transform_checker(example, ipt.assemble_python_lines)
380 transform_checker(example, ipt.assemble_python_lines)
376
381
377
382
378 def test_help_end():
383 def test_help_end():
379 tt.check_pairs(transform_and_reset(ipt.help_end), syntax['end_help'])
384 tt.check_pairs(transform_and_reset(ipt.help_end), syntax['end_help'])
380
385
381 def test_escaped_noesc():
386 def test_escaped_noesc():
382 tt.check_pairs(transform_and_reset(ipt.escaped_commands), syntax['escaped_noesc'])
387 tt.check_pairs(transform_and_reset(ipt.escaped_commands), syntax['escaped_noesc'])
383
388
384
389
385 def test_escaped_shell():
390 def test_escaped_shell():
386 tt.check_pairs(transform_and_reset(ipt.escaped_commands), syntax['escaped_shell'])
391 tt.check_pairs(transform_and_reset(ipt.escaped_commands), syntax['escaped_shell'])
387
392
388
393
389 def test_escaped_help():
394 def test_escaped_help():
390 tt.check_pairs(transform_and_reset(ipt.escaped_commands), syntax['escaped_help'])
395 tt.check_pairs(transform_and_reset(ipt.escaped_commands), syntax['escaped_help'])
391
396
392
397
393 def test_escaped_magic():
398 def test_escaped_magic():
394 tt.check_pairs(transform_and_reset(ipt.escaped_commands), syntax['escaped_magic'])
399 tt.check_pairs(transform_and_reset(ipt.escaped_commands), syntax['escaped_magic'])
395
400
396
401
397 def test_escaped_quote():
402 def test_escaped_quote():
398 tt.check_pairs(transform_and_reset(ipt.escaped_commands), syntax['escaped_quote'])
403 tt.check_pairs(transform_and_reset(ipt.escaped_commands), syntax['escaped_quote'])
399
404
400
405
401 def test_escaped_quote2():
406 def test_escaped_quote2():
402 tt.check_pairs(transform_and_reset(ipt.escaped_commands), syntax['escaped_quote2'])
407 tt.check_pairs(transform_and_reset(ipt.escaped_commands), syntax['escaped_quote2'])
403
408
404
409
405 def test_escaped_paren():
410 def test_escaped_paren():
406 tt.check_pairs(transform_and_reset(ipt.escaped_commands), syntax['escaped_paren'])
411 tt.check_pairs(transform_and_reset(ipt.escaped_commands), syntax['escaped_paren'])
407
412
408
413
409 def test_cellmagic():
414 def test_cellmagic():
410 for example in syntax_ml['cellmagic']:
415 for example in syntax_ml['cellmagic']:
411 transform_checker(example, ipt.cellmagic)
416 transform_checker(example, ipt.cellmagic)
412
417
413 line_example = [(u'%%bar 123', None),
418 line_example = [(u'%%bar 123', None),
414 (u'hello', None),
419 (u'hello', None),
415 (u'' , u_fmt("get_ipython().run_cell_magic({u}'bar', {u}'123', {u}'hello')")),
420 (u'' , u_fmt("get_ipython().run_cell_magic({u}'bar', {u}'123', {u}'hello')")),
416 ]
421 ]
417 transform_checker(line_example, ipt.cellmagic, end_on_blank_line=True)
422 transform_checker(line_example, ipt.cellmagic, end_on_blank_line=True)
418
423
419 def test_has_comment():
424 def test_has_comment():
420 tests = [('text', False),
425 tests = [('text', False),
421 ('text #comment', True),
426 ('text #comment', True),
422 ('text #comment\n', True),
427 ('text #comment\n', True),
423 ('#comment', True),
428 ('#comment', True),
424 ('#comment\n', True),
429 ('#comment\n', True),
425 ('a = "#string"', False),
430 ('a = "#string"', False),
426 ('a = "#string" # comment', True),
431 ('a = "#string" # comment', True),
427 ('a #comment not "string"', True),
432 ('a #comment not "string"', True),
428 ]
433 ]
429 tt.check_pairs(ipt.has_comment, tests)
434 tt.check_pairs(ipt.has_comment, tests)
430
435
431 @ipt.TokenInputTransformer.wrap
436 @ipt.TokenInputTransformer.wrap
432 def decistmt(tokens):
437 def decistmt(tokens):
433 """Substitute Decimals for floats in a string of statements.
438 """Substitute Decimals for floats in a string of statements.
434
439
435 Based on an example from the tokenize module docs.
440 Based on an example from the tokenize module docs.
436 """
441 """
437 result = []
442 result = []
438 for toknum, tokval, _, _, _ in tokens:
443 for toknum, tokval, _, _, _ in tokens:
439 if toknum == tokenize.NUMBER and '.' in tokval: # replace NUMBER tokens
444 if toknum == tokenize.NUMBER and '.' in tokval: # replace NUMBER tokens
440 for newtok in [
445 for newtok in [
441 (tokenize.NAME, 'Decimal'),
446 (tokenize.NAME, 'Decimal'),
442 (tokenize.OP, '('),
447 (tokenize.OP, '('),
443 (tokenize.STRING, repr(tokval)),
448 (tokenize.STRING, repr(tokval)),
444 (tokenize.OP, ')')
449 (tokenize.OP, ')')
445 ]:
450 ]:
446 yield newtok
451 yield newtok
447 else:
452 else:
448 yield (toknum, tokval)
453 yield (toknum, tokval)
449
454
450
455
451
456
452 def test_token_input_transformer():
457 def test_token_input_transformer():
453 tests = [(u'1.2', u_fmt(u"Decimal ({u}'1.2')")),
458 tests = [(u'1.2', u_fmt(u"Decimal ({u}'1.2')")),
454 (u'"1.2"', u'"1.2"'),
459 (u'"1.2"', u'"1.2"'),
455 ]
460 ]
456 tt.check_pairs(transform_and_reset(decistmt), tests)
461 tt.check_pairs(transform_and_reset(decistmt), tests)
457 ml_tests = \
462 ml_tests = \
458 [ [(u"a = 1.2; b = '''x", None),
463 [ [(u"a = 1.2; b = '''x", None),
459 (u"y'''", u_fmt(u"a =Decimal ({u}'1.2');b ='''x\ny'''")),
464 (u"y'''", u_fmt(u"a =Decimal ({u}'1.2');b ='''x\ny'''")),
460 ],
465 ],
461 [(u"a = [1.2,", None),
466 [(u"a = [1.2,", None),
462 (u"3]", u_fmt(u"a =[Decimal ({u}'1.2'),\n3 ]")),
467 (u"3]", u_fmt(u"a =[Decimal ({u}'1.2'),\n3 ]")),
463 ],
468 ],
464 [(u"a = '''foo", None), # Test resetting when within a multi-line string
469 [(u"a = '''foo", None), # Test resetting when within a multi-line string
465 (u"bar", None),
470 (u"bar", None),
466 (None, u"a = '''foo\nbar"),
471 (None, u"a = '''foo\nbar"),
467 ],
472 ],
468 ]
473 ]
469 for example in ml_tests:
474 for example in ml_tests:
470 transform_checker(example, decistmt)
475 transform_checker(example, decistmt)
General Comments 0
You need to be logged in to leave comments. Login now