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