Show More
@@ -1,189 +1,203 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 sys | |
20 | import unittest |
|
20 | import unittest | |
21 |
|
21 | |||
22 | from IPython.testing.decorators import skipif |
|
22 | from IPython.testing.decorators import skipif | |
23 | from IPython.utils import py3compat |
|
23 | from IPython.utils import py3compat | |
24 | from IPython.testing import tools as tt |
|
24 | from IPython.testing import tools as tt | |
25 |
|
25 | |||
26 | class InteractiveShellTestCase(unittest.TestCase): |
|
26 | class InteractiveShellTestCase(unittest.TestCase): | |
27 | def rl_hist_entries(self, rl, n): |
|
27 | def rl_hist_entries(self, rl, n): | |
28 | """Get last n readline history entries as a list""" |
|
28 | """Get last n readline history entries as a list""" | |
29 | return [rl.get_history_item(rl.get_current_history_length() - x) |
|
29 | return [rl.get_history_item(rl.get_current_history_length() - x) | |
30 | for x in range(n - 1, -1, -1)] |
|
30 | for x in range(n - 1, -1, -1)] | |
31 |
|
31 | |||
32 | def test_runs_without_rl(self): |
|
32 | def test_runs_without_rl(self): | |
33 | """Test that function does not throw without readline""" |
|
33 | """Test that function does not throw without readline""" | |
34 | ip = get_ipython() |
|
34 | ip = get_ipython() | |
35 | ip.has_readline = False |
|
35 | ip.has_readline = False | |
36 | ip.readline = None |
|
36 | ip.readline = None | |
37 | ip._replace_rlhist_multiline(u'source', 0) |
|
37 | ip._replace_rlhist_multiline(u'source', 0) | |
38 |
|
38 | |||
39 | @skipif(not get_ipython().has_readline, 'no readline') |
|
39 | @skipif(not get_ipython().has_readline, 'no readline') | |
40 | def test_runs_without_remove_history_item(self): |
|
40 | def test_runs_without_remove_history_item(self): | |
41 | """Test that function does not throw on windows without |
|
41 | """Test that function does not throw on windows without | |
42 | remove_history_item""" |
|
42 | remove_history_item""" | |
43 | ip = get_ipython() |
|
43 | ip = get_ipython() | |
44 | if hasattr(ip.readline, 'remove_history_item'): |
|
44 | if hasattr(ip.readline, 'remove_history_item'): | |
45 | del ip.readline.remove_history_item |
|
45 | del ip.readline.remove_history_item | |
46 | ip._replace_rlhist_multiline(u'source', 0) |
|
46 | ip._replace_rlhist_multiline(u'source', 0) | |
47 |
|
47 | |||
48 | @skipif(not get_ipython().has_readline, 'no readline') |
|
48 | @skipif(not get_ipython().has_readline, 'no readline') | |
49 | @skipif(not hasattr(get_ipython().readline, 'remove_history_item'), |
|
49 | @skipif(not hasattr(get_ipython().readline, 'remove_history_item'), | |
50 | 'no remove_history_item') |
|
50 | 'no remove_history_item') | |
51 | def test_replace_multiline_hist_disabled(self): |
|
51 | def test_replace_multiline_hist_disabled(self): | |
52 | """Test that multiline replace does nothing if disabled""" |
|
52 | """Test that multiline replace does nothing if disabled""" | |
53 | ip = get_ipython() |
|
53 | ip = get_ipython() | |
54 | ip.multiline_history = False |
|
54 | ip.multiline_history = False | |
55 |
|
55 | |||
56 | ghist = [u'line1', u'line2'] |
|
56 | ghist = [u'line1', u'line2'] | |
57 | for h in ghist: |
|
57 | for h in ghist: | |
58 | ip.readline.add_history(h) |
|
58 | ip.readline.add_history(h) | |
59 | hlen_b4_cell = ip.readline.get_current_history_length() |
|
59 | hlen_b4_cell = ip.readline.get_current_history_length() | |
60 | hlen_b4_cell = ip._replace_rlhist_multiline(u'sourcβ¬\nsource2', |
|
60 | hlen_b4_cell = ip._replace_rlhist_multiline(u'sourcβ¬\nsource2', | |
61 | hlen_b4_cell) |
|
61 | hlen_b4_cell) | |
62 |
|
62 | |||
63 | self.assertEqual(ip.readline.get_current_history_length(), |
|
63 | self.assertEqual(ip.readline.get_current_history_length(), | |
64 | hlen_b4_cell) |
|
64 | hlen_b4_cell) | |
65 | hist = self.rl_hist_entries(ip.readline, 2) |
|
65 | hist = self.rl_hist_entries(ip.readline, 2) | |
66 | self.assertEqual(hist, ghist) |
|
66 | self.assertEqual(hist, ghist) | |
67 |
|
67 | |||
68 | @skipif(not get_ipython().has_readline, 'no readline') |
|
68 | @skipif(not get_ipython().has_readline, 'no readline') | |
69 | @skipif(not hasattr(get_ipython().readline, 'remove_history_item'), |
|
69 | @skipif(not hasattr(get_ipython().readline, 'remove_history_item'), | |
70 | 'no remove_history_item') |
|
70 | 'no remove_history_item') | |
71 | def test_replace_multiline_hist_adds(self): |
|
71 | def test_replace_multiline_hist_adds(self): | |
72 | """Test that multiline replace function adds history""" |
|
72 | """Test that multiline replace function adds history""" | |
73 | ip = get_ipython() |
|
73 | ip = get_ipython() | |
74 |
|
74 | |||
75 | hlen_b4_cell = ip.readline.get_current_history_length() |
|
75 | hlen_b4_cell = ip.readline.get_current_history_length() | |
76 | hlen_b4_cell = ip._replace_rlhist_multiline(u'sourcβ¬', hlen_b4_cell) |
|
76 | hlen_b4_cell = ip._replace_rlhist_multiline(u'sourcβ¬', hlen_b4_cell) | |
77 |
|
77 | |||
78 | self.assertEqual(hlen_b4_cell, |
|
78 | self.assertEqual(hlen_b4_cell, | |
79 | ip.readline.get_current_history_length()) |
|
79 | ip.readline.get_current_history_length()) | |
80 |
|
80 | |||
81 | @skipif(not get_ipython().has_readline, 'no readline') |
|
81 | @skipif(not get_ipython().has_readline, 'no readline') | |
82 | @skipif(not hasattr(get_ipython().readline, 'remove_history_item'), |
|
82 | @skipif(not hasattr(get_ipython().readline, 'remove_history_item'), | |
83 | 'no remove_history_item') |
|
83 | 'no remove_history_item') | |
84 | def test_replace_multiline_hist_keeps_history(self): |
|
84 | def test_replace_multiline_hist_keeps_history(self): | |
85 | """Test that multiline replace does not delete history""" |
|
85 | """Test that multiline replace does not delete history""" | |
86 | ip = get_ipython() |
|
86 | ip = get_ipython() | |
87 | ip.multiline_history = True |
|
87 | ip.multiline_history = True | |
88 |
|
88 | |||
89 | ghist = [u'line1', u'line2'] |
|
89 | ghist = [u'line1', u'line2'] | |
90 | for h in ghist: |
|
90 | for h in ghist: | |
91 | ip.readline.add_history(h) |
|
91 | ip.readline.add_history(h) | |
92 |
|
92 | |||
93 | #start cell |
|
93 | #start cell | |
94 | hlen_b4_cell = ip.readline.get_current_history_length() |
|
94 | hlen_b4_cell = ip.readline.get_current_history_length() | |
95 | # nothing added to rl history, should do nothing |
|
95 | # nothing added to rl history, should do nothing | |
96 | hlen_b4_cell = ip._replace_rlhist_multiline(u'sourcβ¬\nsource2', |
|
96 | hlen_b4_cell = ip._replace_rlhist_multiline(u'sourcβ¬\nsource2', | |
97 | hlen_b4_cell) |
|
97 | hlen_b4_cell) | |
98 |
|
98 | |||
99 | self.assertEqual(ip.readline.get_current_history_length(), |
|
99 | self.assertEqual(ip.readline.get_current_history_length(), | |
100 | hlen_b4_cell) |
|
100 | hlen_b4_cell) | |
101 | hist = self.rl_hist_entries(ip.readline, 2) |
|
101 | hist = self.rl_hist_entries(ip.readline, 2) | |
102 | self.assertEqual(hist, ghist) |
|
102 | self.assertEqual(hist, ghist) | |
103 |
|
103 | |||
104 |
|
104 | |||
105 | @skipif(not get_ipython().has_readline, 'no readline') |
|
105 | @skipif(not get_ipython().has_readline, 'no readline') | |
106 | @skipif(not hasattr(get_ipython().readline, 'remove_history_item'), |
|
106 | @skipif(not hasattr(get_ipython().readline, 'remove_history_item'), | |
107 | 'no remove_history_item') |
|
107 | 'no remove_history_item') | |
108 | def test_replace_multiline_hist_replaces_twice(self): |
|
108 | def test_replace_multiline_hist_replaces_twice(self): | |
109 | """Test that multiline entries are replaced twice""" |
|
109 | """Test that multiline entries are replaced twice""" | |
110 | ip = get_ipython() |
|
110 | ip = get_ipython() | |
111 | ip.multiline_history = True |
|
111 | ip.multiline_history = True | |
112 |
|
112 | |||
113 | ip.readline.add_history(u'line0') |
|
113 | ip.readline.add_history(u'line0') | |
114 | #start cell |
|
114 | #start cell | |
115 | hlen_b4_cell = ip.readline.get_current_history_length() |
|
115 | hlen_b4_cell = ip.readline.get_current_history_length() | |
116 | ip.readline.add_history('lβ¬ne1') |
|
116 | ip.readline.add_history('lβ¬ne1') | |
117 | ip.readline.add_history('line2') |
|
117 | ip.readline.add_history('line2') | |
118 | #replace cell with single line |
|
118 | #replace cell with single line | |
119 | hlen_b4_cell = ip._replace_rlhist_multiline(u'lβ¬ne1\nline2', |
|
119 | hlen_b4_cell = ip._replace_rlhist_multiline(u'lβ¬ne1\nline2', | |
120 | hlen_b4_cell) |
|
120 | hlen_b4_cell) | |
121 | ip.readline.add_history('lβ¬ne3') |
|
121 | ip.readline.add_history('lβ¬ne3') | |
122 | ip.readline.add_history('line4') |
|
122 | ip.readline.add_history('line4') | |
123 | #replace cell with single line |
|
123 | #replace cell with single line | |
124 | hlen_b4_cell = ip._replace_rlhist_multiline(u'lβ¬ne3\nline4', |
|
124 | hlen_b4_cell = ip._replace_rlhist_multiline(u'lβ¬ne3\nline4', | |
125 | hlen_b4_cell) |
|
125 | hlen_b4_cell) | |
126 |
|
126 | |||
127 | self.assertEqual(ip.readline.get_current_history_length(), |
|
127 | self.assertEqual(ip.readline.get_current_history_length(), | |
128 | hlen_b4_cell) |
|
128 | hlen_b4_cell) | |
129 | hist = self.rl_hist_entries(ip.readline, 3) |
|
129 | hist = self.rl_hist_entries(ip.readline, 3) | |
130 | expected = [u'line0', u'lβ¬ne1\nline2', u'lβ¬ne3\nline4'] |
|
130 | expected = [u'line0', u'lβ¬ne1\nline2', u'lβ¬ne3\nline4'] | |
131 | # perform encoding, in case of casting due to ASCII locale |
|
131 | # perform encoding, in case of casting due to ASCII locale | |
132 | enc = sys.stdin.encoding or "utf-8" |
|
132 | enc = sys.stdin.encoding or "utf-8" | |
133 | expected = [ py3compat.unicode_to_str(e, enc) for e in expected ] |
|
133 | expected = [ py3compat.unicode_to_str(e, enc) for e in expected ] | |
134 | self.assertEqual(hist, expected) |
|
134 | self.assertEqual(hist, expected) | |
135 |
|
135 | |||
136 |
|
136 | |||
137 | @skipif(not get_ipython().has_readline, 'no readline') |
|
137 | @skipif(not get_ipython().has_readline, 'no readline') | |
138 | @skipif(not hasattr(get_ipython().readline, 'remove_history_item'), |
|
138 | @skipif(not hasattr(get_ipython().readline, 'remove_history_item'), | |
139 | 'no remove_history_item') |
|
139 | 'no remove_history_item') | |
140 | def test_replace_multiline_hist_replaces_empty_line(self): |
|
140 | def test_replace_multiline_hist_replaces_empty_line(self): | |
141 | """Test that multiline history skips empty line cells""" |
|
141 | """Test that multiline history skips empty line cells""" | |
142 | ip = get_ipython() |
|
142 | ip = get_ipython() | |
143 | ip.multiline_history = True |
|
143 | ip.multiline_history = True | |
144 |
|
144 | |||
145 | ip.readline.add_history(u'line0') |
|
145 | ip.readline.add_history(u'line0') | |
146 | #start cell |
|
146 | #start cell | |
147 | hlen_b4_cell = ip.readline.get_current_history_length() |
|
147 | hlen_b4_cell = ip.readline.get_current_history_length() | |
148 | ip.readline.add_history('lβ¬ne1') |
|
148 | ip.readline.add_history('lβ¬ne1') | |
149 | ip.readline.add_history('line2') |
|
149 | ip.readline.add_history('line2') | |
150 | hlen_b4_cell = ip._replace_rlhist_multiline(u'lβ¬ne1\nline2', |
|
150 | hlen_b4_cell = ip._replace_rlhist_multiline(u'lβ¬ne1\nline2', | |
151 | hlen_b4_cell) |
|
151 | hlen_b4_cell) | |
152 | ip.readline.add_history('') |
|
152 | ip.readline.add_history('') | |
153 | hlen_b4_cell = ip._replace_rlhist_multiline(u'', hlen_b4_cell) |
|
153 | hlen_b4_cell = ip._replace_rlhist_multiline(u'', hlen_b4_cell) | |
154 | ip.readline.add_history('lβ¬ne3') |
|
154 | ip.readline.add_history('lβ¬ne3') | |
155 | hlen_b4_cell = ip._replace_rlhist_multiline(u'lβ¬ne3', hlen_b4_cell) |
|
155 | hlen_b4_cell = ip._replace_rlhist_multiline(u'lβ¬ne3', hlen_b4_cell) | |
156 | ip.readline.add_history(' ') |
|
156 | ip.readline.add_history(' ') | |
157 | hlen_b4_cell = ip._replace_rlhist_multiline(' ', hlen_b4_cell) |
|
157 | hlen_b4_cell = ip._replace_rlhist_multiline(' ', hlen_b4_cell) | |
158 | ip.readline.add_history('\t') |
|
158 | ip.readline.add_history('\t') | |
159 | ip.readline.add_history('\t ') |
|
159 | ip.readline.add_history('\t ') | |
160 | hlen_b4_cell = ip._replace_rlhist_multiline('\t', hlen_b4_cell) |
|
160 | hlen_b4_cell = ip._replace_rlhist_multiline('\t', hlen_b4_cell) | |
161 | ip.readline.add_history('line4') |
|
161 | ip.readline.add_history('line4') | |
162 | hlen_b4_cell = ip._replace_rlhist_multiline(u'line4', hlen_b4_cell) |
|
162 | hlen_b4_cell = ip._replace_rlhist_multiline(u'line4', hlen_b4_cell) | |
163 |
|
163 | |||
164 | self.assertEqual(ip.readline.get_current_history_length(), |
|
164 | self.assertEqual(ip.readline.get_current_history_length(), | |
165 | hlen_b4_cell) |
|
165 | hlen_b4_cell) | |
166 | hist = self.rl_hist_entries(ip.readline, 4) |
|
166 | hist = self.rl_hist_entries(ip.readline, 4) | |
167 | # expect no empty cells in history |
|
167 | # expect no empty cells in history | |
168 | expected = [u'line0', u'lβ¬ne1\nline2', u'lβ¬ne3', u'line4'] |
|
168 | expected = [u'line0', u'lβ¬ne1\nline2', u'lβ¬ne3', u'line4'] | |
169 | # perform encoding, in case of casting due to ASCII locale |
|
169 | # perform encoding, in case of casting due to ASCII locale | |
170 | enc = sys.stdin.encoding or "utf-8" |
|
170 | enc = sys.stdin.encoding or "utf-8" | |
171 | expected = [ py3compat.unicode_to_str(e, enc) for e in expected ] |
|
171 | expected = [ py3compat.unicode_to_str(e, enc) for e in expected ] | |
172 | self.assertEqual(hist, expected) |
|
172 | self.assertEqual(hist, expected) | |
173 |
|
173 | |||
|
174 | class TerminalMagicsTestCase(unittest.TestCase): | |||
174 | def test_paste_magics_message(self): |
|
175 | def test_paste_magics_message(self): | |
175 | """Test that an IndentationError while using paste magics doesn't |
|
176 | """Test that an IndentationError while using paste magics doesn't | |
176 | trigger a message about paste magics and also the opposite.""" |
|
177 | trigger a message about paste magics and also the opposite.""" | |
177 |
|
178 | |||
178 | ip = get_ipython() |
|
179 | ip = get_ipython() | |
179 | s = ('for a in range(5):\n' |
|
180 | s = ('for a in range(5):\n' | |
180 | 'print(a)') |
|
181 | 'print(a)') | |
181 |
|
182 | |||
182 | tm = ip.magics_manager.registry['TerminalMagics'] |
|
183 | tm = ip.magics_manager.registry['TerminalMagics'] | |
183 | with tt.AssertPrints("If you want to paste code into IPython, try the " |
|
184 | with tt.AssertPrints("If you want to paste code into IPython, try the " | |
184 | "%paste and %cpaste magic functions."): |
|
185 | "%paste and %cpaste magic functions."): | |
185 | ip.run_cell(s) |
|
186 | ip.run_cell(s) | |
186 |
|
187 | |||
187 | with tt.AssertNotPrints("If you want to paste code into IPython, try the " |
|
188 | with tt.AssertNotPrints("If you want to paste code into IPython, try the " | |
188 | "%paste and %cpaste magic functions."): |
|
189 | "%paste and %cpaste magic functions."): | |
189 | tm.store_or_execute(s, name=None) |
|
190 | tm.store_or_execute(s, name=None) | |
|
191 | ||||
|
192 | def test_paste_magics_blankline(self): | |||
|
193 | """Test that code with a blank line doesn't get split (gh-3246).""" | |||
|
194 | ip = get_ipython() | |||
|
195 | s = ('def pasted_func(a):\n' | |||
|
196 | ' b = a+1\n' | |||
|
197 | '\n' | |||
|
198 | ' return b') | |||
|
199 | ||||
|
200 | tm = ip.magics_manager.registry['TerminalMagics'] | |||
|
201 | tm.store_or_execute(s, name=None) | |||
|
202 | ||||
|
203 | self.assertEqual(ip.user_ns['pasted_func'](54), 55) |
General Comments 0
You need to be logged in to leave comments.
Login now