Show More
@@ -1,461 +1,454 b'' | |||||
1 | """Tests for various magic functions. |
|
1 | """Tests for various magic functions. | |
2 |
|
2 | |||
3 | Needs to be run by nose (to make ipython session available). |
|
3 | Needs to be run by nose (to make ipython session available). | |
4 | """ |
|
4 | """ | |
5 | from __future__ import absolute_import |
|
5 | from __future__ import absolute_import | |
6 |
|
6 | |||
7 | #----------------------------------------------------------------------------- |
|
7 | #----------------------------------------------------------------------------- | |
8 | # Imports |
|
8 | # Imports | |
9 | #----------------------------------------------------------------------------- |
|
9 | #----------------------------------------------------------------------------- | |
10 |
|
10 | |||
11 | import os |
|
11 | import os | |
12 | import sys |
|
12 | import sys | |
13 | import tempfile |
|
13 | import tempfile | |
14 | import types |
|
14 | import types | |
15 | from StringIO import StringIO |
|
15 | from StringIO import StringIO | |
16 |
|
16 | |||
17 | import nose.tools as nt |
|
17 | import nose.tools as nt | |
18 |
|
18 | |||
19 | from IPython.utils.path import get_long_path_name |
|
19 | from IPython.utils.path import get_long_path_name | |
20 | from IPython.testing import decorators as dec |
|
20 | from IPython.testing import decorators as dec | |
21 | from IPython.testing import tools as tt |
|
21 | from IPython.testing import tools as tt | |
22 | from IPython.utils import py3compat |
|
22 | from IPython.utils import py3compat | |
23 |
|
23 | |||
24 | #----------------------------------------------------------------------------- |
|
24 | #----------------------------------------------------------------------------- | |
25 | # Test functions begin |
|
25 | # Test functions begin | |
26 | #----------------------------------------------------------------------------- |
|
26 | #----------------------------------------------------------------------------- | |
27 | def test_rehashx(): |
|
27 | def test_rehashx(): | |
28 | # clear up everything |
|
28 | # clear up everything | |
29 | _ip = get_ipython() |
|
29 | _ip = get_ipython() | |
30 | _ip.alias_manager.alias_table.clear() |
|
30 | _ip.alias_manager.alias_table.clear() | |
31 | del _ip.db['syscmdlist'] |
|
31 | del _ip.db['syscmdlist'] | |
32 |
|
32 | |||
33 | _ip.magic('rehashx') |
|
33 | _ip.magic('rehashx') | |
34 | # Practically ALL ipython development systems will have more than 10 aliases |
|
34 | # Practically ALL ipython development systems will have more than 10 aliases | |
35 |
|
35 | |||
36 | yield (nt.assert_true, len(_ip.alias_manager.alias_table) > 10) |
|
36 | yield (nt.assert_true, len(_ip.alias_manager.alias_table) > 10) | |
37 | for key, val in _ip.alias_manager.alias_table.iteritems(): |
|
37 | for key, val in _ip.alias_manager.alias_table.iteritems(): | |
38 | # we must strip dots from alias names |
|
38 | # we must strip dots from alias names | |
39 | nt.assert_true('.' not in key) |
|
39 | nt.assert_true('.' not in key) | |
40 |
|
40 | |||
41 | # rehashx must fill up syscmdlist |
|
41 | # rehashx must fill up syscmdlist | |
42 | scoms = _ip.db['syscmdlist'] |
|
42 | scoms = _ip.db['syscmdlist'] | |
43 | yield (nt.assert_true, len(scoms) > 10) |
|
43 | yield (nt.assert_true, len(scoms) > 10) | |
44 |
|
44 | |||
45 |
|
45 | |||
46 | def test_magic_parse_options(): |
|
46 | def test_magic_parse_options(): | |
47 | """Test that we don't mangle paths when parsing magic options.""" |
|
47 | """Test that we don't mangle paths when parsing magic options.""" | |
48 | ip = get_ipython() |
|
48 | ip = get_ipython() | |
49 | path = 'c:\\x' |
|
49 | path = 'c:\\x' | |
50 | opts = ip.parse_options('-f %s' % path,'f:')[0] |
|
50 | opts = ip.parse_options('-f %s' % path,'f:')[0] | |
51 | # argv splitting is os-dependent |
|
51 | # argv splitting is os-dependent | |
52 | if os.name == 'posix': |
|
52 | if os.name == 'posix': | |
53 | expected = 'c:x' |
|
53 | expected = 'c:x' | |
54 | else: |
|
54 | else: | |
55 | expected = path |
|
55 | expected = path | |
56 | nt.assert_equals(opts['f'], expected) |
|
56 | nt.assert_equals(opts['f'], expected) | |
57 |
|
57 | |||
58 |
|
58 | |||
59 | def doctest_hist_f(): |
|
59 | def doctest_hist_f(): | |
60 | """Test %hist -f with temporary filename. |
|
60 | """Test %hist -f with temporary filename. | |
61 |
|
61 | |||
62 | In [9]: import tempfile |
|
62 | In [9]: import tempfile | |
63 |
|
63 | |||
64 | In [10]: tfile = tempfile.mktemp('.py','tmp-ipython-') |
|
64 | In [10]: tfile = tempfile.mktemp('.py','tmp-ipython-') | |
65 |
|
65 | |||
66 | In [11]: %hist -nl -f $tfile 3 |
|
66 | In [11]: %hist -nl -f $tfile 3 | |
67 |
|
67 | |||
68 | In [13]: import os; os.unlink(tfile) |
|
68 | In [13]: import os; os.unlink(tfile) | |
69 | """ |
|
69 | """ | |
70 |
|
70 | |||
71 |
|
71 | |||
72 | def doctest_hist_r(): |
|
72 | def doctest_hist_r(): | |
73 | """Test %hist -r |
|
73 | """Test %hist -r | |
74 |
|
74 | |||
75 | XXX - This test is not recording the output correctly. For some reason, in |
|
75 | XXX - This test is not recording the output correctly. For some reason, in | |
76 | testing mode the raw history isn't getting populated. No idea why. |
|
76 | testing mode the raw history isn't getting populated. No idea why. | |
77 | Disabling the output checking for now, though at least we do run it. |
|
77 | Disabling the output checking for now, though at least we do run it. | |
78 |
|
78 | |||
79 | In [1]: 'hist' in _ip.lsmagic() |
|
79 | In [1]: 'hist' in _ip.lsmagic() | |
80 | Out[1]: True |
|
80 | Out[1]: True | |
81 |
|
81 | |||
82 | In [2]: x=1 |
|
82 | In [2]: x=1 | |
83 |
|
83 | |||
84 | In [3]: %hist -rl 2 |
|
84 | In [3]: %hist -rl 2 | |
85 | x=1 # random |
|
85 | x=1 # random | |
86 | %hist -r 2 |
|
86 | %hist -r 2 | |
87 | """ |
|
87 | """ | |
88 |
|
88 | |||
89 | def doctest_hist_op(): |
|
89 | def doctest_hist_op(): | |
90 | """Test %hist -op |
|
90 | """Test %hist -op | |
91 |
|
91 | |||
92 | In [1]: class b(float): |
|
92 | In [1]: class b(float): | |
93 | ...: pass |
|
93 | ...: pass | |
94 | ...: |
|
94 | ...: | |
95 |
|
95 | |||
96 | In [2]: class s(object): |
|
96 | In [2]: class s(object): | |
97 | ...: def __str__(self): |
|
97 | ...: def __str__(self): | |
98 | ...: return 's' |
|
98 | ...: return 's' | |
99 | ...: |
|
99 | ...: | |
100 |
|
100 | |||
101 | In [3]: |
|
101 | In [3]: | |
102 |
|
102 | |||
103 | In [4]: class r(b): |
|
103 | In [4]: class r(b): | |
104 | ...: def __repr__(self): |
|
104 | ...: def __repr__(self): | |
105 | ...: return 'r' |
|
105 | ...: return 'r' | |
106 | ...: |
|
106 | ...: | |
107 |
|
107 | |||
108 | In [5]: class sr(s,r): pass |
|
108 | In [5]: class sr(s,r): pass | |
109 | ...: |
|
109 | ...: | |
110 |
|
110 | |||
111 | In [6]: |
|
111 | In [6]: | |
112 |
|
112 | |||
113 | In [7]: bb=b() |
|
113 | In [7]: bb=b() | |
114 |
|
114 | |||
115 | In [8]: ss=s() |
|
115 | In [8]: ss=s() | |
116 |
|
116 | |||
117 | In [9]: rr=r() |
|
117 | In [9]: rr=r() | |
118 |
|
118 | |||
119 | In [10]: ssrr=sr() |
|
119 | In [10]: ssrr=sr() | |
120 |
|
120 | |||
121 | In [11]: 4.5 |
|
121 | In [11]: 4.5 | |
122 | Out[11]: 4.5 |
|
122 | Out[11]: 4.5 | |
123 |
|
123 | |||
124 | In [12]: str(ss) |
|
124 | In [12]: str(ss) | |
125 | Out[12]: 's' |
|
125 | Out[12]: 's' | |
126 |
|
126 | |||
127 | In [13]: |
|
127 | In [13]: | |
128 |
|
128 | |||
129 | In [14]: %hist -op |
|
129 | In [14]: %hist -op | |
130 | >>> class b: |
|
130 | >>> class b: | |
131 | ... pass |
|
131 | ... pass | |
132 | ... |
|
132 | ... | |
133 | >>> class s(b): |
|
133 | >>> class s(b): | |
134 | ... def __str__(self): |
|
134 | ... def __str__(self): | |
135 | ... return 's' |
|
135 | ... return 's' | |
136 | ... |
|
136 | ... | |
137 | >>> |
|
137 | >>> | |
138 | >>> class r(b): |
|
138 | >>> class r(b): | |
139 | ... def __repr__(self): |
|
139 | ... def __repr__(self): | |
140 | ... return 'r' |
|
140 | ... return 'r' | |
141 | ... |
|
141 | ... | |
142 | >>> class sr(s,r): pass |
|
142 | >>> class sr(s,r): pass | |
143 | >>> |
|
143 | >>> | |
144 | >>> bb=b() |
|
144 | >>> bb=b() | |
145 | >>> ss=s() |
|
145 | >>> ss=s() | |
146 | >>> rr=r() |
|
146 | >>> rr=r() | |
147 | >>> ssrr=sr() |
|
147 | >>> ssrr=sr() | |
148 | >>> 4.5 |
|
148 | >>> 4.5 | |
149 | 4.5 |
|
149 | 4.5 | |
150 | >>> str(ss) |
|
150 | >>> str(ss) | |
151 | 's' |
|
151 | 's' | |
152 | >>> |
|
152 | >>> | |
153 | """ |
|
153 | """ | |
154 |
|
154 | |||
155 | def test_macro(): |
|
155 | def test_macro(): | |
156 | ip = get_ipython() |
|
156 | ip = get_ipython() | |
157 | ip.history_manager.reset() # Clear any existing history. |
|
157 | ip.history_manager.reset() # Clear any existing history. | |
158 | cmds = ["a=1", "def b():\n return a**2", "print(a,b())"] |
|
158 | cmds = ["a=1", "def b():\n return a**2", "print(a,b())"] | |
159 | for i, cmd in enumerate(cmds, start=1): |
|
159 | for i, cmd in enumerate(cmds, start=1): | |
160 | ip.history_manager.store_inputs(i, cmd) |
|
160 | ip.history_manager.store_inputs(i, cmd) | |
161 | ip.magic("macro test 1-3") |
|
161 | ip.magic("macro test 1-3") | |
162 | nt.assert_equal(ip.user_ns["test"].value, "\n".join(cmds)+"\n") |
|
162 | nt.assert_equal(ip.user_ns["test"].value, "\n".join(cmds)+"\n") | |
163 |
|
163 | |||
164 | # List macros. |
|
164 | # List macros. | |
165 | assert "test" in ip.magic("macro") |
|
165 | assert "test" in ip.magic("macro") | |
166 |
|
166 | |||
167 | def test_macro_run(): |
|
167 | def test_macro_run(): | |
168 | """Test that we can run a multi-line macro successfully.""" |
|
168 | """Test that we can run a multi-line macro successfully.""" | |
169 | ip = get_ipython() |
|
169 | ip = get_ipython() | |
170 | ip.history_manager.reset() |
|
170 | ip.history_manager.reset() | |
171 | cmds = ["a=10", "a+=1", py3compat.doctest_refactor_print("print a"), |
|
171 | cmds = ["a=10", "a+=1", py3compat.doctest_refactor_print("print a"), | |
172 | "%macro test 2-3"] |
|
172 | "%macro test 2-3"] | |
173 | for cmd in cmds: |
|
173 | for cmd in cmds: | |
174 | ip.run_cell(cmd) |
|
174 | ip.run_cell(cmd) | |
175 | nt.assert_equal(ip.user_ns["test"].value, |
|
175 | nt.assert_equal(ip.user_ns["test"].value, | |
176 | py3compat.doctest_refactor_print("a+=1\nprint a\n")) |
|
176 | py3compat.doctest_refactor_print("a+=1\nprint a\n")) | |
177 | original_stdout = sys.stdout |
|
177 | with tt.AssertPrints("12"): | |
178 | new_stdout = StringIO() |
|
|||
179 | sys.stdout = new_stdout |
|
|||
180 | try: |
|
|||
181 | ip.run_cell("test") |
|
178 | ip.run_cell("test") | |
182 | nt.assert_true("12" in new_stdout.getvalue()) |
|
179 | with tt.AssertPrints("13"): | |
183 | ip.run_cell("test") |
|
180 | ip.run_cell("test") | |
184 | nt.assert_true("13" in new_stdout.getvalue()) |
|
|||
185 | finally: |
|
|||
186 | sys.stdout = original_stdout |
|
|||
187 | new_stdout.close() |
|
|||
188 |
|
181 | |||
189 |
|
182 | |||
190 | # XXX failing for now, until we get clearcmd out of quarantine. But we should |
|
183 | # XXX failing for now, until we get clearcmd out of quarantine. But we should | |
191 | # fix this and revert the skip to happen only if numpy is not around. |
|
184 | # fix this and revert the skip to happen only if numpy is not around. | |
192 | #@dec.skipif_not_numpy |
|
185 | #@dec.skipif_not_numpy | |
193 | @dec.skip_known_failure |
|
186 | @dec.skip_known_failure | |
194 | def test_numpy_clear_array_undec(): |
|
187 | def test_numpy_clear_array_undec(): | |
195 | from IPython.extensions import clearcmd |
|
188 | from IPython.extensions import clearcmd | |
196 |
|
189 | |||
197 | _ip.ex('import numpy as np') |
|
190 | _ip.ex('import numpy as np') | |
198 | _ip.ex('a = np.empty(2)') |
|
191 | _ip.ex('a = np.empty(2)') | |
199 | yield (nt.assert_true, 'a' in _ip.user_ns) |
|
192 | yield (nt.assert_true, 'a' in _ip.user_ns) | |
200 | _ip.magic('clear array') |
|
193 | _ip.magic('clear array') | |
201 | yield (nt.assert_false, 'a' in _ip.user_ns) |
|
194 | yield (nt.assert_false, 'a' in _ip.user_ns) | |
202 |
|
195 | |||
203 |
|
196 | |||
204 | # Multiple tests for clipboard pasting |
|
197 | # Multiple tests for clipboard pasting | |
205 | @dec.parametric |
|
198 | @dec.parametric | |
206 | def test_paste(): |
|
199 | def test_paste(): | |
207 | _ip = get_ipython() |
|
200 | _ip = get_ipython() | |
208 | def paste(txt, flags='-q'): |
|
201 | def paste(txt, flags='-q'): | |
209 | """Paste input text, by default in quiet mode""" |
|
202 | """Paste input text, by default in quiet mode""" | |
210 | hooks.clipboard_get = lambda : txt |
|
203 | hooks.clipboard_get = lambda : txt | |
211 | _ip.magic('paste '+flags) |
|
204 | _ip.magic('paste '+flags) | |
212 |
|
205 | |||
213 | # Inject fake clipboard hook but save original so we can restore it later |
|
206 | # Inject fake clipboard hook but save original so we can restore it later | |
214 | hooks = _ip.hooks |
|
207 | hooks = _ip.hooks | |
215 | user_ns = _ip.user_ns |
|
208 | user_ns = _ip.user_ns | |
216 | original_clip = hooks.clipboard_get |
|
209 | original_clip = hooks.clipboard_get | |
217 |
|
210 | |||
218 | try: |
|
211 | try: | |
219 | # Run tests with fake clipboard function |
|
212 | # Run tests with fake clipboard function | |
220 | user_ns.pop('x', None) |
|
213 | user_ns.pop('x', None) | |
221 | paste('x=1') |
|
214 | paste('x=1') | |
222 | yield nt.assert_equal(user_ns['x'], 1) |
|
215 | yield nt.assert_equal(user_ns['x'], 1) | |
223 |
|
216 | |||
224 | user_ns.pop('x', None) |
|
217 | user_ns.pop('x', None) | |
225 | paste('>>> x=2') |
|
218 | paste('>>> x=2') | |
226 | yield nt.assert_equal(user_ns['x'], 2) |
|
219 | yield nt.assert_equal(user_ns['x'], 2) | |
227 |
|
220 | |||
228 | paste(""" |
|
221 | paste(""" | |
229 | >>> x = [1,2,3] |
|
222 | >>> x = [1,2,3] | |
230 | >>> y = [] |
|
223 | >>> y = [] | |
231 | >>> for i in x: |
|
224 | >>> for i in x: | |
232 | ... y.append(i**2) |
|
225 | ... y.append(i**2) | |
233 | ... |
|
226 | ... | |
234 | """) |
|
227 | """) | |
235 | yield nt.assert_equal(user_ns['x'], [1,2,3]) |
|
228 | yield nt.assert_equal(user_ns['x'], [1,2,3]) | |
236 | yield nt.assert_equal(user_ns['y'], [1,4,9]) |
|
229 | yield nt.assert_equal(user_ns['y'], [1,4,9]) | |
237 |
|
230 | |||
238 | # Now, test that paste -r works |
|
231 | # Now, test that paste -r works | |
239 | user_ns.pop('x', None) |
|
232 | user_ns.pop('x', None) | |
240 | yield nt.assert_false('x' in user_ns) |
|
233 | yield nt.assert_false('x' in user_ns) | |
241 | _ip.magic('paste -r') |
|
234 | _ip.magic('paste -r') | |
242 | yield nt.assert_equal(user_ns['x'], [1,2,3]) |
|
235 | yield nt.assert_equal(user_ns['x'], [1,2,3]) | |
243 |
|
236 | |||
244 | # Also test paste echoing, by temporarily faking the writer |
|
237 | # Also test paste echoing, by temporarily faking the writer | |
245 | w = StringIO() |
|
238 | w = StringIO() | |
246 | writer = _ip.write |
|
239 | writer = _ip.write | |
247 | _ip.write = w.write |
|
240 | _ip.write = w.write | |
248 | code = """ |
|
241 | code = """ | |
249 | a = 100 |
|
242 | a = 100 | |
250 | b = 200""" |
|
243 | b = 200""" | |
251 | try: |
|
244 | try: | |
252 | paste(code,'') |
|
245 | paste(code,'') | |
253 | out = w.getvalue() |
|
246 | out = w.getvalue() | |
254 | finally: |
|
247 | finally: | |
255 | _ip.write = writer |
|
248 | _ip.write = writer | |
256 | yield nt.assert_equal(user_ns['a'], 100) |
|
249 | yield nt.assert_equal(user_ns['a'], 100) | |
257 | yield nt.assert_equal(user_ns['b'], 200) |
|
250 | yield nt.assert_equal(user_ns['b'], 200) | |
258 | yield nt.assert_equal(out, code+"\n## -- End pasted text --\n") |
|
251 | yield nt.assert_equal(out, code+"\n## -- End pasted text --\n") | |
259 |
|
252 | |||
260 | finally: |
|
253 | finally: | |
261 | # Restore original hook |
|
254 | # Restore original hook | |
262 | hooks.clipboard_get = original_clip |
|
255 | hooks.clipboard_get = original_clip | |
263 |
|
256 | |||
264 |
|
257 | |||
265 | def test_time(): |
|
258 | def test_time(): | |
266 | _ip.magic('time None') |
|
259 | _ip.magic('time None') | |
267 |
|
260 | |||
268 |
|
261 | |||
269 | @py3compat.doctest_refactor_print |
|
262 | @py3compat.doctest_refactor_print | |
270 | def doctest_time(): |
|
263 | def doctest_time(): | |
271 | """ |
|
264 | """ | |
272 | In [10]: %time None |
|
265 | In [10]: %time None | |
273 | CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s |
|
266 | CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s | |
274 | Wall time: 0.00 s |
|
267 | Wall time: 0.00 s | |
275 |
|
268 | |||
276 | In [11]: def f(kmjy): |
|
269 | In [11]: def f(kmjy): | |
277 | ....: %time print 2*kmjy |
|
270 | ....: %time print 2*kmjy | |
278 |
|
271 | |||
279 | In [12]: f(3) |
|
272 | In [12]: f(3) | |
280 | 6 |
|
273 | 6 | |
281 | CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s |
|
274 | CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s | |
282 | Wall time: 0.00 s |
|
275 | Wall time: 0.00 s | |
283 | """ |
|
276 | """ | |
284 |
|
277 | |||
285 |
|
278 | |||
286 | def test_doctest_mode(): |
|
279 | def test_doctest_mode(): | |
287 | "Toggle doctest_mode twice, it should be a no-op and run without error" |
|
280 | "Toggle doctest_mode twice, it should be a no-op and run without error" | |
288 | _ip.magic('doctest_mode') |
|
281 | _ip.magic('doctest_mode') | |
289 | _ip.magic('doctest_mode') |
|
282 | _ip.magic('doctest_mode') | |
290 |
|
283 | |||
291 |
|
284 | |||
292 | def test_parse_options(): |
|
285 | def test_parse_options(): | |
293 | """Tests for basic options parsing in magics.""" |
|
286 | """Tests for basic options parsing in magics.""" | |
294 | # These are only the most minimal of tests, more should be added later. At |
|
287 | # These are only the most minimal of tests, more should be added later. At | |
295 | # the very least we check that basic text/unicode calls work OK. |
|
288 | # the very least we check that basic text/unicode calls work OK. | |
296 | nt.assert_equal(_ip.parse_options('foo', '')[1], 'foo') |
|
289 | nt.assert_equal(_ip.parse_options('foo', '')[1], 'foo') | |
297 | nt.assert_equal(_ip.parse_options(u'foo', '')[1], u'foo') |
|
290 | nt.assert_equal(_ip.parse_options(u'foo', '')[1], u'foo') | |
298 |
|
291 | |||
299 |
|
292 | |||
300 | def test_dirops(): |
|
293 | def test_dirops(): | |
301 | """Test various directory handling operations.""" |
|
294 | """Test various directory handling operations.""" | |
302 | # curpath = lambda :os.path.splitdrive(os.getcwdu())[1].replace('\\','/') |
|
295 | # curpath = lambda :os.path.splitdrive(os.getcwdu())[1].replace('\\','/') | |
303 | curpath = os.getcwdu |
|
296 | curpath = os.getcwdu | |
304 | startdir = os.getcwdu() |
|
297 | startdir = os.getcwdu() | |
305 | ipdir = _ip.ipython_dir |
|
298 | ipdir = _ip.ipython_dir | |
306 | try: |
|
299 | try: | |
307 | _ip.magic('cd "%s"' % ipdir) |
|
300 | _ip.magic('cd "%s"' % ipdir) | |
308 | nt.assert_equal(curpath(), ipdir) |
|
301 | nt.assert_equal(curpath(), ipdir) | |
309 | _ip.magic('cd -') |
|
302 | _ip.magic('cd -') | |
310 | nt.assert_equal(curpath(), startdir) |
|
303 | nt.assert_equal(curpath(), startdir) | |
311 | _ip.magic('pushd "%s"' % ipdir) |
|
304 | _ip.magic('pushd "%s"' % ipdir) | |
312 | nt.assert_equal(curpath(), ipdir) |
|
305 | nt.assert_equal(curpath(), ipdir) | |
313 | _ip.magic('popd') |
|
306 | _ip.magic('popd') | |
314 | nt.assert_equal(curpath(), startdir) |
|
307 | nt.assert_equal(curpath(), startdir) | |
315 | finally: |
|
308 | finally: | |
316 | os.chdir(startdir) |
|
309 | os.chdir(startdir) | |
317 |
|
310 | |||
318 |
|
311 | |||
319 | def check_cpaste(code, should_fail=False): |
|
312 | def check_cpaste(code, should_fail=False): | |
320 | """Execute code via 'cpaste' and ensure it was executed, unless |
|
313 | """Execute code via 'cpaste' and ensure it was executed, unless | |
321 | should_fail is set. |
|
314 | should_fail is set. | |
322 | """ |
|
315 | """ | |
323 | _ip.user_ns['code_ran'] = False |
|
316 | _ip.user_ns['code_ran'] = False | |
324 |
|
317 | |||
325 | src = StringIO() |
|
318 | src = StringIO() | |
326 | src.write('\n') |
|
319 | src.write('\n') | |
327 | src.write(code) |
|
320 | src.write(code) | |
328 | src.write('\n--\n') |
|
321 | src.write('\n--\n') | |
329 | src.seek(0) |
|
322 | src.seek(0) | |
330 |
|
323 | |||
331 | stdin_save = sys.stdin |
|
324 | stdin_save = sys.stdin | |
332 | sys.stdin = src |
|
325 | sys.stdin = src | |
333 |
|
326 | |||
334 | try: |
|
327 | try: | |
335 | _ip.magic('cpaste') |
|
328 | _ip.magic('cpaste') | |
336 | except: |
|
329 | except: | |
337 | if not should_fail: |
|
330 | if not should_fail: | |
338 | raise AssertionError("Failure not expected : '%s'" % |
|
331 | raise AssertionError("Failure not expected : '%s'" % | |
339 | code) |
|
332 | code) | |
340 | else: |
|
333 | else: | |
341 | assert _ip.user_ns['code_ran'] |
|
334 | assert _ip.user_ns['code_ran'] | |
342 | if should_fail: |
|
335 | if should_fail: | |
343 | raise AssertionError("Failure expected : '%s'" % code) |
|
336 | raise AssertionError("Failure expected : '%s'" % code) | |
344 | finally: |
|
337 | finally: | |
345 | sys.stdin = stdin_save |
|
338 | sys.stdin = stdin_save | |
346 |
|
339 | |||
347 |
|
340 | |||
348 | def test_cpaste(): |
|
341 | def test_cpaste(): | |
349 | """Test cpaste magic""" |
|
342 | """Test cpaste magic""" | |
350 |
|
343 | |||
351 | def run(): |
|
344 | def run(): | |
352 | """Marker function: sets a flag when executed. |
|
345 | """Marker function: sets a flag when executed. | |
353 | """ |
|
346 | """ | |
354 | _ip.user_ns['code_ran'] = True |
|
347 | _ip.user_ns['code_ran'] = True | |
355 | return 'run' # return string so '+ run()' doesn't result in success |
|
348 | return 'run' # return string so '+ run()' doesn't result in success | |
356 |
|
349 | |||
357 | tests = {'pass': ["> > > run()", |
|
350 | tests = {'pass': ["> > > run()", | |
358 | ">>> > run()", |
|
351 | ">>> > run()", | |
359 | "+++ run()", |
|
352 | "+++ run()", | |
360 | "++ run()", |
|
353 | "++ run()", | |
361 | " >>> run()"], |
|
354 | " >>> run()"], | |
362 |
|
355 | |||
363 | 'fail': ["+ + run()", |
|
356 | 'fail': ["+ + run()", | |
364 | " ++ run()"]} |
|
357 | " ++ run()"]} | |
365 |
|
358 | |||
366 | _ip.user_ns['run'] = run |
|
359 | _ip.user_ns['run'] = run | |
367 |
|
360 | |||
368 | for code in tests['pass']: |
|
361 | for code in tests['pass']: | |
369 | check_cpaste(code) |
|
362 | check_cpaste(code) | |
370 |
|
363 | |||
371 | for code in tests['fail']: |
|
364 | for code in tests['fail']: | |
372 | check_cpaste(code, should_fail=True) |
|
365 | check_cpaste(code, should_fail=True) | |
373 |
|
366 | |||
374 | def test_xmode(): |
|
367 | def test_xmode(): | |
375 | # Calling xmode three times should be a no-op |
|
368 | # Calling xmode three times should be a no-op | |
376 | xmode = _ip.InteractiveTB.mode |
|
369 | xmode = _ip.InteractiveTB.mode | |
377 | for i in range(3): |
|
370 | for i in range(3): | |
378 | _ip.magic("xmode") |
|
371 | _ip.magic("xmode") | |
379 | nt.assert_equal(_ip.InteractiveTB.mode, xmode) |
|
372 | nt.assert_equal(_ip.InteractiveTB.mode, xmode) | |
380 |
|
373 | |||
381 | def test_reset_hard(): |
|
374 | def test_reset_hard(): | |
382 | monitor = [] |
|
375 | monitor = [] | |
383 | class A(object): |
|
376 | class A(object): | |
384 | def __del__(self): |
|
377 | def __del__(self): | |
385 | monitor.append(1) |
|
378 | monitor.append(1) | |
386 | def __repr__(self): |
|
379 | def __repr__(self): | |
387 | return "<A instance>" |
|
380 | return "<A instance>" | |
388 |
|
381 | |||
389 | _ip.user_ns["a"] = A() |
|
382 | _ip.user_ns["a"] = A() | |
390 | _ip.run_cell("a") |
|
383 | _ip.run_cell("a") | |
391 |
|
384 | |||
392 | nt.assert_equal(monitor, []) |
|
385 | nt.assert_equal(monitor, []) | |
393 | _ip.magic_reset("-f") |
|
386 | _ip.magic_reset("-f") | |
394 | nt.assert_equal(monitor, [1]) |
|
387 | nt.assert_equal(monitor, [1]) | |
395 |
|
388 | |||
396 | class TestXdel(tt.TempFileMixin): |
|
389 | class TestXdel(tt.TempFileMixin): | |
397 | def test_xdel(self): |
|
390 | def test_xdel(self): | |
398 | """Test that references from %run are cleared by xdel.""" |
|
391 | """Test that references from %run are cleared by xdel.""" | |
399 | src = ("class A(object):\n" |
|
392 | src = ("class A(object):\n" | |
400 | " monitor = []\n" |
|
393 | " monitor = []\n" | |
401 | " def __del__(self):\n" |
|
394 | " def __del__(self):\n" | |
402 | " self.monitor.append(1)\n" |
|
395 | " self.monitor.append(1)\n" | |
403 | "a = A()\n") |
|
396 | "a = A()\n") | |
404 | self.mktmp(src) |
|
397 | self.mktmp(src) | |
405 | # %run creates some hidden references... |
|
398 | # %run creates some hidden references... | |
406 | _ip.magic("run %s" % self.fname) |
|
399 | _ip.magic("run %s" % self.fname) | |
407 | # ... as does the displayhook. |
|
400 | # ... as does the displayhook. | |
408 | _ip.run_cell("a") |
|
401 | _ip.run_cell("a") | |
409 |
|
402 | |||
410 | monitor = _ip.user_ns["A"].monitor |
|
403 | monitor = _ip.user_ns["A"].monitor | |
411 | nt.assert_equal(monitor, []) |
|
404 | nt.assert_equal(monitor, []) | |
412 |
|
405 | |||
413 | _ip.magic("xdel a") |
|
406 | _ip.magic("xdel a") | |
414 |
|
407 | |||
415 | # Check that a's __del__ method has been called. |
|
408 | # Check that a's __del__ method has been called. | |
416 | nt.assert_equal(monitor, [1]) |
|
409 | nt.assert_equal(monitor, [1]) | |
417 |
|
410 | |||
418 | def doctest_who(): |
|
411 | def doctest_who(): | |
419 | """doctest for %who |
|
412 | """doctest for %who | |
420 |
|
413 | |||
421 | In [1]: %reset -f |
|
414 | In [1]: %reset -f | |
422 |
|
415 | |||
423 | In [2]: alpha = 123 |
|
416 | In [2]: alpha = 123 | |
424 |
|
417 | |||
425 | In [3]: beta = 'beta' |
|
418 | In [3]: beta = 'beta' | |
426 |
|
419 | |||
427 | In [4]: %who int |
|
420 | In [4]: %who int | |
428 | alpha |
|
421 | alpha | |
429 |
|
422 | |||
430 | In [5]: %who str |
|
423 | In [5]: %who str | |
431 | beta |
|
424 | beta | |
432 |
|
425 | |||
433 | In [6]: %whos |
|
426 | In [6]: %whos | |
434 | Variable Type Data/Info |
|
427 | Variable Type Data/Info | |
435 | ---------------------------- |
|
428 | ---------------------------- | |
436 | alpha int 123 |
|
429 | alpha int 123 | |
437 | beta str beta |
|
430 | beta str beta | |
438 |
|
431 | |||
439 | In [7]: %who_ls |
|
432 | In [7]: %who_ls | |
440 | Out[7]: ['alpha', 'beta'] |
|
433 | Out[7]: ['alpha', 'beta'] | |
441 | """ |
|
434 | """ | |
442 |
|
435 | |||
443 | @py3compat.u_format |
|
436 | @py3compat.u_format | |
444 | def doctest_precision(): |
|
437 | def doctest_precision(): | |
445 | """doctest for %precision |
|
438 | """doctest for %precision | |
446 |
|
439 | |||
447 | In [1]: f = get_ipython().shell.display_formatter.formatters['text/plain'] |
|
440 | In [1]: f = get_ipython().shell.display_formatter.formatters['text/plain'] | |
448 |
|
441 | |||
449 | In [2]: %precision 5 |
|
442 | In [2]: %precision 5 | |
450 | Out[2]: {u}'%.5f' |
|
443 | Out[2]: {u}'%.5f' | |
451 |
|
444 | |||
452 | In [3]: f.float_format |
|
445 | In [3]: f.float_format | |
453 | Out[3]: {u}'%.5f' |
|
446 | Out[3]: {u}'%.5f' | |
454 |
|
447 | |||
455 | In [4]: %precision %e |
|
448 | In [4]: %precision %e | |
456 | Out[4]: {u}'%e' |
|
449 | Out[4]: {u}'%e' | |
457 |
|
450 | |||
458 | In [5]: f(3.1415927) |
|
451 | In [5]: f(3.1415927) | |
459 | Out[5]: {u}'3.141593e+00' |
|
452 | Out[5]: {u}'3.141593e+00' | |
460 | """ |
|
453 | """ | |
461 |
|
454 |
General Comments 0
You need to be logged in to leave comments.
Login now