##// END OF EJS Templates
add test case for readline multi-line history
Julian Taylor -
Show More
@@ -0,0 +1,123 b''
1 # -*- coding: utf-8 -*-
2 """Tests for the key interactiveshell module.
3
4 Authors
5 -------
6 * Julian Taylor
7 """
8 #-----------------------------------------------------------------------------
9 # Copyright (C) 2011 The IPython Development Team
10 #
11 # Distributed under the terms of the BSD License. The full license is in
12 # the file COPYING, distributed as part of this software.
13 #-----------------------------------------------------------------------------
14
15 #-----------------------------------------------------------------------------
16 # Imports
17 #-----------------------------------------------------------------------------
18 # stdlib
19 import unittest
20
21 from IPython.testing.decorators import skipif
22
23 class InteractiveShellTestCase(unittest.TestCase):
24 def rl_hist_entries(self, rl, n):
25 """Get last n readline history entries as a list"""
26 return [rl.get_history_item(rl.get_current_history_length() - x)
27 for x in range(n - 1, -1, -1)]
28
29 def test_runs_without_rl(self):
30 """Test that function does not throw without readline"""
31 ip = get_ipython()
32 ip.has_readline = False
33 ip.readline = None
34 ip._replace_rlhist_multiline(u'source')
35
36 @skipif(not get_ipython().has_readline, 'no readline')
37 def test_replace_multiline_hist_disabled(self):
38 """Test that multiline replace does nothing if disabled"""
39 ip = get_ipython()
40 ip.multiline_history = False
41
42 ghist = [u'line1', u'line2']
43 for h in ghist:
44 ip.readline.add_history(h)
45 ip.hlen_before_cell = ip.readline.get_current_history_length()
46 ip._replace_rlhist_multiline(u'sourc€\nsource2')
47
48 self.assertEquals(ip.readline.get_current_history_length(),
49 ip.hlen_before_cell)
50 hist = self.rl_hist_entries(ip.readline, 2)
51 self.assertEquals(hist, ghist)
52
53 @skipif(not get_ipython().has_readline, 'no readline')
54 def test_replace_multiline_hist_adds(self):
55 """Test that multiline replace function adds history"""
56 ip = get_ipython()
57
58 ip.hlen_before_cell = ip.readline.get_current_history_length()
59 ip._replace_rlhist_multiline(u'sourc€')
60
61 self.assertEquals(ip.hlen_before_cell,
62 ip.readline.get_current_history_length())
63
64 @skipif(not get_ipython().has_readline, 'no readline')
65 def test_replace_multiline_hist_keeps_history(self):
66 """Test that multiline replace does not delete history"""
67 ip = get_ipython()
68 ip.multiline_history = True
69
70 ghist = [u'line1', u'line2']
71 for h in ghist:
72 ip.readline.add_history(h)
73
74 #start cell
75 ip.hlen_before_cell = ip.readline.get_current_history_length()
76 ip._replace_rlhist_multiline(u'sourc€\nsource2')
77
78 self.assertEquals(ip.readline.get_current_history_length(),
79 ip.hlen_before_cell)
80 hist = self.rl_hist_entries(ip.readline, 3)
81 self.assertEquals(hist, ghist + ['sourc€\nsource2'])
82
83 @skipif(not get_ipython().has_readline, 'no readline')
84 def test_replace_multiline_hist_replaces(self):
85 """Test that multiline entries are replaced"""
86 ip = get_ipython()
87 ip.multiline_history = True
88
89 ip.readline.add_history(u'line0')
90 #start cell
91 ip.hlen_before_cell = ip.readline.get_current_history_length()
92 ip.readline.add_history('l€ne1')
93 ip.readline.add_history('line2')
94 #replace cell with single line
95 ip._replace_rlhist_multiline(u'l€ne1\nline2')
96
97 self.assertEquals(ip.readline.get_current_history_length(),
98 ip.hlen_before_cell)
99 hist = self.rl_hist_entries(ip.readline, 2)
100 self.assertEquals(hist, [u'line0', 'l€ne1\nline2'])
101
102 @skipif(not get_ipython().has_readline, 'no readline')
103 def test_replace_multiline_hist_replaces_twice(self):
104 """Test that multiline entries are replaced twice"""
105 ip = get_ipython()
106 ip.multiline_history = True
107
108 ip.readline.add_history(u'line0')
109 #start cell
110 ip.hlen_before_cell = ip.readline.get_current_history_length()
111 ip.readline.add_history('l€ne1')
112 ip.readline.add_history('line2')
113 #replace cell with single line
114 ip._replace_rlhist_multiline(u'l€ne1\nline2')
115 ip.readline.add_history('l€ne3')
116 ip.readline.add_history('line4')
117 #replace cell with single line
118 ip._replace_rlhist_multiline(u'l€ne3\nline4')
119
120 self.assertEquals(ip.readline.get_current_history_length(),
121 ip.hlen_before_cell)
122 hist = self.rl_hist_entries(ip.readline, 3)
123 self.assertEquals(hist, ['line0', 'l€ne1\nline2', 'l€ne3\nline4'])
General Comments 0
You need to be logged in to leave comments. Login now