1
"""Tests for various magic functions.
3
Needs to be run by nose (to make ipython session available).
11
import nose.tools as nt
13
from IPython.platutils import find_cmd, get_long_path_name
14
from IPython.testing import decorators as dec
15
from IPython.testing import tools as tt
17
#-----------------------------------------------------------------------------
18
# Test functions begin
22
_ip.IP.alias_table.clear()
23
del _ip.db['syscmdlist']
26
# Practically ALL ipython development systems will have more than 10 aliases
28
yield (nt.assert_true, len(_ip.IP.alias_table) > 10)
29
for key, val in _ip.IP.alias_table.items():
30
# we must strip dots from alias names
31
nt.assert_true('.' not in key)
33
# rehashx must fill up syscmdlist
34
scoms = _ip.db['syscmdlist']
35
yield (nt.assert_true, len(scoms) > 10)
39
"""Test %hist -f with temporary filename.
41
In [9]: import tempfile
43
In [10]: tfile = tempfile.mktemp('.py','tmp-ipython-')
45
In [11]: %hist -n -f $tfile 3
52
XXX - This test is not recording the output correctly. Not sure why...
54
In [20]: 'hist' in _ip.IP.lsmagic()
64
# This test is known to fail on win32.
65
# See ticket https://bugs.launchpad.net/bugs/366334
67
"""Test that object's __del__ methods are called on exit."""
68
test_dir = os.path.dirname(__file__)
69
del_file = os.path.join(test_dir,'obj_del.py')
70
ipython_cmd = find_cmd('ipython')
71
out = _ip.IP.getoutput('%s %s' % (ipython_cmd, del_file))
72
nt.assert_equals(out,'obj_del.py: object A deleted')
76
# Simple tests of ShadowHist class - test generator.
77
import os, shutil, tempfile
79
from IPython.Extensions import pickleshare
80
from IPython.history import ShadowHist
82
tfile = tempfile.mktemp('','tmp-ipython-')
84
db = pickleshare.PickleShareDB(tfile)
92
yield nt.assert_equals,s.all(),[(1, 'hello'), (2, 'world'), (3, 'karhu')]
94
yield nt.assert_equal,s.get(2),'world'
99
def test_numpy_clear_array_undec():
100
from IPython.Extensions import clearcmd
102
_ip.ex('import numpy as np')
103
_ip.ex('a = np.empty(2)')
104
yield (nt.assert_true, 'a' in _ip.user_ns)
105
_ip.magic('clear array')
106
yield (nt.assert_false, 'a' in _ip.user_ns)
110
def test_fail_dec(*a,**k):
111
yield nt.assert_true, False
113
@dec.skip('This one shouldn not run')
114
def test_fail_dec2(*a,**k):
115
yield nt.assert_true, False
117
@dec.skipknownfailure
118
def test_fail_dec3(*a,**k):
119
yield nt.assert_true, False
122
def doctest_refbug():
123
"""Very nasty problem with references held by multiple runs of a script.
124
See: https://bugs.launchpad.net/ipython/+bug/269966
126
In [1]: _ip.IP.clear_main_mod_cache()
140
#-----------------------------------------------------------------------------
142
#-----------------------------------------------------------------------------
144
# %run is critical enough that it's a good idea to have a solid collection of
145
# tests for it, some as doctests and some as normal tests.
147
def doctest_run_ns():
148
"""Classes declared %run scripts must be instantiable afterwards.
150
In [11]: run tclass foo
152
In [12]: isinstance(f(),foo)
157
def doctest_run_ns2():
158
"""Classes declared %run scripts must be instantiable afterwards.
160
In [4]: run tclass C-first_pass
162
In [5]: run tclass C-second_pass
163
tclass.py: deleting object: C-first_pass
166
def doctest_run_builtins():
167
"""Check that %run doesn't damage __builtins__ via a doctest.
169
This is similar to the test_run_builtins, but I want *both* forms of the
170
test to catch any possible glitches in our testing machinery, since that
171
modifies %run somewhat. So for this, we have both a normal test (below)
172
and a doctest (this one).
174
In [1]: import tempfile
176
In [2]: bid1 = id(__builtins__)
178
In [3]: fname = tempfile.mkstemp()[1]
180
In [3]: f = open(fname,'w')
182
In [4]: f.write('pass\\n')
186
In [6]: print type(__builtins__)
189
In [7]: %run "$fname"
193
In [8]: bid2 = id(__builtins__)
195
In [9]: print type(__builtins__)
198
In [10]: bid1 == bid2
202
....: os.unlink(fname)
208
# For some tests, it will be handy to organize them in a class with a common
209
# setup that makes a temp file
211
class TestMagicRun(object):
214
"""Make a valid python temp file."""
215
fname = tempfile.mkstemp()[1]
222
def run_tmpfile(self):
223
# This fails on Windows if self.tmpfile.name has spaces or "~" in it.
224
# See below and ticket https://bugs.launchpad.net/bugs/366353
225
_ip.magic('run "%s"' % self.fname)
227
def test_builtins_id(self):
228
"""Check that %run doesn't damage __builtins__ """
230
# Test that the id of __builtins__ is not modified by %run
231
bid1 = id(_ip.user_ns['__builtins__'])
233
bid2 = id(_ip.user_ns['__builtins__'])
234
tt.assert_equals(bid1, bid2)
236
def test_builtins_type(self):
237
"""Check that the type of __builtins__ doesn't change with %run.
239
However, the above could pass if __builtins__ was already modified to
240
be a dict (it should be a module) by a previous use of %run. So we
241
also check explicitly that it really is a module:
244
tt.assert_equals(type(_ip.user_ns['__builtins__']),type(sys))
246
def test_prompts(self):
247
"""Test that prompts correctly generate after %run"""
249
p2 = str(_ip.IP.outputcache.prompt2).strip()
250
nt.assert_equals(p2[:3], '...')
255
os.unlink(self.fname)
257
# On Windows, even though we close the file, we still can't delete
258
# it. I have no clue why
261
# Multiple tests for clipboard pasting
265
hooks.clipboard_get = lambda : txt
268
# Inject fake clipboard hook but save original so we can restore it later
270
user_ns = _ip.user_ns
271
original_clip = hooks.clipboard_get
274
# This try/except with an emtpy except clause is here only because
275
# try/yield/finally is invalid syntax in Python 2.4. This will be
276
# removed when we drop 2.4-compatibility, and the emtpy except below
277
# will be changed to a finally.
279
# Run tests with fake clipboard function
280
user_ns.pop('x', None)
282
yield (nt.assert_equal, user_ns['x'], 1)
284
user_ns.pop('x', None)
286
yield (nt.assert_equal, user_ns['x'], 2)
295
yield (nt.assert_equal, user_ns['x'], [1,2,3])
296
yield (nt.assert_equal, user_ns['y'], [1,4,9])
300
# This should be in a finally clause, instead of the bare except above.
301
# Restore original hook
302
hooks.clipboard_get = original_clip