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