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