##// END OF EJS Templates
Add test code for history session offset.
Thomas Kluyver -
Show More
@@ -1,48 +1,65 b''
1 """Tests for the IPython tab-completion machinery.
1 """Tests for the IPython tab-completion machinery.
2 """
2 """
3 #-----------------------------------------------------------------------------
3 #-----------------------------------------------------------------------------
4 # Module imports
4 # Module imports
5 #-----------------------------------------------------------------------------
5 #-----------------------------------------------------------------------------
6
6
7 # stdlib
7 # stdlib
8 import os
8 import os
9 import sys
9 import sys
10 import unittest
10 import unittest
11
11
12 # third party
12 # third party
13 import nose.tools as nt
13 import nose.tools as nt
14
14
15 # our own packages
15 # our own packages
16 from IPython.utils.tempdir import TemporaryDirectory
16 from IPython.utils.tempdir import TemporaryDirectory
17 from IPython.core.history import HistoryManager
17 from IPython.core.history import HistoryManager
18
18
19 def test_history():
19 def test_history():
20
20
21 ip = get_ipython()
21 ip = get_ipython()
22 with TemporaryDirectory() as tmpdir:
22 with TemporaryDirectory() as tmpdir:
23 #tmpdir = '/software/temp'
23 #tmpdir = '/software/temp'
24 histfile = os.path.realpath(os.path.join(tmpdir, 'history.json'))
24 histfile = os.path.realpath(os.path.join(tmpdir, 'history.json'))
25 # Ensure that we restore the history management that we mess with in
25 # Ensure that we restore the history management that we mess with in
26 # this test doesn't affect the IPython instance used by the test suite
26 # this test doesn't affect the IPython instance used by the test suite
27 # beyond this test.
27 # beyond this test.
28 hist_manager_ori = ip.history_manager
28 hist_manager_ori = ip.history_manager
29 try:
29 try:
30 ip.history_manager = HistoryManager(ip)
30 ip.history_manager = HistoryManager(ip)
31 ip.history_manager.hist_file = histfile
31 ip.history_manager.hist_file = histfile
32 print 'test',histfile
32 print 'test',histfile
33 hist = ['a=1\n', 'def f():\n test = 1\n return test\n', 'b=2\n']
33 hist = ['a=1', 'def f():\n test = 1\n return test', 'b=2']
34 # test save and load
34 # test save and load
35 ip.history_manager.input_hist_raw[:] = []
35 ip.history_manager.input_hist_raw[:] = []
36 for h in hist:
36 for h in hist:
37 ip.history_manager.input_hist_raw.append(h)
37 ip.history_manager.store_inputs(h)
38 ip.save_history()
38 ip.save_history()
39 ip.history_manager.input_hist_raw[:] = []
39 ip.history_manager.input_hist_raw[:] = []
40 ip.reload_history()
40 ip.reload_history()
41 print type(ip.history_manager.input_hist_raw)
41 print type(ip.history_manager.input_hist_raw)
42 print ip.history_manager.input_hist_raw
42 print ip.history_manager.input_hist_raw
43 nt.assert_equal(len(ip.history_manager.input_hist_raw), len(hist))
43 nt.assert_equal(len(ip.history_manager.input_hist_raw), len(hist))
44 for i,h in enumerate(hist):
44 for i,h in enumerate(hist):
45 nt.assert_equal(hist[i], ip.history_manager.input_hist_raw[i])
45 nt.assert_equal(hist[i], ip.history_manager.input_hist_raw[i])
46
47 # Test that session offset works.
48 ip.history_manager.session_offset = \
49 len(ip.history_manager.input_hist_raw) -1
50 newcmds = ["z=5","class X(object):\n pass", "k='p'"]
51 for cmd in newcmds:
52 ip.history_manager.store_inputs(cmd)
53 gothist = ip.history_manager.get_history((1,4),
54 raw=True, output=False)
55 nt.assert_equal(gothist, dict(zip([1,2,3], newcmds)))
56
57 # Cross testing: check that magic %save picks up on the session
58 # offset.
59 testfilename = os.path.realpath(os.path.join(tmpdir, "test.py"))
60 ip.magic_save(testfilename + " 1-3")
61 testfile = open(testfilename, "r")
62 nt.assert_equal(testfile.read(), "\n".join(newcmds))
46 finally:
63 finally:
47 # Restore history manager
64 # Restore history manager
48 ip.history_manager = hist_manager_ori
65 ip.history_manager = hist_manager_ori
General Comments 0
You need to be logged in to leave comments. Login now