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