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