##// END OF EJS Templates
Apply pygrade --36plus on IPython/core/tests/test_inputtransformer.py....
Matthias Bussonnier -
Show More
@@ -37,30 +37,30 b' def transform_checker(tests, transformer, **kwargs):'
37 37
38 38 syntax = \
39 39 dict(assign_system =
40 [(u'a =! ls', "a = get_ipython().getoutput('ls')"),
41 (u'b = !ls', "b = get_ipython().getoutput('ls')"),
42 (u'c= !ls', "c = get_ipython().getoutput('ls')"),
43 (u'd == !ls', u'd == !ls'), # Invalid syntax, but we leave == alone.
40 [('a =! ls', "a = get_ipython().getoutput('ls')"),
41 ('b = !ls', "b = get_ipython().getoutput('ls')"),
42 ('c= !ls', "c = get_ipython().getoutput('ls')"),
43 ('d == !ls', 'd == !ls'), # Invalid syntax, but we leave == alone.
44 44 ('x=1', 'x=1'), # normal input is unmodified
45 45 (' ',' '), # blank lines are kept intact
46 46 # Tuple unpacking
47 (u"a, b = !echo 'a\\nb'", u"a, b = get_ipython().getoutput(\"echo 'a\\\\nb'\")"),
48 (u"a,= !echo 'a'", u"a, = get_ipython().getoutput(\"echo 'a'\")"),
49 (u"a, *bc = !echo 'a\\nb\\nc'", u"a, *bc = get_ipython().getoutput(\"echo 'a\\\\nb\\\\nc'\")"),
47 ("a, b = !echo 'a\\nb'", "a, b = get_ipython().getoutput(\"echo 'a\\\\nb'\")"),
48 ("a,= !echo 'a'", "a, = get_ipython().getoutput(\"echo 'a'\")"),
49 ("a, *bc = !echo 'a\\nb\\nc'", "a, *bc = get_ipython().getoutput(\"echo 'a\\\\nb\\\\nc'\")"),
50 50 # Tuple unpacking with regular Python expressions, not our syntax.
51 (u"a, b = range(2)", u"a, b = range(2)"),
52 (u"a, = range(1)", u"a, = range(1)"),
53 (u"a, *bc = range(3)", u"a, *bc = range(3)"),
51 ("a, b = range(2)", "a, b = range(2)"),
52 ("a, = range(1)", "a, = range(1)"),
53 ("a, *bc = range(3)", "a, *bc = range(3)"),
54 54 ],
55 55
56 56 assign_magic =
57 [(u'a =% who', "a = get_ipython().run_line_magic('who', '')"),
58 (u'b = %who', "b = get_ipython().run_line_magic('who', '')"),
59 (u'c= %ls', "c = get_ipython().run_line_magic('ls', '')"),
60 (u'd == %ls', u'd == %ls'), # Invalid syntax, but we leave == alone.
57 [('a =% who', "a = get_ipython().run_line_magic('who', '')"),
58 ('b = %who', "b = get_ipython().run_line_magic('who', '')"),
59 ('c= %ls', "c = get_ipython().run_line_magic('ls', '')"),
60 ('d == %ls', 'd == %ls'), # Invalid syntax, but we leave == alone.
61 61 ('x=1', 'x=1'), # normal input is unmodified
62 62 (' ',' '), # blank lines are kept intact
63 (u"a, b = %foo", u"a, b = get_ipython().run_line_magic('foo', '')"),
63 ("a, b = %foo", "a, b = get_ipython().run_line_magic('foo', '')"),
64 64 ],
65 65
66 66 classic_prompt =
@@ -83,51 +83,51 b' syntax = \\'
83 83
84 84 # System calls
85 85 escaped_shell =
86 [ (u'!ls', "get_ipython().system('ls')"),
86 [ ('!ls', "get_ipython().system('ls')"),
87 87 # Double-escape shell, this means to capture the output of the
88 88 # subprocess and return it
89 (u'!!ls', "get_ipython().getoutput('ls')"),
89 ('!!ls', "get_ipython().getoutput('ls')"),
90 90 ],
91 91
92 92 # Help/object info
93 93 escaped_help =
94 [ (u'?', 'get_ipython().show_usage()'),
95 (u'?x1', "get_ipython().run_line_magic('pinfo', 'x1')"),
96 (u'??x2', "get_ipython().run_line_magic('pinfo2', 'x2')"),
97 (u'?a.*s', "get_ipython().run_line_magic('psearch', 'a.*s')"),
98 (u'?%hist1', "get_ipython().run_line_magic('pinfo', '%hist1')"),
99 (u'?%%hist2', "get_ipython().run_line_magic('pinfo', '%%hist2')"),
100 (u'?abc = qwe', "get_ipython().run_line_magic('pinfo', 'abc')"),
94 [ ('?', 'get_ipython().show_usage()'),
95 ('?x1', "get_ipython().run_line_magic('pinfo', 'x1')"),
96 ('??x2', "get_ipython().run_line_magic('pinfo2', 'x2')"),
97 ('?a.*s', "get_ipython().run_line_magic('psearch', 'a.*s')"),
98 ('?%hist1', "get_ipython().run_line_magic('pinfo', '%hist1')"),
99 ('?%%hist2', "get_ipython().run_line_magic('pinfo', '%%hist2')"),
100 ('?abc = qwe', "get_ipython().run_line_magic('pinfo', 'abc')"),
101 101 ],
102 102
103 103 end_help =
104 [ (u'x3?', "get_ipython().run_line_magic('pinfo', 'x3')"),
105 (u'x4??', "get_ipython().run_line_magic('pinfo2', 'x4')"),
106 (u'%hist1?', "get_ipython().run_line_magic('pinfo', '%hist1')"),
107 (u'%hist2??', "get_ipython().run_line_magic('pinfo2', '%hist2')"),
108 (u'%%hist3?', "get_ipython().run_line_magic('pinfo', '%%hist3')"),
109 (u'%%hist4??', "get_ipython().run_line_magic('pinfo2', '%%hist4')"),
110 (u'Ο€.foo?', "get_ipython().run_line_magic('pinfo', 'Ο€.foo')"),
111 (u'f*?', "get_ipython().run_line_magic('psearch', 'f*')"),
112 (u'ax.*aspe*?', "get_ipython().run_line_magic('psearch', 'ax.*aspe*')"),
113 (u'a = abc?', "get_ipython().set_next_input('a = abc');"
104 [ ('x3?', "get_ipython().run_line_magic('pinfo', 'x3')"),
105 ('x4??', "get_ipython().run_line_magic('pinfo2', 'x4')"),
106 ('%hist1?', "get_ipython().run_line_magic('pinfo', '%hist1')"),
107 ('%hist2??', "get_ipython().run_line_magic('pinfo2', '%hist2')"),
108 ('%%hist3?', "get_ipython().run_line_magic('pinfo', '%%hist3')"),
109 ('%%hist4??', "get_ipython().run_line_magic('pinfo2', '%%hist4')"),
110 ('Ο€.foo?', "get_ipython().run_line_magic('pinfo', 'Ο€.foo')"),
111 ('f*?', "get_ipython().run_line_magic('psearch', 'f*')"),
112 ('ax.*aspe*?', "get_ipython().run_line_magic('psearch', 'ax.*aspe*')"),
113 ('a = abc?', "get_ipython().set_next_input('a = abc');"
114 114 "get_ipython().run_line_magic('pinfo', 'abc')"),
115 (u'a = abc.qe??', "get_ipython().set_next_input('a = abc.qe');"
115 ('a = abc.qe??', "get_ipython().set_next_input('a = abc.qe');"
116 116 "get_ipython().run_line_magic('pinfo2', 'abc.qe')"),
117 (u'a = *.items?', "get_ipython().set_next_input('a = *.items');"
117 ('a = *.items?', "get_ipython().set_next_input('a = *.items');"
118 118 "get_ipython().run_line_magic('psearch', '*.items')"),
119 (u'plot(a?', "get_ipython().set_next_input('plot(a');"
119 ('plot(a?', "get_ipython().set_next_input('plot(a');"
120 120 "get_ipython().run_line_magic('pinfo', 'a')"),
121 (u'a*2 #comment?', 'a*2 #comment?'),
121 ('a*2 #comment?', 'a*2 #comment?'),
122 122 ],
123 123
124 124 # Explicit magic calls
125 125 escaped_magic =
126 [ (u'%cd', "get_ipython().run_line_magic('cd', '')"),
127 (u'%cd /home', "get_ipython().run_line_magic('cd', '/home')"),
126 [ ('%cd', "get_ipython().run_line_magic('cd', '')"),
127 ('%cd /home', "get_ipython().run_line_magic('cd', '/home')"),
128 128 # Backslashes need to be escaped.
129 (u'%cd C:\\User', "get_ipython().run_line_magic('cd', 'C:\\\\User')"),
130 (u' %magic', " get_ipython().run_line_magic('magic', '')"),
129 ('%cd C:\\User', "get_ipython().run_line_magic('cd', 'C:\\\\User')"),
130 (' %magic', " get_ipython().run_line_magic('magic', '')"),
131 131 ],
132 132
133 133 # Quoting with separate arguments
@@ -156,11 +156,11 b' syntax = \\'
156 156
157 157 # Check that we transform prompts before other transforms
158 158 mixed =
159 [ (u'In [1]: %lsmagic', "get_ipython().run_line_magic('lsmagic', '')"),
160 (u'>>> %lsmagic', "get_ipython().run_line_magic('lsmagic', '')"),
161 (u'In [2]: !ls', "get_ipython().system('ls')"),
162 (u'In [3]: abs?', "get_ipython().run_line_magic('pinfo', 'abs')"),
163 (u'In [4]: b = %who', "b = get_ipython().run_line_magic('who', '')"),
159 [ ('In [1]: %lsmagic', "get_ipython().run_line_magic('lsmagic', '')"),
160 ('>>> %lsmagic', "get_ipython().run_line_magic('lsmagic', '')"),
161 ('In [2]: !ls', "get_ipython().system('ls')"),
162 ('In [3]: abs?', "get_ipython().run_line_magic('pinfo', 'abs')"),
163 ('In [4]: b = %who', "b = get_ipython().run_line_magic('who', '')"),
164 164 ],
165 165 )
166 166
@@ -275,15 +275,15 b' syntax_ml = \\'
275 275 ],
276 276
277 277 cellmagic =
278 [ [(u'%%foo a', None),
278 [ [('%%foo a', None),
279 279 (None, "get_ipython().run_cell_magic('foo', 'a', '')"),
280 280 ],
281 [(u'%%bar 123', None),
282 (u'hello', None),
281 [('%%bar 123', None),
282 ('hello', None),
283 283 (None , "get_ipython().run_cell_magic('bar', '123', 'hello')"),
284 284 ],
285 [(u'a=5', 'a=5'),
286 (u'%%cellmagic', '%%cellmagic'),
285 [('a=5', 'a=5'),
286 ('%%cellmagic', '%%cellmagic'),
287 287 ],
288 288 ],
289 289
@@ -298,21 +298,21 b' syntax_ml = \\'
298 298 ],
299 299
300 300 assign_magic =
301 [ [(u'a = %bc de \\', None),
302 (u'fg', "a = get_ipython().run_line_magic('bc', 'de fg')"),
301 [ [('a = %bc de \\', None),
302 ('fg', "a = get_ipython().run_line_magic('bc', 'de fg')"),
303 303 ],
304 [(u'a = %bc de \\', None),
305 (u'fg\\', None),
304 [('a = %bc de \\', None),
305 ('fg\\', None),
306 306 (None, "a = get_ipython().run_line_magic('bc', 'de fg')"),
307 307 ],
308 308 ],
309 309
310 310 assign_system =
311 [ [(u'a = !bc de \\', None),
312 (u'fg', "a = get_ipython().getoutput('bc de fg')"),
311 [ [('a = !bc de \\', None),
312 ('fg', "a = get_ipython().getoutput('bc de fg')"),
313 313 ],
314 [(u'a = !bc de \\', None),
315 (u'fg\\', None),
314 [('a = !bc de \\', None),
315 ('fg\\', None),
316 316 (None, "a = get_ipython().getoutput('bc de fg')"),
317 317 ],
318 318 ],
@@ -335,8 +335,8 b' def test_classic_prompt():'
335 335 # Check that we don't transform the second line if the first is obviously
336 336 # IPython syntax
337 337 transform_checker([
338 (u'%foo', '%foo'),
339 (u'>>> bar', '>>> bar'),
338 ('%foo', '%foo'),
339 ('>>> bar', '>>> bar'),
340 340 ], ipt.classic_prompt)
341 341
342 342
@@ -347,20 +347,20 b' def test_ipy_prompt():'
347 347
348 348 # Check that we don't transform the second line if we're inside a cell magic
349 349 transform_checker([
350 (u'%%foo', '%%foo'),
351 (u'In [1]: bar', 'In [1]: bar'),
350 ('%%foo', '%%foo'),
351 ('In [1]: bar', 'In [1]: bar'),
352 352 ], ipt.ipy_prompt)
353 353
354 354 def test_assemble_logical_lines():
355 355 tests = \
356 [ [(u"a = \\", None),
357 (u"123", u"a = 123"),
356 [ [("a = \\", None),
357 ("123", "a = 123"),
358 358 ],
359 [(u"a = \\", None), # Test resetting when within a multi-line string
360 (u"12 *\\", None),
361 (None, u"a = 12 *"),
359 [("a = \\", None), # Test resetting when within a multi-line string
360 ("12 *\\", None),
361 (None, "a = 12 *"),
362 362 ],
363 [(u"# foo\\", u"# foo\\"), # Comments can't be continued like this
363 [("# foo\\", "# foo\\"), # Comments can't be continued like this
364 364 ],
365 365 ]
366 366 for example in tests:
@@ -368,24 +368,24 b' def test_assemble_logical_lines():'
368 368
369 369 def test_assemble_python_lines():
370 370 tests = \
371 [ [(u"a = '''", None),
372 (u"abc'''", u"a = '''\nabc'''"),
371 [ [("a = '''", None),
372 ("abc'''", "a = '''\nabc'''"),
373 373 ],
374 [(u"a = '''", None), # Test resetting when within a multi-line string
375 (u"def", None),
376 (None, u"a = '''\ndef"),
374 [("a = '''", None), # Test resetting when within a multi-line string
375 ("def", None),
376 (None, "a = '''\ndef"),
377 377 ],
378 [(u"a = [1,", None),
379 (u"2]", u"a = [1,\n2]"),
378 [("a = [1,", None),
379 ("2]", "a = [1,\n2]"),
380 380 ],
381 [(u"a = [1,", None), # Test resetting when within a multi-line string
382 (u"2,", None),
383 (None, u"a = [1,\n2,"),
381 [("a = [1,", None), # Test resetting when within a multi-line string
382 ("2,", None),
383 (None, "a = [1,\n2,"),
384 384 ],
385 [(u"a = '''", None), # Test line continuation within a multi-line string
386 (u"abc\\", None),
387 (u"def", None),
388 (u"'''", u"a = '''\nabc\\\ndef\n'''"),
385 [("a = '''", None), # Test line continuation within a multi-line string
386 ("abc\\", None),
387 ("def", None),
388 ("'''", "a = '''\nabc\\\ndef\n'''"),
389 389 ],
390 390 ] + syntax_ml['multiline_datastructure']
391 391 for example in tests:
@@ -427,9 +427,9 b' def test_cellmagic():'
427 427 for example in syntax_ml['cellmagic']:
428 428 transform_checker(example, ipt.cellmagic)
429 429
430 line_example = [(u'%%bar 123', None),
431 (u'hello', None),
432 (u'' , "get_ipython().run_cell_magic('bar', '123', 'hello')"),
430 line_example = [('%%bar 123', None),
431 ('hello', None),
432 ('' , "get_ipython().run_cell_magic('bar', '123', 'hello')"),
433 433 ]
434 434 transform_checker(line_example, ipt.cellmagic, end_on_blank_line=True)
435 435
@@ -454,33 +454,32 b' def decistmt(tokens):'
454 454 result = []
455 455 for toknum, tokval, _, _, _ in tokens:
456 456 if toknum == tokenize.NUMBER and '.' in tokval: # replace NUMBER tokens
457 for newtok in [
457 yield from [
458 458 (tokenize.NAME, 'Decimal'),
459 459 (tokenize.OP, '('),
460 460 (tokenize.STRING, repr(tokval)),
461 461 (tokenize.OP, ')')
462 ]:
463 yield newtok
462 ]
464 463 else:
465 464 yield (toknum, tokval)
466 465
467 466
468 467
469 468 def test_token_input_transformer():
470 tests = [(u'1.2', u"Decimal ('1.2')"),
471 (u'"1.2"', u'"1.2"'),
469 tests = [('1.2', "Decimal ('1.2')"),
470 ('"1.2"', '"1.2"'),
472 471 ]
473 472 tt.check_pairs(transform_and_reset(decistmt), tests)
474 473 ml_tests = \
475 [ [(u"a = 1.2; b = '''x", None),
476 (u"y'''", u"a =Decimal ('1.2');b ='''x\ny'''"),
474 [ [("a = 1.2; b = '''x", None),
475 ("y'''", "a =Decimal ('1.2');b ='''x\ny'''"),
477 476 ],
478 [(u"a = [1.2,", None),
479 (u"3]", u"a =[Decimal ('1.2'),\n3 ]"),
477 [("a = [1.2,", None),
478 ("3]", "a =[Decimal ('1.2'),\n3 ]"),
480 479 ],
481 [(u"a = '''foo", None), # Test resetting when within a multi-line string
482 (u"bar", None),
483 (None, u"a = '''foo\nbar"),
480 [("a = '''foo", None), # Test resetting when within a multi-line string
481 ("bar", None),
482 (None, "a = '''foo\nbar"),
484 483 ],
485 484 ]
486 485 for example in ml_tests:
General Comments 0
You need to be logged in to leave comments. Login now