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