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