##// END OF EJS Templates
Test that specifying a range beyond the end of the current session doesn't raise an error.
Thomas Kluyver -
Show More
@@ -1,115 +1,118 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 sys
10 import sys
11 import unittest
11 import unittest
12 from datetime import datetime
12 from datetime import datetime
13 # third party
13 # third party
14 import nose.tools as nt
14 import nose.tools as nt
15
15
16 # our own packages
16 # our own packages
17 from IPython.utils.tempdir import TemporaryDirectory
17 from IPython.utils.tempdir import TemporaryDirectory
18 from IPython.core.history import HistoryManager, extract_hist_ranges
18 from IPython.core.history import HistoryManager, extract_hist_ranges
19 from IPython.utils import py3compat
19 from IPython.utils import py3compat
20
20
21 def setUp():
21 def setUp():
22 nt.assert_equal(sys.getdefaultencoding(), "utf-8" if py3compat.PY3 else "ascii")
22 nt.assert_equal(sys.getdefaultencoding(), "utf-8" if py3compat.PY3 else "ascii")
23
23
24 def test_history():
24 def test_history():
25 ip = get_ipython()
25 ip = get_ipython()
26 with TemporaryDirectory() as tmpdir:
26 with TemporaryDirectory() as tmpdir:
27 hist_manager_ori = ip.history_manager
27 hist_manager_ori = ip.history_manager
28 hist_file = os.path.join(tmpdir, 'history.sqlite')
28 hist_file = os.path.join(tmpdir, 'history.sqlite')
29 try:
29 try:
30 ip.history_manager = HistoryManager(shell=ip, hist_file=hist_file)
30 ip.history_manager = HistoryManager(shell=ip, hist_file=hist_file)
31 hist = ['a=1', 'def f():\n test = 1\n return test', u"b='β‚¬Γ†ΒΎΓ·ΓŸ'"]
31 hist = ['a=1', 'def f():\n test = 1\n return test', u"b='β‚¬Γ†ΒΎΓ·ΓŸ'"]
32 for i, h in enumerate(hist, start=1):
32 for i, h in enumerate(hist, start=1):
33 ip.history_manager.store_inputs(i, h)
33 ip.history_manager.store_inputs(i, h)
34
34
35 ip.history_manager.db_log_output = True
35 ip.history_manager.db_log_output = True
36 # Doesn't match the input, but we'll just check it's stored.
36 # Doesn't match the input, but we'll just check it's stored.
37 ip.history_manager.output_hist_reprs[3] = "spam"
37 ip.history_manager.output_hist_reprs[3] = "spam"
38 ip.history_manager.store_output(3)
38 ip.history_manager.store_output(3)
39
39
40 nt.assert_equal(ip.history_manager.input_hist_raw, [''] + hist)
40 nt.assert_equal(ip.history_manager.input_hist_raw, [''] + hist)
41
41
42 # Check whether specifying a range beyond the end of the current
43 # session results in an error (gh-804)
44 ip.magic('%hist 2-500')
42
45
43 # New session
46 # New session
44 ip.history_manager.reset()
47 ip.history_manager.reset()
45 newcmds = ["z=5","class X(object):\n pass", "k='p'"]
48 newcmds = ["z=5","class X(object):\n pass", "k='p'"]
46 for i, cmd in enumerate(newcmds, start=1):
49 for i, cmd in enumerate(newcmds, start=1):
47 ip.history_manager.store_inputs(i, cmd)
50 ip.history_manager.store_inputs(i, cmd)
48 gothist = ip.history_manager.get_range(start=1, stop=4)
51 gothist = ip.history_manager.get_range(start=1, stop=4)
49 nt.assert_equal(list(gothist), zip([0,0,0],[1,2,3], newcmds))
52 nt.assert_equal(list(gothist), zip([0,0,0],[1,2,3], newcmds))
50 # Previous session:
53 # Previous session:
51 gothist = ip.history_manager.get_range(-1, 1, 4)
54 gothist = ip.history_manager.get_range(-1, 1, 4)
52 nt.assert_equal(list(gothist), zip([1,1,1],[1,2,3], hist))
55 nt.assert_equal(list(gothist), zip([1,1,1],[1,2,3], hist))
53
56
54 # Check get_hist_tail
57 # Check get_hist_tail
55 gothist = ip.history_manager.get_tail(4, output=True,
58 gothist = ip.history_manager.get_tail(4, output=True,
56 include_latest=True)
59 include_latest=True)
57 expected = [(1, 3, (hist[-1], "spam")),
60 expected = [(1, 3, (hist[-1], "spam")),
58 (2, 1, (newcmds[0], None)),
61 (2, 1, (newcmds[0], None)),
59 (2, 2, (newcmds[1], None)),
62 (2, 2, (newcmds[1], None)),
60 (2, 3, (newcmds[2], None)),]
63 (2, 3, (newcmds[2], None)),]
61 nt.assert_equal(list(gothist), expected)
64 nt.assert_equal(list(gothist), expected)
62
65
63 gothist = ip.history_manager.get_tail(2)
66 gothist = ip.history_manager.get_tail(2)
64 expected = [(2, 1, newcmds[0]),
67 expected = [(2, 1, newcmds[0]),
65 (2, 2, newcmds[1])]
68 (2, 2, newcmds[1])]
66 nt.assert_equal(list(gothist), expected)
69 nt.assert_equal(list(gothist), expected)
67
70
68 # Check get_hist_search
71 # Check get_hist_search
69 gothist = ip.history_manager.search("*test*")
72 gothist = ip.history_manager.search("*test*")
70 nt.assert_equal(list(gothist), [(1,2,hist[1])] )
73 nt.assert_equal(list(gothist), [(1,2,hist[1])] )
71 gothist = ip.history_manager.search("b*", output=True)
74 gothist = ip.history_manager.search("b*", output=True)
72 nt.assert_equal(list(gothist), [(1,3,(hist[2],"spam"))] )
75 nt.assert_equal(list(gothist), [(1,3,(hist[2],"spam"))] )
73
76
74 # Cross testing: check that magic %save can get previous session.
77 # Cross testing: check that magic %save can get previous session.
75 testfilename = os.path.realpath(os.path.join(tmpdir, "test.py"))
78 testfilename = os.path.realpath(os.path.join(tmpdir, "test.py"))
76 ip.magic_save(testfilename + " ~1/1-3")
79 ip.magic_save(testfilename + " ~1/1-3")
77 testfile = open(testfilename, "r")
80 testfile = open(testfilename, "r")
78 nt.assert_equal(testfile.read().decode("utf-8"),
81 nt.assert_equal(testfile.read().decode("utf-8"),
79 "# coding: utf-8\n" + "\n".join(hist))
82 "# coding: utf-8\n" + "\n".join(hist))
80
83
81 # Duplicate line numbers - check that it doesn't crash, and
84 # Duplicate line numbers - check that it doesn't crash, and
82 # gets a new session
85 # gets a new session
83 ip.history_manager.store_inputs(1, "rogue")
86 ip.history_manager.store_inputs(1, "rogue")
84 ip.history_manager.writeout_cache()
87 ip.history_manager.writeout_cache()
85 nt.assert_equal(ip.history_manager.session_number, 3)
88 nt.assert_equal(ip.history_manager.session_number, 3)
86 finally:
89 finally:
87 # Restore history manager
90 # Restore history manager
88 ip.history_manager = hist_manager_ori
91 ip.history_manager = hist_manager_ori
89
92
90
93
91 def test_extract_hist_ranges():
94 def test_extract_hist_ranges():
92 instr = "1 2/3 ~4/5-6 ~4/7-~4/9 ~9/2-~7/5"
95 instr = "1 2/3 ~4/5-6 ~4/7-~4/9 ~9/2-~7/5"
93 expected = [(0, 1, 2), # 0 == current session
96 expected = [(0, 1, 2), # 0 == current session
94 (2, 3, 4),
97 (2, 3, 4),
95 (-4, 5, 7),
98 (-4, 5, 7),
96 (-4, 7, 10),
99 (-4, 7, 10),
97 (-9, 2, None), # None == to end
100 (-9, 2, None), # None == to end
98 (-8, 1, None),
101 (-8, 1, None),
99 (-7, 1, 6)]
102 (-7, 1, 6)]
100 actual = list(extract_hist_ranges(instr))
103 actual = list(extract_hist_ranges(instr))
101 nt.assert_equal(actual, expected)
104 nt.assert_equal(actual, expected)
102
105
103 def test_magic_rerun():
106 def test_magic_rerun():
104 """Simple test for %rerun (no args -> rerun last line)"""
107 """Simple test for %rerun (no args -> rerun last line)"""
105 ip = get_ipython()
108 ip = get_ipython()
106 ip.run_cell("a = 10")
109 ip.run_cell("a = 10")
107 ip.run_cell("a += 1")
110 ip.run_cell("a += 1")
108 nt.assert_equal(ip.user_ns["a"], 11)
111 nt.assert_equal(ip.user_ns["a"], 11)
109 ip.run_cell("%rerun")
112 ip.run_cell("%rerun")
110 nt.assert_equal(ip.user_ns["a"], 12)
113 nt.assert_equal(ip.user_ns["a"], 12)
111
114
112 def test_timestamp_type():
115 def test_timestamp_type():
113 ip = get_ipython()
116 ip = get_ipython()
114 info = ip.history_manager.get_session_info()
117 info = ip.history_manager.get_session_info()
115 nt.assert_true(isinstance(info[1], datetime))
118 nt.assert_true(isinstance(info[1], datetime))
General Comments 0
You need to be logged in to leave comments. Login now