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