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