##// END OF EJS Templates
Add duplicated entry in test_history
Takafumi Arakaki -
Show More
@@ -1,163 +1,167 b''
1 1 # coding: utf-8
2 2 """Tests for the IPython tab-completion machinery.
3 3 """
4 4 #-----------------------------------------------------------------------------
5 5 # Module imports
6 6 #-----------------------------------------------------------------------------
7 7
8 8 # stdlib
9 9 import os
10 10 import shutil
11 11 import sys
12 12 import tempfile
13 13 import unittest
14 14 from datetime import datetime
15 15
16 16 # third party
17 17 import nose.tools as nt
18 18
19 19 # our own packages
20 20 from IPython.config.loader import Config
21 21 from IPython.utils.tempdir import TemporaryDirectory
22 22 from IPython.core.history import HistoryManager, extract_hist_ranges
23 23 from IPython.utils import py3compat
24 24
25 25 def setUp():
26 26 nt.assert_equal(sys.getdefaultencoding(), "utf-8" if py3compat.PY3 else "ascii")
27 27
28 28 def test_history():
29 29 ip = get_ipython()
30 30 with TemporaryDirectory() as tmpdir:
31 31 hist_manager_ori = ip.history_manager
32 32 hist_file = os.path.join(tmpdir, 'history.sqlite')
33 33 try:
34 34 ip.history_manager = HistoryManager(shell=ip, hist_file=hist_file)
35 35 hist = [u'a=1', u'def f():\n test = 1\n return test', u"b='β‚¬Γ†ΒΎΓ·ΓŸ'"]
36 36 for i, h in enumerate(hist, start=1):
37 37 ip.history_manager.store_inputs(i, h)
38 38
39 39 ip.history_manager.db_log_output = True
40 40 # Doesn't match the input, but we'll just check it's stored.
41 41 ip.history_manager.output_hist_reprs[3] = "spam"
42 42 ip.history_manager.store_output(3)
43 43
44 44 nt.assert_equal(ip.history_manager.input_hist_raw, [''] + hist)
45 45
46 46 # Detailed tests for _get_range_session
47 47 grs = ip.history_manager._get_range_session
48 48 nt.assert_equal(list(grs(start=2,stop=-1)), zip([0], [2], hist[1:-1]))
49 49 nt.assert_equal(list(grs(start=-2)), zip([0,0], [2,3], hist[-2:]))
50 50 nt.assert_equal(list(grs(output=True)), zip([0,0,0], [1,2,3], zip(hist, [None,None,'spam'])))
51 51
52 52 # Check whether specifying a range beyond the end of the current
53 53 # session results in an error (gh-804)
54 54 ip.magic('%hist 2-500')
55 55
56 56 # Check that we can write non-ascii characters to a file
57 57 ip.magic("%%hist -f %s" % os.path.join(tmpdir, "test1"))
58 58 ip.magic("%%hist -pf %s" % os.path.join(tmpdir, "test2"))
59 59 ip.magic("%%hist -nf %s" % os.path.join(tmpdir, "test3"))
60 60 ip.magic("%%save %s 1-10" % os.path.join(tmpdir, "test4"))
61 61
62 62 # New session
63 63 ip.history_manager.reset()
64 newcmds = ["z=5","class X(object):\n pass", "k='p'"]
64 newcmds = [u"z=5",
65 u"class X(object):\n pass",
66 u"k='p'",
67 u"z=5"]
65 68 for i, cmd in enumerate(newcmds, start=1):
66 69 ip.history_manager.store_inputs(i, cmd)
67 70 gothist = ip.history_manager.get_range(start=1, stop=4)
68 71 nt.assert_equal(list(gothist), zip([0,0,0],[1,2,3], newcmds))
69 72 # Previous session:
70 73 gothist = ip.history_manager.get_range(-1, 1, 4)
71 74 nt.assert_equal(list(gothist), zip([1,1,1],[1,2,3], hist))
72 75
76 newhist = [(2, i + 1, c) for (i, c) in enumerate(newcmds)]
77
73 78 # Check get_hist_tail
74 gothist = ip.history_manager.get_tail(4, output=True,
79 gothist = ip.history_manager.get_tail(5, output=True,
75 80 include_latest=True)
76 expected = [(1, 3, (hist[-1], "spam")),
77 (2, 1, (newcmds[0], None)),
78 (2, 2, (newcmds[1], None)),
79 (2, 3, (newcmds[2], None)),]
81 expected = [(1, 3, (hist[-1], "spam"))] \
82 + [(s, n, (c, None)) for (s, n, c) in newhist]
80 83 nt.assert_equal(list(gothist), expected)
81 84
82 85 gothist = ip.history_manager.get_tail(2)
83 expected = [(2, 1, newcmds[0]),
84 (2, 2, newcmds[1])]
86 expected = newhist[-3:-1]
85 87 nt.assert_equal(list(gothist), expected)
86 88
87 89 # Check get_hist_search
88 90 gothist = ip.history_manager.search("*test*")
89 91 nt.assert_equal(list(gothist), [(1,2,hist[1])] )
90 92 gothist = ip.history_manager.search("*=*")
91 93 nt.assert_equal(list(gothist),
92 94 [(1, 1, hist[0]),
93 95 (1, 2, hist[1]),
94 96 (1, 3, hist[2]),
95 (2, 1, newcmds[0]),
96 (2, 3, newcmds[2])])
97 gothist = ip.history_manager.search("*=*", n=3)
97 newhist[0],
98 newhist[2],
99 newhist[3]])
100 gothist = ip.history_manager.search("*=*", n=4)
98 101 nt.assert_equal(list(gothist),
99 102 [(1, 3, hist[2]),
100 (2, 1, newcmds[0]),
101 (2, 3, newcmds[2])])
103 newhist[0],
104 newhist[2],
105 newhist[3]])
102 106 gothist = ip.history_manager.search("b*", output=True)
103 107 nt.assert_equal(list(gothist), [(1,3,(hist[2],"spam"))] )
104 108
105 109 # Cross testing: check that magic %save can get previous session.
106 110 testfilename = os.path.realpath(os.path.join(tmpdir, "test.py"))
107 111 ip.magic("save " + testfilename + " ~1/1-3")
108 112 with py3compat.open(testfilename, encoding='utf-8') as testfile:
109 113 nt.assert_equal(testfile.read(),
110 114 u"# coding: utf-8\n" + u"\n".join(hist)+u"\n")
111 115
112 116 # Duplicate line numbers - check that it doesn't crash, and
113 117 # gets a new session
114 118 ip.history_manager.store_inputs(1, "rogue")
115 119 ip.history_manager.writeout_cache()
116 120 nt.assert_equal(ip.history_manager.session_number, 3)
117 121 finally:
118 122 # Restore history manager
119 123 ip.history_manager = hist_manager_ori
120 124
121 125
122 126 def test_extract_hist_ranges():
123 127 instr = "1 2/3 ~4/5-6 ~4/7-~4/9 ~9/2-~7/5"
124 128 expected = [(0, 1, 2), # 0 == current session
125 129 (2, 3, 4),
126 130 (-4, 5, 7),
127 131 (-4, 7, 10),
128 132 (-9, 2, None), # None == to end
129 133 (-8, 1, None),
130 134 (-7, 1, 6)]
131 135 actual = list(extract_hist_ranges(instr))
132 136 nt.assert_equal(actual, expected)
133 137
134 138 def test_magic_rerun():
135 139 """Simple test for %rerun (no args -> rerun last line)"""
136 140 ip = get_ipython()
137 141 ip.run_cell("a = 10", store_history=True)
138 142 ip.run_cell("a += 1", store_history=True)
139 143 nt.assert_equal(ip.user_ns["a"], 11)
140 144 ip.run_cell("%rerun", store_history=True)
141 145 nt.assert_equal(ip.user_ns["a"], 12)
142 146
143 147 def test_timestamp_type():
144 148 ip = get_ipython()
145 149 info = ip.history_manager.get_session_info()
146 150 nt.assert_true(isinstance(info[1], datetime))
147 151
148 152 def test_hist_file_config():
149 153 cfg = Config()
150 154 tfile = tempfile.NamedTemporaryFile(delete=False)
151 155 cfg.HistoryManager.hist_file = tfile.name
152 156 try:
153 157 hm = HistoryManager(shell=get_ipython(), config=cfg)
154 158 nt.assert_equal(hm.hist_file, cfg.HistoryManager.hist_file)
155 159 finally:
156 160 try:
157 161 os.remove(tfile.name)
158 162 except OSError:
159 163 # same catch as in testing.tools.TempFileMixin
160 164 # On Windows, even though we close the file, we still can't
161 165 # delete it. I have no clue why
162 166 pass
163 167
General Comments 0
You need to be logged in to leave comments. Login now