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