##// END OF EJS Templates
Add test code for history session offset.
Thomas Kluyver -
Show More
@@ -1,48 +1,65 b''
1 1 """Tests for the IPython tab-completion machinery.
2 2 """
3 3 #-----------------------------------------------------------------------------
4 4 # Module imports
5 5 #-----------------------------------------------------------------------------
6 6
7 7 # stdlib
8 8 import os
9 9 import sys
10 10 import unittest
11 11
12 12 # third party
13 13 import nose.tools as nt
14 14
15 15 # our own packages
16 16 from IPython.utils.tempdir import TemporaryDirectory
17 17 from IPython.core.history import HistoryManager
18 18
19 19 def test_history():
20 20
21 21 ip = get_ipython()
22 22 with TemporaryDirectory() as tmpdir:
23 23 #tmpdir = '/software/temp'
24 24 histfile = os.path.realpath(os.path.join(tmpdir, 'history.json'))
25 25 # Ensure that we restore the history management that we mess with in
26 26 # this test doesn't affect the IPython instance used by the test suite
27 27 # beyond this test.
28 28 hist_manager_ori = ip.history_manager
29 29 try:
30 30 ip.history_manager = HistoryManager(ip)
31 31 ip.history_manager.hist_file = histfile
32 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 34 # test save and load
35 35 ip.history_manager.input_hist_raw[:] = []
36 36 for h in hist:
37 ip.history_manager.input_hist_raw.append(h)
37 ip.history_manager.store_inputs(h)
38 38 ip.save_history()
39 39 ip.history_manager.input_hist_raw[:] = []
40 40 ip.reload_history()
41 41 print type(ip.history_manager.input_hist_raw)
42 42 print ip.history_manager.input_hist_raw
43 43 nt.assert_equal(len(ip.history_manager.input_hist_raw), len(hist))
44 44 for i,h in enumerate(hist):
45 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 63 finally:
47 64 # Restore history manager
48 65 ip.history_manager = hist_manager_ori
General Comments 0
You need to be logged in to leave comments. Login now