Show More
@@ -1,346 +1,346 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 | |
|
13 | 13 | import nose.tools as nt |
|
14 | 14 | |
|
15 | 15 | from IPython.testing import decorators as dec |
|
16 | 16 | from IPython.testing import tools as tt |
|
17 | 17 | from IPython.utils import py3compat |
|
18 | 18 | |
|
19 | 19 | #----------------------------------------------------------------------------- |
|
20 | 20 | # Test functions begin |
|
21 | 21 | #----------------------------------------------------------------------------- |
|
22 | 22 | |
|
23 | 23 | def test_rehashx(): |
|
24 | 24 | # clear up everything |
|
25 | 25 | _ip = get_ipython() |
|
26 | 26 | _ip.alias_manager.alias_table.clear() |
|
27 | 27 | del _ip.db['syscmdlist'] |
|
28 | 28 | |
|
29 | 29 | _ip.magic('rehashx') |
|
30 | 30 | # Practically ALL ipython development systems will have more than 10 aliases |
|
31 | 31 | |
|
32 | 32 | yield (nt.assert_true, len(_ip.alias_manager.alias_table) > 10) |
|
33 | 33 | for key, val in _ip.alias_manager.alias_table.iteritems(): |
|
34 | 34 | # we must strip dots from alias names |
|
35 | 35 | nt.assert_true('.' not in key) |
|
36 | 36 | |
|
37 | 37 | # rehashx must fill up syscmdlist |
|
38 | 38 | scoms = _ip.db['syscmdlist'] |
|
39 | 39 | yield (nt.assert_true, len(scoms) > 10) |
|
40 | 40 | |
|
41 | 41 | |
|
42 | 42 | def test_magic_parse_options(): |
|
43 | 43 | """Test that we don't mangle paths when parsing magic options.""" |
|
44 | 44 | ip = get_ipython() |
|
45 | 45 | path = 'c:\\x' |
|
46 | 46 | opts = ip.parse_options('-f %s' % path,'f:')[0] |
|
47 | 47 | # argv splitting is os-dependent |
|
48 | 48 | if os.name == 'posix': |
|
49 | 49 | expected = 'c:x' |
|
50 | 50 | else: |
|
51 | 51 | expected = path |
|
52 | 52 | nt.assert_equals(opts['f'], expected) |
|
53 | 53 | |
|
54 | 54 | |
|
55 | 55 | @dec.skip_without('sqlite3') |
|
56 | 56 | def doctest_hist_f(): |
|
57 | 57 | """Test %hist -f with temporary filename. |
|
58 | 58 | |
|
59 | 59 | In [9]: import tempfile |
|
60 | 60 | |
|
61 | 61 | In [10]: tfile = tempfile.mktemp('.py','tmp-ipython-') |
|
62 | 62 | |
|
63 | 63 | In [11]: %hist -nl -f $tfile 3 |
|
64 | 64 | |
|
65 | 65 | In [13]: import os; os.unlink(tfile) |
|
66 | 66 | """ |
|
67 | 67 | |
|
68 | 68 | |
|
69 | 69 | @dec.skip_without('sqlite3') |
|
70 | 70 | def doctest_hist_r(): |
|
71 | 71 | """Test %hist -r |
|
72 | 72 | |
|
73 | 73 | XXX - This test is not recording the output correctly. For some reason, in |
|
74 | 74 | testing mode the raw history isn't getting populated. No idea why. |
|
75 | 75 | Disabling the output checking for now, though at least we do run it. |
|
76 | 76 | |
|
77 | 77 | In [1]: 'hist' in _ip.lsmagic() |
|
78 | 78 | Out[1]: True |
|
79 | 79 | |
|
80 | 80 | In [2]: x=1 |
|
81 | 81 | |
|
82 | 82 | In [3]: %hist -rl 2 |
|
83 | 83 | x=1 # random |
|
84 | 84 | %hist -r 2 |
|
85 | 85 | """ |
|
86 | 86 | |
|
87 | 87 | |
|
88 | 88 | @dec.skip_without('sqlite3') |
|
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 | |
|
156 | 156 | @dec.skip_without('sqlite3') |
|
157 | 157 | def test_macro(): |
|
158 | 158 | ip = get_ipython() |
|
159 | 159 | ip.history_manager.reset() # Clear any existing history. |
|
160 | 160 | cmds = ["a=1", "def b():\n return a**2", "print(a,b())"] |
|
161 | 161 | for i, cmd in enumerate(cmds, start=1): |
|
162 | 162 | ip.history_manager.store_inputs(i, cmd) |
|
163 | 163 | ip.magic("macro test 1-3") |
|
164 | 164 | nt.assert_equal(ip.user_ns["test"].value, "\n".join(cmds)+"\n") |
|
165 | 165 | |
|
166 | 166 | # List macros. |
|
167 | 167 | assert "test" in ip.magic("macro") |
|
168 | 168 | |
|
169 | 169 | |
|
170 | 170 | @dec.skip_without('sqlite3') |
|
171 | 171 | def test_macro_run(): |
|
172 | 172 | """Test that we can run a multi-line macro successfully.""" |
|
173 | 173 | ip = get_ipython() |
|
174 | 174 | ip.history_manager.reset() |
|
175 | 175 | cmds = ["a=10", "a+=1", py3compat.doctest_refactor_print("print a"), |
|
176 | 176 | "%macro test 2-3"] |
|
177 | 177 | for cmd in cmds: |
|
178 | 178 | ip.run_cell(cmd, store_history=True) |
|
179 | 179 | nt.assert_equal(ip.user_ns["test"].value, |
|
180 | 180 | py3compat.doctest_refactor_print("a+=1\nprint a\n")) |
|
181 | 181 | with tt.AssertPrints("12"): |
|
182 | 182 | ip.run_cell("test") |
|
183 | 183 | with tt.AssertPrints("13"): |
|
184 | 184 | ip.run_cell("test") |
|
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 | def test_time(): |
|
202 | 202 | _ip.magic('time None') |
|
203 | 203 | |
|
204 | 204 | |
|
205 | 205 | @py3compat.doctest_refactor_print |
|
206 | 206 | def doctest_time(): |
|
207 | 207 | """ |
|
208 | 208 | In [10]: %time None |
|
209 | 209 | CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s |
|
210 | 210 | Wall time: 0.00 s |
|
211 | 211 | |
|
212 | 212 | In [11]: def f(kmjy): |
|
213 | 213 | ....: %time print 2*kmjy |
|
214 | 214 | |
|
215 | 215 | In [12]: f(3) |
|
216 | 216 | 6 |
|
217 | 217 | CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s |
|
218 | 218 | Wall time: 0.00 s |
|
219 | 219 | """ |
|
220 | 220 | |
|
221 | 221 | |
|
222 | 222 | def test_doctest_mode(): |
|
223 | 223 | "Toggle doctest_mode twice, it should be a no-op and run without error" |
|
224 | 224 | _ip.magic('doctest_mode') |
|
225 | 225 | _ip.magic('doctest_mode') |
|
226 | 226 | |
|
227 | 227 | |
|
228 | 228 | def test_parse_options(): |
|
229 | 229 | """Tests for basic options parsing in magics.""" |
|
230 | 230 | # These are only the most minimal of tests, more should be added later. At |
|
231 | 231 | # the very least we check that basic text/unicode calls work OK. |
|
232 | 232 | nt.assert_equal(_ip.parse_options('foo', '')[1], 'foo') |
|
233 | 233 | nt.assert_equal(_ip.parse_options(u'foo', '')[1], u'foo') |
|
234 | 234 | |
|
235 | 235 | |
|
236 | 236 | def test_dirops(): |
|
237 | 237 | """Test various directory handling operations.""" |
|
238 | 238 | # curpath = lambda :os.path.splitdrive(os.getcwdu())[1].replace('\\','/') |
|
239 | 239 | curpath = os.getcwdu |
|
240 | 240 | startdir = os.getcwdu() |
|
241 | ipdir = _ip.ipython_dir | |
|
241 | ipdir = os.path.realpath(_ip.ipython_dir) | |
|
242 | 242 | try: |
|
243 | 243 | _ip.magic('cd "%s"' % ipdir) |
|
244 | 244 | nt.assert_equal(curpath(), ipdir) |
|
245 | 245 | _ip.magic('cd -') |
|
246 | 246 | nt.assert_equal(curpath(), startdir) |
|
247 | 247 | _ip.magic('pushd "%s"' % ipdir) |
|
248 | 248 | nt.assert_equal(curpath(), ipdir) |
|
249 | 249 | _ip.magic('popd') |
|
250 | 250 | nt.assert_equal(curpath(), startdir) |
|
251 | 251 | finally: |
|
252 | 252 | os.chdir(startdir) |
|
253 | 253 | |
|
254 | 254 | |
|
255 | 255 | def test_xmode(): |
|
256 | 256 | # Calling xmode three times should be a no-op |
|
257 | 257 | xmode = _ip.InteractiveTB.mode |
|
258 | 258 | for i in range(3): |
|
259 | 259 | _ip.magic("xmode") |
|
260 | 260 | nt.assert_equal(_ip.InteractiveTB.mode, xmode) |
|
261 | 261 | |
|
262 | 262 | def test_reset_hard(): |
|
263 | 263 | monitor = [] |
|
264 | 264 | class A(object): |
|
265 | 265 | def __del__(self): |
|
266 | 266 | monitor.append(1) |
|
267 | 267 | def __repr__(self): |
|
268 | 268 | return "<A instance>" |
|
269 | 269 | |
|
270 | 270 | _ip.user_ns["a"] = A() |
|
271 | 271 | _ip.run_cell("a") |
|
272 | 272 | |
|
273 | 273 | nt.assert_equal(monitor, []) |
|
274 | 274 | _ip.magic_reset("-f") |
|
275 | 275 | nt.assert_equal(monitor, [1]) |
|
276 | 276 | |
|
277 | 277 | class TestXdel(tt.TempFileMixin): |
|
278 | 278 | def test_xdel(self): |
|
279 | 279 | """Test that references from %run are cleared by xdel.""" |
|
280 | 280 | src = ("class A(object):\n" |
|
281 | 281 | " monitor = []\n" |
|
282 | 282 | " def __del__(self):\n" |
|
283 | 283 | " self.monitor.append(1)\n" |
|
284 | 284 | "a = A()\n") |
|
285 | 285 | self.mktmp(src) |
|
286 | 286 | # %run creates some hidden references... |
|
287 | 287 | _ip.magic("run %s" % self.fname) |
|
288 | 288 | # ... as does the displayhook. |
|
289 | 289 | _ip.run_cell("a") |
|
290 | 290 | |
|
291 | 291 | monitor = _ip.user_ns["A"].monitor |
|
292 | 292 | nt.assert_equal(monitor, []) |
|
293 | 293 | |
|
294 | 294 | _ip.magic("xdel a") |
|
295 | 295 | |
|
296 | 296 | # Check that a's __del__ method has been called. |
|
297 | 297 | nt.assert_equal(monitor, [1]) |
|
298 | 298 | |
|
299 | 299 | def doctest_who(): |
|
300 | 300 | """doctest for %who |
|
301 | 301 | |
|
302 | 302 | In [1]: %reset -f |
|
303 | 303 | |
|
304 | 304 | In [2]: alpha = 123 |
|
305 | 305 | |
|
306 | 306 | In [3]: beta = 'beta' |
|
307 | 307 | |
|
308 | 308 | In [4]: %who int |
|
309 | 309 | alpha |
|
310 | 310 | |
|
311 | 311 | In [5]: %who str |
|
312 | 312 | beta |
|
313 | 313 | |
|
314 | 314 | In [6]: %whos |
|
315 | 315 | Variable Type Data/Info |
|
316 | 316 | ---------------------------- |
|
317 | 317 | alpha int 123 |
|
318 | 318 | beta str beta |
|
319 | 319 | |
|
320 | 320 | In [7]: %who_ls |
|
321 | 321 | Out[7]: ['alpha', 'beta'] |
|
322 | 322 | """ |
|
323 | 323 | |
|
324 | 324 | @py3compat.u_format |
|
325 | 325 | def doctest_precision(): |
|
326 | 326 | """doctest for %precision |
|
327 | 327 | |
|
328 | 328 | In [1]: f = get_ipython().shell.display_formatter.formatters['text/plain'] |
|
329 | 329 | |
|
330 | 330 | In [2]: %precision 5 |
|
331 | 331 | Out[2]: {u}'%.5f' |
|
332 | 332 | |
|
333 | 333 | In [3]: f.float_format |
|
334 | 334 | Out[3]: {u}'%.5f' |
|
335 | 335 | |
|
336 | 336 | In [4]: %precision %e |
|
337 | 337 | Out[4]: {u}'%e' |
|
338 | 338 | |
|
339 | 339 | In [5]: f(3.1415927) |
|
340 | 340 | Out[5]: {u}'3.141593e+00' |
|
341 | 341 | """ |
|
342 | 342 | |
|
343 | 343 | def test_psearch(): |
|
344 | 344 | with tt.AssertPrints("dict.fromkeys"): |
|
345 | 345 | _ip.run_cell("dict.fr*?") |
|
346 | 346 |
General Comments 0
You need to be logged in to leave comments.
Login now