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