Show More
@@ -1,168 +1,171 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 sys | |||
19 | import unittest |
|
20 | import unittest | |
20 |
|
21 | |||
21 | from IPython.testing.decorators import skipif |
|
22 | from IPython.testing.decorators import skipif | |
22 | from IPython.utils import py3compat |
|
23 | from IPython.utils import py3compat | |
23 |
|
24 | |||
24 | class InteractiveShellTestCase(unittest.TestCase): |
|
25 | class InteractiveShellTestCase(unittest.TestCase): | |
25 | def rl_hist_entries(self, rl, n): |
|
26 | def rl_hist_entries(self, rl, n): | |
26 | """Get last n readline history entries as a list""" |
|
27 | """Get last n readline history entries as a list""" | |
27 | return [rl.get_history_item(rl.get_current_history_length() - x) |
|
28 | return [rl.get_history_item(rl.get_current_history_length() - x) | |
28 | for x in range(n - 1, -1, -1)] |
|
29 | for x in range(n - 1, -1, -1)] | |
29 |
|
30 | |||
30 | def test_runs_without_rl(self): |
|
31 | def test_runs_without_rl(self): | |
31 | """Test that function does not throw without readline""" |
|
32 | """Test that function does not throw without readline""" | |
32 | ip = get_ipython() |
|
33 | ip = get_ipython() | |
33 | ip.has_readline = False |
|
34 | ip.has_readline = False | |
34 | ip.readline = None |
|
35 | ip.readline = None | |
35 | ip._replace_rlhist_multiline(u'source', 0) |
|
36 | ip._replace_rlhist_multiline(u'source', 0) | |
36 |
|
37 | |||
37 | @skipif(not get_ipython().has_readline, 'no readline') |
|
38 | @skipif(not get_ipython().has_readline, 'no readline') | |
38 | def test_runs_without_remove_history_item(self): |
|
39 | def test_runs_without_remove_history_item(self): | |
39 | """Test that function does not throw on windows without |
|
40 | """Test that function does not throw on windows without | |
40 | remove_history_item""" |
|
41 | remove_history_item""" | |
41 | ip = get_ipython() |
|
42 | ip = get_ipython() | |
42 | if hasattr(ip.readline, 'remove_history_item'): |
|
43 | if hasattr(ip.readline, 'remove_history_item'): | |
43 | del ip.readline.remove_history_item |
|
44 | del ip.readline.remove_history_item | |
44 | ip._replace_rlhist_multiline(u'source', 0) |
|
45 | ip._replace_rlhist_multiline(u'source', 0) | |
45 |
|
46 | |||
46 | @skipif(not get_ipython().has_readline, 'no readline') |
|
47 | @skipif(not get_ipython().has_readline, 'no readline') | |
47 | @skipif(not hasattr(get_ipython().readline, 'remove_history_item'), |
|
48 | @skipif(not hasattr(get_ipython().readline, 'remove_history_item'), | |
48 | 'no remove_history_item') |
|
49 | 'no remove_history_item') | |
49 | def test_replace_multiline_hist_disabled(self): |
|
50 | def test_replace_multiline_hist_disabled(self): | |
50 | """Test that multiline replace does nothing if disabled""" |
|
51 | """Test that multiline replace does nothing if disabled""" | |
51 | ip = get_ipython() |
|
52 | ip = get_ipython() | |
52 | ip.multiline_history = False |
|
53 | ip.multiline_history = False | |
53 |
|
54 | |||
54 | ghist = [u'line1', u'line2'] |
|
55 | ghist = [u'line1', u'line2'] | |
55 | for h in ghist: |
|
56 | for h in ghist: | |
56 | ip.readline.add_history(h) |
|
57 | ip.readline.add_history(h) | |
57 | hlen_b4_cell = ip.readline.get_current_history_length() |
|
58 | hlen_b4_cell = ip.readline.get_current_history_length() | |
58 | hlen_b4_cell = ip._replace_rlhist_multiline(u'sourc€\nsource2', |
|
59 | hlen_b4_cell = ip._replace_rlhist_multiline(u'sourc€\nsource2', | |
59 | hlen_b4_cell) |
|
60 | hlen_b4_cell) | |
60 |
|
61 | |||
61 | self.assertEquals(ip.readline.get_current_history_length(), |
|
62 | self.assertEquals(ip.readline.get_current_history_length(), | |
62 | hlen_b4_cell) |
|
63 | hlen_b4_cell) | |
63 | hist = self.rl_hist_entries(ip.readline, 2) |
|
64 | hist = self.rl_hist_entries(ip.readline, 2) | |
64 | self.assertEquals(hist, ghist) |
|
65 | self.assertEquals(hist, ghist) | |
65 |
|
66 | |||
66 | @skipif(not get_ipython().has_readline, 'no readline') |
|
67 | @skipif(not get_ipython().has_readline, 'no readline') | |
67 | @skipif(not hasattr(get_ipython().readline, 'remove_history_item'), |
|
68 | @skipif(not hasattr(get_ipython().readline, 'remove_history_item'), | |
68 | 'no remove_history_item') |
|
69 | 'no remove_history_item') | |
69 | def test_replace_multiline_hist_adds(self): |
|
70 | def test_replace_multiline_hist_adds(self): | |
70 | """Test that multiline replace function adds history""" |
|
71 | """Test that multiline replace function adds history""" | |
71 | ip = get_ipython() |
|
72 | ip = get_ipython() | |
72 |
|
73 | |||
73 | hlen_b4_cell = ip.readline.get_current_history_length() |
|
74 | hlen_b4_cell = ip.readline.get_current_history_length() | |
74 | hlen_b4_cell = ip._replace_rlhist_multiline(u'sourc€', hlen_b4_cell) |
|
75 | hlen_b4_cell = ip._replace_rlhist_multiline(u'sourc€', hlen_b4_cell) | |
75 |
|
76 | |||
76 | self.assertEquals(hlen_b4_cell, |
|
77 | self.assertEquals(hlen_b4_cell, | |
77 | ip.readline.get_current_history_length()) |
|
78 | ip.readline.get_current_history_length()) | |
78 |
|
79 | |||
79 | @skipif(not get_ipython().has_readline, 'no readline') |
|
80 | @skipif(not get_ipython().has_readline, 'no readline') | |
80 | @skipif(not hasattr(get_ipython().readline, 'remove_history_item'), |
|
81 | @skipif(not hasattr(get_ipython().readline, 'remove_history_item'), | |
81 | 'no remove_history_item') |
|
82 | 'no remove_history_item') | |
82 | def test_replace_multiline_hist_keeps_history(self): |
|
83 | def test_replace_multiline_hist_keeps_history(self): | |
83 | """Test that multiline replace does not delete history""" |
|
84 | """Test that multiline replace does not delete history""" | |
84 | ip = get_ipython() |
|
85 | ip = get_ipython() | |
85 | ip.multiline_history = True |
|
86 | ip.multiline_history = True | |
86 |
|
87 | |||
87 | ghist = [u'line1', u'line2'] |
|
88 | ghist = [u'line1', u'line2'] | |
88 | for h in ghist: |
|
89 | for h in ghist: | |
89 | ip.readline.add_history(h) |
|
90 | ip.readline.add_history(h) | |
90 |
|
91 | |||
91 | #start cell |
|
92 | #start cell | |
92 | hlen_b4_cell = ip.readline.get_current_history_length() |
|
93 | hlen_b4_cell = ip.readline.get_current_history_length() | |
93 | # nothing added to rl history, should do nothing |
|
94 | # nothing added to rl history, should do nothing | |
94 | hlen_b4_cell = ip._replace_rlhist_multiline(u'sourc€\nsource2', |
|
95 | hlen_b4_cell = ip._replace_rlhist_multiline(u'sourc€\nsource2', | |
95 | hlen_b4_cell) |
|
96 | hlen_b4_cell) | |
96 |
|
97 | |||
97 | self.assertEquals(ip.readline.get_current_history_length(), |
|
98 | self.assertEquals(ip.readline.get_current_history_length(), | |
98 | hlen_b4_cell) |
|
99 | hlen_b4_cell) | |
99 | hist = self.rl_hist_entries(ip.readline, 2) |
|
100 | hist = self.rl_hist_entries(ip.readline, 2) | |
100 | self.assertEquals(hist, ghist) |
|
101 | self.assertEquals(hist, ghist) | |
101 |
|
102 | |||
102 |
|
103 | |||
103 | @skipif(not get_ipython().has_readline, 'no readline') |
|
104 | @skipif(not get_ipython().has_readline, 'no readline') | |
104 | @skipif(not hasattr(get_ipython().readline, 'remove_history_item'), |
|
105 | @skipif(not hasattr(get_ipython().readline, 'remove_history_item'), | |
105 | 'no remove_history_item') |
|
106 | 'no remove_history_item') | |
106 | def test_replace_multiline_hist_replaces_twice(self): |
|
107 | def test_replace_multiline_hist_replaces_twice(self): | |
107 | """Test that multiline entries are replaced twice""" |
|
108 | """Test that multiline entries are replaced twice""" | |
108 | ip = get_ipython() |
|
109 | ip = get_ipython() | |
109 | ip.multiline_history = True |
|
110 | ip.multiline_history = True | |
110 |
|
111 | |||
111 | ip.readline.add_history(u'line0') |
|
112 | ip.readline.add_history(u'line0') | |
112 | #start cell |
|
113 | #start cell | |
113 | hlen_b4_cell = ip.readline.get_current_history_length() |
|
114 | hlen_b4_cell = ip.readline.get_current_history_length() | |
114 | ip.readline.add_history('l€ne1') |
|
115 | ip.readline.add_history('l€ne1') | |
115 | ip.readline.add_history('line2') |
|
116 | ip.readline.add_history('line2') | |
116 | #replace cell with single line |
|
117 | #replace cell with single line | |
117 | hlen_b4_cell = ip._replace_rlhist_multiline(u'l€ne1\nline2', |
|
118 | hlen_b4_cell = ip._replace_rlhist_multiline(u'l€ne1\nline2', | |
118 | hlen_b4_cell) |
|
119 | hlen_b4_cell) | |
119 | ip.readline.add_history('l€ne3') |
|
120 | ip.readline.add_history('l€ne3') | |
120 | ip.readline.add_history('line4') |
|
121 | ip.readline.add_history('line4') | |
121 | #replace cell with single line |
|
122 | #replace cell with single line | |
122 | hlen_b4_cell = ip._replace_rlhist_multiline(u'l€ne3\nline4', |
|
123 | hlen_b4_cell = ip._replace_rlhist_multiline(u'l€ne3\nline4', | |
123 | hlen_b4_cell) |
|
124 | hlen_b4_cell) | |
124 |
|
125 | |||
125 | self.assertEquals(ip.readline.get_current_history_length(), |
|
126 | self.assertEquals(ip.readline.get_current_history_length(), | |
126 | hlen_b4_cell) |
|
127 | hlen_b4_cell) | |
127 | hist = self.rl_hist_entries(ip.readline, 3) |
|
128 | hist = self.rl_hist_entries(ip.readline, 3) | |
128 | expected = [u'line0', u'l€ne1\nline2', u'l€ne3\nline4'] |
|
129 | expected = [u'line0', u'l€ne1\nline2', u'l€ne3\nline4'] | |
129 | # perform encoding, in case of casting due to ASCII locale |
|
130 | # perform encoding, in case of casting due to ASCII locale | |
130 | expected = [ py3compat.unicode_to_str(e) for e in expected ] |
|
131 | enc = sys.stdin.encoding or "utf-8" | |
|
132 | expected = [ py3compat.unicode_to_str(e, enc) for e in expected ] | |||
131 | self.assertEquals(hist, expected) |
|
133 | self.assertEquals(hist, expected) | |
132 |
|
134 | |||
133 |
|
135 | |||
134 | @skipif(not get_ipython().has_readline, 'no readline') |
|
136 | @skipif(not get_ipython().has_readline, 'no readline') | |
135 | @skipif(not hasattr(get_ipython().readline, 'remove_history_item'), |
|
137 | @skipif(not hasattr(get_ipython().readline, 'remove_history_item'), | |
136 | 'no remove_history_item') |
|
138 | 'no remove_history_item') | |
137 | def test_replace_multiline_hist_replaces_empty_line(self): |
|
139 | def test_replace_multiline_hist_replaces_empty_line(self): | |
138 | """Test that multiline history skips empty line cells""" |
|
140 | """Test that multiline history skips empty line cells""" | |
139 | ip = get_ipython() |
|
141 | ip = get_ipython() | |
140 | ip.multiline_history = True |
|
142 | ip.multiline_history = True | |
141 |
|
143 | |||
142 | ip.readline.add_history(u'line0') |
|
144 | ip.readline.add_history(u'line0') | |
143 | #start cell |
|
145 | #start cell | |
144 | hlen_b4_cell = ip.readline.get_current_history_length() |
|
146 | hlen_b4_cell = ip.readline.get_current_history_length() | |
145 | ip.readline.add_history('l€ne1') |
|
147 | ip.readline.add_history('l€ne1') | |
146 | ip.readline.add_history('line2') |
|
148 | ip.readline.add_history('line2') | |
147 | hlen_b4_cell = ip._replace_rlhist_multiline(u'l€ne1\nline2', |
|
149 | hlen_b4_cell = ip._replace_rlhist_multiline(u'l€ne1\nline2', | |
148 | hlen_b4_cell) |
|
150 | hlen_b4_cell) | |
149 | ip.readline.add_history('') |
|
151 | ip.readline.add_history('') | |
150 | hlen_b4_cell = ip._replace_rlhist_multiline(u'', hlen_b4_cell) |
|
152 | hlen_b4_cell = ip._replace_rlhist_multiline(u'', hlen_b4_cell) | |
151 | ip.readline.add_history('l€ne3') |
|
153 | ip.readline.add_history('l€ne3') | |
152 | hlen_b4_cell = ip._replace_rlhist_multiline(u'l€ne3', hlen_b4_cell) |
|
154 | hlen_b4_cell = ip._replace_rlhist_multiline(u'l€ne3', hlen_b4_cell) | |
153 | ip.readline.add_history(' ') |
|
155 | ip.readline.add_history(' ') | |
154 | hlen_b4_cell = ip._replace_rlhist_multiline(' ', hlen_b4_cell) |
|
156 | hlen_b4_cell = ip._replace_rlhist_multiline(' ', hlen_b4_cell) | |
155 | ip.readline.add_history('\t') |
|
157 | ip.readline.add_history('\t') | |
156 | ip.readline.add_history('\t ') |
|
158 | ip.readline.add_history('\t ') | |
157 | hlen_b4_cell = ip._replace_rlhist_multiline('\t', hlen_b4_cell) |
|
159 | hlen_b4_cell = ip._replace_rlhist_multiline('\t', hlen_b4_cell) | |
158 | ip.readline.add_history('line4') |
|
160 | ip.readline.add_history('line4') | |
159 | hlen_b4_cell = ip._replace_rlhist_multiline(u'line4', hlen_b4_cell) |
|
161 | hlen_b4_cell = ip._replace_rlhist_multiline(u'line4', hlen_b4_cell) | |
160 |
|
162 | |||
161 | self.assertEquals(ip.readline.get_current_history_length(), |
|
163 | self.assertEquals(ip.readline.get_current_history_length(), | |
162 | hlen_b4_cell) |
|
164 | hlen_b4_cell) | |
163 | hist = self.rl_hist_entries(ip.readline, 4) |
|
165 | hist = self.rl_hist_entries(ip.readline, 4) | |
164 | # expect no empty cells in history |
|
166 | # expect no empty cells in history | |
165 | expected = [u'line0', u'l€ne1\nline2', u'l€ne3', u'line4'] |
|
167 | expected = [u'line0', u'l€ne1\nline2', u'l€ne3', u'line4'] | |
166 | # perform encoding, in case of casting due to ASCII locale |
|
168 | # perform encoding, in case of casting due to ASCII locale | |
167 | expected = [ py3compat.unicode_to_str(e) for e in expected ] |
|
169 | enc = sys.stdin.encoding or "utf-8" | |
|
170 | expected = [ py3compat.unicode_to_str(e, enc) for e in expected ] | |||
168 | self.assertEquals(hist, expected) |
|
171 | self.assertEquals(hist, expected) |
General Comments 0
You need to be logged in to leave comments.
Login now