##// END OF EJS Templates
add time test
Matthias Bussonnier -
Show More
@@ -1,1170 +1,1179 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 """Tests for various magic functions.
2 """Tests for various magic functions.
3
3
4 Needs to be run by nose (to make ipython session available).
4 Needs to be run by nose (to make ipython session available).
5 """
5 """
6
6
7 import io
7 import io
8 import os
8 import os
9 import re
9 import re
10 import sys
10 import sys
11 import warnings
11 import warnings
12 from textwrap import dedent
12 from textwrap import dedent
13 from unittest import TestCase
13 from unittest import TestCase
14 from importlib import invalidate_caches
14 from importlib import invalidate_caches
15 from io import StringIO
15 from io import StringIO
16
16
17 import nose.tools as nt
17 import nose.tools as nt
18
18
19 import shlex
19 import shlex
20
20
21 from IPython import get_ipython
21 from IPython import get_ipython
22 from IPython.core import magic
22 from IPython.core import magic
23 from IPython.core.error import UsageError
23 from IPython.core.error import UsageError
24 from IPython.core.magic import (Magics, magics_class, line_magic,
24 from IPython.core.magic import (Magics, magics_class, line_magic,
25 cell_magic,
25 cell_magic,
26 register_line_magic, register_cell_magic)
26 register_line_magic, register_cell_magic)
27 from IPython.core.magics import execution, script, code, logging, osm
27 from IPython.core.magics import execution, script, code, logging, osm
28 from IPython.testing import decorators as dec
28 from IPython.testing import decorators as dec
29 from IPython.testing import tools as tt
29 from IPython.testing import tools as tt
30 from IPython.utils.io import capture_output
30 from IPython.utils.io import capture_output
31 from IPython.utils.tempdir import (TemporaryDirectory,
31 from IPython.utils.tempdir import (TemporaryDirectory,
32 TemporaryWorkingDirectory)
32 TemporaryWorkingDirectory)
33 from IPython.utils.process import find_cmd
33 from IPython.utils.process import find_cmd
34
34
35
35
36 @magic.magics_class
36 @magic.magics_class
37 class DummyMagics(magic.Magics): pass
37 class DummyMagics(magic.Magics): pass
38
38
39 def test_extract_code_ranges():
39 def test_extract_code_ranges():
40 instr = "1 3 5-6 7-9 10:15 17: :10 10- -13 :"
40 instr = "1 3 5-6 7-9 10:15 17: :10 10- -13 :"
41 expected = [(0, 1),
41 expected = [(0, 1),
42 (2, 3),
42 (2, 3),
43 (4, 6),
43 (4, 6),
44 (6, 9),
44 (6, 9),
45 (9, 14),
45 (9, 14),
46 (16, None),
46 (16, None),
47 (None, 9),
47 (None, 9),
48 (9, None),
48 (9, None),
49 (None, 13),
49 (None, 13),
50 (None, None)]
50 (None, None)]
51 actual = list(code.extract_code_ranges(instr))
51 actual = list(code.extract_code_ranges(instr))
52 nt.assert_equal(actual, expected)
52 nt.assert_equal(actual, expected)
53
53
54 def test_extract_symbols():
54 def test_extract_symbols():
55 source = """import foo\na = 10\ndef b():\n return 42\n\n\nclass A: pass\n\n\n"""
55 source = """import foo\na = 10\ndef b():\n return 42\n\n\nclass A: pass\n\n\n"""
56 symbols_args = ["a", "b", "A", "A,b", "A,a", "z"]
56 symbols_args = ["a", "b", "A", "A,b", "A,a", "z"]
57 expected = [([], ['a']),
57 expected = [([], ['a']),
58 (["def b():\n return 42\n"], []),
58 (["def b():\n return 42\n"], []),
59 (["class A: pass\n"], []),
59 (["class A: pass\n"], []),
60 (["class A: pass\n", "def b():\n return 42\n"], []),
60 (["class A: pass\n", "def b():\n return 42\n"], []),
61 (["class A: pass\n"], ['a']),
61 (["class A: pass\n"], ['a']),
62 ([], ['z'])]
62 ([], ['z'])]
63 for symbols, exp in zip(symbols_args, expected):
63 for symbols, exp in zip(symbols_args, expected):
64 nt.assert_equal(code.extract_symbols(source, symbols), exp)
64 nt.assert_equal(code.extract_symbols(source, symbols), exp)
65
65
66
66
67 def test_extract_symbols_raises_exception_with_non_python_code():
67 def test_extract_symbols_raises_exception_with_non_python_code():
68 source = ("=begin A Ruby program :)=end\n"
68 source = ("=begin A Ruby program :)=end\n"
69 "def hello\n"
69 "def hello\n"
70 "puts 'Hello world'\n"
70 "puts 'Hello world'\n"
71 "end")
71 "end")
72 with nt.assert_raises(SyntaxError):
72 with nt.assert_raises(SyntaxError):
73 code.extract_symbols(source, "hello")
73 code.extract_symbols(source, "hello")
74
74
75
75
76 def test_magic_not_found():
76 def test_magic_not_found():
77 # magic not found raises UsageError
77 # magic not found raises UsageError
78 with nt.assert_raises(UsageError):
78 with nt.assert_raises(UsageError):
79 _ip.magic('doesntexist')
79 _ip.magic('doesntexist')
80
80
81 # ensure result isn't success when a magic isn't found
81 # ensure result isn't success when a magic isn't found
82 result = _ip.run_cell('%doesntexist')
82 result = _ip.run_cell('%doesntexist')
83 assert isinstance(result.error_in_exec, UsageError)
83 assert isinstance(result.error_in_exec, UsageError)
84
84
85
85
86 def test_cell_magic_not_found():
86 def test_cell_magic_not_found():
87 # magic not found raises UsageError
87 # magic not found raises UsageError
88 with nt.assert_raises(UsageError):
88 with nt.assert_raises(UsageError):
89 _ip.run_cell_magic('doesntexist', 'line', 'cell')
89 _ip.run_cell_magic('doesntexist', 'line', 'cell')
90
90
91 # ensure result isn't success when a magic isn't found
91 # ensure result isn't success when a magic isn't found
92 result = _ip.run_cell('%%doesntexist')
92 result = _ip.run_cell('%%doesntexist')
93 assert isinstance(result.error_in_exec, UsageError)
93 assert isinstance(result.error_in_exec, UsageError)
94
94
95
95
96 def test_magic_error_status():
96 def test_magic_error_status():
97 def fail(shell):
97 def fail(shell):
98 1/0
98 1/0
99 _ip.register_magic_function(fail)
99 _ip.register_magic_function(fail)
100 result = _ip.run_cell('%fail')
100 result = _ip.run_cell('%fail')
101 assert isinstance(result.error_in_exec, ZeroDivisionError)
101 assert isinstance(result.error_in_exec, ZeroDivisionError)
102
102
103
103
104 def test_config():
104 def test_config():
105 """ test that config magic does not raise
105 """ test that config magic does not raise
106 can happen if Configurable init is moved too early into
106 can happen if Configurable init is moved too early into
107 Magics.__init__ as then a Config object will be registered as a
107 Magics.__init__ as then a Config object will be registered as a
108 magic.
108 magic.
109 """
109 """
110 ## should not raise.
110 ## should not raise.
111 _ip.magic('config')
111 _ip.magic('config')
112
112
113 def test_config_available_configs():
113 def test_config_available_configs():
114 """ test that config magic prints available configs in unique and
114 """ test that config magic prints available configs in unique and
115 sorted order. """
115 sorted order. """
116 with capture_output() as captured:
116 with capture_output() as captured:
117 _ip.magic('config')
117 _ip.magic('config')
118
118
119 stdout = captured.stdout
119 stdout = captured.stdout
120 config_classes = stdout.strip().split('\n')[1:]
120 config_classes = stdout.strip().split('\n')[1:]
121 nt.assert_list_equal(config_classes, sorted(set(config_classes)))
121 nt.assert_list_equal(config_classes, sorted(set(config_classes)))
122
122
123 def test_config_print_class():
123 def test_config_print_class():
124 """ test that config with a classname prints the class's options. """
124 """ test that config with a classname prints the class's options. """
125 with capture_output() as captured:
125 with capture_output() as captured:
126 _ip.magic('config TerminalInteractiveShell')
126 _ip.magic('config TerminalInteractiveShell')
127
127
128 stdout = captured.stdout
128 stdout = captured.stdout
129 if not re.match("TerminalInteractiveShell.* options", stdout.splitlines()[0]):
129 if not re.match("TerminalInteractiveShell.* options", stdout.splitlines()[0]):
130 print(stdout)
130 print(stdout)
131 raise AssertionError("1st line of stdout not like "
131 raise AssertionError("1st line of stdout not like "
132 "'TerminalInteractiveShell.* options'")
132 "'TerminalInteractiveShell.* options'")
133
133
134 def test_rehashx():
134 def test_rehashx():
135 # clear up everything
135 # clear up everything
136 _ip.alias_manager.clear_aliases()
136 _ip.alias_manager.clear_aliases()
137 del _ip.db['syscmdlist']
137 del _ip.db['syscmdlist']
138
138
139 _ip.magic('rehashx')
139 _ip.magic('rehashx')
140 # Practically ALL ipython development systems will have more than 10 aliases
140 # Practically ALL ipython development systems will have more than 10 aliases
141
141
142 nt.assert_true(len(_ip.alias_manager.aliases) > 10)
142 nt.assert_true(len(_ip.alias_manager.aliases) > 10)
143 for name, cmd in _ip.alias_manager.aliases:
143 for name, cmd in _ip.alias_manager.aliases:
144 # we must strip dots from alias names
144 # we must strip dots from alias names
145 nt.assert_not_in('.', name)
145 nt.assert_not_in('.', name)
146
146
147 # rehashx must fill up syscmdlist
147 # rehashx must fill up syscmdlist
148 scoms = _ip.db['syscmdlist']
148 scoms = _ip.db['syscmdlist']
149 nt.assert_true(len(scoms) > 10)
149 nt.assert_true(len(scoms) > 10)
150
150
151
151
152
152
153 def test_magic_parse_options():
153 def test_magic_parse_options():
154 """Test that we don't mangle paths when parsing magic options."""
154 """Test that we don't mangle paths when parsing magic options."""
155 ip = get_ipython()
155 ip = get_ipython()
156 path = 'c:\\x'
156 path = 'c:\\x'
157 m = DummyMagics(ip)
157 m = DummyMagics(ip)
158 opts = m.parse_options('-f %s' % path,'f:')[0]
158 opts = m.parse_options('-f %s' % path,'f:')[0]
159 # argv splitting is os-dependent
159 # argv splitting is os-dependent
160 if os.name == 'posix':
160 if os.name == 'posix':
161 expected = 'c:x'
161 expected = 'c:x'
162 else:
162 else:
163 expected = path
163 expected = path
164 nt.assert_equal(opts['f'], expected)
164 nt.assert_equal(opts['f'], expected)
165
165
166 def test_magic_parse_long_options():
166 def test_magic_parse_long_options():
167 """Magic.parse_options can handle --foo=bar long options"""
167 """Magic.parse_options can handle --foo=bar long options"""
168 ip = get_ipython()
168 ip = get_ipython()
169 m = DummyMagics(ip)
169 m = DummyMagics(ip)
170 opts, _ = m.parse_options('--foo --bar=bubble', 'a', 'foo', 'bar=')
170 opts, _ = m.parse_options('--foo --bar=bubble', 'a', 'foo', 'bar=')
171 nt.assert_in('foo', opts)
171 nt.assert_in('foo', opts)
172 nt.assert_in('bar', opts)
172 nt.assert_in('bar', opts)
173 nt.assert_equal(opts['bar'], "bubble")
173 nt.assert_equal(opts['bar'], "bubble")
174
174
175
175
176 @dec.skip_without('sqlite3')
176 @dec.skip_without('sqlite3')
177 def doctest_hist_f():
177 def doctest_hist_f():
178 """Test %hist -f with temporary filename.
178 """Test %hist -f with temporary filename.
179
179
180 In [9]: import tempfile
180 In [9]: import tempfile
181
181
182 In [10]: tfile = tempfile.mktemp('.py','tmp-ipython-')
182 In [10]: tfile = tempfile.mktemp('.py','tmp-ipython-')
183
183
184 In [11]: %hist -nl -f $tfile 3
184 In [11]: %hist -nl -f $tfile 3
185
185
186 In [13]: import os; os.unlink(tfile)
186 In [13]: import os; os.unlink(tfile)
187 """
187 """
188
188
189
189
190 @dec.skip_without('sqlite3')
190 @dec.skip_without('sqlite3')
191 def doctest_hist_r():
191 def doctest_hist_r():
192 """Test %hist -r
192 """Test %hist -r
193
193
194 XXX - This test is not recording the output correctly. For some reason, in
194 XXX - This test is not recording the output correctly. For some reason, in
195 testing mode the raw history isn't getting populated. No idea why.
195 testing mode the raw history isn't getting populated. No idea why.
196 Disabling the output checking for now, though at least we do run it.
196 Disabling the output checking for now, though at least we do run it.
197
197
198 In [1]: 'hist' in _ip.lsmagic()
198 In [1]: 'hist' in _ip.lsmagic()
199 Out[1]: True
199 Out[1]: True
200
200
201 In [2]: x=1
201 In [2]: x=1
202
202
203 In [3]: %hist -rl 2
203 In [3]: %hist -rl 2
204 x=1 # random
204 x=1 # random
205 %hist -r 2
205 %hist -r 2
206 """
206 """
207
207
208
208
209 @dec.skip_without('sqlite3')
209 @dec.skip_without('sqlite3')
210 def doctest_hist_op():
210 def doctest_hist_op():
211 """Test %hist -op
211 """Test %hist -op
212
212
213 In [1]: class b(float):
213 In [1]: class b(float):
214 ...: pass
214 ...: pass
215 ...:
215 ...:
216
216
217 In [2]: class s(object):
217 In [2]: class s(object):
218 ...: def __str__(self):
218 ...: def __str__(self):
219 ...: return 's'
219 ...: return 's'
220 ...:
220 ...:
221
221
222 In [3]:
222 In [3]:
223
223
224 In [4]: class r(b):
224 In [4]: class r(b):
225 ...: def __repr__(self):
225 ...: def __repr__(self):
226 ...: return 'r'
226 ...: return 'r'
227 ...:
227 ...:
228
228
229 In [5]: class sr(s,r): pass
229 In [5]: class sr(s,r): pass
230 ...:
230 ...:
231
231
232 In [6]:
232 In [6]:
233
233
234 In [7]: bb=b()
234 In [7]: bb=b()
235
235
236 In [8]: ss=s()
236 In [8]: ss=s()
237
237
238 In [9]: rr=r()
238 In [9]: rr=r()
239
239
240 In [10]: ssrr=sr()
240 In [10]: ssrr=sr()
241
241
242 In [11]: 4.5
242 In [11]: 4.5
243 Out[11]: 4.5
243 Out[11]: 4.5
244
244
245 In [12]: str(ss)
245 In [12]: str(ss)
246 Out[12]: 's'
246 Out[12]: 's'
247
247
248 In [13]:
248 In [13]:
249
249
250 In [14]: %hist -op
250 In [14]: %hist -op
251 >>> class b:
251 >>> class b:
252 ... pass
252 ... pass
253 ...
253 ...
254 >>> class s(b):
254 >>> class s(b):
255 ... def __str__(self):
255 ... def __str__(self):
256 ... return 's'
256 ... return 's'
257 ...
257 ...
258 >>>
258 >>>
259 >>> class r(b):
259 >>> class r(b):
260 ... def __repr__(self):
260 ... def __repr__(self):
261 ... return 'r'
261 ... return 'r'
262 ...
262 ...
263 >>> class sr(s,r): pass
263 >>> class sr(s,r): pass
264 >>>
264 >>>
265 >>> bb=b()
265 >>> bb=b()
266 >>> ss=s()
266 >>> ss=s()
267 >>> rr=r()
267 >>> rr=r()
268 >>> ssrr=sr()
268 >>> ssrr=sr()
269 >>> 4.5
269 >>> 4.5
270 4.5
270 4.5
271 >>> str(ss)
271 >>> str(ss)
272 's'
272 's'
273 >>>
273 >>>
274 """
274 """
275
275
276 def test_hist_pof():
276 def test_hist_pof():
277 ip = get_ipython()
277 ip = get_ipython()
278 ip.run_cell(u"1+2", store_history=True)
278 ip.run_cell(u"1+2", store_history=True)
279 #raise Exception(ip.history_manager.session_number)
279 #raise Exception(ip.history_manager.session_number)
280 #raise Exception(list(ip.history_manager._get_range_session()))
280 #raise Exception(list(ip.history_manager._get_range_session()))
281 with TemporaryDirectory() as td:
281 with TemporaryDirectory() as td:
282 tf = os.path.join(td, 'hist.py')
282 tf = os.path.join(td, 'hist.py')
283 ip.run_line_magic('history', '-pof %s' % tf)
283 ip.run_line_magic('history', '-pof %s' % tf)
284 assert os.path.isfile(tf)
284 assert os.path.isfile(tf)
285
285
286
286
287 @dec.skip_without('sqlite3')
287 @dec.skip_without('sqlite3')
288 def test_macro():
288 def test_macro():
289 ip = get_ipython()
289 ip = get_ipython()
290 ip.history_manager.reset() # Clear any existing history.
290 ip.history_manager.reset() # Clear any existing history.
291 cmds = ["a=1", "def b():\n return a**2", "print(a,b())"]
291 cmds = ["a=1", "def b():\n return a**2", "print(a,b())"]
292 for i, cmd in enumerate(cmds, start=1):
292 for i, cmd in enumerate(cmds, start=1):
293 ip.history_manager.store_inputs(i, cmd)
293 ip.history_manager.store_inputs(i, cmd)
294 ip.magic("macro test 1-3")
294 ip.magic("macro test 1-3")
295 nt.assert_equal(ip.user_ns["test"].value, "\n".join(cmds)+"\n")
295 nt.assert_equal(ip.user_ns["test"].value, "\n".join(cmds)+"\n")
296
296
297 # List macros
297 # List macros
298 nt.assert_in("test", ip.magic("macro"))
298 nt.assert_in("test", ip.magic("macro"))
299
299
300
300
301 @dec.skip_without('sqlite3')
301 @dec.skip_without('sqlite3')
302 def test_macro_run():
302 def test_macro_run():
303 """Test that we can run a multi-line macro successfully."""
303 """Test that we can run a multi-line macro successfully."""
304 ip = get_ipython()
304 ip = get_ipython()
305 ip.history_manager.reset()
305 ip.history_manager.reset()
306 cmds = ["a=10", "a+=1", "print(a)", "%macro test 2-3"]
306 cmds = ["a=10", "a+=1", "print(a)", "%macro test 2-3"]
307 for cmd in cmds:
307 for cmd in cmds:
308 ip.run_cell(cmd, store_history=True)
308 ip.run_cell(cmd, store_history=True)
309 nt.assert_equal(ip.user_ns["test"].value, "a+=1\nprint(a)\n")
309 nt.assert_equal(ip.user_ns["test"].value, "a+=1\nprint(a)\n")
310 with tt.AssertPrints("12"):
310 with tt.AssertPrints("12"):
311 ip.run_cell("test")
311 ip.run_cell("test")
312 with tt.AssertPrints("13"):
312 with tt.AssertPrints("13"):
313 ip.run_cell("test")
313 ip.run_cell("test")
314
314
315
315
316 def test_magic_magic():
316 def test_magic_magic():
317 """Test %magic"""
317 """Test %magic"""
318 ip = get_ipython()
318 ip = get_ipython()
319 with capture_output() as captured:
319 with capture_output() as captured:
320 ip.magic("magic")
320 ip.magic("magic")
321
321
322 stdout = captured.stdout
322 stdout = captured.stdout
323 nt.assert_in('%magic', stdout)
323 nt.assert_in('%magic', stdout)
324 nt.assert_in('IPython', stdout)
324 nt.assert_in('IPython', stdout)
325 nt.assert_in('Available', stdout)
325 nt.assert_in('Available', stdout)
326
326
327
327
328 @dec.skipif_not_numpy
328 @dec.skipif_not_numpy
329 def test_numpy_reset_array_undec():
329 def test_numpy_reset_array_undec():
330 "Test '%reset array' functionality"
330 "Test '%reset array' functionality"
331 _ip.ex('import numpy as np')
331 _ip.ex('import numpy as np')
332 _ip.ex('a = np.empty(2)')
332 _ip.ex('a = np.empty(2)')
333 nt.assert_in('a', _ip.user_ns)
333 nt.assert_in('a', _ip.user_ns)
334 _ip.magic('reset -f array')
334 _ip.magic('reset -f array')
335 nt.assert_not_in('a', _ip.user_ns)
335 nt.assert_not_in('a', _ip.user_ns)
336
336
337 def test_reset_out():
337 def test_reset_out():
338 "Test '%reset out' magic"
338 "Test '%reset out' magic"
339 _ip.run_cell("parrot = 'dead'", store_history=True)
339 _ip.run_cell("parrot = 'dead'", store_history=True)
340 # test '%reset -f out', make an Out prompt
340 # test '%reset -f out', make an Out prompt
341 _ip.run_cell("parrot", store_history=True)
341 _ip.run_cell("parrot", store_history=True)
342 nt.assert_true('dead' in [_ip.user_ns[x] for x in ('_','__','___')])
342 nt.assert_true('dead' in [_ip.user_ns[x] for x in ('_','__','___')])
343 _ip.magic('reset -f out')
343 _ip.magic('reset -f out')
344 nt.assert_false('dead' in [_ip.user_ns[x] for x in ('_','__','___')])
344 nt.assert_false('dead' in [_ip.user_ns[x] for x in ('_','__','___')])
345 nt.assert_equal(len(_ip.user_ns['Out']), 0)
345 nt.assert_equal(len(_ip.user_ns['Out']), 0)
346
346
347 def test_reset_in():
347 def test_reset_in():
348 "Test '%reset in' magic"
348 "Test '%reset in' magic"
349 # test '%reset -f in'
349 # test '%reset -f in'
350 _ip.run_cell("parrot", store_history=True)
350 _ip.run_cell("parrot", store_history=True)
351 nt.assert_true('parrot' in [_ip.user_ns[x] for x in ('_i','_ii','_iii')])
351 nt.assert_true('parrot' in [_ip.user_ns[x] for x in ('_i','_ii','_iii')])
352 _ip.magic('%reset -f in')
352 _ip.magic('%reset -f in')
353 nt.assert_false('parrot' in [_ip.user_ns[x] for x in ('_i','_ii','_iii')])
353 nt.assert_false('parrot' in [_ip.user_ns[x] for x in ('_i','_ii','_iii')])
354 nt.assert_equal(len(set(_ip.user_ns['In'])), 1)
354 nt.assert_equal(len(set(_ip.user_ns['In'])), 1)
355
355
356 def test_reset_dhist():
356 def test_reset_dhist():
357 "Test '%reset dhist' magic"
357 "Test '%reset dhist' magic"
358 _ip.run_cell("tmp = [d for d in _dh]") # copy before clearing
358 _ip.run_cell("tmp = [d for d in _dh]") # copy before clearing
359 _ip.magic('cd ' + os.path.dirname(nt.__file__))
359 _ip.magic('cd ' + os.path.dirname(nt.__file__))
360 _ip.magic('cd -')
360 _ip.magic('cd -')
361 nt.assert_true(len(_ip.user_ns['_dh']) > 0)
361 nt.assert_true(len(_ip.user_ns['_dh']) > 0)
362 _ip.magic('reset -f dhist')
362 _ip.magic('reset -f dhist')
363 nt.assert_equal(len(_ip.user_ns['_dh']), 0)
363 nt.assert_equal(len(_ip.user_ns['_dh']), 0)
364 _ip.run_cell("_dh = [d for d in tmp]") #restore
364 _ip.run_cell("_dh = [d for d in tmp]") #restore
365
365
366 def test_reset_in_length():
366 def test_reset_in_length():
367 "Test that '%reset in' preserves In[] length"
367 "Test that '%reset in' preserves In[] length"
368 _ip.run_cell("print 'foo'")
368 _ip.run_cell("print 'foo'")
369 _ip.run_cell("reset -f in")
369 _ip.run_cell("reset -f in")
370 nt.assert_equal(len(_ip.user_ns['In']), _ip.displayhook.prompt_count+1)
370 nt.assert_equal(len(_ip.user_ns['In']), _ip.displayhook.prompt_count+1)
371
371
372 def test_tb_syntaxerror():
372 def test_tb_syntaxerror():
373 """test %tb after a SyntaxError"""
373 """test %tb after a SyntaxError"""
374 ip = get_ipython()
374 ip = get_ipython()
375 ip.run_cell("for")
375 ip.run_cell("for")
376
376
377 # trap and validate stdout
377 # trap and validate stdout
378 save_stdout = sys.stdout
378 save_stdout = sys.stdout
379 try:
379 try:
380 sys.stdout = StringIO()
380 sys.stdout = StringIO()
381 ip.run_cell("%tb")
381 ip.run_cell("%tb")
382 out = sys.stdout.getvalue()
382 out = sys.stdout.getvalue()
383 finally:
383 finally:
384 sys.stdout = save_stdout
384 sys.stdout = save_stdout
385 # trim output, and only check the last line
385 # trim output, and only check the last line
386 last_line = out.rstrip().splitlines()[-1].strip()
386 last_line = out.rstrip().splitlines()[-1].strip()
387 nt.assert_equal(last_line, "SyntaxError: invalid syntax")
387 nt.assert_equal(last_line, "SyntaxError: invalid syntax")
388
388
389
389
390 def test_time():
390 def test_time():
391 ip = get_ipython()
391 ip = get_ipython()
392
392
393 with tt.AssertPrints("Wall time: "):
393 with tt.AssertPrints("Wall time: "):
394 ip.run_cell("%time None")
394 ip.run_cell("%time None")
395
395
396 ip.run_cell("def f(kmjy):\n"
396 ip.run_cell("def f(kmjy):\n"
397 " %time print (2*kmjy)")
397 " %time print (2*kmjy)")
398
398
399 with tt.AssertPrints("Wall time: "):
399 with tt.AssertPrints("Wall time: "):
400 with tt.AssertPrints("hihi", suppress=False):
400 with tt.AssertPrints("hihi", suppress=False):
401 ip.run_cell("f('hi')")
401 ip.run_cell("f('hi')")
402
402
403 def test_time_last_not_expression():
404 ip.run_cell("%%time\n"
405 "var_1 = 1\n"
406 "var_2 = 2\n")
407 assert ip.user_ns['var_1'] == 1
408 del ip.user_ns['var_1']
409 assert ip.user_ns['var_2'] == 2
410 del ip.user_ns['var_2']
411
403
412
404 @dec.skip_win32
413 @dec.skip_win32
405 def test_time2():
414 def test_time2():
406 ip = get_ipython()
415 ip = get_ipython()
407
416
408 with tt.AssertPrints("CPU times: user "):
417 with tt.AssertPrints("CPU times: user "):
409 ip.run_cell("%time None")
418 ip.run_cell("%time None")
410
419
411 def test_time3():
420 def test_time3():
412 """Erroneous magic function calls, issue gh-3334"""
421 """Erroneous magic function calls, issue gh-3334"""
413 ip = get_ipython()
422 ip = get_ipython()
414 ip.user_ns.pop('run', None)
423 ip.user_ns.pop('run', None)
415
424
416 with tt.AssertNotPrints("not found", channel='stderr'):
425 with tt.AssertNotPrints("not found", channel='stderr'):
417 ip.run_cell("%%time\n"
426 ip.run_cell("%%time\n"
418 "run = 0\n"
427 "run = 0\n"
419 "run += 1")
428 "run += 1")
420
429
421 def test_multiline_time():
430 def test_multiline_time():
422 """Make sure last statement from time return a value."""
431 """Make sure last statement from time return a value."""
423 ip = get_ipython()
432 ip = get_ipython()
424 ip.user_ns.pop('run', None)
433 ip.user_ns.pop('run', None)
425
434
426 ip.run_cell(dedent("""\
435 ip.run_cell(dedent("""\
427 %%time
436 %%time
428 a = "ho"
437 a = "ho"
429 b = "hey"
438 b = "hey"
430 a+b
439 a+b
431 """))
440 """))
432 nt.assert_equal(ip.user_ns_hidden['_'], 'hohey')
441 nt.assert_equal(ip.user_ns_hidden['_'], 'hohey')
433
442
434 def test_time_local_ns():
443 def test_time_local_ns():
435 """
444 """
436 Test that local_ns is actually global_ns when running a cell magic
445 Test that local_ns is actually global_ns when running a cell magic
437 """
446 """
438 ip = get_ipython()
447 ip = get_ipython()
439 ip.run_cell("%%time\n"
448 ip.run_cell("%%time\n"
440 "myvar = 1")
449 "myvar = 1")
441 nt.assert_equal(ip.user_ns['myvar'], 1)
450 nt.assert_equal(ip.user_ns['myvar'], 1)
442 del ip.user_ns['myvar']
451 del ip.user_ns['myvar']
443
452
444 def test_doctest_mode():
453 def test_doctest_mode():
445 "Toggle doctest_mode twice, it should be a no-op and run without error"
454 "Toggle doctest_mode twice, it should be a no-op and run without error"
446 _ip.magic('doctest_mode')
455 _ip.magic('doctest_mode')
447 _ip.magic('doctest_mode')
456 _ip.magic('doctest_mode')
448
457
449
458
450 def test_parse_options():
459 def test_parse_options():
451 """Tests for basic options parsing in magics."""
460 """Tests for basic options parsing in magics."""
452 # These are only the most minimal of tests, more should be added later. At
461 # These are only the most minimal of tests, more should be added later. At
453 # the very least we check that basic text/unicode calls work OK.
462 # the very least we check that basic text/unicode calls work OK.
454 m = DummyMagics(_ip)
463 m = DummyMagics(_ip)
455 nt.assert_equal(m.parse_options('foo', '')[1], 'foo')
464 nt.assert_equal(m.parse_options('foo', '')[1], 'foo')
456 nt.assert_equal(m.parse_options(u'foo', '')[1], u'foo')
465 nt.assert_equal(m.parse_options(u'foo', '')[1], u'foo')
457
466
458
467
459 def test_dirops():
468 def test_dirops():
460 """Test various directory handling operations."""
469 """Test various directory handling operations."""
461 # curpath = lambda :os.path.splitdrive(os.getcwd())[1].replace('\\','/')
470 # curpath = lambda :os.path.splitdrive(os.getcwd())[1].replace('\\','/')
462 curpath = os.getcwd
471 curpath = os.getcwd
463 startdir = os.getcwd()
472 startdir = os.getcwd()
464 ipdir = os.path.realpath(_ip.ipython_dir)
473 ipdir = os.path.realpath(_ip.ipython_dir)
465 try:
474 try:
466 _ip.magic('cd "%s"' % ipdir)
475 _ip.magic('cd "%s"' % ipdir)
467 nt.assert_equal(curpath(), ipdir)
476 nt.assert_equal(curpath(), ipdir)
468 _ip.magic('cd -')
477 _ip.magic('cd -')
469 nt.assert_equal(curpath(), startdir)
478 nt.assert_equal(curpath(), startdir)
470 _ip.magic('pushd "%s"' % ipdir)
479 _ip.magic('pushd "%s"' % ipdir)
471 nt.assert_equal(curpath(), ipdir)
480 nt.assert_equal(curpath(), ipdir)
472 _ip.magic('popd')
481 _ip.magic('popd')
473 nt.assert_equal(curpath(), startdir)
482 nt.assert_equal(curpath(), startdir)
474 finally:
483 finally:
475 os.chdir(startdir)
484 os.chdir(startdir)
476
485
477
486
478 def test_cd_force_quiet():
487 def test_cd_force_quiet():
479 """Test OSMagics.cd_force_quiet option"""
488 """Test OSMagics.cd_force_quiet option"""
480 _ip.config.OSMagics.cd_force_quiet = True
489 _ip.config.OSMagics.cd_force_quiet = True
481 osmagics = osm.OSMagics(shell=_ip)
490 osmagics = osm.OSMagics(shell=_ip)
482
491
483 startdir = os.getcwd()
492 startdir = os.getcwd()
484 ipdir = os.path.realpath(_ip.ipython_dir)
493 ipdir = os.path.realpath(_ip.ipython_dir)
485
494
486 try:
495 try:
487 with tt.AssertNotPrints(ipdir):
496 with tt.AssertNotPrints(ipdir):
488 osmagics.cd('"%s"' % ipdir)
497 osmagics.cd('"%s"' % ipdir)
489 with tt.AssertNotPrints(startdir):
498 with tt.AssertNotPrints(startdir):
490 osmagics.cd('-')
499 osmagics.cd('-')
491 finally:
500 finally:
492 os.chdir(startdir)
501 os.chdir(startdir)
493
502
494
503
495 def test_xmode():
504 def test_xmode():
496 # Calling xmode three times should be a no-op
505 # Calling xmode three times should be a no-op
497 xmode = _ip.InteractiveTB.mode
506 xmode = _ip.InteractiveTB.mode
498 for i in range(4):
507 for i in range(4):
499 _ip.magic("xmode")
508 _ip.magic("xmode")
500 nt.assert_equal(_ip.InteractiveTB.mode, xmode)
509 nt.assert_equal(_ip.InteractiveTB.mode, xmode)
501
510
502 def test_reset_hard():
511 def test_reset_hard():
503 monitor = []
512 monitor = []
504 class A(object):
513 class A(object):
505 def __del__(self):
514 def __del__(self):
506 monitor.append(1)
515 monitor.append(1)
507 def __repr__(self):
516 def __repr__(self):
508 return "<A instance>"
517 return "<A instance>"
509
518
510 _ip.user_ns["a"] = A()
519 _ip.user_ns["a"] = A()
511 _ip.run_cell("a")
520 _ip.run_cell("a")
512
521
513 nt.assert_equal(monitor, [])
522 nt.assert_equal(monitor, [])
514 _ip.magic("reset -f")
523 _ip.magic("reset -f")
515 nt.assert_equal(monitor, [1])
524 nt.assert_equal(monitor, [1])
516
525
517 class TestXdel(tt.TempFileMixin):
526 class TestXdel(tt.TempFileMixin):
518 def test_xdel(self):
527 def test_xdel(self):
519 """Test that references from %run are cleared by xdel."""
528 """Test that references from %run are cleared by xdel."""
520 src = ("class A(object):\n"
529 src = ("class A(object):\n"
521 " monitor = []\n"
530 " monitor = []\n"
522 " def __del__(self):\n"
531 " def __del__(self):\n"
523 " self.monitor.append(1)\n"
532 " self.monitor.append(1)\n"
524 "a = A()\n")
533 "a = A()\n")
525 self.mktmp(src)
534 self.mktmp(src)
526 # %run creates some hidden references...
535 # %run creates some hidden references...
527 _ip.magic("run %s" % self.fname)
536 _ip.magic("run %s" % self.fname)
528 # ... as does the displayhook.
537 # ... as does the displayhook.
529 _ip.run_cell("a")
538 _ip.run_cell("a")
530
539
531 monitor = _ip.user_ns["A"].monitor
540 monitor = _ip.user_ns["A"].monitor
532 nt.assert_equal(monitor, [])
541 nt.assert_equal(monitor, [])
533
542
534 _ip.magic("xdel a")
543 _ip.magic("xdel a")
535
544
536 # Check that a's __del__ method has been called.
545 # Check that a's __del__ method has been called.
537 nt.assert_equal(monitor, [1])
546 nt.assert_equal(monitor, [1])
538
547
539 def doctest_who():
548 def doctest_who():
540 """doctest for %who
549 """doctest for %who
541
550
542 In [1]: %reset -f
551 In [1]: %reset -f
543
552
544 In [2]: alpha = 123
553 In [2]: alpha = 123
545
554
546 In [3]: beta = 'beta'
555 In [3]: beta = 'beta'
547
556
548 In [4]: %who int
557 In [4]: %who int
549 alpha
558 alpha
550
559
551 In [5]: %who str
560 In [5]: %who str
552 beta
561 beta
553
562
554 In [6]: %whos
563 In [6]: %whos
555 Variable Type Data/Info
564 Variable Type Data/Info
556 ----------------------------
565 ----------------------------
557 alpha int 123
566 alpha int 123
558 beta str beta
567 beta str beta
559
568
560 In [7]: %who_ls
569 In [7]: %who_ls
561 Out[7]: ['alpha', 'beta']
570 Out[7]: ['alpha', 'beta']
562 """
571 """
563
572
564 def test_whos():
573 def test_whos():
565 """Check that whos is protected against objects where repr() fails."""
574 """Check that whos is protected against objects where repr() fails."""
566 class A(object):
575 class A(object):
567 def __repr__(self):
576 def __repr__(self):
568 raise Exception()
577 raise Exception()
569 _ip.user_ns['a'] = A()
578 _ip.user_ns['a'] = A()
570 _ip.magic("whos")
579 _ip.magic("whos")
571
580
572 def doctest_precision():
581 def doctest_precision():
573 """doctest for %precision
582 """doctest for %precision
574
583
575 In [1]: f = get_ipython().display_formatter.formatters['text/plain']
584 In [1]: f = get_ipython().display_formatter.formatters['text/plain']
576
585
577 In [2]: %precision 5
586 In [2]: %precision 5
578 Out[2]: '%.5f'
587 Out[2]: '%.5f'
579
588
580 In [3]: f.float_format
589 In [3]: f.float_format
581 Out[3]: '%.5f'
590 Out[3]: '%.5f'
582
591
583 In [4]: %precision %e
592 In [4]: %precision %e
584 Out[4]: '%e'
593 Out[4]: '%e'
585
594
586 In [5]: f(3.1415927)
595 In [5]: f(3.1415927)
587 Out[5]: '3.141593e+00'
596 Out[5]: '3.141593e+00'
588 """
597 """
589
598
590 def test_psearch():
599 def test_psearch():
591 with tt.AssertPrints("dict.fromkeys"):
600 with tt.AssertPrints("dict.fromkeys"):
592 _ip.run_cell("dict.fr*?")
601 _ip.run_cell("dict.fr*?")
593
602
594 def test_timeit_shlex():
603 def test_timeit_shlex():
595 """test shlex issues with timeit (#1109)"""
604 """test shlex issues with timeit (#1109)"""
596 _ip.ex("def f(*a,**kw): pass")
605 _ip.ex("def f(*a,**kw): pass")
597 _ip.magic('timeit -n1 "this is a bug".count(" ")')
606 _ip.magic('timeit -n1 "this is a bug".count(" ")')
598 _ip.magic('timeit -r1 -n1 f(" ", 1)')
607 _ip.magic('timeit -r1 -n1 f(" ", 1)')
599 _ip.magic('timeit -r1 -n1 f(" ", 1, " ", 2, " ")')
608 _ip.magic('timeit -r1 -n1 f(" ", 1, " ", 2, " ")')
600 _ip.magic('timeit -r1 -n1 ("a " + "b")')
609 _ip.magic('timeit -r1 -n1 ("a " + "b")')
601 _ip.magic('timeit -r1 -n1 f("a " + "b")')
610 _ip.magic('timeit -r1 -n1 f("a " + "b")')
602 _ip.magic('timeit -r1 -n1 f("a " + "b ")')
611 _ip.magic('timeit -r1 -n1 f("a " + "b ")')
603
612
604
613
605 def test_timeit_special_syntax():
614 def test_timeit_special_syntax():
606 "Test %%timeit with IPython special syntax"
615 "Test %%timeit with IPython special syntax"
607 @register_line_magic
616 @register_line_magic
608 def lmagic(line):
617 def lmagic(line):
609 ip = get_ipython()
618 ip = get_ipython()
610 ip.user_ns['lmagic_out'] = line
619 ip.user_ns['lmagic_out'] = line
611
620
612 # line mode test
621 # line mode test
613 _ip.run_line_magic('timeit', '-n1 -r1 %lmagic my line')
622 _ip.run_line_magic('timeit', '-n1 -r1 %lmagic my line')
614 nt.assert_equal(_ip.user_ns['lmagic_out'], 'my line')
623 nt.assert_equal(_ip.user_ns['lmagic_out'], 'my line')
615 # cell mode test
624 # cell mode test
616 _ip.run_cell_magic('timeit', '-n1 -r1', '%lmagic my line2')
625 _ip.run_cell_magic('timeit', '-n1 -r1', '%lmagic my line2')
617 nt.assert_equal(_ip.user_ns['lmagic_out'], 'my line2')
626 nt.assert_equal(_ip.user_ns['lmagic_out'], 'my line2')
618
627
619 def test_timeit_return():
628 def test_timeit_return():
620 """
629 """
621 test whether timeit -o return object
630 test whether timeit -o return object
622 """
631 """
623
632
624 res = _ip.run_line_magic('timeit','-n10 -r10 -o 1')
633 res = _ip.run_line_magic('timeit','-n10 -r10 -o 1')
625 assert(res is not None)
634 assert(res is not None)
626
635
627 def test_timeit_quiet():
636 def test_timeit_quiet():
628 """
637 """
629 test quiet option of timeit magic
638 test quiet option of timeit magic
630 """
639 """
631 with tt.AssertNotPrints("loops"):
640 with tt.AssertNotPrints("loops"):
632 _ip.run_cell("%timeit -n1 -r1 -q 1")
641 _ip.run_cell("%timeit -n1 -r1 -q 1")
633
642
634 def test_timeit_return_quiet():
643 def test_timeit_return_quiet():
635 with tt.AssertNotPrints("loops"):
644 with tt.AssertNotPrints("loops"):
636 res = _ip.run_line_magic('timeit', '-n1 -r1 -q -o 1')
645 res = _ip.run_line_magic('timeit', '-n1 -r1 -q -o 1')
637 assert (res is not None)
646 assert (res is not None)
638
647
639 def test_timeit_invalid_return():
648 def test_timeit_invalid_return():
640 with nt.assert_raises_regex(SyntaxError, "outside function"):
649 with nt.assert_raises_regex(SyntaxError, "outside function"):
641 _ip.run_line_magic('timeit', 'return')
650 _ip.run_line_magic('timeit', 'return')
642
651
643 @dec.skipif(execution.profile is None)
652 @dec.skipif(execution.profile is None)
644 def test_prun_special_syntax():
653 def test_prun_special_syntax():
645 "Test %%prun with IPython special syntax"
654 "Test %%prun with IPython special syntax"
646 @register_line_magic
655 @register_line_magic
647 def lmagic(line):
656 def lmagic(line):
648 ip = get_ipython()
657 ip = get_ipython()
649 ip.user_ns['lmagic_out'] = line
658 ip.user_ns['lmagic_out'] = line
650
659
651 # line mode test
660 # line mode test
652 _ip.run_line_magic('prun', '-q %lmagic my line')
661 _ip.run_line_magic('prun', '-q %lmagic my line')
653 nt.assert_equal(_ip.user_ns['lmagic_out'], 'my line')
662 nt.assert_equal(_ip.user_ns['lmagic_out'], 'my line')
654 # cell mode test
663 # cell mode test
655 _ip.run_cell_magic('prun', '-q', '%lmagic my line2')
664 _ip.run_cell_magic('prun', '-q', '%lmagic my line2')
656 nt.assert_equal(_ip.user_ns['lmagic_out'], 'my line2')
665 nt.assert_equal(_ip.user_ns['lmagic_out'], 'my line2')
657
666
658 @dec.skipif(execution.profile is None)
667 @dec.skipif(execution.profile is None)
659 def test_prun_quotes():
668 def test_prun_quotes():
660 "Test that prun does not clobber string escapes (GH #1302)"
669 "Test that prun does not clobber string escapes (GH #1302)"
661 _ip.magic(r"prun -q x = '\t'")
670 _ip.magic(r"prun -q x = '\t'")
662 nt.assert_equal(_ip.user_ns['x'], '\t')
671 nt.assert_equal(_ip.user_ns['x'], '\t')
663
672
664 def test_extension():
673 def test_extension():
665 # Debugging information for failures of this test
674 # Debugging information for failures of this test
666 print('sys.path:')
675 print('sys.path:')
667 for p in sys.path:
676 for p in sys.path:
668 print(' ', p)
677 print(' ', p)
669 print('CWD', os.getcwd())
678 print('CWD', os.getcwd())
670
679
671 nt.assert_raises(ImportError, _ip.magic, "load_ext daft_extension")
680 nt.assert_raises(ImportError, _ip.magic, "load_ext daft_extension")
672 daft_path = os.path.join(os.path.dirname(__file__), "daft_extension")
681 daft_path = os.path.join(os.path.dirname(__file__), "daft_extension")
673 sys.path.insert(0, daft_path)
682 sys.path.insert(0, daft_path)
674 try:
683 try:
675 _ip.user_ns.pop('arq', None)
684 _ip.user_ns.pop('arq', None)
676 invalidate_caches() # Clear import caches
685 invalidate_caches() # Clear import caches
677 _ip.magic("load_ext daft_extension")
686 _ip.magic("load_ext daft_extension")
678 nt.assert_equal(_ip.user_ns['arq'], 185)
687 nt.assert_equal(_ip.user_ns['arq'], 185)
679 _ip.magic("unload_ext daft_extension")
688 _ip.magic("unload_ext daft_extension")
680 assert 'arq' not in _ip.user_ns
689 assert 'arq' not in _ip.user_ns
681 finally:
690 finally:
682 sys.path.remove(daft_path)
691 sys.path.remove(daft_path)
683
692
684
693
685 def test_notebook_export_json():
694 def test_notebook_export_json():
686 _ip = get_ipython()
695 _ip = get_ipython()
687 _ip.history_manager.reset() # Clear any existing history.
696 _ip.history_manager.reset() # Clear any existing history.
688 cmds = [u"a=1", u"def b():\n return a**2", u"print('noΓ«l, Γ©tΓ©', b())"]
697 cmds = [u"a=1", u"def b():\n return a**2", u"print('noΓ«l, Γ©tΓ©', b())"]
689 for i, cmd in enumerate(cmds, start=1):
698 for i, cmd in enumerate(cmds, start=1):
690 _ip.history_manager.store_inputs(i, cmd)
699 _ip.history_manager.store_inputs(i, cmd)
691 with TemporaryDirectory() as td:
700 with TemporaryDirectory() as td:
692 outfile = os.path.join(td, "nb.ipynb")
701 outfile = os.path.join(td, "nb.ipynb")
693 _ip.magic("notebook -e %s" % outfile)
702 _ip.magic("notebook -e %s" % outfile)
694
703
695
704
696 class TestEnv(TestCase):
705 class TestEnv(TestCase):
697
706
698 def test_env(self):
707 def test_env(self):
699 env = _ip.magic("env")
708 env = _ip.magic("env")
700 self.assertTrue(isinstance(env, dict))
709 self.assertTrue(isinstance(env, dict))
701
710
702 def test_env_get_set_simple(self):
711 def test_env_get_set_simple(self):
703 env = _ip.magic("env var val1")
712 env = _ip.magic("env var val1")
704 self.assertEqual(env, None)
713 self.assertEqual(env, None)
705 self.assertEqual(os.environ['var'], 'val1')
714 self.assertEqual(os.environ['var'], 'val1')
706 self.assertEqual(_ip.magic("env var"), 'val1')
715 self.assertEqual(_ip.magic("env var"), 'val1')
707 env = _ip.magic("env var=val2")
716 env = _ip.magic("env var=val2")
708 self.assertEqual(env, None)
717 self.assertEqual(env, None)
709 self.assertEqual(os.environ['var'], 'val2')
718 self.assertEqual(os.environ['var'], 'val2')
710
719
711 def test_env_get_set_complex(self):
720 def test_env_get_set_complex(self):
712 env = _ip.magic("env var 'val1 '' 'val2")
721 env = _ip.magic("env var 'val1 '' 'val2")
713 self.assertEqual(env, None)
722 self.assertEqual(env, None)
714 self.assertEqual(os.environ['var'], "'val1 '' 'val2")
723 self.assertEqual(os.environ['var'], "'val1 '' 'val2")
715 self.assertEqual(_ip.magic("env var"), "'val1 '' 'val2")
724 self.assertEqual(_ip.magic("env var"), "'val1 '' 'val2")
716 env = _ip.magic('env var=val2 val3="val4')
725 env = _ip.magic('env var=val2 val3="val4')
717 self.assertEqual(env, None)
726 self.assertEqual(env, None)
718 self.assertEqual(os.environ['var'], 'val2 val3="val4')
727 self.assertEqual(os.environ['var'], 'val2 val3="val4')
719
728
720 def test_env_set_bad_input(self):
729 def test_env_set_bad_input(self):
721 self.assertRaises(UsageError, lambda: _ip.magic("set_env var"))
730 self.assertRaises(UsageError, lambda: _ip.magic("set_env var"))
722
731
723 def test_env_set_whitespace(self):
732 def test_env_set_whitespace(self):
724 self.assertRaises(UsageError, lambda: _ip.magic("env var A=B"))
733 self.assertRaises(UsageError, lambda: _ip.magic("env var A=B"))
725
734
726
735
727 class CellMagicTestCase(TestCase):
736 class CellMagicTestCase(TestCase):
728
737
729 def check_ident(self, magic):
738 def check_ident(self, magic):
730 # Manually called, we get the result
739 # Manually called, we get the result
731 out = _ip.run_cell_magic(magic, 'a', 'b')
740 out = _ip.run_cell_magic(magic, 'a', 'b')
732 nt.assert_equal(out, ('a','b'))
741 nt.assert_equal(out, ('a','b'))
733 # Via run_cell, it goes into the user's namespace via displayhook
742 # Via run_cell, it goes into the user's namespace via displayhook
734 _ip.run_cell('%%' + magic +' c\nd\n')
743 _ip.run_cell('%%' + magic +' c\nd\n')
735 nt.assert_equal(_ip.user_ns['_'], ('c','d\n'))
744 nt.assert_equal(_ip.user_ns['_'], ('c','d\n'))
736
745
737 def test_cell_magic_func_deco(self):
746 def test_cell_magic_func_deco(self):
738 "Cell magic using simple decorator"
747 "Cell magic using simple decorator"
739 @register_cell_magic
748 @register_cell_magic
740 def cellm(line, cell):
749 def cellm(line, cell):
741 return line, cell
750 return line, cell
742
751
743 self.check_ident('cellm')
752 self.check_ident('cellm')
744
753
745 def test_cell_magic_reg(self):
754 def test_cell_magic_reg(self):
746 "Cell magic manually registered"
755 "Cell magic manually registered"
747 def cellm(line, cell):
756 def cellm(line, cell):
748 return line, cell
757 return line, cell
749
758
750 _ip.register_magic_function(cellm, 'cell', 'cellm2')
759 _ip.register_magic_function(cellm, 'cell', 'cellm2')
751 self.check_ident('cellm2')
760 self.check_ident('cellm2')
752
761
753 def test_cell_magic_class(self):
762 def test_cell_magic_class(self):
754 "Cell magics declared via a class"
763 "Cell magics declared via a class"
755 @magics_class
764 @magics_class
756 class MyMagics(Magics):
765 class MyMagics(Magics):
757
766
758 @cell_magic
767 @cell_magic
759 def cellm3(self, line, cell):
768 def cellm3(self, line, cell):
760 return line, cell
769 return line, cell
761
770
762 _ip.register_magics(MyMagics)
771 _ip.register_magics(MyMagics)
763 self.check_ident('cellm3')
772 self.check_ident('cellm3')
764
773
765 def test_cell_magic_class2(self):
774 def test_cell_magic_class2(self):
766 "Cell magics declared via a class, #2"
775 "Cell magics declared via a class, #2"
767 @magics_class
776 @magics_class
768 class MyMagics2(Magics):
777 class MyMagics2(Magics):
769
778
770 @cell_magic('cellm4')
779 @cell_magic('cellm4')
771 def cellm33(self, line, cell):
780 def cellm33(self, line, cell):
772 return line, cell
781 return line, cell
773
782
774 _ip.register_magics(MyMagics2)
783 _ip.register_magics(MyMagics2)
775 self.check_ident('cellm4')
784 self.check_ident('cellm4')
776 # Check that nothing is registered as 'cellm33'
785 # Check that nothing is registered as 'cellm33'
777 c33 = _ip.find_cell_magic('cellm33')
786 c33 = _ip.find_cell_magic('cellm33')
778 nt.assert_equal(c33, None)
787 nt.assert_equal(c33, None)
779
788
780 def test_file():
789 def test_file():
781 """Basic %%writefile"""
790 """Basic %%writefile"""
782 ip = get_ipython()
791 ip = get_ipython()
783 with TemporaryDirectory() as td:
792 with TemporaryDirectory() as td:
784 fname = os.path.join(td, 'file1')
793 fname = os.path.join(td, 'file1')
785 ip.run_cell_magic("writefile", fname, u'\n'.join([
794 ip.run_cell_magic("writefile", fname, u'\n'.join([
786 'line1',
795 'line1',
787 'line2',
796 'line2',
788 ]))
797 ]))
789 with open(fname) as f:
798 with open(fname) as f:
790 s = f.read()
799 s = f.read()
791 nt.assert_in('line1\n', s)
800 nt.assert_in('line1\n', s)
792 nt.assert_in('line2', s)
801 nt.assert_in('line2', s)
793
802
794 @dec.skip_win32
803 @dec.skip_win32
795 def test_file_single_quote():
804 def test_file_single_quote():
796 """Basic %%writefile with embedded single quotes"""
805 """Basic %%writefile with embedded single quotes"""
797 ip = get_ipython()
806 ip = get_ipython()
798 with TemporaryDirectory() as td:
807 with TemporaryDirectory() as td:
799 fname = os.path.join(td, '\'file1\'')
808 fname = os.path.join(td, '\'file1\'')
800 ip.run_cell_magic("writefile", fname, u'\n'.join([
809 ip.run_cell_magic("writefile", fname, u'\n'.join([
801 'line1',
810 'line1',
802 'line2',
811 'line2',
803 ]))
812 ]))
804 with open(fname) as f:
813 with open(fname) as f:
805 s = f.read()
814 s = f.read()
806 nt.assert_in('line1\n', s)
815 nt.assert_in('line1\n', s)
807 nt.assert_in('line2', s)
816 nt.assert_in('line2', s)
808
817
809 @dec.skip_win32
818 @dec.skip_win32
810 def test_file_double_quote():
819 def test_file_double_quote():
811 """Basic %%writefile with embedded double quotes"""
820 """Basic %%writefile with embedded double quotes"""
812 ip = get_ipython()
821 ip = get_ipython()
813 with TemporaryDirectory() as td:
822 with TemporaryDirectory() as td:
814 fname = os.path.join(td, '"file1"')
823 fname = os.path.join(td, '"file1"')
815 ip.run_cell_magic("writefile", fname, u'\n'.join([
824 ip.run_cell_magic("writefile", fname, u'\n'.join([
816 'line1',
825 'line1',
817 'line2',
826 'line2',
818 ]))
827 ]))
819 with open(fname) as f:
828 with open(fname) as f:
820 s = f.read()
829 s = f.read()
821 nt.assert_in('line1\n', s)
830 nt.assert_in('line1\n', s)
822 nt.assert_in('line2', s)
831 nt.assert_in('line2', s)
823
832
824 def test_file_var_expand():
833 def test_file_var_expand():
825 """%%writefile $filename"""
834 """%%writefile $filename"""
826 ip = get_ipython()
835 ip = get_ipython()
827 with TemporaryDirectory() as td:
836 with TemporaryDirectory() as td:
828 fname = os.path.join(td, 'file1')
837 fname = os.path.join(td, 'file1')
829 ip.user_ns['filename'] = fname
838 ip.user_ns['filename'] = fname
830 ip.run_cell_magic("writefile", '$filename', u'\n'.join([
839 ip.run_cell_magic("writefile", '$filename', u'\n'.join([
831 'line1',
840 'line1',
832 'line2',
841 'line2',
833 ]))
842 ]))
834 with open(fname) as f:
843 with open(fname) as f:
835 s = f.read()
844 s = f.read()
836 nt.assert_in('line1\n', s)
845 nt.assert_in('line1\n', s)
837 nt.assert_in('line2', s)
846 nt.assert_in('line2', s)
838
847
839 def test_file_unicode():
848 def test_file_unicode():
840 """%%writefile with unicode cell"""
849 """%%writefile with unicode cell"""
841 ip = get_ipython()
850 ip = get_ipython()
842 with TemporaryDirectory() as td:
851 with TemporaryDirectory() as td:
843 fname = os.path.join(td, 'file1')
852 fname = os.path.join(td, 'file1')
844 ip.run_cell_magic("writefile", fname, u'\n'.join([
853 ip.run_cell_magic("writefile", fname, u'\n'.join([
845 u'linΓ©1',
854 u'linΓ©1',
846 u'linΓ©2',
855 u'linΓ©2',
847 ]))
856 ]))
848 with io.open(fname, encoding='utf-8') as f:
857 with io.open(fname, encoding='utf-8') as f:
849 s = f.read()
858 s = f.read()
850 nt.assert_in(u'linΓ©1\n', s)
859 nt.assert_in(u'linΓ©1\n', s)
851 nt.assert_in(u'linΓ©2', s)
860 nt.assert_in(u'linΓ©2', s)
852
861
853 def test_file_amend():
862 def test_file_amend():
854 """%%writefile -a amends files"""
863 """%%writefile -a amends files"""
855 ip = get_ipython()
864 ip = get_ipython()
856 with TemporaryDirectory() as td:
865 with TemporaryDirectory() as td:
857 fname = os.path.join(td, 'file2')
866 fname = os.path.join(td, 'file2')
858 ip.run_cell_magic("writefile", fname, u'\n'.join([
867 ip.run_cell_magic("writefile", fname, u'\n'.join([
859 'line1',
868 'line1',
860 'line2',
869 'line2',
861 ]))
870 ]))
862 ip.run_cell_magic("writefile", "-a %s" % fname, u'\n'.join([
871 ip.run_cell_magic("writefile", "-a %s" % fname, u'\n'.join([
863 'line3',
872 'line3',
864 'line4',
873 'line4',
865 ]))
874 ]))
866 with open(fname) as f:
875 with open(fname) as f:
867 s = f.read()
876 s = f.read()
868 nt.assert_in('line1\n', s)
877 nt.assert_in('line1\n', s)
869 nt.assert_in('line3\n', s)
878 nt.assert_in('line3\n', s)
870
879
871 def test_file_spaces():
880 def test_file_spaces():
872 """%%file with spaces in filename"""
881 """%%file with spaces in filename"""
873 ip = get_ipython()
882 ip = get_ipython()
874 with TemporaryWorkingDirectory() as td:
883 with TemporaryWorkingDirectory() as td:
875 fname = "file name"
884 fname = "file name"
876 ip.run_cell_magic("file", '"%s"'%fname, u'\n'.join([
885 ip.run_cell_magic("file", '"%s"'%fname, u'\n'.join([
877 'line1',
886 'line1',
878 'line2',
887 'line2',
879 ]))
888 ]))
880 with open(fname) as f:
889 with open(fname) as f:
881 s = f.read()
890 s = f.read()
882 nt.assert_in('line1\n', s)
891 nt.assert_in('line1\n', s)
883 nt.assert_in('line2', s)
892 nt.assert_in('line2', s)
884
893
885 def test_script_config():
894 def test_script_config():
886 ip = get_ipython()
895 ip = get_ipython()
887 ip.config.ScriptMagics.script_magics = ['whoda']
896 ip.config.ScriptMagics.script_magics = ['whoda']
888 sm = script.ScriptMagics(shell=ip)
897 sm = script.ScriptMagics(shell=ip)
889 nt.assert_in('whoda', sm.magics['cell'])
898 nt.assert_in('whoda', sm.magics['cell'])
890
899
891 @dec.skip_win32
900 @dec.skip_win32
892 def test_script_out():
901 def test_script_out():
893 ip = get_ipython()
902 ip = get_ipython()
894 ip.run_cell_magic("script", "--out output sh", "echo 'hi'")
903 ip.run_cell_magic("script", "--out output sh", "echo 'hi'")
895 nt.assert_equal(ip.user_ns['output'], 'hi\n')
904 nt.assert_equal(ip.user_ns['output'], 'hi\n')
896
905
897 @dec.skip_win32
906 @dec.skip_win32
898 def test_script_err():
907 def test_script_err():
899 ip = get_ipython()
908 ip = get_ipython()
900 ip.run_cell_magic("script", "--err error sh", "echo 'hello' >&2")
909 ip.run_cell_magic("script", "--err error sh", "echo 'hello' >&2")
901 nt.assert_equal(ip.user_ns['error'], 'hello\n')
910 nt.assert_equal(ip.user_ns['error'], 'hello\n')
902
911
903 @dec.skip_win32
912 @dec.skip_win32
904 def test_script_out_err():
913 def test_script_out_err():
905 ip = get_ipython()
914 ip = get_ipython()
906 ip.run_cell_magic("script", "--out output --err error sh", "echo 'hi'\necho 'hello' >&2")
915 ip.run_cell_magic("script", "--out output --err error sh", "echo 'hi'\necho 'hello' >&2")
907 nt.assert_equal(ip.user_ns['output'], 'hi\n')
916 nt.assert_equal(ip.user_ns['output'], 'hi\n')
908 nt.assert_equal(ip.user_ns['error'], 'hello\n')
917 nt.assert_equal(ip.user_ns['error'], 'hello\n')
909
918
910 @dec.skip_win32
919 @dec.skip_win32
911 def test_script_bg_out():
920 def test_script_bg_out():
912 ip = get_ipython()
921 ip = get_ipython()
913 ip.run_cell_magic("script", "--bg --out output sh", "echo 'hi'")
922 ip.run_cell_magic("script", "--bg --out output sh", "echo 'hi'")
914
923
915 nt.assert_equal(ip.user_ns['output'].read(), b'hi\n')
924 nt.assert_equal(ip.user_ns['output'].read(), b'hi\n')
916 ip.user_ns['output'].close()
925 ip.user_ns['output'].close()
917
926
918 @dec.skip_win32
927 @dec.skip_win32
919 def test_script_bg_err():
928 def test_script_bg_err():
920 ip = get_ipython()
929 ip = get_ipython()
921 ip.run_cell_magic("script", "--bg --err error sh", "echo 'hello' >&2")
930 ip.run_cell_magic("script", "--bg --err error sh", "echo 'hello' >&2")
922 nt.assert_equal(ip.user_ns['error'].read(), b'hello\n')
931 nt.assert_equal(ip.user_ns['error'].read(), b'hello\n')
923 ip.user_ns['error'].close()
932 ip.user_ns['error'].close()
924
933
925 @dec.skip_win32
934 @dec.skip_win32
926 def test_script_bg_out_err():
935 def test_script_bg_out_err():
927 ip = get_ipython()
936 ip = get_ipython()
928 ip.run_cell_magic("script", "--bg --out output --err error sh", "echo 'hi'\necho 'hello' >&2")
937 ip.run_cell_magic("script", "--bg --out output --err error sh", "echo 'hi'\necho 'hello' >&2")
929 nt.assert_equal(ip.user_ns['output'].read(), b'hi\n')
938 nt.assert_equal(ip.user_ns['output'].read(), b'hi\n')
930 nt.assert_equal(ip.user_ns['error'].read(), b'hello\n')
939 nt.assert_equal(ip.user_ns['error'].read(), b'hello\n')
931 ip.user_ns['output'].close()
940 ip.user_ns['output'].close()
932 ip.user_ns['error'].close()
941 ip.user_ns['error'].close()
933
942
934 def test_script_defaults():
943 def test_script_defaults():
935 ip = get_ipython()
944 ip = get_ipython()
936 for cmd in ['sh', 'bash', 'perl', 'ruby']:
945 for cmd in ['sh', 'bash', 'perl', 'ruby']:
937 try:
946 try:
938 find_cmd(cmd)
947 find_cmd(cmd)
939 except Exception:
948 except Exception:
940 pass
949 pass
941 else:
950 else:
942 nt.assert_in(cmd, ip.magics_manager.magics['cell'])
951 nt.assert_in(cmd, ip.magics_manager.magics['cell'])
943
952
944
953
945 @magics_class
954 @magics_class
946 class FooFoo(Magics):
955 class FooFoo(Magics):
947 """class with both %foo and %%foo magics"""
956 """class with both %foo and %%foo magics"""
948 @line_magic('foo')
957 @line_magic('foo')
949 def line_foo(self, line):
958 def line_foo(self, line):
950 "I am line foo"
959 "I am line foo"
951 pass
960 pass
952
961
953 @cell_magic("foo")
962 @cell_magic("foo")
954 def cell_foo(self, line, cell):
963 def cell_foo(self, line, cell):
955 "I am cell foo, not line foo"
964 "I am cell foo, not line foo"
956 pass
965 pass
957
966
958 def test_line_cell_info():
967 def test_line_cell_info():
959 """%%foo and %foo magics are distinguishable to inspect"""
968 """%%foo and %foo magics are distinguishable to inspect"""
960 ip = get_ipython()
969 ip = get_ipython()
961 ip.magics_manager.register(FooFoo)
970 ip.magics_manager.register(FooFoo)
962 oinfo = ip.object_inspect('foo')
971 oinfo = ip.object_inspect('foo')
963 nt.assert_true(oinfo['found'])
972 nt.assert_true(oinfo['found'])
964 nt.assert_true(oinfo['ismagic'])
973 nt.assert_true(oinfo['ismagic'])
965
974
966 oinfo = ip.object_inspect('%%foo')
975 oinfo = ip.object_inspect('%%foo')
967 nt.assert_true(oinfo['found'])
976 nt.assert_true(oinfo['found'])
968 nt.assert_true(oinfo['ismagic'])
977 nt.assert_true(oinfo['ismagic'])
969 nt.assert_equal(oinfo['docstring'], FooFoo.cell_foo.__doc__)
978 nt.assert_equal(oinfo['docstring'], FooFoo.cell_foo.__doc__)
970
979
971 oinfo = ip.object_inspect('%foo')
980 oinfo = ip.object_inspect('%foo')
972 nt.assert_true(oinfo['found'])
981 nt.assert_true(oinfo['found'])
973 nt.assert_true(oinfo['ismagic'])
982 nt.assert_true(oinfo['ismagic'])
974 nt.assert_equal(oinfo['docstring'], FooFoo.line_foo.__doc__)
983 nt.assert_equal(oinfo['docstring'], FooFoo.line_foo.__doc__)
975
984
976 def test_multiple_magics():
985 def test_multiple_magics():
977 ip = get_ipython()
986 ip = get_ipython()
978 foo1 = FooFoo(ip)
987 foo1 = FooFoo(ip)
979 foo2 = FooFoo(ip)
988 foo2 = FooFoo(ip)
980 mm = ip.magics_manager
989 mm = ip.magics_manager
981 mm.register(foo1)
990 mm.register(foo1)
982 nt.assert_true(mm.magics['line']['foo'].__self__ is foo1)
991 nt.assert_true(mm.magics['line']['foo'].__self__ is foo1)
983 mm.register(foo2)
992 mm.register(foo2)
984 nt.assert_true(mm.magics['line']['foo'].__self__ is foo2)
993 nt.assert_true(mm.magics['line']['foo'].__self__ is foo2)
985
994
986 def test_alias_magic():
995 def test_alias_magic():
987 """Test %alias_magic."""
996 """Test %alias_magic."""
988 ip = get_ipython()
997 ip = get_ipython()
989 mm = ip.magics_manager
998 mm = ip.magics_manager
990
999
991 # Basic operation: both cell and line magics are created, if possible.
1000 # Basic operation: both cell and line magics are created, if possible.
992 ip.run_line_magic('alias_magic', 'timeit_alias timeit')
1001 ip.run_line_magic('alias_magic', 'timeit_alias timeit')
993 nt.assert_in('timeit_alias', mm.magics['line'])
1002 nt.assert_in('timeit_alias', mm.magics['line'])
994 nt.assert_in('timeit_alias', mm.magics['cell'])
1003 nt.assert_in('timeit_alias', mm.magics['cell'])
995
1004
996 # --cell is specified, line magic not created.
1005 # --cell is specified, line magic not created.
997 ip.run_line_magic('alias_magic', '--cell timeit_cell_alias timeit')
1006 ip.run_line_magic('alias_magic', '--cell timeit_cell_alias timeit')
998 nt.assert_not_in('timeit_cell_alias', mm.magics['line'])
1007 nt.assert_not_in('timeit_cell_alias', mm.magics['line'])
999 nt.assert_in('timeit_cell_alias', mm.magics['cell'])
1008 nt.assert_in('timeit_cell_alias', mm.magics['cell'])
1000
1009
1001 # Test that line alias is created successfully.
1010 # Test that line alias is created successfully.
1002 ip.run_line_magic('alias_magic', '--line env_alias env')
1011 ip.run_line_magic('alias_magic', '--line env_alias env')
1003 nt.assert_equal(ip.run_line_magic('env', ''),
1012 nt.assert_equal(ip.run_line_magic('env', ''),
1004 ip.run_line_magic('env_alias', ''))
1013 ip.run_line_magic('env_alias', ''))
1005
1014
1006 # Test that line alias with parameters passed in is created successfully.
1015 # Test that line alias with parameters passed in is created successfully.
1007 ip.run_line_magic('alias_magic', '--line history_alias history --params ' + shlex.quote('3'))
1016 ip.run_line_magic('alias_magic', '--line history_alias history --params ' + shlex.quote('3'))
1008 nt.assert_in('history_alias', mm.magics['line'])
1017 nt.assert_in('history_alias', mm.magics['line'])
1009
1018
1010
1019
1011 def test_save():
1020 def test_save():
1012 """Test %save."""
1021 """Test %save."""
1013 ip = get_ipython()
1022 ip = get_ipython()
1014 ip.history_manager.reset() # Clear any existing history.
1023 ip.history_manager.reset() # Clear any existing history.
1015 cmds = [u"a=1", u"def b():\n return a**2", u"print(a, b())"]
1024 cmds = [u"a=1", u"def b():\n return a**2", u"print(a, b())"]
1016 for i, cmd in enumerate(cmds, start=1):
1025 for i, cmd in enumerate(cmds, start=1):
1017 ip.history_manager.store_inputs(i, cmd)
1026 ip.history_manager.store_inputs(i, cmd)
1018 with TemporaryDirectory() as tmpdir:
1027 with TemporaryDirectory() as tmpdir:
1019 file = os.path.join(tmpdir, "testsave.py")
1028 file = os.path.join(tmpdir, "testsave.py")
1020 ip.run_line_magic("save", "%s 1-10" % file)
1029 ip.run_line_magic("save", "%s 1-10" % file)
1021 with open(file) as f:
1030 with open(file) as f:
1022 content = f.read()
1031 content = f.read()
1023 nt.assert_equal(content.count(cmds[0]), 1)
1032 nt.assert_equal(content.count(cmds[0]), 1)
1024 nt.assert_in('coding: utf-8', content)
1033 nt.assert_in('coding: utf-8', content)
1025 ip.run_line_magic("save", "-a %s 1-10" % file)
1034 ip.run_line_magic("save", "-a %s 1-10" % file)
1026 with open(file) as f:
1035 with open(file) as f:
1027 content = f.read()
1036 content = f.read()
1028 nt.assert_equal(content.count(cmds[0]), 2)
1037 nt.assert_equal(content.count(cmds[0]), 2)
1029 nt.assert_in('coding: utf-8', content)
1038 nt.assert_in('coding: utf-8', content)
1030
1039
1031
1040
1032 def test_store():
1041 def test_store():
1033 """Test %store."""
1042 """Test %store."""
1034 ip = get_ipython()
1043 ip = get_ipython()
1035 ip.run_line_magic('load_ext', 'storemagic')
1044 ip.run_line_magic('load_ext', 'storemagic')
1036
1045
1037 # make sure the storage is empty
1046 # make sure the storage is empty
1038 ip.run_line_magic('store', '-z')
1047 ip.run_line_magic('store', '-z')
1039 ip.user_ns['var'] = 42
1048 ip.user_ns['var'] = 42
1040 ip.run_line_magic('store', 'var')
1049 ip.run_line_magic('store', 'var')
1041 ip.user_ns['var'] = 39
1050 ip.user_ns['var'] = 39
1042 ip.run_line_magic('store', '-r')
1051 ip.run_line_magic('store', '-r')
1043 nt.assert_equal(ip.user_ns['var'], 42)
1052 nt.assert_equal(ip.user_ns['var'], 42)
1044
1053
1045 ip.run_line_magic('store', '-d var')
1054 ip.run_line_magic('store', '-d var')
1046 ip.user_ns['var'] = 39
1055 ip.user_ns['var'] = 39
1047 ip.run_line_magic('store' , '-r')
1056 ip.run_line_magic('store' , '-r')
1048 nt.assert_equal(ip.user_ns['var'], 39)
1057 nt.assert_equal(ip.user_ns['var'], 39)
1049
1058
1050
1059
1051 def _run_edit_test(arg_s, exp_filename=None,
1060 def _run_edit_test(arg_s, exp_filename=None,
1052 exp_lineno=-1,
1061 exp_lineno=-1,
1053 exp_contents=None,
1062 exp_contents=None,
1054 exp_is_temp=None):
1063 exp_is_temp=None):
1055 ip = get_ipython()
1064 ip = get_ipython()
1056 M = code.CodeMagics(ip)
1065 M = code.CodeMagics(ip)
1057 last_call = ['','']
1066 last_call = ['','']
1058 opts,args = M.parse_options(arg_s,'prxn:')
1067 opts,args = M.parse_options(arg_s,'prxn:')
1059 filename, lineno, is_temp = M._find_edit_target(ip, args, opts, last_call)
1068 filename, lineno, is_temp = M._find_edit_target(ip, args, opts, last_call)
1060
1069
1061 if exp_filename is not None:
1070 if exp_filename is not None:
1062 nt.assert_equal(exp_filename, filename)
1071 nt.assert_equal(exp_filename, filename)
1063 if exp_contents is not None:
1072 if exp_contents is not None:
1064 with io.open(filename, 'r', encoding='utf-8') as f:
1073 with io.open(filename, 'r', encoding='utf-8') as f:
1065 contents = f.read()
1074 contents = f.read()
1066 nt.assert_equal(exp_contents, contents)
1075 nt.assert_equal(exp_contents, contents)
1067 if exp_lineno != -1:
1076 if exp_lineno != -1:
1068 nt.assert_equal(exp_lineno, lineno)
1077 nt.assert_equal(exp_lineno, lineno)
1069 if exp_is_temp is not None:
1078 if exp_is_temp is not None:
1070 nt.assert_equal(exp_is_temp, is_temp)
1079 nt.assert_equal(exp_is_temp, is_temp)
1071
1080
1072
1081
1073 def test_edit_interactive():
1082 def test_edit_interactive():
1074 """%edit on interactively defined objects"""
1083 """%edit on interactively defined objects"""
1075 ip = get_ipython()
1084 ip = get_ipython()
1076 n = ip.execution_count
1085 n = ip.execution_count
1077 ip.run_cell(u"def foo(): return 1", store_history=True)
1086 ip.run_cell(u"def foo(): return 1", store_history=True)
1078
1087
1079 try:
1088 try:
1080 _run_edit_test("foo")
1089 _run_edit_test("foo")
1081 except code.InteractivelyDefined as e:
1090 except code.InteractivelyDefined as e:
1082 nt.assert_equal(e.index, n)
1091 nt.assert_equal(e.index, n)
1083 else:
1092 else:
1084 raise AssertionError("Should have raised InteractivelyDefined")
1093 raise AssertionError("Should have raised InteractivelyDefined")
1085
1094
1086
1095
1087 def test_edit_cell():
1096 def test_edit_cell():
1088 """%edit [cell id]"""
1097 """%edit [cell id]"""
1089 ip = get_ipython()
1098 ip = get_ipython()
1090
1099
1091 ip.run_cell(u"def foo(): return 1", store_history=True)
1100 ip.run_cell(u"def foo(): return 1", store_history=True)
1092
1101
1093 # test
1102 # test
1094 _run_edit_test("1", exp_contents=ip.user_ns['In'][1], exp_is_temp=True)
1103 _run_edit_test("1", exp_contents=ip.user_ns['In'][1], exp_is_temp=True)
1095
1104
1096 def test_bookmark():
1105 def test_bookmark():
1097 ip = get_ipython()
1106 ip = get_ipython()
1098 ip.run_line_magic('bookmark', 'bmname')
1107 ip.run_line_magic('bookmark', 'bmname')
1099 with tt.AssertPrints('bmname'):
1108 with tt.AssertPrints('bmname'):
1100 ip.run_line_magic('bookmark', '-l')
1109 ip.run_line_magic('bookmark', '-l')
1101 ip.run_line_magic('bookmark', '-d bmname')
1110 ip.run_line_magic('bookmark', '-d bmname')
1102
1111
1103 def test_ls_magic():
1112 def test_ls_magic():
1104 ip = get_ipython()
1113 ip = get_ipython()
1105 json_formatter = ip.display_formatter.formatters['application/json']
1114 json_formatter = ip.display_formatter.formatters['application/json']
1106 json_formatter.enabled = True
1115 json_formatter.enabled = True
1107 lsmagic = ip.magic('lsmagic')
1116 lsmagic = ip.magic('lsmagic')
1108 with warnings.catch_warnings(record=True) as w:
1117 with warnings.catch_warnings(record=True) as w:
1109 j = json_formatter(lsmagic)
1118 j = json_formatter(lsmagic)
1110 nt.assert_equal(sorted(j), ['cell', 'line'])
1119 nt.assert_equal(sorted(j), ['cell', 'line'])
1111 nt.assert_equal(w, []) # no warnings
1120 nt.assert_equal(w, []) # no warnings
1112
1121
1113 def test_strip_initial_indent():
1122 def test_strip_initial_indent():
1114 def sii(s):
1123 def sii(s):
1115 lines = s.splitlines()
1124 lines = s.splitlines()
1116 return '\n'.join(code.strip_initial_indent(lines))
1125 return '\n'.join(code.strip_initial_indent(lines))
1117
1126
1118 nt.assert_equal(sii(" a = 1\nb = 2"), "a = 1\nb = 2")
1127 nt.assert_equal(sii(" a = 1\nb = 2"), "a = 1\nb = 2")
1119 nt.assert_equal(sii(" a\n b\nc"), "a\n b\nc")
1128 nt.assert_equal(sii(" a\n b\nc"), "a\n b\nc")
1120 nt.assert_equal(sii("a\n b"), "a\n b")
1129 nt.assert_equal(sii("a\n b"), "a\n b")
1121
1130
1122 def test_logging_magic_quiet_from_arg():
1131 def test_logging_magic_quiet_from_arg():
1123 _ip.config.LoggingMagics.quiet = False
1132 _ip.config.LoggingMagics.quiet = False
1124 lm = logging.LoggingMagics(shell=_ip)
1133 lm = logging.LoggingMagics(shell=_ip)
1125 with TemporaryDirectory() as td:
1134 with TemporaryDirectory() as td:
1126 try:
1135 try:
1127 with tt.AssertNotPrints(re.compile("Activating.*")):
1136 with tt.AssertNotPrints(re.compile("Activating.*")):
1128 lm.logstart('-q {}'.format(
1137 lm.logstart('-q {}'.format(
1129 os.path.join(td, "quiet_from_arg.log")))
1138 os.path.join(td, "quiet_from_arg.log")))
1130 finally:
1139 finally:
1131 _ip.logger.logstop()
1140 _ip.logger.logstop()
1132
1141
1133 def test_logging_magic_quiet_from_config():
1142 def test_logging_magic_quiet_from_config():
1134 _ip.config.LoggingMagics.quiet = True
1143 _ip.config.LoggingMagics.quiet = True
1135 lm = logging.LoggingMagics(shell=_ip)
1144 lm = logging.LoggingMagics(shell=_ip)
1136 with TemporaryDirectory() as td:
1145 with TemporaryDirectory() as td:
1137 try:
1146 try:
1138 with tt.AssertNotPrints(re.compile("Activating.*")):
1147 with tt.AssertNotPrints(re.compile("Activating.*")):
1139 lm.logstart(os.path.join(td, "quiet_from_config.log"))
1148 lm.logstart(os.path.join(td, "quiet_from_config.log"))
1140 finally:
1149 finally:
1141 _ip.logger.logstop()
1150 _ip.logger.logstop()
1142
1151
1143
1152
1144 def test_logging_magic_not_quiet():
1153 def test_logging_magic_not_quiet():
1145 _ip.config.LoggingMagics.quiet = False
1154 _ip.config.LoggingMagics.quiet = False
1146 lm = logging.LoggingMagics(shell=_ip)
1155 lm = logging.LoggingMagics(shell=_ip)
1147 with TemporaryDirectory() as td:
1156 with TemporaryDirectory() as td:
1148 try:
1157 try:
1149 with tt.AssertPrints(re.compile("Activating.*")):
1158 with tt.AssertPrints(re.compile("Activating.*")):
1150 lm.logstart(os.path.join(td, "not_quiet.log"))
1159 lm.logstart(os.path.join(td, "not_quiet.log"))
1151 finally:
1160 finally:
1152 _ip.logger.logstop()
1161 _ip.logger.logstop()
1153
1162
1154
1163
1155 def test_time_no_var_expand():
1164 def test_time_no_var_expand():
1156 _ip.user_ns['a'] = 5
1165 _ip.user_ns['a'] = 5
1157 _ip.user_ns['b'] = []
1166 _ip.user_ns['b'] = []
1158 _ip.magic('time b.append("{a}")')
1167 _ip.magic('time b.append("{a}")')
1159 assert _ip.user_ns['b'] == ['{a}']
1168 assert _ip.user_ns['b'] == ['{a}']
1160
1169
1161
1170
1162 # this is slow, put at the end for local testing.
1171 # this is slow, put at the end for local testing.
1163 def test_timeit_arguments():
1172 def test_timeit_arguments():
1164 "Test valid timeit arguments, should not cause SyntaxError (GH #1269)"
1173 "Test valid timeit arguments, should not cause SyntaxError (GH #1269)"
1165 if sys.version_info < (3,7):
1174 if sys.version_info < (3,7):
1166 _ip.magic("timeit -n1 -r1 ('#')")
1175 _ip.magic("timeit -n1 -r1 ('#')")
1167 else:
1176 else:
1168 # 3.7 optimize no-op statement like above out, and complain there is
1177 # 3.7 optimize no-op statement like above out, and complain there is
1169 # nothing in the for loop.
1178 # nothing in the for loop.
1170 _ip.magic("timeit -n1 -r1 a=('#')")
1179 _ip.magic("timeit -n1 -r1 a=('#')")
General Comments 0
You need to be logged in to leave comments. Login now