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