##// END OF EJS Templates
add extra blank lines on the test to proof they are ignored
Martín Gaitán -
Show More
@@ -1,932 +1,932 b''
1 1 # -*- coding: utf-8 -*-
2 2 """Tests for various magic functions.
3 3
4 4 Needs to be run by nose (to make ipython session available).
5 5 """
6 6 from __future__ import absolute_import
7 7
8 8 #-----------------------------------------------------------------------------
9 9 # Imports
10 10 #-----------------------------------------------------------------------------
11 11
12 12 import io
13 13 import os
14 14 import sys
15 15 from StringIO import StringIO
16 16 from unittest import TestCase
17 17
18 18 try:
19 19 from importlib import invalidate_caches # Required from Python 3.3
20 20 except ImportError:
21 21 def invalidate_caches():
22 22 pass
23 23
24 24 import nose.tools as nt
25 25
26 26 from IPython.core import magic
27 27 from IPython.core.magic import (Magics, magics_class, line_magic,
28 28 cell_magic, line_cell_magic,
29 29 register_line_magic, register_cell_magic,
30 30 register_line_cell_magic)
31 31 from IPython.core.magics import execution, script, code
32 32 from IPython.nbformat.v3.tests.nbexamples import nb0
33 33 from IPython.nbformat import current
34 34 from IPython.testing import decorators as dec
35 35 from IPython.testing import tools as tt
36 36 from IPython.utils import py3compat
37 37 from IPython.utils.io import capture_output
38 38 from IPython.utils.tempdir import TemporaryDirectory
39 39 from IPython.utils.process import find_cmd
40 40
41 41 #-----------------------------------------------------------------------------
42 42 # Test functions begin
43 43 #-----------------------------------------------------------------------------
44 44
45 45 @magic.magics_class
46 46 class DummyMagics(magic.Magics): pass
47 47
48 48 def test_extract_code_ranges():
49 49 instr = "1 3 5-6 7-9 10:15 17: :10 10- -13 :"
50 50 expected = [(0, 1),
51 51 (2, 3),
52 52 (4, 6),
53 53 (6, 9),
54 54 (9, 14),
55 55 (16, None),
56 56 (None, 9),
57 57 (9, None),
58 58 (None, 13),
59 59 (None, None)]
60 60 actual = list(code.extract_code_ranges(instr))
61 61 nt.assert_equal(actual, expected)
62 62
63 63
64 64 def test_extract_symbols():
65 source = """import foo\na = 10\ndef b():\n return 42\nclass A: pass\n"""
65 source = """import foo\na = 10\ndef b():\n return 42\n\n\nclass A: pass\n\n\n"""
66 66 symbols_args = ["a", "b", "A", "A,b", "A,a", "z"]
67 67 expected = [[],
68 68 ["def b():\n return 42\n"],
69 69 ["class A: pass\n"],
70 70 ["class A: pass\n", "def b():\n return 42\n"],
71 71 ["class A: pass\n"],
72 72 []]
73 73 for symbols, exp in zip(symbols_args, expected):
74 74 nt.assert_equal(code.extract_symbols(source, symbols), exp)
75 75
76 76
77 77 def test_extract_symbols_ignores_non_python_code():
78 78 source = ("=begin A Ruby program :)=end\n"
79 79 "def hello\n"
80 80 "puts 'Hello world'\n"
81 81 "end")
82 82 nt.assert_equal(code.extract_symbols(source, "hello"), [])
83 83
84 84
85 85 def test_rehashx():
86 86 # clear up everything
87 87 _ip = get_ipython()
88 88 _ip.alias_manager.clear_aliases()
89 89 del _ip.db['syscmdlist']
90 90
91 91 _ip.magic('rehashx')
92 92 # Practically ALL ipython development systems will have more than 10 aliases
93 93
94 94 nt.assert_true(len(_ip.alias_manager.aliases) > 10)
95 95 for name, cmd in _ip.alias_manager.aliases:
96 96 # we must strip dots from alias names
97 97 nt.assert_not_in('.', name)
98 98
99 99 # rehashx must fill up syscmdlist
100 100 scoms = _ip.db['syscmdlist']
101 101 nt.assert_true(len(scoms) > 10)
102 102
103 103
104 104 def test_magic_parse_options():
105 105 """Test that we don't mangle paths when parsing magic options."""
106 106 ip = get_ipython()
107 107 path = 'c:\\x'
108 108 m = DummyMagics(ip)
109 109 opts = m.parse_options('-f %s' % path,'f:')[0]
110 110 # argv splitting is os-dependent
111 111 if os.name == 'posix':
112 112 expected = 'c:x'
113 113 else:
114 114 expected = path
115 115 nt.assert_equal(opts['f'], expected)
116 116
117 117 def test_magic_parse_long_options():
118 118 """Magic.parse_options can handle --foo=bar long options"""
119 119 ip = get_ipython()
120 120 m = DummyMagics(ip)
121 121 opts, _ = m.parse_options('--foo --bar=bubble', 'a', 'foo', 'bar=')
122 122 nt.assert_in('foo', opts)
123 123 nt.assert_in('bar', opts)
124 124 nt.assert_equal(opts['bar'], "bubble")
125 125
126 126
127 127 @dec.skip_without('sqlite3')
128 128 def doctest_hist_f():
129 129 """Test %hist -f with temporary filename.
130 130
131 131 In [9]: import tempfile
132 132
133 133 In [10]: tfile = tempfile.mktemp('.py','tmp-ipython-')
134 134
135 135 In [11]: %hist -nl -f $tfile 3
136 136
137 137 In [13]: import os; os.unlink(tfile)
138 138 """
139 139
140 140
141 141 @dec.skip_without('sqlite3')
142 142 def doctest_hist_r():
143 143 """Test %hist -r
144 144
145 145 XXX - This test is not recording the output correctly. For some reason, in
146 146 testing mode the raw history isn't getting populated. No idea why.
147 147 Disabling the output checking for now, though at least we do run it.
148 148
149 149 In [1]: 'hist' in _ip.lsmagic()
150 150 Out[1]: True
151 151
152 152 In [2]: x=1
153 153
154 154 In [3]: %hist -rl 2
155 155 x=1 # random
156 156 %hist -r 2
157 157 """
158 158
159 159
160 160 @dec.skip_without('sqlite3')
161 161 def doctest_hist_op():
162 162 """Test %hist -op
163 163
164 164 In [1]: class b(float):
165 165 ...: pass
166 166 ...:
167 167
168 168 In [2]: class s(object):
169 169 ...: def __str__(self):
170 170 ...: return 's'
171 171 ...:
172 172
173 173 In [3]:
174 174
175 175 In [4]: class r(b):
176 176 ...: def __repr__(self):
177 177 ...: return 'r'
178 178 ...:
179 179
180 180 In [5]: class sr(s,r): pass
181 181 ...:
182 182
183 183 In [6]:
184 184
185 185 In [7]: bb=b()
186 186
187 187 In [8]: ss=s()
188 188
189 189 In [9]: rr=r()
190 190
191 191 In [10]: ssrr=sr()
192 192
193 193 In [11]: 4.5
194 194 Out[11]: 4.5
195 195
196 196 In [12]: str(ss)
197 197 Out[12]: 's'
198 198
199 199 In [13]:
200 200
201 201 In [14]: %hist -op
202 202 >>> class b:
203 203 ... pass
204 204 ...
205 205 >>> class s(b):
206 206 ... def __str__(self):
207 207 ... return 's'
208 208 ...
209 209 >>>
210 210 >>> class r(b):
211 211 ... def __repr__(self):
212 212 ... return 'r'
213 213 ...
214 214 >>> class sr(s,r): pass
215 215 >>>
216 216 >>> bb=b()
217 217 >>> ss=s()
218 218 >>> rr=r()
219 219 >>> ssrr=sr()
220 220 >>> 4.5
221 221 4.5
222 222 >>> str(ss)
223 223 's'
224 224 >>>
225 225 """
226 226
227 227
228 228 @dec.skip_without('sqlite3')
229 229 def test_macro():
230 230 ip = get_ipython()
231 231 ip.history_manager.reset() # Clear any existing history.
232 232 cmds = ["a=1", "def b():\n return a**2", "print(a,b())"]
233 233 for i, cmd in enumerate(cmds, start=1):
234 234 ip.history_manager.store_inputs(i, cmd)
235 235 ip.magic("macro test 1-3")
236 236 nt.assert_equal(ip.user_ns["test"].value, "\n".join(cmds)+"\n")
237 237
238 238 # List macros
239 239 nt.assert_in("test", ip.magic("macro"))
240 240
241 241
242 242 @dec.skip_without('sqlite3')
243 243 def test_macro_run():
244 244 """Test that we can run a multi-line macro successfully."""
245 245 ip = get_ipython()
246 246 ip.history_manager.reset()
247 247 cmds = ["a=10", "a+=1", py3compat.doctest_refactor_print("print a"),
248 248 "%macro test 2-3"]
249 249 for cmd in cmds:
250 250 ip.run_cell(cmd, store_history=True)
251 251 nt.assert_equal(ip.user_ns["test"].value,
252 252 py3compat.doctest_refactor_print("a+=1\nprint a\n"))
253 253 with tt.AssertPrints("12"):
254 254 ip.run_cell("test")
255 255 with tt.AssertPrints("13"):
256 256 ip.run_cell("test")
257 257
258 258
259 259 def test_magic_magic():
260 260 """Test %magic"""
261 261 ip = get_ipython()
262 262 with capture_output() as captured:
263 263 ip.magic("magic")
264 264
265 265 stdout = captured.stdout
266 266 nt.assert_in('%magic', stdout)
267 267 nt.assert_in('IPython', stdout)
268 268 nt.assert_in('Available', stdout)
269 269
270 270
271 271 @dec.skipif_not_numpy
272 272 def test_numpy_reset_array_undec():
273 273 "Test '%reset array' functionality"
274 274 _ip.ex('import numpy as np')
275 275 _ip.ex('a = np.empty(2)')
276 276 nt.assert_in('a', _ip.user_ns)
277 277 _ip.magic('reset -f array')
278 278 nt.assert_not_in('a', _ip.user_ns)
279 279
280 280 def test_reset_out():
281 281 "Test '%reset out' magic"
282 282 _ip.run_cell("parrot = 'dead'", store_history=True)
283 283 # test '%reset -f out', make an Out prompt
284 284 _ip.run_cell("parrot", store_history=True)
285 285 nt.assert_true('dead' in [_ip.user_ns[x] for x in '_','__','___'])
286 286 _ip.magic('reset -f out')
287 287 nt.assert_false('dead' in [_ip.user_ns[x] for x in '_','__','___'])
288 288 nt.assert_equal(len(_ip.user_ns['Out']), 0)
289 289
290 290 def test_reset_in():
291 291 "Test '%reset in' magic"
292 292 # test '%reset -f in'
293 293 _ip.run_cell("parrot", store_history=True)
294 294 nt.assert_true('parrot' in [_ip.user_ns[x] for x in '_i','_ii','_iii'])
295 295 _ip.magic('%reset -f in')
296 296 nt.assert_false('parrot' in [_ip.user_ns[x] for x in '_i','_ii','_iii'])
297 297 nt.assert_equal(len(set(_ip.user_ns['In'])), 1)
298 298
299 299 def test_reset_dhist():
300 300 "Test '%reset dhist' magic"
301 301 _ip.run_cell("tmp = [d for d in _dh]") # copy before clearing
302 302 _ip.magic('cd ' + os.path.dirname(nt.__file__))
303 303 _ip.magic('cd -')
304 304 nt.assert_true(len(_ip.user_ns['_dh']) > 0)
305 305 _ip.magic('reset -f dhist')
306 306 nt.assert_equal(len(_ip.user_ns['_dh']), 0)
307 307 _ip.run_cell("_dh = [d for d in tmp]") #restore
308 308
309 309 def test_reset_in_length():
310 310 "Test that '%reset in' preserves In[] length"
311 311 _ip.run_cell("print 'foo'")
312 312 _ip.run_cell("reset -f in")
313 313 nt.assert_equal(len(_ip.user_ns['In']), _ip.displayhook.prompt_count+1)
314 314
315 315 def test_tb_syntaxerror():
316 316 """test %tb after a SyntaxError"""
317 317 ip = get_ipython()
318 318 ip.run_cell("for")
319 319
320 320 # trap and validate stdout
321 321 save_stdout = sys.stdout
322 322 try:
323 323 sys.stdout = StringIO()
324 324 ip.run_cell("%tb")
325 325 out = sys.stdout.getvalue()
326 326 finally:
327 327 sys.stdout = save_stdout
328 328 # trim output, and only check the last line
329 329 last_line = out.rstrip().splitlines()[-1].strip()
330 330 nt.assert_equal(last_line, "SyntaxError: invalid syntax")
331 331
332 332
333 333 def test_time():
334 334 ip = get_ipython()
335 335
336 336 with tt.AssertPrints("Wall time: "):
337 337 ip.run_cell("%time None")
338 338
339 339 ip.run_cell("def f(kmjy):\n"
340 340 " %time print (2*kmjy)")
341 341
342 342 with tt.AssertPrints("Wall time: "):
343 343 with tt.AssertPrints("hihi", suppress=False):
344 344 ip.run_cell("f('hi')")
345 345
346 346
347 347 @dec.skip_win32
348 348 def test_time2():
349 349 ip = get_ipython()
350 350
351 351 with tt.AssertPrints("CPU times: user "):
352 352 ip.run_cell("%time None")
353 353
354 354 def test_time3():
355 355 """Erroneous magic function calls, issue gh-3334"""
356 356 ip = get_ipython()
357 357 ip.user_ns.pop('run', None)
358 358
359 359 with tt.AssertNotPrints("not found", channel='stderr'):
360 360 ip.run_cell("%%time\n"
361 361 "run = 0\n"
362 362 "run += 1")
363 363
364 364 def test_doctest_mode():
365 365 "Toggle doctest_mode twice, it should be a no-op and run without error"
366 366 _ip.magic('doctest_mode')
367 367 _ip.magic('doctest_mode')
368 368
369 369
370 370 def test_parse_options():
371 371 """Tests for basic options parsing in magics."""
372 372 # These are only the most minimal of tests, more should be added later. At
373 373 # the very least we check that basic text/unicode calls work OK.
374 374 m = DummyMagics(_ip)
375 375 nt.assert_equal(m.parse_options('foo', '')[1], 'foo')
376 376 nt.assert_equal(m.parse_options(u'foo', '')[1], u'foo')
377 377
378 378
379 379 def test_dirops():
380 380 """Test various directory handling operations."""
381 381 # curpath = lambda :os.path.splitdrive(os.getcwdu())[1].replace('\\','/')
382 382 curpath = os.getcwdu
383 383 startdir = os.getcwdu()
384 384 ipdir = os.path.realpath(_ip.ipython_dir)
385 385 try:
386 386 _ip.magic('cd "%s"' % ipdir)
387 387 nt.assert_equal(curpath(), ipdir)
388 388 _ip.magic('cd -')
389 389 nt.assert_equal(curpath(), startdir)
390 390 _ip.magic('pushd "%s"' % ipdir)
391 391 nt.assert_equal(curpath(), ipdir)
392 392 _ip.magic('popd')
393 393 nt.assert_equal(curpath(), startdir)
394 394 finally:
395 395 os.chdir(startdir)
396 396
397 397
398 398 def test_xmode():
399 399 # Calling xmode three times should be a no-op
400 400 xmode = _ip.InteractiveTB.mode
401 401 for i in range(3):
402 402 _ip.magic("xmode")
403 403 nt.assert_equal(_ip.InteractiveTB.mode, xmode)
404 404
405 405 def test_reset_hard():
406 406 monitor = []
407 407 class A(object):
408 408 def __del__(self):
409 409 monitor.append(1)
410 410 def __repr__(self):
411 411 return "<A instance>"
412 412
413 413 _ip.user_ns["a"] = A()
414 414 _ip.run_cell("a")
415 415
416 416 nt.assert_equal(monitor, [])
417 417 _ip.magic("reset -f")
418 418 nt.assert_equal(monitor, [1])
419 419
420 420 class TestXdel(tt.TempFileMixin):
421 421 def test_xdel(self):
422 422 """Test that references from %run are cleared by xdel."""
423 423 src = ("class A(object):\n"
424 424 " monitor = []\n"
425 425 " def __del__(self):\n"
426 426 " self.monitor.append(1)\n"
427 427 "a = A()\n")
428 428 self.mktmp(src)
429 429 # %run creates some hidden references...
430 430 _ip.magic("run %s" % self.fname)
431 431 # ... as does the displayhook.
432 432 _ip.run_cell("a")
433 433
434 434 monitor = _ip.user_ns["A"].monitor
435 435 nt.assert_equal(monitor, [])
436 436
437 437 _ip.magic("xdel a")
438 438
439 439 # Check that a's __del__ method has been called.
440 440 nt.assert_equal(monitor, [1])
441 441
442 442 def doctest_who():
443 443 """doctest for %who
444 444
445 445 In [1]: %reset -f
446 446
447 447 In [2]: alpha = 123
448 448
449 449 In [3]: beta = 'beta'
450 450
451 451 In [4]: %who int
452 452 alpha
453 453
454 454 In [5]: %who str
455 455 beta
456 456
457 457 In [6]: %whos
458 458 Variable Type Data/Info
459 459 ----------------------------
460 460 alpha int 123
461 461 beta str beta
462 462
463 463 In [7]: %who_ls
464 464 Out[7]: ['alpha', 'beta']
465 465 """
466 466
467 467 def test_whos():
468 468 """Check that whos is protected against objects where repr() fails."""
469 469 class A(object):
470 470 def __repr__(self):
471 471 raise Exception()
472 472 _ip.user_ns['a'] = A()
473 473 _ip.magic("whos")
474 474
475 475 @py3compat.u_format
476 476 def doctest_precision():
477 477 """doctest for %precision
478 478
479 479 In [1]: f = get_ipython().display_formatter.formatters['text/plain']
480 480
481 481 In [2]: %precision 5
482 482 Out[2]: {u}'%.5f'
483 483
484 484 In [3]: f.float_format
485 485 Out[3]: {u}'%.5f'
486 486
487 487 In [4]: %precision %e
488 488 Out[4]: {u}'%e'
489 489
490 490 In [5]: f(3.1415927)
491 491 Out[5]: {u}'3.141593e+00'
492 492 """
493 493
494 494 def test_psearch():
495 495 with tt.AssertPrints("dict.fromkeys"):
496 496 _ip.run_cell("dict.fr*?")
497 497
498 498 def test_timeit_shlex():
499 499 """test shlex issues with timeit (#1109)"""
500 500 _ip.ex("def f(*a,**kw): pass")
501 501 _ip.magic('timeit -n1 "this is a bug".count(" ")')
502 502 _ip.magic('timeit -r1 -n1 f(" ", 1)')
503 503 _ip.magic('timeit -r1 -n1 f(" ", 1, " ", 2, " ")')
504 504 _ip.magic('timeit -r1 -n1 ("a " + "b")')
505 505 _ip.magic('timeit -r1 -n1 f("a " + "b")')
506 506 _ip.magic('timeit -r1 -n1 f("a " + "b ")')
507 507
508 508
509 509 def test_timeit_arguments():
510 510 "Test valid timeit arguments, should not cause SyntaxError (GH #1269)"
511 511 _ip.magic("timeit ('#')")
512 512
513 513
514 514 def test_timeit_special_syntax():
515 515 "Test %%timeit with IPython special syntax"
516 516 @register_line_magic
517 517 def lmagic(line):
518 518 ip = get_ipython()
519 519 ip.user_ns['lmagic_out'] = line
520 520
521 521 # line mode test
522 522 _ip.run_line_magic('timeit', '-n1 -r1 %lmagic my line')
523 523 nt.assert_equal(_ip.user_ns['lmagic_out'], 'my line')
524 524 # cell mode test
525 525 _ip.run_cell_magic('timeit', '-n1 -r1', '%lmagic my line2')
526 526 nt.assert_equal(_ip.user_ns['lmagic_out'], 'my line2')
527 527
528 528 def test_timeit_return():
529 529 """
530 530 test wether timeit -o return object
531 531 """
532 532
533 533 res = _ip.run_line_magic('timeit','-n10 -r10 -o 1')
534 534 assert(res is not None)
535 535
536 536 def test_timeit_quiet():
537 537 """
538 538 test quiet option of timeit magic
539 539 """
540 540 with tt.AssertNotPrints("loops"):
541 541 _ip.run_cell("%timeit -n1 -r1 -q 1")
542 542
543 543 @dec.skipif(execution.profile is None)
544 544 def test_prun_special_syntax():
545 545 "Test %%prun with IPython special syntax"
546 546 @register_line_magic
547 547 def lmagic(line):
548 548 ip = get_ipython()
549 549 ip.user_ns['lmagic_out'] = line
550 550
551 551 # line mode test
552 552 _ip.run_line_magic('prun', '-q %lmagic my line')
553 553 nt.assert_equal(_ip.user_ns['lmagic_out'], 'my line')
554 554 # cell mode test
555 555 _ip.run_cell_magic('prun', '-q', '%lmagic my line2')
556 556 nt.assert_equal(_ip.user_ns['lmagic_out'], 'my line2')
557 557
558 558 @dec.skipif(execution.profile is None)
559 559 def test_prun_quotes():
560 560 "Test that prun does not clobber string escapes (GH #1302)"
561 561 _ip.magic(r"prun -q x = '\t'")
562 562 nt.assert_equal(_ip.user_ns['x'], '\t')
563 563
564 564 def test_extension():
565 565 tmpdir = TemporaryDirectory()
566 566 orig_ipython_dir = _ip.ipython_dir
567 567 try:
568 568 _ip.ipython_dir = tmpdir.name
569 569 nt.assert_raises(ImportError, _ip.magic, "load_ext daft_extension")
570 570 url = os.path.join(os.path.dirname(__file__), "daft_extension.py")
571 571 _ip.magic("install_ext %s" % url)
572 572 _ip.user_ns.pop('arq', None)
573 573 invalidate_caches() # Clear import caches
574 574 _ip.magic("load_ext daft_extension")
575 575 nt.assert_equal(_ip.user_ns['arq'], 185)
576 576 _ip.magic("unload_ext daft_extension")
577 577 assert 'arq' not in _ip.user_ns
578 578 finally:
579 579 _ip.ipython_dir = orig_ipython_dir
580 580 tmpdir.cleanup()
581 581
582 582 def test_notebook_export_json():
583 583 with TemporaryDirectory() as td:
584 584 outfile = os.path.join(td, "nb.ipynb")
585 585 _ip.ex(py3compat.u_format(u"u = {u}'héllo'"))
586 586 _ip.magic("notebook -e %s" % outfile)
587 587
588 588 def test_notebook_export_py():
589 589 with TemporaryDirectory() as td:
590 590 outfile = os.path.join(td, "nb.py")
591 591 _ip.ex(py3compat.u_format(u"u = {u}'héllo'"))
592 592 _ip.magic("notebook -e %s" % outfile)
593 593
594 594 def test_notebook_reformat_py():
595 595 with TemporaryDirectory() as td:
596 596 infile = os.path.join(td, "nb.ipynb")
597 597 with io.open(infile, 'w', encoding='utf-8') as f:
598 598 current.write(nb0, f, 'json')
599 599
600 600 _ip.ex(py3compat.u_format(u"u = {u}'héllo'"))
601 601 _ip.magic("notebook -f py %s" % infile)
602 602
603 603 def test_notebook_reformat_json():
604 604 with TemporaryDirectory() as td:
605 605 infile = os.path.join(td, "nb.py")
606 606 with io.open(infile, 'w', encoding='utf-8') as f:
607 607 current.write(nb0, f, 'py')
608 608
609 609 _ip.ex(py3compat.u_format(u"u = {u}'héllo'"))
610 610 _ip.magic("notebook -f ipynb %s" % infile)
611 611 _ip.magic("notebook -f json %s" % infile)
612 612
613 613 def test_env():
614 614 env = _ip.magic("env")
615 615 assert isinstance(env, dict), type(env)
616 616
617 617
618 618 class CellMagicTestCase(TestCase):
619 619
620 620 def check_ident(self, magic):
621 621 # Manually called, we get the result
622 622 out = _ip.run_cell_magic(magic, 'a', 'b')
623 623 nt.assert_equal(out, ('a','b'))
624 624 # Via run_cell, it goes into the user's namespace via displayhook
625 625 _ip.run_cell('%%' + magic +' c\nd')
626 626 nt.assert_equal(_ip.user_ns['_'], ('c','d'))
627 627
628 628 def test_cell_magic_func_deco(self):
629 629 "Cell magic using simple decorator"
630 630 @register_cell_magic
631 631 def cellm(line, cell):
632 632 return line, cell
633 633
634 634 self.check_ident('cellm')
635 635
636 636 def test_cell_magic_reg(self):
637 637 "Cell magic manually registered"
638 638 def cellm(line, cell):
639 639 return line, cell
640 640
641 641 _ip.register_magic_function(cellm, 'cell', 'cellm2')
642 642 self.check_ident('cellm2')
643 643
644 644 def test_cell_magic_class(self):
645 645 "Cell magics declared via a class"
646 646 @magics_class
647 647 class MyMagics(Magics):
648 648
649 649 @cell_magic
650 650 def cellm3(self, line, cell):
651 651 return line, cell
652 652
653 653 _ip.register_magics(MyMagics)
654 654 self.check_ident('cellm3')
655 655
656 656 def test_cell_magic_class2(self):
657 657 "Cell magics declared via a class, #2"
658 658 @magics_class
659 659 class MyMagics2(Magics):
660 660
661 661 @cell_magic('cellm4')
662 662 def cellm33(self, line, cell):
663 663 return line, cell
664 664
665 665 _ip.register_magics(MyMagics2)
666 666 self.check_ident('cellm4')
667 667 # Check that nothing is registered as 'cellm33'
668 668 c33 = _ip.find_cell_magic('cellm33')
669 669 nt.assert_equal(c33, None)
670 670
671 671 def test_file():
672 672 """Basic %%file"""
673 673 ip = get_ipython()
674 674 with TemporaryDirectory() as td:
675 675 fname = os.path.join(td, 'file1')
676 676 ip.run_cell_magic("file", fname, u'\n'.join([
677 677 'line1',
678 678 'line2',
679 679 ]))
680 680 with open(fname) as f:
681 681 s = f.read()
682 682 nt.assert_in('line1\n', s)
683 683 nt.assert_in('line2', s)
684 684
685 685 def test_file_var_expand():
686 686 """%%file $filename"""
687 687 ip = get_ipython()
688 688 with TemporaryDirectory() as td:
689 689 fname = os.path.join(td, 'file1')
690 690 ip.user_ns['filename'] = fname
691 691 ip.run_cell_magic("file", '$filename', u'\n'.join([
692 692 'line1',
693 693 'line2',
694 694 ]))
695 695 with open(fname) as f:
696 696 s = f.read()
697 697 nt.assert_in('line1\n', s)
698 698 nt.assert_in('line2', s)
699 699
700 700 def test_file_unicode():
701 701 """%%file with unicode cell"""
702 702 ip = get_ipython()
703 703 with TemporaryDirectory() as td:
704 704 fname = os.path.join(td, 'file1')
705 705 ip.run_cell_magic("file", fname, u'\n'.join([
706 706 u'liné1',
707 707 u'liné2',
708 708 ]))
709 709 with io.open(fname, encoding='utf-8') as f:
710 710 s = f.read()
711 711 nt.assert_in(u'liné1\n', s)
712 712 nt.assert_in(u'liné2', s)
713 713
714 714 def test_file_amend():
715 715 """%%file -a amends files"""
716 716 ip = get_ipython()
717 717 with TemporaryDirectory() as td:
718 718 fname = os.path.join(td, 'file2')
719 719 ip.run_cell_magic("file", fname, u'\n'.join([
720 720 'line1',
721 721 'line2',
722 722 ]))
723 723 ip.run_cell_magic("file", "-a %s" % fname, u'\n'.join([
724 724 'line3',
725 725 'line4',
726 726 ]))
727 727 with open(fname) as f:
728 728 s = f.read()
729 729 nt.assert_in('line1\n', s)
730 730 nt.assert_in('line3\n', s)
731 731
732 732
733 733 def test_script_config():
734 734 ip = get_ipython()
735 735 ip.config.ScriptMagics.script_magics = ['whoda']
736 736 sm = script.ScriptMagics(shell=ip)
737 737 nt.assert_in('whoda', sm.magics['cell'])
738 738
739 739 @dec.skip_win32
740 740 def test_script_out():
741 741 ip = get_ipython()
742 742 ip.run_cell_magic("script", "--out output sh", "echo 'hi'")
743 743 nt.assert_equal(ip.user_ns['output'], 'hi\n')
744 744
745 745 @dec.skip_win32
746 746 def test_script_err():
747 747 ip = get_ipython()
748 748 ip.run_cell_magic("script", "--err error sh", "echo 'hello' >&2")
749 749 nt.assert_equal(ip.user_ns['error'], 'hello\n')
750 750
751 751 @dec.skip_win32
752 752 def test_script_out_err():
753 753 ip = get_ipython()
754 754 ip.run_cell_magic("script", "--out output --err error sh", "echo 'hi'\necho 'hello' >&2")
755 755 nt.assert_equal(ip.user_ns['output'], 'hi\n')
756 756 nt.assert_equal(ip.user_ns['error'], 'hello\n')
757 757
758 758 @dec.skip_win32
759 759 def test_script_bg_out():
760 760 ip = get_ipython()
761 761 ip.run_cell_magic("script", "--bg --out output sh", "echo 'hi'")
762 762 nt.assert_equal(ip.user_ns['output'].read(), b'hi\n')
763 763
764 764 @dec.skip_win32
765 765 def test_script_bg_err():
766 766 ip = get_ipython()
767 767 ip.run_cell_magic("script", "--bg --err error sh", "echo 'hello' >&2")
768 768 nt.assert_equal(ip.user_ns['error'].read(), b'hello\n')
769 769
770 770 @dec.skip_win32
771 771 def test_script_bg_out_err():
772 772 ip = get_ipython()
773 773 ip.run_cell_magic("script", "--bg --out output --err error sh", "echo 'hi'\necho 'hello' >&2")
774 774 nt.assert_equal(ip.user_ns['output'].read(), b'hi\n')
775 775 nt.assert_equal(ip.user_ns['error'].read(), b'hello\n')
776 776
777 777 def test_script_defaults():
778 778 ip = get_ipython()
779 779 for cmd in ['sh', 'bash', 'perl', 'ruby']:
780 780 try:
781 781 find_cmd(cmd)
782 782 except Exception:
783 783 pass
784 784 else:
785 785 nt.assert_in(cmd, ip.magics_manager.magics['cell'])
786 786
787 787
788 788 @magics_class
789 789 class FooFoo(Magics):
790 790 """class with both %foo and %%foo magics"""
791 791 @line_magic('foo')
792 792 def line_foo(self, line):
793 793 "I am line foo"
794 794 pass
795 795
796 796 @cell_magic("foo")
797 797 def cell_foo(self, line, cell):
798 798 "I am cell foo, not line foo"
799 799 pass
800 800
801 801 def test_line_cell_info():
802 802 """%%foo and %foo magics are distinguishable to inspect"""
803 803 ip = get_ipython()
804 804 ip.magics_manager.register(FooFoo)
805 805 oinfo = ip.object_inspect('foo')
806 806 nt.assert_true(oinfo['found'])
807 807 nt.assert_true(oinfo['ismagic'])
808 808
809 809 oinfo = ip.object_inspect('%%foo')
810 810 nt.assert_true(oinfo['found'])
811 811 nt.assert_true(oinfo['ismagic'])
812 812 nt.assert_equal(oinfo['docstring'], FooFoo.cell_foo.__doc__)
813 813
814 814 oinfo = ip.object_inspect('%foo')
815 815 nt.assert_true(oinfo['found'])
816 816 nt.assert_true(oinfo['ismagic'])
817 817 nt.assert_equal(oinfo['docstring'], FooFoo.line_foo.__doc__)
818 818
819 819 def test_multiple_magics():
820 820 ip = get_ipython()
821 821 foo1 = FooFoo(ip)
822 822 foo2 = FooFoo(ip)
823 823 mm = ip.magics_manager
824 824 mm.register(foo1)
825 825 nt.assert_true(mm.magics['line']['foo'].im_self is foo1)
826 826 mm.register(foo2)
827 827 nt.assert_true(mm.magics['line']['foo'].im_self is foo2)
828 828
829 829 def test_alias_magic():
830 830 """Test %alias_magic."""
831 831 ip = get_ipython()
832 832 mm = ip.magics_manager
833 833
834 834 # Basic operation: both cell and line magics are created, if possible.
835 835 ip.run_line_magic('alias_magic', 'timeit_alias timeit')
836 836 nt.assert_in('timeit_alias', mm.magics['line'])
837 837 nt.assert_in('timeit_alias', mm.magics['cell'])
838 838
839 839 # --cell is specified, line magic not created.
840 840 ip.run_line_magic('alias_magic', '--cell timeit_cell_alias timeit')
841 841 nt.assert_not_in('timeit_cell_alias', mm.magics['line'])
842 842 nt.assert_in('timeit_cell_alias', mm.magics['cell'])
843 843
844 844 # Test that line alias is created successfully.
845 845 ip.run_line_magic('alias_magic', '--line env_alias env')
846 846 nt.assert_equal(ip.run_line_magic('env', ''),
847 847 ip.run_line_magic('env_alias', ''))
848 848
849 849 def test_save():
850 850 """Test %save."""
851 851 ip = get_ipython()
852 852 ip.history_manager.reset() # Clear any existing history.
853 853 cmds = [u"a=1", u"def b():\n return a**2", u"print(a, b())"]
854 854 for i, cmd in enumerate(cmds, start=1):
855 855 ip.history_manager.store_inputs(i, cmd)
856 856 with TemporaryDirectory() as tmpdir:
857 857 file = os.path.join(tmpdir, "testsave.py")
858 858 ip.run_line_magic("save", "%s 1-10" % file)
859 859 with open(file) as f:
860 860 content = f.read()
861 861 nt.assert_equal(content.count(cmds[0]), 1)
862 862 nt.assert_in('coding: utf-8', content)
863 863 ip.run_line_magic("save", "-a %s 1-10" % file)
864 864 with open(file) as f:
865 865 content = f.read()
866 866 nt.assert_equal(content.count(cmds[0]), 2)
867 867 nt.assert_in('coding: utf-8', content)
868 868
869 869
870 870 def test_store():
871 871 """Test %store."""
872 872 ip = get_ipython()
873 873 ip.run_line_magic('load_ext', 'storemagic')
874 874
875 875 # make sure the storage is empty
876 876 ip.run_line_magic('store', '-z')
877 877 ip.user_ns['var'] = 42
878 878 ip.run_line_magic('store', 'var')
879 879 ip.user_ns['var'] = 39
880 880 ip.run_line_magic('store', '-r')
881 881 nt.assert_equal(ip.user_ns['var'], 42)
882 882
883 883 ip.run_line_magic('store', '-d var')
884 884 ip.user_ns['var'] = 39
885 885 ip.run_line_magic('store' , '-r')
886 886 nt.assert_equal(ip.user_ns['var'], 39)
887 887
888 888
889 889 def _run_edit_test(arg_s, exp_filename=None,
890 890 exp_lineno=-1,
891 891 exp_contents=None,
892 892 exp_is_temp=None):
893 893 ip = get_ipython()
894 894 M = code.CodeMagics(ip)
895 895 last_call = ['','']
896 896 opts,args = M.parse_options(arg_s,'prxn:')
897 897 filename, lineno, is_temp = M._find_edit_target(ip, args, opts, last_call)
898 898
899 899 if exp_filename is not None:
900 900 nt.assert_equal(exp_filename, filename)
901 901 if exp_contents is not None:
902 902 with io.open(filename, 'r') as f:
903 903 contents = f.read()
904 904 nt.assert_equal(exp_contents, contents)
905 905 if exp_lineno != -1:
906 906 nt.assert_equal(exp_lineno, lineno)
907 907 if exp_is_temp is not None:
908 908 nt.assert_equal(exp_is_temp, is_temp)
909 909
910 910
911 911 def test_edit_interactive():
912 912 """%edit on interactively defined objects"""
913 913 ip = get_ipython()
914 914 n = ip.execution_count
915 915 ip.run_cell(u"def foo(): return 1", store_history=True)
916 916
917 917 try:
918 918 _run_edit_test("foo")
919 919 except code.InteractivelyDefined as e:
920 920 nt.assert_equal(e.index, n)
921 921 else:
922 922 raise AssertionError("Should have raised InteractivelyDefined")
923 923
924 924
925 925 def test_edit_cell():
926 926 """%edit [cell id]"""
927 927 ip = get_ipython()
928 928
929 929 ip.run_cell(u"def foo(): return 1", store_history=True)
930 930
931 931 # test
932 932 _run_edit_test("1", exp_contents=ip.user_ns['In'][1], exp_is_temp=True)
General Comments 0
You need to be logged in to leave comments. Login now