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