Show More
@@ -1,448 +1,451 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 | [('>>> def f(x):', 'def f(x):'), |
|
189 | [('>>> def f(x):', 'def f(x):'), | |
190 | ('...', ''), |
|
190 | ('...', ''), | |
191 | ('... return x', ' return x'), |
|
191 | ('... return x', ' return x'), | |
192 | ], |
|
192 | ], | |
193 | ], |
|
193 | ], | |
194 |
|
194 | |||
195 | ipy_prompt = |
|
195 | ipy_prompt = | |
196 | [ [('In [24]: for i in range(10):','for i in range(10):'), |
|
196 | [ [('In [24]: for i in range(10):','for i in range(10):'), | |
197 | (' ....: print i',' print i'), |
|
197 | (' ....: print i',' print i'), | |
198 | (' ....: ', ''), |
|
198 | (' ....: ', ''), | |
199 | ], |
|
199 | ], | |
200 | [('In [2]: a="""','a="""'), |
|
200 | [('In [2]: a="""','a="""'), | |
201 | (' ...: 123"""','123"""'), |
|
201 | (' ...: 123"""','123"""'), | |
202 | ], |
|
202 | ], | |
203 | [('a="""','a="""'), |
|
203 | [('a="""','a="""'), | |
204 | (' ...: 123','123'), |
|
204 | (' ...: 123','123'), | |
205 | (' ...: 456"""','456"""'), |
|
205 | (' ...: 456"""','456"""'), | |
206 | ], |
|
206 | ], | |
207 | [('a="""','a="""'), |
|
207 | [('a="""','a="""'), | |
208 | ('123','123'), |
|
208 | ('123','123'), | |
209 | (' ...: 456"""',' ...: 456"""'), |
|
209 | (' ...: 456"""',' ...: 456"""'), | |
210 | ], |
|
210 | ], | |
211 | ], |
|
211 | ], | |
212 |
|
212 | |||
213 | strip_encoding_cookie = |
|
213 | strip_encoding_cookie = | |
214 | [ |
|
214 | [ | |
215 | [ |
|
215 | [ | |
216 | ('# -*- coding: utf-8 -*-', ''), |
|
216 | ('# -*- coding: utf-8 -*-', ''), | |
217 | ('foo', 'foo'), |
|
217 | ('foo', 'foo'), | |
218 | ], |
|
218 | ], | |
219 | [ |
|
219 | [ | |
220 | ('#!/usr/bin/env python', '#!/usr/bin/env python'), |
|
220 | ('#!/usr/bin/env python', '#!/usr/bin/env python'), | |
221 | ('# -*- coding: latin-1 -*-', ''), |
|
221 | ('# -*- coding: latin-1 -*-', ''), | |
222 | # only the first-two lines |
|
222 | # only the first-two lines | |
223 | ('# -*- coding: latin-1 -*-', '# -*- coding: latin-1 -*-'), |
|
223 | ('# -*- coding: latin-1 -*-', '# -*- coding: latin-1 -*-'), | |
224 | ], |
|
224 | ], | |
225 | ], |
|
225 | ], | |
226 |
|
226 | |||
227 | multiline_datastructure_prompt = |
|
227 | multiline_datastructure_prompt = | |
228 | [ [('>>> a = [1,','a = [1,'), |
|
228 | [ [('>>> a = [1,','a = [1,'), | |
229 | ('... 2]','2]'), |
|
229 | ('... 2]','2]'), | |
230 | ], |
|
230 | ], | |
231 | ], |
|
231 | ], | |
232 |
|
232 | |||
233 | multiline_datastructure = |
|
233 | multiline_datastructure = | |
234 | [ [('b = ("%s"', None), |
|
234 | [ [('b = ("%s"', None), | |
235 | ('# comment', None), |
|
235 | ('# comment', None), | |
236 | ('%foo )', 'b = ("%s"\n# comment\n%foo )'), |
|
236 | ('%foo )', 'b = ("%s"\n# comment\n%foo )'), | |
237 | ], |
|
237 | ], | |
238 | ], |
|
238 | ], | |
239 |
|
239 | |||
240 | leading_indent = |
|
240 | leading_indent = | |
241 | [ [(' print "hi"','print "hi"'), |
|
241 | [ [(' print "hi"','print "hi"'), | |
242 | ], |
|
242 | ], | |
243 | [(' for a in range(5):','for a in range(5):'), |
|
243 | [(' for a in range(5):','for a in range(5):'), | |
244 | (' a*2',' a*2'), |
|
244 | (' a*2',' a*2'), | |
245 | ], |
|
245 | ], | |
246 | [(' a="""','a="""'), |
|
246 | [(' a="""','a="""'), | |
247 | (' 123"""','123"""'), |
|
247 | (' 123"""','123"""'), | |
248 | ], |
|
248 | ], | |
249 | [('a="""','a="""'), |
|
249 | [('a="""','a="""'), | |
250 | (' 123"""',' 123"""'), |
|
250 | (' 123"""',' 123"""'), | |
251 | ], |
|
251 | ], | |
252 | ], |
|
252 | ], | |
253 |
|
253 | |||
254 | cellmagic = |
|
254 | cellmagic = | |
255 | [ [(u'%%foo a', None), |
|
255 | [ [(u'%%foo a', None), | |
256 | (None, u_fmt("get_ipython().run_cell_magic({u}'foo', {u}'a', {u}'')")), |
|
256 | (None, u_fmt("get_ipython().run_cell_magic({u}'foo', {u}'a', {u}'')")), | |
257 | ], |
|
257 | ], | |
258 | [(u'%%bar 123', None), |
|
258 | [(u'%%bar 123', None), | |
259 | (u'hello', None), |
|
259 | (u'hello', None), | |
260 | (None , u_fmt("get_ipython().run_cell_magic({u}'bar', {u}'123', {u}'hello')")), |
|
260 | (None , u_fmt("get_ipython().run_cell_magic({u}'bar', {u}'123', {u}'hello')")), | |
261 | ], |
|
261 | ], | |
|
262 | [(u'a=5', 'a=5'), | |||
|
263 | (u'%%cellmagic', '%%cellmagic'), | |||
|
264 | ], | |||
262 | ], |
|
265 | ], | |
263 |
|
266 | |||
264 | escaped = |
|
267 | escaped = | |
265 | [ [('%abc def \\', None), |
|
268 | [ [('%abc def \\', None), | |
266 | ('ghi', u_fmt("get_ipython().magic({u}'abc def ghi')")), |
|
269 | ('ghi', u_fmt("get_ipython().magic({u}'abc def ghi')")), | |
267 | ], |
|
270 | ], | |
268 | [('%abc def \\', None), |
|
271 | [('%abc def \\', None), | |
269 | ('ghi\\', None), |
|
272 | ('ghi\\', None), | |
270 | (None, u_fmt("get_ipython().magic({u}'abc def ghi')")), |
|
273 | (None, u_fmt("get_ipython().magic({u}'abc def ghi')")), | |
271 | ], |
|
274 | ], | |
272 | ], |
|
275 | ], | |
273 |
|
276 | |||
274 | assign_magic = |
|
277 | assign_magic = | |
275 | [ [(u'a = %bc de \\', None), |
|
278 | [ [(u'a = %bc de \\', None), | |
276 | (u'fg', u_fmt("a = get_ipython().magic({u}'bc de fg')")), |
|
279 | (u'fg', u_fmt("a = get_ipython().magic({u}'bc de fg')")), | |
277 | ], |
|
280 | ], | |
278 | [(u'a = %bc de \\', None), |
|
281 | [(u'a = %bc de \\', None), | |
279 | (u'fg\\', None), |
|
282 | (u'fg\\', None), | |
280 | (None, u_fmt("a = get_ipython().magic({u}'bc de fg')")), |
|
283 | (None, u_fmt("a = get_ipython().magic({u}'bc de fg')")), | |
281 | ], |
|
284 | ], | |
282 | ], |
|
285 | ], | |
283 |
|
286 | |||
284 | assign_system = |
|
287 | assign_system = | |
285 | [ [(u'a = !bc de \\', None), |
|
288 | [ [(u'a = !bc de \\', None), | |
286 | (u'fg', u_fmt("a = get_ipython().getoutput({u}'bc de fg')")), |
|
289 | (u'fg', u_fmt("a = get_ipython().getoutput({u}'bc de fg')")), | |
287 | ], |
|
290 | ], | |
288 | [(u'a = !bc de \\', None), |
|
291 | [(u'a = !bc de \\', None), | |
289 | (u'fg\\', None), |
|
292 | (u'fg\\', None), | |
290 | (None, u_fmt("a = get_ipython().getoutput({u}'bc de fg')")), |
|
293 | (None, u_fmt("a = get_ipython().getoutput({u}'bc de fg')")), | |
291 | ], |
|
294 | ], | |
292 | ], |
|
295 | ], | |
293 | ) |
|
296 | ) | |
294 |
|
297 | |||
295 |
|
298 | |||
296 | def test_assign_system(): |
|
299 | def test_assign_system(): | |
297 | tt.check_pairs(transform_and_reset(ipt.assign_from_system), syntax['assign_system']) |
|
300 | tt.check_pairs(transform_and_reset(ipt.assign_from_system), syntax['assign_system']) | |
298 |
|
301 | |||
299 | def test_assign_magic(): |
|
302 | def test_assign_magic(): | |
300 | tt.check_pairs(transform_and_reset(ipt.assign_from_magic), syntax['assign_magic']) |
|
303 | tt.check_pairs(transform_and_reset(ipt.assign_from_magic), syntax['assign_magic']) | |
301 |
|
304 | |||
302 | def test_classic_prompt(): |
|
305 | def test_classic_prompt(): | |
303 | tt.check_pairs(transform_and_reset(ipt.classic_prompt), syntax['classic_prompt']) |
|
306 | tt.check_pairs(transform_and_reset(ipt.classic_prompt), syntax['classic_prompt']) | |
304 | for example in syntax_ml['classic_prompt']: |
|
307 | for example in syntax_ml['classic_prompt']: | |
305 | transform_checker(example, ipt.classic_prompt) |
|
308 | transform_checker(example, ipt.classic_prompt) | |
306 | for example in syntax_ml['multiline_datastructure_prompt']: |
|
309 | for example in syntax_ml['multiline_datastructure_prompt']: | |
307 | transform_checker(example, ipt.classic_prompt) |
|
310 | transform_checker(example, ipt.classic_prompt) | |
308 |
|
311 | |||
309 |
|
312 | |||
310 | def test_ipy_prompt(): |
|
313 | def test_ipy_prompt(): | |
311 | tt.check_pairs(transform_and_reset(ipt.ipy_prompt), syntax['ipy_prompt']) |
|
314 | tt.check_pairs(transform_and_reset(ipt.ipy_prompt), syntax['ipy_prompt']) | |
312 | for example in syntax_ml['ipy_prompt']: |
|
315 | for example in syntax_ml['ipy_prompt']: | |
313 | transform_checker(example, ipt.ipy_prompt) |
|
316 | transform_checker(example, ipt.ipy_prompt) | |
314 |
|
317 | |||
315 | def test_coding_cookie(): |
|
318 | def test_coding_cookie(): | |
316 | tt.check_pairs(transform_and_reset(ipt.strip_encoding_cookie), syntax['strip_encoding_cookie']) |
|
319 | tt.check_pairs(transform_and_reset(ipt.strip_encoding_cookie), syntax['strip_encoding_cookie']) | |
317 | for example in syntax_ml['strip_encoding_cookie']: |
|
320 | for example in syntax_ml['strip_encoding_cookie']: | |
318 | transform_checker(example, ipt.strip_encoding_cookie) |
|
321 | transform_checker(example, ipt.strip_encoding_cookie) | |
319 |
|
322 | |||
320 | def test_assemble_logical_lines(): |
|
323 | def test_assemble_logical_lines(): | |
321 | tests = \ |
|
324 | tests = \ | |
322 | [ [(u"a = \\", None), |
|
325 | [ [(u"a = \\", None), | |
323 | (u"123", u"a = 123"), |
|
326 | (u"123", u"a = 123"), | |
324 | ], |
|
327 | ], | |
325 | [(u"a = \\", None), # Test resetting when within a multi-line string |
|
328 | [(u"a = \\", None), # Test resetting when within a multi-line string | |
326 | (u"12 *\\", None), |
|
329 | (u"12 *\\", None), | |
327 | (None, u"a = 12 *"), |
|
330 | (None, u"a = 12 *"), | |
328 | ], |
|
331 | ], | |
329 | [(u"# foo\\", u"# foo\\"), # Comments can't be continued like this |
|
332 | [(u"# foo\\", u"# foo\\"), # Comments can't be continued like this | |
330 | ], |
|
333 | ], | |
331 | ] |
|
334 | ] | |
332 | for example in tests: |
|
335 | for example in tests: | |
333 | transform_checker(example, ipt.assemble_logical_lines) |
|
336 | transform_checker(example, ipt.assemble_logical_lines) | |
334 |
|
337 | |||
335 | def test_assemble_python_lines(): |
|
338 | def test_assemble_python_lines(): | |
336 | tests = \ |
|
339 | tests = \ | |
337 | [ [(u"a = '''", None), |
|
340 | [ [(u"a = '''", None), | |
338 | (u"abc'''", u"a = '''\nabc'''"), |
|
341 | (u"abc'''", u"a = '''\nabc'''"), | |
339 | ], |
|
342 | ], | |
340 | [(u"a = '''", None), # Test resetting when within a multi-line string |
|
343 | [(u"a = '''", None), # Test resetting when within a multi-line string | |
341 | (u"def", None), |
|
344 | (u"def", None), | |
342 | (None, u"a = '''\ndef"), |
|
345 | (None, u"a = '''\ndef"), | |
343 | ], |
|
346 | ], | |
344 | [(u"a = [1,", None), |
|
347 | [(u"a = [1,", None), | |
345 | (u"2]", u"a = [1,\n2]"), |
|
348 | (u"2]", u"a = [1,\n2]"), | |
346 | ], |
|
349 | ], | |
347 | [(u"a = [1,", None), # Test resetting when within a multi-line string |
|
350 | [(u"a = [1,", None), # Test resetting when within a multi-line string | |
348 | (u"2,", None), |
|
351 | (u"2,", None), | |
349 | (None, u"a = [1,\n2,"), |
|
352 | (None, u"a = [1,\n2,"), | |
350 | ], |
|
353 | ], | |
351 | ] + syntax_ml['multiline_datastructure'] |
|
354 | ] + syntax_ml['multiline_datastructure'] | |
352 | for example in tests: |
|
355 | for example in tests: | |
353 | transform_checker(example, ipt.assemble_python_lines) |
|
356 | transform_checker(example, ipt.assemble_python_lines) | |
354 |
|
357 | |||
355 |
|
358 | |||
356 | def test_help_end(): |
|
359 | def test_help_end(): | |
357 | tt.check_pairs(transform_and_reset(ipt.help_end), syntax['end_help']) |
|
360 | tt.check_pairs(transform_and_reset(ipt.help_end), syntax['end_help']) | |
358 |
|
361 | |||
359 | def test_escaped_noesc(): |
|
362 | def test_escaped_noesc(): | |
360 | tt.check_pairs(transform_and_reset(ipt.escaped_commands), syntax['escaped_noesc']) |
|
363 | tt.check_pairs(transform_and_reset(ipt.escaped_commands), syntax['escaped_noesc']) | |
361 |
|
364 | |||
362 |
|
365 | |||
363 | def test_escaped_shell(): |
|
366 | def test_escaped_shell(): | |
364 | tt.check_pairs(transform_and_reset(ipt.escaped_commands), syntax['escaped_shell']) |
|
367 | tt.check_pairs(transform_and_reset(ipt.escaped_commands), syntax['escaped_shell']) | |
365 |
|
368 | |||
366 |
|
369 | |||
367 | def test_escaped_help(): |
|
370 | def test_escaped_help(): | |
368 | tt.check_pairs(transform_and_reset(ipt.escaped_commands), syntax['escaped_help']) |
|
371 | tt.check_pairs(transform_and_reset(ipt.escaped_commands), syntax['escaped_help']) | |
369 |
|
372 | |||
370 |
|
373 | |||
371 | def test_escaped_magic(): |
|
374 | def test_escaped_magic(): | |
372 | tt.check_pairs(transform_and_reset(ipt.escaped_commands), syntax['escaped_magic']) |
|
375 | tt.check_pairs(transform_and_reset(ipt.escaped_commands), syntax['escaped_magic']) | |
373 |
|
376 | |||
374 |
|
377 | |||
375 | def test_escaped_quote(): |
|
378 | def test_escaped_quote(): | |
376 | tt.check_pairs(transform_and_reset(ipt.escaped_commands), syntax['escaped_quote']) |
|
379 | tt.check_pairs(transform_and_reset(ipt.escaped_commands), syntax['escaped_quote']) | |
377 |
|
380 | |||
378 |
|
381 | |||
379 | def test_escaped_quote2(): |
|
382 | def test_escaped_quote2(): | |
380 | tt.check_pairs(transform_and_reset(ipt.escaped_commands), syntax['escaped_quote2']) |
|
383 | tt.check_pairs(transform_and_reset(ipt.escaped_commands), syntax['escaped_quote2']) | |
381 |
|
384 | |||
382 |
|
385 | |||
383 | def test_escaped_paren(): |
|
386 | def test_escaped_paren(): | |
384 | tt.check_pairs(transform_and_reset(ipt.escaped_commands), syntax['escaped_paren']) |
|
387 | tt.check_pairs(transform_and_reset(ipt.escaped_commands), syntax['escaped_paren']) | |
385 |
|
388 | |||
386 |
|
389 | |||
387 | def test_cellmagic(): |
|
390 | def test_cellmagic(): | |
388 | for example in syntax_ml['cellmagic']: |
|
391 | for example in syntax_ml['cellmagic']: | |
389 | transform_checker(example, ipt.cellmagic) |
|
392 | transform_checker(example, ipt.cellmagic) | |
390 |
|
393 | |||
391 | line_example = [(u'%%bar 123', None), |
|
394 | line_example = [(u'%%bar 123', None), | |
392 | (u'hello', None), |
|
395 | (u'hello', None), | |
393 | (u'' , u_fmt("get_ipython().run_cell_magic({u}'bar', {u}'123', {u}'hello')")), |
|
396 | (u'' , u_fmt("get_ipython().run_cell_magic({u}'bar', {u}'123', {u}'hello')")), | |
394 | ] |
|
397 | ] | |
395 | transform_checker(line_example, ipt.cellmagic, end_on_blank_line=True) |
|
398 | transform_checker(line_example, ipt.cellmagic, end_on_blank_line=True) | |
396 |
|
399 | |||
397 | def test_has_comment(): |
|
400 | def test_has_comment(): | |
398 | tests = [('text', False), |
|
401 | tests = [('text', False), | |
399 | ('text #comment', True), |
|
402 | ('text #comment', True), | |
400 | ('text #comment\n', True), |
|
403 | ('text #comment\n', True), | |
401 | ('#comment', True), |
|
404 | ('#comment', True), | |
402 | ('#comment\n', True), |
|
405 | ('#comment\n', True), | |
403 | ('a = "#string"', False), |
|
406 | ('a = "#string"', False), | |
404 | ('a = "#string" # comment', True), |
|
407 | ('a = "#string" # comment', True), | |
405 | ('a #comment not "string"', True), |
|
408 | ('a #comment not "string"', True), | |
406 | ] |
|
409 | ] | |
407 | tt.check_pairs(ipt.has_comment, tests) |
|
410 | tt.check_pairs(ipt.has_comment, tests) | |
408 |
|
411 | |||
409 | @ipt.TokenInputTransformer.wrap |
|
412 | @ipt.TokenInputTransformer.wrap | |
410 | def decistmt(tokens): |
|
413 | def decistmt(tokens): | |
411 | """Substitute Decimals for floats in a string of statements. |
|
414 | """Substitute Decimals for floats in a string of statements. | |
412 |
|
415 | |||
413 | Based on an example from the tokenize module docs. |
|
416 | Based on an example from the tokenize module docs. | |
414 | """ |
|
417 | """ | |
415 | result = [] |
|
418 | result = [] | |
416 | for toknum, tokval, _, _, _ in tokens: |
|
419 | for toknum, tokval, _, _, _ in tokens: | |
417 | if toknum == tokenize.NUMBER and '.' in tokval: # replace NUMBER tokens |
|
420 | if toknum == tokenize.NUMBER and '.' in tokval: # replace NUMBER tokens | |
418 | for newtok in [ |
|
421 | for newtok in [ | |
419 | (tokenize.NAME, 'Decimal'), |
|
422 | (tokenize.NAME, 'Decimal'), | |
420 | (tokenize.OP, '('), |
|
423 | (tokenize.OP, '('), | |
421 | (tokenize.STRING, repr(tokval)), |
|
424 | (tokenize.STRING, repr(tokval)), | |
422 | (tokenize.OP, ')') |
|
425 | (tokenize.OP, ')') | |
423 | ]: |
|
426 | ]: | |
424 | yield newtok |
|
427 | yield newtok | |
425 | else: |
|
428 | else: | |
426 | yield (toknum, tokval) |
|
429 | yield (toknum, tokval) | |
427 |
|
430 | |||
428 |
|
431 | |||
429 |
|
432 | |||
430 | def test_token_input_transformer(): |
|
433 | def test_token_input_transformer(): | |
431 | tests = [(u'1.2', u_fmt(u"Decimal ({u}'1.2')")), |
|
434 | tests = [(u'1.2', u_fmt(u"Decimal ({u}'1.2')")), | |
432 | (u'"1.2"', u'"1.2"'), |
|
435 | (u'"1.2"', u'"1.2"'), | |
433 | ] |
|
436 | ] | |
434 | tt.check_pairs(transform_and_reset(decistmt), tests) |
|
437 | tt.check_pairs(transform_and_reset(decistmt), tests) | |
435 | ml_tests = \ |
|
438 | ml_tests = \ | |
436 | [ [(u"a = 1.2; b = '''x", None), |
|
439 | [ [(u"a = 1.2; b = '''x", None), | |
437 | (u"y'''", u_fmt(u"a =Decimal ({u}'1.2');b ='''x\ny'''")), |
|
440 | (u"y'''", u_fmt(u"a =Decimal ({u}'1.2');b ='''x\ny'''")), | |
438 | ], |
|
441 | ], | |
439 | [(u"a = [1.2,", None), |
|
442 | [(u"a = [1.2,", None), | |
440 | (u"3]", u_fmt(u"a =[Decimal ({u}'1.2'),\n3 ]")), |
|
443 | (u"3]", u_fmt(u"a =[Decimal ({u}'1.2'),\n3 ]")), | |
441 | ], |
|
444 | ], | |
442 | [(u"a = '''foo", None), # Test resetting when within a multi-line string |
|
445 | [(u"a = '''foo", None), # Test resetting when within a multi-line string | |
443 | (u"bar", None), |
|
446 | (u"bar", None), | |
444 | (None, u"a = '''foo\nbar"), |
|
447 | (None, u"a = '''foo\nbar"), | |
445 | ], |
|
448 | ], | |
446 | ] |
|
449 | ] | |
447 | for example in ml_tests: |
|
450 | for example in ml_tests: | |
448 | transform_checker(example, decistmt) |
|
451 | transform_checker(example, decistmt) |
General Comments 0
You need to be logged in to leave comments.
Login now