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