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