Show More
@@ -1,345 +1,345 | |||
|
1 | 1 | """Tests for the IPython tab-completion machinery. |
|
2 | 2 | """ |
|
3 | 3 | #----------------------------------------------------------------------------- |
|
4 | 4 | # Module imports |
|
5 | 5 | #----------------------------------------------------------------------------- |
|
6 | 6 | |
|
7 | 7 | # stdlib |
|
8 | 8 | import os |
|
9 | 9 | import sys |
|
10 | 10 | import unittest |
|
11 | 11 | |
|
12 | 12 | # third party |
|
13 | 13 | import nose.tools as nt |
|
14 | 14 | |
|
15 | 15 | # our own packages |
|
16 | 16 | from IPython.config.loader import Config |
|
17 | 17 | from IPython.core import completer |
|
18 | 18 | from IPython.external.decorators import knownfailureif |
|
19 | 19 | from IPython.utils.tempdir import TemporaryDirectory |
|
20 | 20 | from IPython.utils.generics import complete_object |
|
21 | 21 | |
|
22 | 22 | #----------------------------------------------------------------------------- |
|
23 | 23 | # Test functions |
|
24 | 24 | #----------------------------------------------------------------------------- |
|
25 | 25 | def test_protect_filename(): |
|
26 | 26 | pairs = [ ('abc','abc'), |
|
27 | 27 | (' abc',r'\ abc'), |
|
28 | 28 | ('a bc',r'a\ bc'), |
|
29 | 29 | ('a bc',r'a\ \ bc'), |
|
30 | 30 | (' bc',r'\ \ bc'), |
|
31 | 31 | ] |
|
32 | 32 | # On posix, we also protect parens and other special characters |
|
33 | 33 | if sys.platform != 'win32': |
|
34 | 34 | pairs.extend( [('a(bc',r'a\(bc'), |
|
35 | 35 | ('a)bc',r'a\)bc'), |
|
36 | 36 | ('a( )bc',r'a\(\ \)bc'), |
|
37 | 37 | ('a[1]bc', r'a\[1\]bc'), |
|
38 | 38 | ('a{1}bc', r'a\{1\}bc'), |
|
39 | 39 | ('a#bc', r'a\#bc'), |
|
40 | 40 | ('a?bc', r'a\?bc'), |
|
41 | 41 | ('a=bc', r'a\=bc'), |
|
42 | 42 | ('a\\bc', r'a\\bc'), |
|
43 | 43 | ('a|bc', r'a\|bc'), |
|
44 | 44 | ('a;bc', r'a\;bc'), |
|
45 | 45 | ('a:bc', r'a\:bc'), |
|
46 | 46 | ("a'bc", r"a\'bc"), |
|
47 | 47 | ('a*bc', r'a\*bc'), |
|
48 | 48 | ('a"bc', r'a\"bc'), |
|
49 | 49 | ('a^bc', r'a\^bc'), |
|
50 | 50 | ('a&bc', r'a\&bc'), |
|
51 | 51 | ] ) |
|
52 | 52 | # run the actual tests |
|
53 | 53 | for s1, s2 in pairs: |
|
54 | 54 | s1p = completer.protect_filename(s1) |
|
55 |
nt.assert_equal |
|
|
55 | nt.assert_equal(s1p, s2) | |
|
56 | 56 | |
|
57 | 57 | |
|
58 | 58 | def check_line_split(splitter, test_specs): |
|
59 | 59 | for part1, part2, split in test_specs: |
|
60 | 60 | cursor_pos = len(part1) |
|
61 | 61 | line = part1+part2 |
|
62 | 62 | out = splitter.split_line(line, cursor_pos) |
|
63 | 63 | nt.assert_equal(out, split) |
|
64 | 64 | |
|
65 | 65 | |
|
66 | 66 | def test_line_split(): |
|
67 | 67 | """Basice line splitter test with default specs.""" |
|
68 | 68 | sp = completer.CompletionSplitter() |
|
69 | 69 | # The format of the test specs is: part1, part2, expected answer. Parts 1 |
|
70 | 70 | # and 2 are joined into the 'line' sent to the splitter, as if the cursor |
|
71 | 71 | # was at the end of part1. So an empty part2 represents someone hitting |
|
72 | 72 | # tab at the end of the line, the most common case. |
|
73 | 73 | t = [('run some/scrip', '', 'some/scrip'), |
|
74 | 74 | ('run scripts/er', 'ror.py foo', 'scripts/er'), |
|
75 | 75 | ('echo $HOM', '', 'HOM'), |
|
76 | 76 | ('print sys.pa', '', 'sys.pa'), |
|
77 | 77 | ('print(sys.pa', '', 'sys.pa'), |
|
78 | 78 | ("execfile('scripts/er", '', 'scripts/er'), |
|
79 | 79 | ('a[x.', '', 'x.'), |
|
80 | 80 | ('a[x.', 'y', 'x.'), |
|
81 | 81 | ('cd "some_file/', '', 'some_file/'), |
|
82 | 82 | ] |
|
83 | 83 | check_line_split(sp, t) |
|
84 | 84 | # Ensure splitting works OK with unicode by re-running the tests with |
|
85 | 85 | # all inputs turned into unicode |
|
86 | 86 | check_line_split(sp, [ map(unicode, p) for p in t] ) |
|
87 | 87 | |
|
88 | 88 | |
|
89 | 89 | def test_custom_completion_error(): |
|
90 | 90 | """Test that errors from custom attribute completers are silenced.""" |
|
91 | 91 | ip = get_ipython() |
|
92 | 92 | class A(object): pass |
|
93 | 93 | ip.user_ns['a'] = A() |
|
94 | 94 | |
|
95 | 95 | @complete_object.when_type(A) |
|
96 | 96 | def complete_A(a, existing_completions): |
|
97 | 97 | raise TypeError("this should be silenced") |
|
98 | 98 | |
|
99 | 99 | ip.complete("a.") |
|
100 | 100 | |
|
101 | 101 | |
|
102 | 102 | def test_unicode_completions(): |
|
103 | 103 | ip = get_ipython() |
|
104 | 104 | # Some strings that trigger different types of completion. Check them both |
|
105 | 105 | # in str and unicode forms |
|
106 | 106 | s = ['ru', '%ru', 'cd /', 'floa', 'float(x)/'] |
|
107 | 107 | for t in s + map(unicode, s): |
|
108 | 108 | # We don't need to check exact completion values (they may change |
|
109 | 109 | # depending on the state of the namespace, but at least no exceptions |
|
110 | 110 | # should be thrown and the return value should be a pair of text, list |
|
111 | 111 | # values. |
|
112 | 112 | text, matches = ip.complete(t) |
|
113 | 113 | nt.assert_true(isinstance(text, basestring)) |
|
114 | 114 | nt.assert_true(isinstance(matches, list)) |
|
115 | 115 | |
|
116 | 116 | |
|
117 | 117 | class CompletionSplitterTestCase(unittest.TestCase): |
|
118 | 118 | def setUp(self): |
|
119 | 119 | self.sp = completer.CompletionSplitter() |
|
120 | 120 | |
|
121 | 121 | def test_delim_setting(self): |
|
122 | 122 | self.sp.delims = ' ' |
|
123 | 123 | nt.assert_equal(self.sp.delims, ' ') |
|
124 | 124 | nt.assert_equal(self.sp._delim_expr, '[\ ]') |
|
125 | 125 | |
|
126 | 126 | def test_spaces(self): |
|
127 | 127 | """Test with only spaces as split chars.""" |
|
128 | 128 | self.sp.delims = ' ' |
|
129 | 129 | t = [('foo', '', 'foo'), |
|
130 | 130 | ('run foo', '', 'foo'), |
|
131 | 131 | ('run foo', 'bar', 'foo'), |
|
132 | 132 | ] |
|
133 | 133 | check_line_split(self.sp, t) |
|
134 | 134 | |
|
135 | 135 | |
|
136 | 136 | def test_has_open_quotes1(): |
|
137 | 137 | for s in ["'", "'''", "'hi' '"]: |
|
138 | 138 | nt.assert_equal(completer.has_open_quotes(s), "'") |
|
139 | 139 | |
|
140 | 140 | |
|
141 | 141 | def test_has_open_quotes2(): |
|
142 | 142 | for s in ['"', '"""', '"hi" "']: |
|
143 | 143 | nt.assert_equal(completer.has_open_quotes(s), '"') |
|
144 | 144 | |
|
145 | 145 | |
|
146 | 146 | def test_has_open_quotes3(): |
|
147 | 147 | for s in ["''", "''' '''", "'hi' 'ipython'"]: |
|
148 | 148 | nt.assert_false(completer.has_open_quotes(s)) |
|
149 | 149 | |
|
150 | 150 | |
|
151 | 151 | def test_has_open_quotes4(): |
|
152 | 152 | for s in ['""', '""" """', '"hi" "ipython"']: |
|
153 | 153 | nt.assert_false(completer.has_open_quotes(s)) |
|
154 | 154 | |
|
155 | 155 | |
|
156 | 156 | @knownfailureif(sys.platform == 'win32', "abspath completions fail on Windows") |
|
157 | 157 | def test_abspath_file_completions(): |
|
158 | 158 | ip = get_ipython() |
|
159 | 159 | with TemporaryDirectory() as tmpdir: |
|
160 | 160 | prefix = os.path.join(tmpdir, 'foo') |
|
161 | 161 | suffixes = map(str, [1,2]) |
|
162 | 162 | names = [prefix+s for s in suffixes] |
|
163 | 163 | for n in names: |
|
164 | 164 | open(n, 'w').close() |
|
165 | 165 | |
|
166 | 166 | # Check simple completion |
|
167 | 167 | c = ip.complete(prefix)[1] |
|
168 | 168 | nt.assert_equal(c, names) |
|
169 | 169 | |
|
170 | 170 | # Now check with a function call |
|
171 | 171 | cmd = 'a = f("%s' % prefix |
|
172 | 172 | c = ip.complete(prefix, cmd)[1] |
|
173 | 173 | comp = [prefix+s for s in suffixes] |
|
174 | 174 | nt.assert_equal(c, comp) |
|
175 | 175 | |
|
176 | 176 | |
|
177 | 177 | def test_local_file_completions(): |
|
178 | 178 | ip = get_ipython() |
|
179 | 179 | cwd = os.getcwdu() |
|
180 | 180 | try: |
|
181 | 181 | with TemporaryDirectory() as tmpdir: |
|
182 | 182 | os.chdir(tmpdir) |
|
183 | 183 | prefix = './foo' |
|
184 | 184 | suffixes = map(str, [1,2]) |
|
185 | 185 | names = [prefix+s for s in suffixes] |
|
186 | 186 | for n in names: |
|
187 | 187 | open(n, 'w').close() |
|
188 | 188 | |
|
189 | 189 | # Check simple completion |
|
190 | 190 | c = ip.complete(prefix)[1] |
|
191 | 191 | nt.assert_equal(c, names) |
|
192 | 192 | |
|
193 | 193 | # Now check with a function call |
|
194 | 194 | cmd = 'a = f("%s' % prefix |
|
195 | 195 | c = ip.complete(prefix, cmd)[1] |
|
196 | 196 | comp = [prefix+s for s in suffixes] |
|
197 | 197 | nt.assert_equal(c, comp) |
|
198 | 198 | finally: |
|
199 | 199 | # prevent failures from making chdir stick |
|
200 | 200 | os.chdir(cwd) |
|
201 | 201 | |
|
202 | 202 | |
|
203 | 203 | def test_greedy_completions(): |
|
204 | 204 | ip = get_ipython() |
|
205 | 205 | greedy_original = ip.Completer.greedy |
|
206 | 206 | try: |
|
207 | 207 | ip.Completer.greedy = False |
|
208 | 208 | ip.ex('a=range(5)') |
|
209 | 209 | _,c = ip.complete('.',line='a[0].') |
|
210 | 210 | nt.assert_false('a[0].real' in c, |
|
211 | 211 | "Shouldn't have completed on a[0]: %s"%c) |
|
212 | 212 | ip.Completer.greedy = True |
|
213 | 213 | _,c = ip.complete('.',line='a[0].') |
|
214 | 214 | nt.assert_true('a[0].real' in c, "Should have completed on a[0]: %s"%c) |
|
215 | 215 | finally: |
|
216 | 216 | ip.Completer.greedy = greedy_original |
|
217 | 217 | |
|
218 | 218 | |
|
219 | 219 | def test_omit__names(): |
|
220 | 220 | # also happens to test IPCompleter as a configurable |
|
221 | 221 | ip = get_ipython() |
|
222 | 222 | ip._hidden_attr = 1 |
|
223 | 223 | c = ip.Completer |
|
224 | 224 | ip.ex('ip=get_ipython()') |
|
225 | 225 | cfg = Config() |
|
226 | 226 | cfg.IPCompleter.omit__names = 0 |
|
227 | 227 | c.update_config(cfg) |
|
228 | 228 | s,matches = c.complete('ip.') |
|
229 | 229 | nt.assert_true('ip.__str__' in matches) |
|
230 | 230 | nt.assert_true('ip._hidden_attr' in matches) |
|
231 | 231 | cfg.IPCompleter.omit__names = 1 |
|
232 | 232 | c.update_config(cfg) |
|
233 | 233 | s,matches = c.complete('ip.') |
|
234 | 234 | nt.assert_false('ip.__str__' in matches) |
|
235 | 235 | nt.assert_true('ip._hidden_attr' in matches) |
|
236 | 236 | cfg.IPCompleter.omit__names = 2 |
|
237 | 237 | c.update_config(cfg) |
|
238 | 238 | s,matches = c.complete('ip.') |
|
239 | 239 | nt.assert_false('ip.__str__' in matches) |
|
240 | 240 | nt.assert_false('ip._hidden_attr' in matches) |
|
241 | 241 | del ip._hidden_attr |
|
242 | 242 | |
|
243 | 243 | |
|
244 | 244 | def test_limit_to__all__False_ok(): |
|
245 | 245 | ip = get_ipython() |
|
246 | 246 | c = ip.Completer |
|
247 | 247 | ip.ex('class D: x=24') |
|
248 | 248 | ip.ex('d=D()') |
|
249 | 249 | cfg = Config() |
|
250 | 250 | cfg.IPCompleter.limit_to__all__ = False |
|
251 | 251 | c.update_config(cfg) |
|
252 | 252 | s, matches = c.complete('d.') |
|
253 | 253 | nt.assert_true('d.x' in matches) |
|
254 | 254 | |
|
255 | 255 | |
|
256 | 256 | def test_limit_to__all__True_ok(): |
|
257 | 257 | ip = get_ipython() |
|
258 | 258 | c = ip.Completer |
|
259 | 259 | ip.ex('class D: x=24') |
|
260 | 260 | ip.ex('d=D()') |
|
261 | 261 | ip.ex("d.__all__=['z']") |
|
262 | 262 | cfg = Config() |
|
263 | 263 | cfg.IPCompleter.limit_to__all__ = True |
|
264 | 264 | c.update_config(cfg) |
|
265 | 265 | s, matches = c.complete('d.') |
|
266 | 266 | nt.assert_true('d.z' in matches) |
|
267 | 267 | nt.assert_false('d.x' in matches) |
|
268 | 268 | |
|
269 | 269 | |
|
270 | 270 | def test_get__all__entries_ok(): |
|
271 | 271 | class A(object): |
|
272 | 272 | __all__ = ['x', 1] |
|
273 | 273 | words = completer.get__all__entries(A()) |
|
274 | 274 | nt.assert_equal(words, ['x']) |
|
275 | 275 | |
|
276 | 276 | |
|
277 | 277 | def test_get__all__entries_no__all__ok(): |
|
278 | 278 | class A(object): |
|
279 | 279 | pass |
|
280 | 280 | words = completer.get__all__entries(A()) |
|
281 | 281 | nt.assert_equal(words, []) |
|
282 | 282 | |
|
283 | 283 | |
|
284 | 284 | def test_func_kw_completions(): |
|
285 | 285 | ip = get_ipython() |
|
286 | 286 | c = ip.Completer |
|
287 | 287 | ip.ex('def myfunc(a=1,b=2): return a+b') |
|
288 | 288 | s, matches = c.complete(None, 'myfunc(1,b') |
|
289 | 289 | nt.assert_in('b=', matches) |
|
290 | 290 | # Simulate completing with cursor right after b (pos==10): |
|
291 | 291 | s, matches = c.complete(None,'myfunc(1,b)', 10) |
|
292 | 292 | nt.assert_in('b=', matches) |
|
293 | 293 | s, matches = c.complete(None,'myfunc(a="escaped\\")string",b') |
|
294 | 294 | nt.assert_in('b=', matches) |
|
295 | 295 | |
|
296 | 296 | |
|
297 | 297 | def test_line_magics(): |
|
298 | 298 | ip = get_ipython() |
|
299 | 299 | c = ip.Completer |
|
300 | 300 | s, matches = c.complete(None, 'lsmag') |
|
301 | 301 | nt.assert_in('%lsmagic', matches) |
|
302 | 302 | s, matches = c.complete(None, '%lsmag') |
|
303 | 303 | nt.assert_in('%lsmagic', matches) |
|
304 | 304 | |
|
305 | 305 | |
|
306 | 306 | def test_cell_magics(): |
|
307 | 307 | from IPython.core.magic import register_cell_magic |
|
308 | 308 | |
|
309 | 309 | @register_cell_magic |
|
310 | 310 | def _foo_cellm(line, cell): |
|
311 | 311 | pass |
|
312 | 312 | |
|
313 | 313 | ip = get_ipython() |
|
314 | 314 | c = ip.Completer |
|
315 | 315 | |
|
316 | 316 | s, matches = c.complete(None, '_foo_ce') |
|
317 | 317 | nt.assert_in('%%_foo_cellm', matches) |
|
318 | 318 | s, matches = c.complete(None, '%%_foo_ce') |
|
319 | 319 | nt.assert_in('%%_foo_cellm', matches) |
|
320 | 320 | |
|
321 | 321 | |
|
322 | 322 | def test_line_cell_magics(): |
|
323 | 323 | from IPython.core.magic import register_line_cell_magic |
|
324 | 324 | |
|
325 | 325 | @register_line_cell_magic |
|
326 | 326 | def _bar_cellm(line, cell): |
|
327 | 327 | pass |
|
328 | 328 | |
|
329 | 329 | ip = get_ipython() |
|
330 | 330 | c = ip.Completer |
|
331 | 331 | |
|
332 | 332 | # The policy here is trickier, see comments in completion code. The |
|
333 | 333 | # returned values depend on whether the user passes %% or not explicitly, |
|
334 | 334 | # and this will show a difference if the same name is both a line and cell |
|
335 | 335 | # magic. |
|
336 | 336 | s, matches = c.complete(None, '_bar_ce') |
|
337 | 337 | nt.assert_in('%_bar_cellm', matches) |
|
338 | 338 | nt.assert_in('%%_bar_cellm', matches) |
|
339 | 339 | s, matches = c.complete(None, '%_bar_ce') |
|
340 | 340 | nt.assert_in('%_bar_cellm', matches) |
|
341 | 341 | nt.assert_in('%%_bar_cellm', matches) |
|
342 | 342 | s, matches = c.complete(None, '%%_bar_ce') |
|
343 | 343 | nt.assert_not_in('%_bar_cellm', matches) |
|
344 | 344 | nt.assert_in('%%_bar_cellm', matches) |
|
345 | 345 |
@@ -1,35 +1,35 | |||
|
1 | 1 | """Tests for debugging machinery. |
|
2 | 2 | """ |
|
3 | 3 | #----------------------------------------------------------------------------- |
|
4 | 4 | # Copyright (c) 2012, The IPython Development Team. |
|
5 | 5 | # |
|
6 | 6 | # Distributed under the terms of the Modified BSD License. |
|
7 | 7 | # |
|
8 | 8 | # The full license is in the file COPYING.txt, distributed with this software. |
|
9 | 9 | #----------------------------------------------------------------------------- |
|
10 | 10 | |
|
11 | 11 | #----------------------------------------------------------------------------- |
|
12 | 12 | # Imports |
|
13 | 13 | #----------------------------------------------------------------------------- |
|
14 | 14 | |
|
15 | 15 | # third-party |
|
16 | 16 | import nose.tools as nt |
|
17 | 17 | |
|
18 | 18 | # Our own |
|
19 | 19 | from IPython.core import debugger |
|
20 | 20 | |
|
21 | 21 | #----------------------------------------------------------------------------- |
|
22 | 22 | # Tests |
|
23 | 23 | #----------------------------------------------------------------------------- |
|
24 | 24 | |
|
25 | 25 | def test_longer_repr(): |
|
26 | 26 | from repr import repr as trepr |
|
27 | 27 | |
|
28 | 28 | a = '1234567890'* 7 |
|
29 | 29 | ar = "'1234567890123456789012345678901234567890123456789012345678901234567890'" |
|
30 | 30 | a_trunc = "'123456789012...8901234567890'" |
|
31 |
nt.assert_equal |
|
|
31 | nt.assert_equal(trepr(a), a_trunc) | |
|
32 | 32 | # The creation of our tracer modifies the repr module's repr function |
|
33 | 33 | # in-place, since that global is used directly by the stdlib's pdb module. |
|
34 | 34 | t = debugger.Tracer() |
|
35 |
nt.assert_equal |
|
|
35 | nt.assert_equal(trepr(a), ar) |
@@ -1,91 +1,91 | |||
|
1 | 1 | """Tests for the Formatters. |
|
2 | 2 | """ |
|
3 | 3 | |
|
4 | 4 | from math import pi |
|
5 | 5 | |
|
6 | 6 | try: |
|
7 | 7 | import numpy |
|
8 | 8 | except: |
|
9 | 9 | numpy = None |
|
10 | 10 | import nose.tools as nt |
|
11 | 11 | |
|
12 | 12 | from IPython.core.formatters import FormatterABC, PlainTextFormatter |
|
13 | 13 | from IPython.lib import pretty |
|
14 | 14 | |
|
15 | 15 | class A(object): |
|
16 | 16 | def __repr__(self): |
|
17 | 17 | return 'A()' |
|
18 | 18 | |
|
19 | 19 | class B(A): |
|
20 | 20 | def __repr__(self): |
|
21 | 21 | return 'B()' |
|
22 | 22 | |
|
23 | 23 | class BadPretty(object): |
|
24 | 24 | _repr_pretty_ = None |
|
25 | 25 | |
|
26 | 26 | class GoodPretty(object): |
|
27 | 27 | def _repr_pretty_(self, pp, cycle): |
|
28 | 28 | pp.text('foo') |
|
29 | 29 | |
|
30 | 30 | def __repr__(self): |
|
31 | 31 | return 'GoodPretty()' |
|
32 | 32 | |
|
33 | 33 | def foo_printer(obj, pp, cycle): |
|
34 | 34 | pp.text('foo') |
|
35 | 35 | |
|
36 | 36 | def test_pretty(): |
|
37 | 37 | f = PlainTextFormatter() |
|
38 | 38 | f.for_type(A, foo_printer) |
|
39 |
nt.assert_equal |
|
|
40 |
nt.assert_equal |
|
|
41 |
nt.assert_equal |
|
|
39 | nt.assert_equal(f(A()), 'foo') | |
|
40 | nt.assert_equal(f(B()), 'foo') | |
|
41 | nt.assert_equal(f(GoodPretty()), 'foo') | |
|
42 | 42 | # Just don't raise an exception for the following: |
|
43 | 43 | f(BadPretty()) |
|
44 | 44 | |
|
45 | 45 | f.pprint = False |
|
46 |
nt.assert_equal |
|
|
47 |
nt.assert_equal |
|
|
48 |
nt.assert_equal |
|
|
46 | nt.assert_equal(f(A()), 'A()') | |
|
47 | nt.assert_equal(f(B()), 'B()') | |
|
48 | nt.assert_equal(f(GoodPretty()), 'GoodPretty()') | |
|
49 | 49 | |
|
50 | 50 | |
|
51 | 51 | def test_deferred(): |
|
52 | 52 | f = PlainTextFormatter() |
|
53 | 53 | |
|
54 | 54 | def test_precision(): |
|
55 | 55 | """test various values for float_precision.""" |
|
56 | 56 | f = PlainTextFormatter() |
|
57 |
nt.assert_equal |
|
|
57 | nt.assert_equal(f(pi), repr(pi)) | |
|
58 | 58 | f.float_precision = 0 |
|
59 | 59 | if numpy: |
|
60 | 60 | po = numpy.get_printoptions() |
|
61 |
nt.assert_equal |
|
|
62 |
nt.assert_equal |
|
|
61 | nt.assert_equal(po['precision'], 0) | |
|
62 | nt.assert_equal(f(pi), '3') | |
|
63 | 63 | f.float_precision = 2 |
|
64 | 64 | if numpy: |
|
65 | 65 | po = numpy.get_printoptions() |
|
66 |
nt.assert_equal |
|
|
67 |
nt.assert_equal |
|
|
66 | nt.assert_equal(po['precision'], 2) | |
|
67 | nt.assert_equal(f(pi), '3.14') | |
|
68 | 68 | f.float_precision = '%g' |
|
69 | 69 | if numpy: |
|
70 | 70 | po = numpy.get_printoptions() |
|
71 |
nt.assert_equal |
|
|
72 |
nt.assert_equal |
|
|
71 | nt.assert_equal(po['precision'], 2) | |
|
72 | nt.assert_equal(f(pi), '3.14159') | |
|
73 | 73 | f.float_precision = '%e' |
|
74 |
nt.assert_equal |
|
|
74 | nt.assert_equal(f(pi), '3.141593e+00') | |
|
75 | 75 | f.float_precision = '' |
|
76 | 76 | if numpy: |
|
77 | 77 | po = numpy.get_printoptions() |
|
78 |
nt.assert_equal |
|
|
79 |
nt.assert_equal |
|
|
78 | nt.assert_equal(po['precision'], 8) | |
|
79 | nt.assert_equal(f(pi), repr(pi)) | |
|
80 | 80 | |
|
81 | 81 | def test_bad_precision(): |
|
82 | 82 | """test various invalid values for float_precision.""" |
|
83 | 83 | f = PlainTextFormatter() |
|
84 | 84 | def set_fp(p): |
|
85 | 85 | f.float_precision=p |
|
86 | 86 | nt.assert_raises(ValueError, set_fp, '%') |
|
87 | 87 | nt.assert_raises(ValueError, set_fp, '%.3f%i') |
|
88 | 88 | nt.assert_raises(ValueError, set_fp, 'foo') |
|
89 | 89 | nt.assert_raises(ValueError, set_fp, -1) |
|
90 | 90 | |
|
91 | 91 |
@@ -1,169 +1,169 | |||
|
1 | 1 | """Tests for input handlers. |
|
2 | 2 | """ |
|
3 | 3 | #----------------------------------------------------------------------------- |
|
4 | 4 | # Module imports |
|
5 | 5 | #----------------------------------------------------------------------------- |
|
6 | 6 | |
|
7 | 7 | # third party |
|
8 | 8 | import nose.tools as nt |
|
9 | 9 | |
|
10 | 10 | # our own packages |
|
11 | 11 | from IPython.core import autocall |
|
12 | 12 | from IPython.testing import tools as tt |
|
13 | 13 | from IPython.testing.globalipapp import get_ipython |
|
14 | 14 | from IPython.utils import py3compat |
|
15 | 15 | |
|
16 | 16 | #----------------------------------------------------------------------------- |
|
17 | 17 | # Globals |
|
18 | 18 | #----------------------------------------------------------------------------- |
|
19 | 19 | |
|
20 | 20 | # Get the public instance of IPython |
|
21 | 21 | ip = get_ipython() |
|
22 | 22 | |
|
23 | 23 | failures = [] |
|
24 | 24 | num_tests = 0 |
|
25 | 25 | |
|
26 | 26 | #----------------------------------------------------------------------------- |
|
27 | 27 | # Test functions |
|
28 | 28 | #----------------------------------------------------------------------------- |
|
29 | 29 | |
|
30 | 30 | class CallableIndexable(object): |
|
31 | 31 | def __getitem__(self, idx): return True |
|
32 | 32 | def __call__(self, *args, **kws): return True |
|
33 | 33 | |
|
34 | 34 | |
|
35 | 35 | class Autocallable(autocall.IPyAutocall): |
|
36 | 36 | def __call__(self): |
|
37 | 37 | return "called" |
|
38 | 38 | |
|
39 | 39 | |
|
40 | 40 | def run(tests): |
|
41 | 41 | """Loop through a list of (pre, post) inputs, where pre is the string |
|
42 | 42 | handed to ipython, and post is how that string looks after it's been |
|
43 | 43 | transformed (i.e. ipython's notion of _i)""" |
|
44 | 44 | tt.check_pairs(ip.prefilter_manager.prefilter_lines, tests) |
|
45 | 45 | |
|
46 | 46 | |
|
47 | 47 | def test_handlers(): |
|
48 | 48 | # alias expansion |
|
49 | 49 | |
|
50 | 50 | # We're using 'true' as our syscall of choice because it doesn't |
|
51 | 51 | # write anything to stdout. |
|
52 | 52 | |
|
53 | 53 | # Turn off actual execution of aliases, because it's noisy |
|
54 | 54 | old_system_cmd = ip.system |
|
55 | 55 | ip.system = lambda cmd: None |
|
56 | 56 | |
|
57 | 57 | |
|
58 | 58 | ip.alias_manager.alias_table['an_alias'] = (0, 'true') |
|
59 | 59 | # These are useful for checking a particular recursive alias issue |
|
60 | 60 | ip.alias_manager.alias_table['top'] = (0, 'd:/cygwin/top') |
|
61 | 61 | ip.alias_manager.alias_table['d'] = (0, 'true') |
|
62 | 62 | run([(i,py3compat.u_format(o)) for i,o in \ |
|
63 | 63 | [("an_alias", "get_ipython().system({u}'true ')"), # alias |
|
64 | 64 | # Below: recursive aliases should expand whitespace-surrounded |
|
65 | 65 | # chars, *not* initial chars which happen to be aliases: |
|
66 | 66 | ("top", "get_ipython().system({u}'d:/cygwin/top ')"), |
|
67 | 67 | ]]) |
|
68 | 68 | ip.system = old_system_cmd |
|
69 | 69 | |
|
70 | 70 | call_idx = CallableIndexable() |
|
71 | 71 | ip.user_ns['call_idx'] = call_idx |
|
72 | 72 | |
|
73 | 73 | # For many of the below, we're also checking that leading whitespace |
|
74 | 74 | # turns off the esc char, which it should unless there is a continuation |
|
75 | 75 | # line. |
|
76 | 76 | run([(i,py3compat.u_format(o)) for i,o in \ |
|
77 | 77 | [('"no change"', '"no change"'), # normal |
|
78 | 78 | (u"!true", "get_ipython().system({u}'true')"), # shell_escapes |
|
79 | 79 | (u"!! true", "get_ipython().magic({u}'sx true')"), # shell_escapes + magic |
|
80 | 80 | (u"!!true", "get_ipython().magic({u}'sx true')"), # shell_escapes + magic |
|
81 | 81 | (u"%lsmagic", "get_ipython().magic({u}'lsmagic ')"), # magic |
|
82 | 82 | (u"lsmagic", "get_ipython().magic({u}'lsmagic ')"), # magic |
|
83 | 83 | #("a = b # PYTHON-MODE", '_i'), # emacs -- avoids _in cache |
|
84 | 84 | |
|
85 | 85 | # post-esc-char whitespace goes inside |
|
86 | 86 | (u"! true", "get_ipython().system({u}' true')"), |
|
87 | 87 | |
|
88 | 88 | # handle_help |
|
89 | 89 | |
|
90 | 90 | # These are weak tests -- just looking at what the help handlers |
|
91 | 91 | # logs, which is not how it really does its work. But it still |
|
92 | 92 | # lets us check the key paths through the handler. |
|
93 | 93 | |
|
94 | 94 | ("x=1 # what?", "x=1 # what?"), # no help if valid python |
|
95 | 95 | ]]) |
|
96 | 96 | |
|
97 | 97 | # multi_line_specials |
|
98 | 98 | ip.prefilter_manager.multi_line_specials = False |
|
99 | 99 | # W/ multi_line_specials off, leading ws kills esc chars/autoexpansion |
|
100 | 100 | run([ |
|
101 | 101 | (u'if 1:\n !true', u'if 1:\n !true'), |
|
102 | 102 | (u'if 1:\n lsmagic', u'if 1:\n lsmagic'), |
|
103 | 103 | (u'if 1:\n an_alias', u'if 1:\n an_alias'), |
|
104 | 104 | ]) |
|
105 | 105 | |
|
106 | 106 | ip.prefilter_manager.multi_line_specials = True |
|
107 | 107 | # initial indents must be preserved. |
|
108 | 108 | run([(i,py3compat.u_format(o)) for i,o in \ |
|
109 | 109 | [(u'if 1:\n !true', "if 1:\n get_ipython().system({u}'true')"), |
|
110 | 110 | (u'if 2:\n lsmagic', "if 2:\n get_ipython().magic({u}'lsmagic ')"), |
|
111 | 111 | (u'if 1:\n an_alias', "if 1:\n get_ipython().system({u}'true ')"), |
|
112 | 112 | # Weird one |
|
113 | 113 | (u'if 1:\n !!true', "if 1:\n get_ipython().magic({u}'sx true')"), |
|
114 | 114 | |
|
115 | 115 | # Even with m_l_s on, autocall is off even with special chars |
|
116 | 116 | ('if 1:\n /fun 1 2', 'if 1:\n /fun 1 2'), |
|
117 | 117 | ('if 1:\n ;fun 1 2', 'if 1:\n ;fun 1 2'), |
|
118 | 118 | ('if 1:\n ,fun 1 2', 'if 1:\n ,fun 1 2'), |
|
119 | 119 | ('if 1:\n ?fun 1 2', 'if 1:\n ?fun 1 2'), |
|
120 | 120 | # What about !! |
|
121 | 121 | ]]) |
|
122 | 122 | |
|
123 | 123 | # Objects which are instances of IPyAutocall are *always* autocalled |
|
124 | 124 | autocallable = Autocallable() |
|
125 | 125 | ip.user_ns['autocallable'] = autocallable |
|
126 | 126 | |
|
127 | 127 | # auto |
|
128 | 128 | ip.magic('autocall 0') |
|
129 | 129 | # Only explicit escapes or instances of IPyAutocallable should get |
|
130 | 130 | # expanded |
|
131 | 131 | run([ |
|
132 | 132 | ('len "abc"', 'len "abc"'), |
|
133 | 133 | ('autocallable', 'autocallable()'), |
|
134 | 134 | # Don't add extra brackets (gh-1117) |
|
135 | 135 | ('autocallable()', 'autocallable()'), |
|
136 | 136 | (",list 1 2 3", 'list("1", "2", "3")'), |
|
137 | 137 | (";list 1 2 3", 'list("1 2 3")'), |
|
138 | 138 | ("/len range(1,4)", 'len(range(1,4))'), |
|
139 | 139 | ]) |
|
140 | 140 | ip.magic('autocall 1') |
|
141 | 141 | run([ |
|
142 | 142 | (",list 1 2 3", 'list("1", "2", "3")'), |
|
143 | 143 | (";list 1 2 3", 'list("1 2 3")'), |
|
144 | 144 | ("/len range(1,4)", 'len(range(1,4))'), |
|
145 | 145 | ('len "abc"', 'len("abc")'), |
|
146 | 146 | ('len "abc";', 'len("abc");'), # ; is special -- moves out of parens |
|
147 | 147 | # Autocall is turned off if first arg is [] and the object |
|
148 | 148 | # is both callable and indexable. Like so: |
|
149 | 149 | ('len [1,2]', 'len([1,2])'), # len doesn't support __getitem__... |
|
150 | 150 | ('call_idx [1]', 'call_idx [1]'), # call_idx *does*.. |
|
151 | 151 | ('call_idx 1', 'call_idx(1)'), |
|
152 | 152 | ('len', 'len'), # only at 2 does it auto-call on single args |
|
153 | 153 | ]) |
|
154 | 154 | ip.magic('autocall 2') |
|
155 | 155 | run([ |
|
156 | 156 | (",list 1 2 3", 'list("1", "2", "3")'), |
|
157 | 157 | (";list 1 2 3", 'list("1 2 3")'), |
|
158 | 158 | ("/len range(1,4)", 'len(range(1,4))'), |
|
159 | 159 | ('len "abc"', 'len("abc")'), |
|
160 | 160 | ('len "abc";', 'len("abc");'), |
|
161 | 161 | ('len [1,2]', 'len([1,2])'), |
|
162 | 162 | ('call_idx [1]', 'call_idx [1]'), |
|
163 | 163 | ('call_idx 1', 'call_idx(1)'), |
|
164 | 164 | # This is what's different: |
|
165 | 165 | ('len', 'len()'), # only at 2 does it auto-call on single args |
|
166 | 166 | ]) |
|
167 | 167 | ip.magic('autocall 1') |
|
168 | 168 | |
|
169 |
nt.assert_equal |
|
|
169 | nt.assert_equal(failures, []) |
@@ -1,151 +1,151 | |||
|
1 | 1 | # coding: utf-8 |
|
2 | 2 | """Tests for the IPython tab-completion machinery. |
|
3 | 3 | """ |
|
4 | 4 | #----------------------------------------------------------------------------- |
|
5 | 5 | # Module imports |
|
6 | 6 | #----------------------------------------------------------------------------- |
|
7 | 7 | |
|
8 | 8 | # stdlib |
|
9 | 9 | import os |
|
10 | 10 | import shutil |
|
11 | 11 | import sys |
|
12 | 12 | import tempfile |
|
13 | 13 | import unittest |
|
14 | 14 | from datetime import datetime |
|
15 | 15 | |
|
16 | 16 | # third party |
|
17 | 17 | import nose.tools as nt |
|
18 | 18 | |
|
19 | 19 | # our own packages |
|
20 | 20 | from IPython.config.loader import Config |
|
21 | 21 | from IPython.utils.tempdir import TemporaryDirectory |
|
22 | 22 | from IPython.core.history import HistoryManager, extract_hist_ranges |
|
23 | 23 | from IPython.utils import py3compat |
|
24 | 24 | |
|
25 | 25 | def setUp(): |
|
26 | 26 | nt.assert_equal(sys.getdefaultencoding(), "utf-8" if py3compat.PY3 else "ascii") |
|
27 | 27 | |
|
28 | 28 | def test_history(): |
|
29 | 29 | ip = get_ipython() |
|
30 | 30 | with TemporaryDirectory() as tmpdir: |
|
31 | 31 | hist_manager_ori = ip.history_manager |
|
32 | 32 | hist_file = os.path.join(tmpdir, 'history.sqlite') |
|
33 | 33 | try: |
|
34 | 34 | ip.history_manager = HistoryManager(shell=ip, hist_file=hist_file) |
|
35 | 35 | hist = [u'a=1', u'def f():\n test = 1\n return test', u"b='β¬ΓΒΎΓ·Γ'"] |
|
36 | 36 | for i, h in enumerate(hist, start=1): |
|
37 | 37 | ip.history_manager.store_inputs(i, h) |
|
38 | 38 | |
|
39 | 39 | ip.history_manager.db_log_output = True |
|
40 | 40 | # Doesn't match the input, but we'll just check it's stored. |
|
41 | 41 | ip.history_manager.output_hist_reprs[3] = "spam" |
|
42 | 42 | ip.history_manager.store_output(3) |
|
43 | 43 | |
|
44 | 44 | nt.assert_equal(ip.history_manager.input_hist_raw, [''] + hist) |
|
45 | 45 | |
|
46 | 46 | # Detailed tests for _get_range_session |
|
47 | 47 | grs = ip.history_manager._get_range_session |
|
48 | 48 | nt.assert_equal(list(grs(start=2,stop=-1)), zip([0], [2], hist[1:-1])) |
|
49 | 49 | nt.assert_equal(list(grs(start=-2)), zip([0,0], [2,3], hist[-2:])) |
|
50 | 50 | nt.assert_equal(list(grs(output=True)), zip([0,0,0], [1,2,3], zip(hist, [None,None,'spam']))) |
|
51 | 51 | |
|
52 | 52 | # Check whether specifying a range beyond the end of the current |
|
53 | 53 | # session results in an error (gh-804) |
|
54 | 54 | ip.magic('%hist 2-500') |
|
55 | 55 | |
|
56 | 56 | # Check that we can write non-ascii characters to a file |
|
57 | 57 | ip.magic("%%hist -f %s" % os.path.join(tmpdir, "test1")) |
|
58 | 58 | ip.magic("%%hist -pf %s" % os.path.join(tmpdir, "test2")) |
|
59 | 59 | ip.magic("%%hist -nf %s" % os.path.join(tmpdir, "test3")) |
|
60 | 60 | ip.magic("%%save %s 1-10" % os.path.join(tmpdir, "test4")) |
|
61 | 61 | |
|
62 | 62 | # New session |
|
63 | 63 | ip.history_manager.reset() |
|
64 | 64 | newcmds = ["z=5","class X(object):\n pass", "k='p'"] |
|
65 | 65 | for i, cmd in enumerate(newcmds, start=1): |
|
66 | 66 | ip.history_manager.store_inputs(i, cmd) |
|
67 | 67 | gothist = ip.history_manager.get_range(start=1, stop=4) |
|
68 | 68 | nt.assert_equal(list(gothist), zip([0,0,0],[1,2,3], newcmds)) |
|
69 | 69 | # Previous session: |
|
70 | 70 | gothist = ip.history_manager.get_range(-1, 1, 4) |
|
71 | 71 | nt.assert_equal(list(gothist), zip([1,1,1],[1,2,3], hist)) |
|
72 | 72 | |
|
73 | 73 | # Check get_hist_tail |
|
74 | 74 | gothist = ip.history_manager.get_tail(4, output=True, |
|
75 | 75 | include_latest=True) |
|
76 | 76 | expected = [(1, 3, (hist[-1], "spam")), |
|
77 | 77 | (2, 1, (newcmds[0], None)), |
|
78 | 78 | (2, 2, (newcmds[1], None)), |
|
79 | 79 | (2, 3, (newcmds[2], None)),] |
|
80 | 80 | nt.assert_equal(list(gothist), expected) |
|
81 | 81 | |
|
82 | 82 | gothist = ip.history_manager.get_tail(2) |
|
83 | 83 | expected = [(2, 1, newcmds[0]), |
|
84 | 84 | (2, 2, newcmds[1])] |
|
85 | 85 | nt.assert_equal(list(gothist), expected) |
|
86 | 86 | |
|
87 | 87 | # Check get_hist_search |
|
88 | 88 | gothist = ip.history_manager.search("*test*") |
|
89 | 89 | nt.assert_equal(list(gothist), [(1,2,hist[1])] ) |
|
90 | 90 | gothist = ip.history_manager.search("b*", output=True) |
|
91 | 91 | nt.assert_equal(list(gothist), [(1,3,(hist[2],"spam"))] ) |
|
92 | 92 | |
|
93 | 93 | # Cross testing: check that magic %save can get previous session. |
|
94 | 94 | testfilename = os.path.realpath(os.path.join(tmpdir, "test.py")) |
|
95 | 95 | ip.magic("save " + testfilename + " ~1/1-3") |
|
96 | 96 | with py3compat.open(testfilename, encoding='utf-8') as testfile: |
|
97 | 97 | nt.assert_equal(testfile.read(), |
|
98 | 98 | u"# coding: utf-8\n" + u"\n".join(hist)+u"\n") |
|
99 | 99 | |
|
100 | 100 | # Duplicate line numbers - check that it doesn't crash, and |
|
101 | 101 | # gets a new session |
|
102 | 102 | ip.history_manager.store_inputs(1, "rogue") |
|
103 | 103 | ip.history_manager.writeout_cache() |
|
104 | 104 | nt.assert_equal(ip.history_manager.session_number, 3) |
|
105 | 105 | finally: |
|
106 | 106 | # Restore history manager |
|
107 | 107 | ip.history_manager = hist_manager_ori |
|
108 | 108 | |
|
109 | 109 | |
|
110 | 110 | def test_extract_hist_ranges(): |
|
111 | 111 | instr = "1 2/3 ~4/5-6 ~4/7-~4/9 ~9/2-~7/5" |
|
112 | 112 | expected = [(0, 1, 2), # 0 == current session |
|
113 | 113 | (2, 3, 4), |
|
114 | 114 | (-4, 5, 7), |
|
115 | 115 | (-4, 7, 10), |
|
116 | 116 | (-9, 2, None), # None == to end |
|
117 | 117 | (-8, 1, None), |
|
118 | 118 | (-7, 1, 6)] |
|
119 | 119 | actual = list(extract_hist_ranges(instr)) |
|
120 | 120 | nt.assert_equal(actual, expected) |
|
121 | 121 | |
|
122 | 122 | def test_magic_rerun(): |
|
123 | 123 | """Simple test for %rerun (no args -> rerun last line)""" |
|
124 | 124 | ip = get_ipython() |
|
125 | 125 | ip.run_cell("a = 10", store_history=True) |
|
126 | 126 | ip.run_cell("a += 1", store_history=True) |
|
127 | 127 | nt.assert_equal(ip.user_ns["a"], 11) |
|
128 | 128 | ip.run_cell("%rerun", store_history=True) |
|
129 | 129 | nt.assert_equal(ip.user_ns["a"], 12) |
|
130 | 130 | |
|
131 | 131 | def test_timestamp_type(): |
|
132 | 132 | ip = get_ipython() |
|
133 | 133 | info = ip.history_manager.get_session_info() |
|
134 | 134 | nt.assert_true(isinstance(info[1], datetime)) |
|
135 | 135 | |
|
136 | 136 | def test_hist_file_config(): |
|
137 | 137 | cfg = Config() |
|
138 | 138 | tfile = tempfile.NamedTemporaryFile(delete=False) |
|
139 | 139 | cfg.HistoryManager.hist_file = tfile.name |
|
140 | 140 | try: |
|
141 | 141 | hm = HistoryManager(shell=get_ipython(), config=cfg) |
|
142 |
nt.assert_equal |
|
|
142 | nt.assert_equal(hm.hist_file, cfg.HistoryManager.hist_file) | |
|
143 | 143 | finally: |
|
144 | 144 | try: |
|
145 | 145 | os.remove(tfile.name) |
|
146 | 146 | except OSError: |
|
147 | 147 | # same catch as in testing.tools.TempFileMixin |
|
148 | 148 | # On Windows, even though we close the file, we still can't |
|
149 | 149 | # delete it. I have no clue why |
|
150 | 150 | pass |
|
151 | 151 |
@@ -1,845 +1,845 | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | """Tests for the inputsplitter module. |
|
3 | 3 | |
|
4 | 4 | Authors |
|
5 | 5 | ------- |
|
6 | 6 | * Fernando Perez |
|
7 | 7 | * Robert Kern |
|
8 | 8 | """ |
|
9 | 9 | #----------------------------------------------------------------------------- |
|
10 | 10 | # Copyright (C) 2010-2011 The IPython Development Team |
|
11 | 11 | # |
|
12 | 12 | # Distributed under the terms of the BSD License. The full license is in |
|
13 | 13 | # the file COPYING, distributed as part of this software. |
|
14 | 14 | #----------------------------------------------------------------------------- |
|
15 | 15 | |
|
16 | 16 | #----------------------------------------------------------------------------- |
|
17 | 17 | # Imports |
|
18 | 18 | #----------------------------------------------------------------------------- |
|
19 | 19 | # stdlib |
|
20 | 20 | import unittest |
|
21 | 21 | import sys |
|
22 | 22 | |
|
23 | 23 | # Third party |
|
24 | 24 | import nose.tools as nt |
|
25 | 25 | |
|
26 | 26 | # Our own |
|
27 | 27 | from IPython.core import inputsplitter as isp |
|
28 | 28 | from IPython.testing import tools as tt |
|
29 | 29 | from IPython.utils import py3compat |
|
30 | 30 | |
|
31 | 31 | #----------------------------------------------------------------------------- |
|
32 | 32 | # Semi-complete examples (also used as tests) |
|
33 | 33 | #----------------------------------------------------------------------------- |
|
34 | 34 | |
|
35 | 35 | # Note: at the bottom, there's a slightly more complete version of this that |
|
36 | 36 | # can be useful during development of code here. |
|
37 | 37 | |
|
38 | 38 | def mini_interactive_loop(input_func): |
|
39 | 39 | """Minimal example of the logic of an interactive interpreter loop. |
|
40 | 40 | |
|
41 | 41 | This serves as an example, and it is used by the test system with a fake |
|
42 | 42 | raw_input that simulates interactive input.""" |
|
43 | 43 | |
|
44 | 44 | from IPython.core.inputsplitter import InputSplitter |
|
45 | 45 | |
|
46 | 46 | isp = InputSplitter() |
|
47 | 47 | # In practice, this input loop would be wrapped in an outside loop to read |
|
48 | 48 | # input indefinitely, until some exit/quit command was issued. Here we |
|
49 | 49 | # only illustrate the basic inner loop. |
|
50 | 50 | while isp.push_accepts_more(): |
|
51 | 51 | indent = ' '*isp.indent_spaces |
|
52 | 52 | prompt = '>>> ' + indent |
|
53 | 53 | line = indent + input_func(prompt) |
|
54 | 54 | isp.push(line) |
|
55 | 55 | |
|
56 | 56 | # Here we just return input so we can use it in a test suite, but a real |
|
57 | 57 | # interpreter would instead send it for execution somewhere. |
|
58 | 58 | src = isp.source_reset() |
|
59 | 59 | #print 'Input source was:\n', src # dbg |
|
60 | 60 | return src |
|
61 | 61 | |
|
62 | 62 | #----------------------------------------------------------------------------- |
|
63 | 63 | # Test utilities, just for local use |
|
64 | 64 | #----------------------------------------------------------------------------- |
|
65 | 65 | |
|
66 | 66 | def assemble(block): |
|
67 | 67 | """Assemble a block into multi-line sub-blocks.""" |
|
68 | 68 | return ['\n'.join(sub_block)+'\n' for sub_block in block] |
|
69 | 69 | |
|
70 | 70 | |
|
71 | 71 | def pseudo_input(lines): |
|
72 | 72 | """Return a function that acts like raw_input but feeds the input list.""" |
|
73 | 73 | ilines = iter(lines) |
|
74 | 74 | def raw_in(prompt): |
|
75 | 75 | try: |
|
76 | 76 | return next(ilines) |
|
77 | 77 | except StopIteration: |
|
78 | 78 | return '' |
|
79 | 79 | return raw_in |
|
80 | 80 | |
|
81 | 81 | #----------------------------------------------------------------------------- |
|
82 | 82 | # Tests |
|
83 | 83 | #----------------------------------------------------------------------------- |
|
84 | 84 | def test_spaces(): |
|
85 | 85 | tests = [('', 0), |
|
86 | 86 | (' ', 1), |
|
87 | 87 | ('\n', 0), |
|
88 | 88 | (' \n', 1), |
|
89 | 89 | ('x', 0), |
|
90 | 90 | (' x', 1), |
|
91 | 91 | (' x',2), |
|
92 | 92 | (' x',4), |
|
93 | 93 | # Note: tabs are counted as a single whitespace! |
|
94 | 94 | ('\tx', 1), |
|
95 | 95 | ('\t x', 2), |
|
96 | 96 | ] |
|
97 | 97 | tt.check_pairs(isp.num_ini_spaces, tests) |
|
98 | 98 | |
|
99 | 99 | |
|
100 | 100 | def test_remove_comments(): |
|
101 | 101 | tests = [('text', 'text'), |
|
102 | 102 | ('text # comment', 'text '), |
|
103 | 103 | ('text # comment\n', 'text \n'), |
|
104 | 104 | ('text # comment \n', 'text \n'), |
|
105 | 105 | ('line # c \nline\n','line \nline\n'), |
|
106 | 106 | ('line # c \nline#c2 \nline\nline #c\n\n', |
|
107 | 107 | 'line \nline\nline\nline \n\n'), |
|
108 | 108 | ] |
|
109 | 109 | tt.check_pairs(isp.remove_comments, tests) |
|
110 | 110 | |
|
111 | 111 | def test_has_comment(): |
|
112 | 112 | tests = [('text', False), |
|
113 | 113 | ('text #comment', True), |
|
114 | 114 | ('text #comment\n', True), |
|
115 | 115 | ('#comment', True), |
|
116 | 116 | ('#comment\n', True), |
|
117 | 117 | ('a = "#string"', False), |
|
118 | 118 | ('a = "#string" # comment', True), |
|
119 | 119 | ('a #comment not "string"', True), |
|
120 | 120 | ] |
|
121 | 121 | tt.check_pairs(isp.has_comment, tests) |
|
122 | 122 | |
|
123 | 123 | |
|
124 | 124 | def test_get_input_encoding(): |
|
125 | 125 | encoding = isp.get_input_encoding() |
|
126 | 126 | nt.assert_true(isinstance(encoding, basestring)) |
|
127 | 127 | # simple-minded check that at least encoding a simple string works with the |
|
128 | 128 | # encoding we got. |
|
129 | 129 | nt.assert_equal(u'test'.encode(encoding), b'test') |
|
130 | 130 | |
|
131 | 131 | |
|
132 | 132 | class NoInputEncodingTestCase(unittest.TestCase): |
|
133 | 133 | def setUp(self): |
|
134 | 134 | self.old_stdin = sys.stdin |
|
135 | 135 | class X: pass |
|
136 | 136 | fake_stdin = X() |
|
137 | 137 | sys.stdin = fake_stdin |
|
138 | 138 | |
|
139 | 139 | def test(self): |
|
140 | 140 | # Verify that if sys.stdin has no 'encoding' attribute we do the right |
|
141 | 141 | # thing |
|
142 | 142 | enc = isp.get_input_encoding() |
|
143 | 143 | self.assertEqual(enc, 'ascii') |
|
144 | 144 | |
|
145 | 145 | def tearDown(self): |
|
146 | 146 | sys.stdin = self.old_stdin |
|
147 | 147 | |
|
148 | 148 | |
|
149 | 149 | class InputSplitterTestCase(unittest.TestCase): |
|
150 | 150 | def setUp(self): |
|
151 | 151 | self.isp = isp.InputSplitter() |
|
152 | 152 | |
|
153 | 153 | def test_reset(self): |
|
154 | 154 | isp = self.isp |
|
155 | 155 | isp.push('x=1') |
|
156 | 156 | isp.reset() |
|
157 | 157 | self.assertEqual(isp._buffer, []) |
|
158 | 158 | self.assertEqual(isp.indent_spaces, 0) |
|
159 | 159 | self.assertEqual(isp.source, '') |
|
160 | 160 | self.assertEqual(isp.code, None) |
|
161 | 161 | self.assertEqual(isp._is_complete, False) |
|
162 | 162 | |
|
163 | 163 | def test_source(self): |
|
164 | 164 | self.isp._store('1') |
|
165 | 165 | self.isp._store('2') |
|
166 | 166 | self.assertEqual(self.isp.source, '1\n2\n') |
|
167 | 167 | self.assertTrue(len(self.isp._buffer)>0) |
|
168 | 168 | self.assertEqual(self.isp.source_reset(), '1\n2\n') |
|
169 | 169 | self.assertEqual(self.isp._buffer, []) |
|
170 | 170 | self.assertEqual(self.isp.source, '') |
|
171 | 171 | |
|
172 | 172 | def test_indent(self): |
|
173 | 173 | isp = self.isp # shorthand |
|
174 | 174 | isp.push('x=1') |
|
175 | 175 | self.assertEqual(isp.indent_spaces, 0) |
|
176 | 176 | isp.push('if 1:\n x=1') |
|
177 | 177 | self.assertEqual(isp.indent_spaces, 4) |
|
178 | 178 | isp.push('y=2\n') |
|
179 | 179 | self.assertEqual(isp.indent_spaces, 0) |
|
180 | 180 | |
|
181 | 181 | def test_indent2(self): |
|
182 | 182 | # In cell mode, inputs must be fed in whole blocks, so skip this test |
|
183 | 183 | if self.isp.input_mode == 'cell': return |
|
184 | 184 | |
|
185 | 185 | isp = self.isp |
|
186 | 186 | isp.push('if 1:') |
|
187 | 187 | self.assertEqual(isp.indent_spaces, 4) |
|
188 | 188 | isp.push(' x=1') |
|
189 | 189 | self.assertEqual(isp.indent_spaces, 4) |
|
190 | 190 | # Blank lines shouldn't change the indent level |
|
191 | 191 | isp.push(' '*2) |
|
192 | 192 | self.assertEqual(isp.indent_spaces, 4) |
|
193 | 193 | |
|
194 | 194 | def test_indent3(self): |
|
195 | 195 | # In cell mode, inputs must be fed in whole blocks, so skip this test |
|
196 | 196 | if self.isp.input_mode == 'cell': return |
|
197 | 197 | |
|
198 | 198 | isp = self.isp |
|
199 | 199 | # When a multiline statement contains parens or multiline strings, we |
|
200 | 200 | # shouldn't get confused. |
|
201 | 201 | isp.push("if 1:") |
|
202 | 202 | isp.push(" x = (1+\n 2)") |
|
203 | 203 | self.assertEqual(isp.indent_spaces, 4) |
|
204 | 204 | |
|
205 | 205 | def test_indent4(self): |
|
206 | 206 | # In cell mode, inputs must be fed in whole blocks, so skip this test |
|
207 | 207 | if self.isp.input_mode == 'cell': return |
|
208 | 208 | |
|
209 | 209 | isp = self.isp |
|
210 | 210 | # whitespace after ':' should not screw up indent level |
|
211 | 211 | isp.push('if 1: \n x=1') |
|
212 | 212 | self.assertEqual(isp.indent_spaces, 4) |
|
213 | 213 | isp.push('y=2\n') |
|
214 | 214 | self.assertEqual(isp.indent_spaces, 0) |
|
215 | 215 | isp.push('if 1:\t\n x=1') |
|
216 | 216 | self.assertEqual(isp.indent_spaces, 4) |
|
217 | 217 | isp.push('y=2\n') |
|
218 | 218 | self.assertEqual(isp.indent_spaces, 0) |
|
219 | 219 | |
|
220 | 220 | def test_dedent_pass(self): |
|
221 | 221 | isp = self.isp # shorthand |
|
222 | 222 | # should NOT cause dedent |
|
223 | 223 | isp.push('if 1:\n passes = 5') |
|
224 | 224 | self.assertEqual(isp.indent_spaces, 4) |
|
225 | 225 | isp.push('if 1:\n pass') |
|
226 | 226 | self.assertEqual(isp.indent_spaces, 0) |
|
227 | 227 | isp.push('if 1:\n pass ') |
|
228 | 228 | self.assertEqual(isp.indent_spaces, 0) |
|
229 | 229 | |
|
230 | 230 | def test_dedent_break(self): |
|
231 | 231 | isp = self.isp # shorthand |
|
232 | 232 | # should NOT cause dedent |
|
233 | 233 | isp.push('while 1:\n breaks = 5') |
|
234 | 234 | self.assertEqual(isp.indent_spaces, 4) |
|
235 | 235 | isp.push('while 1:\n break') |
|
236 | 236 | self.assertEqual(isp.indent_spaces, 0) |
|
237 | 237 | isp.push('while 1:\n break ') |
|
238 | 238 | self.assertEqual(isp.indent_spaces, 0) |
|
239 | 239 | |
|
240 | 240 | def test_dedent_continue(self): |
|
241 | 241 | isp = self.isp # shorthand |
|
242 | 242 | # should NOT cause dedent |
|
243 | 243 | isp.push('while 1:\n continues = 5') |
|
244 | 244 | self.assertEqual(isp.indent_spaces, 4) |
|
245 | 245 | isp.push('while 1:\n continue') |
|
246 | 246 | self.assertEqual(isp.indent_spaces, 0) |
|
247 | 247 | isp.push('while 1:\n continue ') |
|
248 | 248 | self.assertEqual(isp.indent_spaces, 0) |
|
249 | 249 | |
|
250 | 250 | def test_dedent_raise(self): |
|
251 | 251 | isp = self.isp # shorthand |
|
252 | 252 | # should NOT cause dedent |
|
253 | 253 | isp.push('if 1:\n raised = 4') |
|
254 | 254 | self.assertEqual(isp.indent_spaces, 4) |
|
255 | 255 | isp.push('if 1:\n raise TypeError()') |
|
256 | 256 | self.assertEqual(isp.indent_spaces, 0) |
|
257 | 257 | isp.push('if 1:\n raise') |
|
258 | 258 | self.assertEqual(isp.indent_spaces, 0) |
|
259 | 259 | isp.push('if 1:\n raise ') |
|
260 | 260 | self.assertEqual(isp.indent_spaces, 0) |
|
261 | 261 | |
|
262 | 262 | def test_dedent_return(self): |
|
263 | 263 | isp = self.isp # shorthand |
|
264 | 264 | # should NOT cause dedent |
|
265 | 265 | isp.push('if 1:\n returning = 4') |
|
266 | 266 | self.assertEqual(isp.indent_spaces, 4) |
|
267 | 267 | isp.push('if 1:\n return 5 + 493') |
|
268 | 268 | self.assertEqual(isp.indent_spaces, 0) |
|
269 | 269 | isp.push('if 1:\n return') |
|
270 | 270 | self.assertEqual(isp.indent_spaces, 0) |
|
271 | 271 | isp.push('if 1:\n return ') |
|
272 | 272 | self.assertEqual(isp.indent_spaces, 0) |
|
273 | 273 | isp.push('if 1:\n return(0)') |
|
274 | 274 | self.assertEqual(isp.indent_spaces, 0) |
|
275 | 275 | |
|
276 | 276 | def test_push(self): |
|
277 | 277 | isp = self.isp |
|
278 | 278 | self.assertTrue(isp.push('x=1')) |
|
279 | 279 | |
|
280 | 280 | def test_push2(self): |
|
281 | 281 | isp = self.isp |
|
282 | 282 | self.assertFalse(isp.push('if 1:')) |
|
283 | 283 | for line in [' x=1', '# a comment', ' y=2']: |
|
284 | 284 | self.assertTrue(isp.push(line)) |
|
285 | 285 | |
|
286 | 286 | def test_push3(self): |
|
287 | 287 | isp = self.isp |
|
288 | 288 | isp.push('if True:') |
|
289 | 289 | isp.push(' a = 1') |
|
290 | 290 | self.assertFalse(isp.push('b = [1,')) |
|
291 | 291 | |
|
292 | 292 | def test_replace_mode(self): |
|
293 | 293 | isp = self.isp |
|
294 | 294 | isp.input_mode = 'cell' |
|
295 | 295 | isp.push('x=1') |
|
296 | 296 | self.assertEqual(isp.source, 'x=1\n') |
|
297 | 297 | isp.push('x=2') |
|
298 | 298 | self.assertEqual(isp.source, 'x=2\n') |
|
299 | 299 | |
|
300 | 300 | def test_push_accepts_more(self): |
|
301 | 301 | isp = self.isp |
|
302 | 302 | isp.push('x=1') |
|
303 | 303 | self.assertFalse(isp.push_accepts_more()) |
|
304 | 304 | |
|
305 | 305 | def test_push_accepts_more2(self): |
|
306 | 306 | # In cell mode, inputs must be fed in whole blocks, so skip this test |
|
307 | 307 | if self.isp.input_mode == 'cell': return |
|
308 | 308 | |
|
309 | 309 | isp = self.isp |
|
310 | 310 | isp.push('if 1:') |
|
311 | 311 | self.assertTrue(isp.push_accepts_more()) |
|
312 | 312 | isp.push(' x=1') |
|
313 | 313 | self.assertTrue(isp.push_accepts_more()) |
|
314 | 314 | isp.push('') |
|
315 | 315 | self.assertFalse(isp.push_accepts_more()) |
|
316 | 316 | |
|
317 | 317 | def test_push_accepts_more3(self): |
|
318 | 318 | isp = self.isp |
|
319 | 319 | isp.push("x = (2+\n3)") |
|
320 | 320 | self.assertFalse(isp.push_accepts_more()) |
|
321 | 321 | |
|
322 | 322 | def test_push_accepts_more4(self): |
|
323 | 323 | # In cell mode, inputs must be fed in whole blocks, so skip this test |
|
324 | 324 | if self.isp.input_mode == 'cell': return |
|
325 | 325 | |
|
326 | 326 | isp = self.isp |
|
327 | 327 | # When a multiline statement contains parens or multiline strings, we |
|
328 | 328 | # shouldn't get confused. |
|
329 | 329 | # FIXME: we should be able to better handle de-dents in statements like |
|
330 | 330 | # multiline strings and multiline expressions (continued with \ or |
|
331 | 331 | # parens). Right now we aren't handling the indentation tracking quite |
|
332 | 332 | # correctly with this, though in practice it may not be too much of a |
|
333 | 333 | # problem. We'll need to see. |
|
334 | 334 | isp.push("if 1:") |
|
335 | 335 | isp.push(" x = (2+") |
|
336 | 336 | isp.push(" 3)") |
|
337 | 337 | self.assertTrue(isp.push_accepts_more()) |
|
338 | 338 | isp.push(" y = 3") |
|
339 | 339 | self.assertTrue(isp.push_accepts_more()) |
|
340 | 340 | isp.push('') |
|
341 | 341 | self.assertFalse(isp.push_accepts_more()) |
|
342 | 342 | |
|
343 | 343 | def test_push_accepts_more5(self): |
|
344 | 344 | # In cell mode, inputs must be fed in whole blocks, so skip this test |
|
345 | 345 | if self.isp.input_mode == 'cell': return |
|
346 | 346 | |
|
347 | 347 | isp = self.isp |
|
348 | 348 | isp.push('try:') |
|
349 | 349 | isp.push(' a = 5') |
|
350 | 350 | isp.push('except:') |
|
351 | 351 | isp.push(' raise') |
|
352 | 352 | self.assertTrue(isp.push_accepts_more()) |
|
353 | 353 | |
|
354 | 354 | def test_continuation(self): |
|
355 | 355 | isp = self.isp |
|
356 | 356 | isp.push("import os, \\") |
|
357 | 357 | self.assertTrue(isp.push_accepts_more()) |
|
358 | 358 | isp.push("sys") |
|
359 | 359 | self.assertFalse(isp.push_accepts_more()) |
|
360 | 360 | |
|
361 | 361 | def test_syntax_error(self): |
|
362 | 362 | isp = self.isp |
|
363 | 363 | # Syntax errors immediately produce a 'ready' block, so the invalid |
|
364 | 364 | # Python can be sent to the kernel for evaluation with possible ipython |
|
365 | 365 | # special-syntax conversion. |
|
366 | 366 | isp.push('run foo') |
|
367 | 367 | self.assertFalse(isp.push_accepts_more()) |
|
368 | 368 | |
|
369 | 369 | def test_unicode(self): |
|
370 | 370 | self.isp.push(u"PΓ©rez") |
|
371 | 371 | self.isp.push(u'\xc3\xa9') |
|
372 | 372 | self.isp.push(u"u'\xc3\xa9'") |
|
373 | 373 | |
|
374 | 374 | def test_line_continuation(self): |
|
375 | 375 | """ Test issue #2108.""" |
|
376 | 376 | isp = self.isp |
|
377 | 377 | # A blank line after a line continuation should not accept more |
|
378 | 378 | isp.push("1 \\\n\n") |
|
379 | 379 | self.assertFalse(isp.push_accepts_more()) |
|
380 | 380 | # Whitespace after a \ is a SyntaxError. The only way to test that |
|
381 | 381 | # here is to test that push doesn't accept more (as with |
|
382 | 382 | # test_syntax_error() above). |
|
383 | 383 | isp.push(r"1 \ ") |
|
384 | 384 | self.assertFalse(isp.push_accepts_more()) |
|
385 | 385 | # Even if the line is continuable (c.f. the regular Python |
|
386 | 386 | # interpreter) |
|
387 | 387 | isp.push(r"(1 \ ") |
|
388 | 388 | self.assertFalse(isp.push_accepts_more()) |
|
389 | 389 | |
|
390 | 390 | class InteractiveLoopTestCase(unittest.TestCase): |
|
391 | 391 | """Tests for an interactive loop like a python shell. |
|
392 | 392 | """ |
|
393 | 393 | def check_ns(self, lines, ns): |
|
394 | 394 | """Validate that the given input lines produce the resulting namespace. |
|
395 | 395 | |
|
396 | 396 | Note: the input lines are given exactly as they would be typed in an |
|
397 | 397 | auto-indenting environment, as mini_interactive_loop above already does |
|
398 | 398 | auto-indenting and prepends spaces to the input. |
|
399 | 399 | """ |
|
400 | 400 | src = mini_interactive_loop(pseudo_input(lines)) |
|
401 | 401 | test_ns = {} |
|
402 | 402 | exec src in test_ns |
|
403 | 403 | # We can't check that the provided ns is identical to the test_ns, |
|
404 | 404 | # because Python fills test_ns with extra keys (copyright, etc). But |
|
405 | 405 | # we can check that the given dict is *contained* in test_ns |
|
406 | 406 | for k,v in ns.iteritems(): |
|
407 | 407 | self.assertEqual(test_ns[k], v) |
|
408 | 408 | |
|
409 | 409 | def test_simple(self): |
|
410 | 410 | self.check_ns(['x=1'], dict(x=1)) |
|
411 | 411 | |
|
412 | 412 | def test_simple2(self): |
|
413 | 413 | self.check_ns(['if 1:', 'x=2'], dict(x=2)) |
|
414 | 414 | |
|
415 | 415 | def test_xy(self): |
|
416 | 416 | self.check_ns(['x=1; y=2'], dict(x=1, y=2)) |
|
417 | 417 | |
|
418 | 418 | def test_abc(self): |
|
419 | 419 | self.check_ns(['if 1:','a=1','b=2','c=3'], dict(a=1, b=2, c=3)) |
|
420 | 420 | |
|
421 | 421 | def test_multi(self): |
|
422 | 422 | self.check_ns(['x =(1+','1+','2)'], dict(x=4)) |
|
423 | 423 | |
|
424 | 424 | |
|
425 | 425 | def test_LineInfo(): |
|
426 | 426 | """Simple test for LineInfo construction and str()""" |
|
427 | 427 | linfo = isp.LineInfo(' %cd /home') |
|
428 |
nt.assert_equal |
|
|
428 | nt.assert_equal(str(linfo), 'LineInfo [ |%|cd|/home]') | |
|
429 | 429 | |
|
430 | 430 | # Transformer tests |
|
431 | 431 | def transform_checker(tests, func): |
|
432 | 432 | """Utility to loop over test inputs""" |
|
433 | 433 | for inp, tr in tests: |
|
434 |
nt.assert_equal |
|
|
434 | nt.assert_equal(func(inp), tr) | |
|
435 | 435 | |
|
436 | 436 | # Data for all the syntax tests in the form of lists of pairs of |
|
437 | 437 | # raw/transformed input. We store it here as a global dict so that we can use |
|
438 | 438 | # it both within single-function tests and also to validate the behavior of the |
|
439 | 439 | # larger objects |
|
440 | 440 | |
|
441 | 441 | syntax = \ |
|
442 | 442 | dict(assign_system = |
|
443 | 443 | [(i,py3compat.u_format(o)) for i,o in \ |
|
444 | 444 | [(u'a =! ls', "a = get_ipython().getoutput({u}'ls')"), |
|
445 | 445 | (u'b = !ls', "b = get_ipython().getoutput({u}'ls')"), |
|
446 | 446 | ('x=1', 'x=1'), # normal input is unmodified |
|
447 | 447 | (' ',' '), # blank lines are kept intact |
|
448 | 448 | ]], |
|
449 | 449 | |
|
450 | 450 | assign_magic = |
|
451 | 451 | [(i,py3compat.u_format(o)) for i,o in \ |
|
452 | 452 | [(u'a =% who', "a = get_ipython().magic({u}'who')"), |
|
453 | 453 | (u'b = %who', "b = get_ipython().magic({u}'who')"), |
|
454 | 454 | ('x=1', 'x=1'), # normal input is unmodified |
|
455 | 455 | (' ',' '), # blank lines are kept intact |
|
456 | 456 | ]], |
|
457 | 457 | |
|
458 | 458 | classic_prompt = |
|
459 | 459 | [('>>> x=1', 'x=1'), |
|
460 | 460 | ('x=1', 'x=1'), # normal input is unmodified |
|
461 | 461 | (' ', ' '), # blank lines are kept intact |
|
462 | 462 | ('... ', ''), # continuation prompts |
|
463 | 463 | ], |
|
464 | 464 | |
|
465 | 465 | ipy_prompt = |
|
466 | 466 | [('In [1]: x=1', 'x=1'), |
|
467 | 467 | ('x=1', 'x=1'), # normal input is unmodified |
|
468 | 468 | (' ',' '), # blank lines are kept intact |
|
469 | 469 | (' ....: ', ''), # continuation prompts |
|
470 | 470 | ], |
|
471 | 471 | |
|
472 | 472 | # Tests for the escape transformer to leave normal code alone |
|
473 | 473 | escaped_noesc = |
|
474 | 474 | [ (' ', ' '), |
|
475 | 475 | ('x=1', 'x=1'), |
|
476 | 476 | ], |
|
477 | 477 | |
|
478 | 478 | # System calls |
|
479 | 479 | escaped_shell = |
|
480 | 480 | [(i,py3compat.u_format(o)) for i,o in \ |
|
481 | 481 | [ (u'!ls', "get_ipython().system({u}'ls')"), |
|
482 | 482 | # Double-escape shell, this means to capture the output of the |
|
483 | 483 | # subprocess and return it |
|
484 | 484 | (u'!!ls', "get_ipython().getoutput({u}'ls')"), |
|
485 | 485 | ]], |
|
486 | 486 | |
|
487 | 487 | # Help/object info |
|
488 | 488 | escaped_help = |
|
489 | 489 | [(i,py3compat.u_format(o)) for i,o in \ |
|
490 | 490 | [ (u'?', 'get_ipython().show_usage()'), |
|
491 | 491 | (u'?x1', "get_ipython().magic({u}'pinfo x1')"), |
|
492 | 492 | (u'??x2', "get_ipython().magic({u}'pinfo2 x2')"), |
|
493 | 493 | (u'?a.*s', "get_ipython().magic({u}'psearch a.*s')"), |
|
494 | 494 | (u'?%hist1', "get_ipython().magic({u}'pinfo %hist1')"), |
|
495 | 495 | (u'?%%hist2', "get_ipython().magic({u}'pinfo %%hist2')"), |
|
496 | 496 | (u'?abc = qwe', "get_ipython().magic({u}'pinfo abc')"), |
|
497 | 497 | ]], |
|
498 | 498 | |
|
499 | 499 | end_help = |
|
500 | 500 | [(i,py3compat.u_format(o)) for i,o in \ |
|
501 | 501 | [ (u'x3?', "get_ipython().magic({u}'pinfo x3')"), |
|
502 | 502 | (u'x4??', "get_ipython().magic({u}'pinfo2 x4')"), |
|
503 | 503 | (u'%hist1?', "get_ipython().magic({u}'pinfo %hist1')"), |
|
504 | 504 | (u'%hist2??', "get_ipython().magic({u}'pinfo2 %hist2')"), |
|
505 | 505 | (u'%%hist3?', "get_ipython().magic({u}'pinfo %%hist3')"), |
|
506 | 506 | (u'%%hist4??', "get_ipython().magic({u}'pinfo2 %%hist4')"), |
|
507 | 507 | (u'f*?', "get_ipython().magic({u}'psearch f*')"), |
|
508 | 508 | (u'ax.*aspe*?', "get_ipython().magic({u}'psearch ax.*aspe*')"), |
|
509 | 509 | (u'a = abc?', "get_ipython().set_next_input({u}'a = abc');" |
|
510 | 510 | "get_ipython().magic({u}'pinfo abc')"), |
|
511 | 511 | (u'a = abc.qe??', "get_ipython().set_next_input({u}'a = abc.qe');" |
|
512 | 512 | "get_ipython().magic({u}'pinfo2 abc.qe')"), |
|
513 | 513 | (u'a = *.items?', "get_ipython().set_next_input({u}'a = *.items');" |
|
514 | 514 | "get_ipython().magic({u}'psearch *.items')"), |
|
515 | 515 | (u'plot(a?', "get_ipython().set_next_input({u}'plot(a');" |
|
516 | 516 | "get_ipython().magic({u}'pinfo a')"), |
|
517 | 517 | (u'a*2 #comment?', 'a*2 #comment?'), |
|
518 | 518 | ]], |
|
519 | 519 | |
|
520 | 520 | # Explicit magic calls |
|
521 | 521 | escaped_magic = |
|
522 | 522 | [(i,py3compat.u_format(o)) for i,o in \ |
|
523 | 523 | [ (u'%cd', "get_ipython().magic({u}'cd')"), |
|
524 | 524 | (u'%cd /home', "get_ipython().magic({u}'cd /home')"), |
|
525 | 525 | # Backslashes need to be escaped. |
|
526 | 526 | (u'%cd C:\\User', "get_ipython().magic({u}'cd C:\\\\User')"), |
|
527 | 527 | (u' %magic', " get_ipython().magic({u}'magic')"), |
|
528 | 528 | ]], |
|
529 | 529 | |
|
530 | 530 | # Quoting with separate arguments |
|
531 | 531 | escaped_quote = |
|
532 | 532 | [ (',f', 'f("")'), |
|
533 | 533 | (',f x', 'f("x")'), |
|
534 | 534 | (' ,f y', ' f("y")'), |
|
535 | 535 | (',f a b', 'f("a", "b")'), |
|
536 | 536 | ], |
|
537 | 537 | |
|
538 | 538 | # Quoting with single argument |
|
539 | 539 | escaped_quote2 = |
|
540 | 540 | [ (';f', 'f("")'), |
|
541 | 541 | (';f x', 'f("x")'), |
|
542 | 542 | (' ;f y', ' f("y")'), |
|
543 | 543 | (';f a b', 'f("a b")'), |
|
544 | 544 | ], |
|
545 | 545 | |
|
546 | 546 | # Simply apply parens |
|
547 | 547 | escaped_paren = |
|
548 | 548 | [ ('/f', 'f()'), |
|
549 | 549 | ('/f x', 'f(x)'), |
|
550 | 550 | (' /f y', ' f(y)'), |
|
551 | 551 | ('/f a b', 'f(a, b)'), |
|
552 | 552 | ], |
|
553 | 553 | |
|
554 | 554 | # Check that we transform prompts before other transforms |
|
555 | 555 | mixed = |
|
556 | 556 | [(i,py3compat.u_format(o)) for i,o in \ |
|
557 | 557 | [ (u'In [1]: %lsmagic', "get_ipython().magic({u}'lsmagic')"), |
|
558 | 558 | (u'>>> %lsmagic', "get_ipython().magic({u}'lsmagic')"), |
|
559 | 559 | (u'In [2]: !ls', "get_ipython().system({u}'ls')"), |
|
560 | 560 | (u'In [3]: abs?', "get_ipython().magic({u}'pinfo abs')"), |
|
561 | 561 | (u'In [4]: b = %who', "b = get_ipython().magic({u}'who')"), |
|
562 | 562 | ]], |
|
563 | 563 | ) |
|
564 | 564 | |
|
565 | 565 | # multiline syntax examples. Each of these should be a list of lists, with |
|
566 | 566 | # each entry itself having pairs of raw/transformed input. The union (with |
|
567 | 567 | # '\n'.join() of the transformed inputs is what the splitter should produce |
|
568 | 568 | # when fed the raw lines one at a time via push. |
|
569 | 569 | syntax_ml = \ |
|
570 | 570 | dict(classic_prompt = |
|
571 | 571 | [ [('>>> for i in range(10):','for i in range(10):'), |
|
572 | 572 | ('... print i',' print i'), |
|
573 | 573 | ('... ', ''), |
|
574 | 574 | ], |
|
575 | 575 | ], |
|
576 | 576 | |
|
577 | 577 | ipy_prompt = |
|
578 | 578 | [ [('In [24]: for i in range(10):','for i in range(10):'), |
|
579 | 579 | (' ....: print i',' print i'), |
|
580 | 580 | (' ....: ', ''), |
|
581 | 581 | ], |
|
582 | 582 | ], |
|
583 | 583 | |
|
584 | 584 | multiline_datastructure = |
|
585 | 585 | [ [('>>> a = [1,','a = [1,'), |
|
586 | 586 | ('... 2]','2]'), |
|
587 | 587 | ], |
|
588 | 588 | ], |
|
589 | 589 | ) |
|
590 | 590 | |
|
591 | 591 | |
|
592 | 592 | def test_assign_system(): |
|
593 | 593 | tt.check_pairs(isp.transform_assign_system, syntax['assign_system']) |
|
594 | 594 | |
|
595 | 595 | |
|
596 | 596 | def test_assign_magic(): |
|
597 | 597 | tt.check_pairs(isp.transform_assign_magic, syntax['assign_magic']) |
|
598 | 598 | |
|
599 | 599 | |
|
600 | 600 | def test_classic_prompt(): |
|
601 | 601 | transform_checker(syntax['classic_prompt'], isp.transform_classic_prompt) |
|
602 | 602 | for example in syntax_ml['classic_prompt']: |
|
603 | 603 | transform_checker(example, isp.transform_classic_prompt) |
|
604 | 604 | |
|
605 | 605 | |
|
606 | 606 | def test_ipy_prompt(): |
|
607 | 607 | transform_checker(syntax['ipy_prompt'], isp.transform_ipy_prompt) |
|
608 | 608 | for example in syntax_ml['ipy_prompt']: |
|
609 | 609 | transform_checker(example, isp.transform_ipy_prompt) |
|
610 | 610 | |
|
611 | 611 | def test_end_help(): |
|
612 | 612 | tt.check_pairs(isp.transform_help_end, syntax['end_help']) |
|
613 | 613 | |
|
614 | 614 | def test_escaped_noesc(): |
|
615 | 615 | tt.check_pairs(isp.transform_escaped, syntax['escaped_noesc']) |
|
616 | 616 | |
|
617 | 617 | |
|
618 | 618 | def test_escaped_shell(): |
|
619 | 619 | tt.check_pairs(isp.transform_escaped, syntax['escaped_shell']) |
|
620 | 620 | |
|
621 | 621 | |
|
622 | 622 | def test_escaped_help(): |
|
623 | 623 | tt.check_pairs(isp.transform_escaped, syntax['escaped_help']) |
|
624 | 624 | |
|
625 | 625 | |
|
626 | 626 | def test_escaped_magic(): |
|
627 | 627 | tt.check_pairs(isp.transform_escaped, syntax['escaped_magic']) |
|
628 | 628 | |
|
629 | 629 | |
|
630 | 630 | def test_escaped_quote(): |
|
631 | 631 | tt.check_pairs(isp.transform_escaped, syntax['escaped_quote']) |
|
632 | 632 | |
|
633 | 633 | |
|
634 | 634 | def test_escaped_quote2(): |
|
635 | 635 | tt.check_pairs(isp.transform_escaped, syntax['escaped_quote2']) |
|
636 | 636 | |
|
637 | 637 | |
|
638 | 638 | def test_escaped_paren(): |
|
639 | 639 | tt.check_pairs(isp.transform_escaped, syntax['escaped_paren']) |
|
640 | 640 | |
|
641 | 641 | |
|
642 | 642 | class IPythonInputTestCase(InputSplitterTestCase): |
|
643 | 643 | """By just creating a new class whose .isp is a different instance, we |
|
644 | 644 | re-run the same test battery on the new input splitter. |
|
645 | 645 | |
|
646 | 646 | In addition, this runs the tests over the syntax and syntax_ml dicts that |
|
647 | 647 | were tested by individual functions, as part of the OO interface. |
|
648 | 648 | |
|
649 | 649 | It also makes some checks on the raw buffer storage. |
|
650 | 650 | """ |
|
651 | 651 | |
|
652 | 652 | def setUp(self): |
|
653 | 653 | self.isp = isp.IPythonInputSplitter(input_mode='line') |
|
654 | 654 | |
|
655 | 655 | def test_syntax(self): |
|
656 | 656 | """Call all single-line syntax tests from the main object""" |
|
657 | 657 | isp = self.isp |
|
658 | 658 | for example in syntax.itervalues(): |
|
659 | 659 | for raw, out_t in example: |
|
660 | 660 | if raw.startswith(' '): |
|
661 | 661 | continue |
|
662 | 662 | |
|
663 | 663 | isp.push(raw+'\n') |
|
664 | 664 | out, out_raw = isp.source_raw_reset() |
|
665 | 665 | self.assertEqual(out.rstrip(), out_t, |
|
666 | 666 | tt.pair_fail_msg.format("inputsplitter",raw, out_t, out)) |
|
667 | 667 | self.assertEqual(out_raw.rstrip(), raw.rstrip()) |
|
668 | 668 | |
|
669 | 669 | def test_syntax_multiline(self): |
|
670 | 670 | isp = self.isp |
|
671 | 671 | for example in syntax_ml.itervalues(): |
|
672 | 672 | out_t_parts = [] |
|
673 | 673 | raw_parts = [] |
|
674 | 674 | for line_pairs in example: |
|
675 | 675 | for lraw, out_t_part in line_pairs: |
|
676 | 676 | isp.push(lraw) |
|
677 | 677 | out_t_parts.append(out_t_part) |
|
678 | 678 | raw_parts.append(lraw) |
|
679 | 679 | |
|
680 | 680 | out, out_raw = isp.source_raw_reset() |
|
681 | 681 | out_t = '\n'.join(out_t_parts).rstrip() |
|
682 | 682 | raw = '\n'.join(raw_parts).rstrip() |
|
683 | 683 | self.assertEqual(out.rstrip(), out_t) |
|
684 | 684 | self.assertEqual(out_raw.rstrip(), raw) |
|
685 | 685 | |
|
686 | 686 | |
|
687 | 687 | class BlockIPythonInputTestCase(IPythonInputTestCase): |
|
688 | 688 | |
|
689 | 689 | # Deactivate tests that don't make sense for the block mode |
|
690 | 690 | test_push3 = test_split = lambda s: None |
|
691 | 691 | |
|
692 | 692 | def setUp(self): |
|
693 | 693 | self.isp = isp.IPythonInputSplitter(input_mode='cell') |
|
694 | 694 | |
|
695 | 695 | def test_syntax_multiline(self): |
|
696 | 696 | isp = self.isp |
|
697 | 697 | for example in syntax_ml.itervalues(): |
|
698 | 698 | raw_parts = [] |
|
699 | 699 | out_t_parts = [] |
|
700 | 700 | for line_pairs in example: |
|
701 | 701 | for raw, out_t_part in line_pairs: |
|
702 | 702 | raw_parts.append(raw) |
|
703 | 703 | out_t_parts.append(out_t_part) |
|
704 | 704 | |
|
705 | 705 | raw = '\n'.join(raw_parts) |
|
706 | 706 | out_t = '\n'.join(out_t_parts) |
|
707 | 707 | |
|
708 | 708 | isp.push(raw) |
|
709 | 709 | out, out_raw = isp.source_raw_reset() |
|
710 | 710 | # Match ignoring trailing whitespace |
|
711 | 711 | self.assertEqual(out.rstrip(), out_t.rstrip()) |
|
712 | 712 | self.assertEqual(out_raw.rstrip(), raw.rstrip()) |
|
713 | 713 | |
|
714 | 714 | def test_syntax_multiline_cell(self): |
|
715 | 715 | isp = self.isp |
|
716 | 716 | for example in syntax_ml.itervalues(): |
|
717 | 717 | |
|
718 | 718 | out_t_parts = [] |
|
719 | 719 | for line_pairs in example: |
|
720 | 720 | raw = '\n'.join(r for r, _ in line_pairs) |
|
721 | 721 | out_t = '\n'.join(t for _,t in line_pairs) |
|
722 | 722 | out = isp.transform_cell(raw) |
|
723 | 723 | # Match ignoring trailing whitespace |
|
724 | 724 | self.assertEqual(out.rstrip(), out_t.rstrip()) |
|
725 | 725 | |
|
726 | 726 | #----------------------------------------------------------------------------- |
|
727 | 727 | # Main - use as a script, mostly for developer experiments |
|
728 | 728 | #----------------------------------------------------------------------------- |
|
729 | 729 | |
|
730 | 730 | if __name__ == '__main__': |
|
731 | 731 | # A simple demo for interactive experimentation. This code will not get |
|
732 | 732 | # picked up by any test suite. |
|
733 | 733 | from IPython.core.inputsplitter import InputSplitter, IPythonInputSplitter |
|
734 | 734 | |
|
735 | 735 | # configure here the syntax to use, prompt and whether to autoindent |
|
736 | 736 | #isp, start_prompt = InputSplitter(), '>>> ' |
|
737 | 737 | isp, start_prompt = IPythonInputSplitter(), 'In> ' |
|
738 | 738 | |
|
739 | 739 | autoindent = True |
|
740 | 740 | #autoindent = False |
|
741 | 741 | |
|
742 | 742 | try: |
|
743 | 743 | while True: |
|
744 | 744 | prompt = start_prompt |
|
745 | 745 | while isp.push_accepts_more(): |
|
746 | 746 | indent = ' '*isp.indent_spaces |
|
747 | 747 | if autoindent: |
|
748 | 748 | line = indent + raw_input(prompt+indent) |
|
749 | 749 | else: |
|
750 | 750 | line = raw_input(prompt) |
|
751 | 751 | isp.push(line) |
|
752 | 752 | prompt = '... ' |
|
753 | 753 | |
|
754 | 754 | # Here we just return input so we can use it in a test suite, but a |
|
755 | 755 | # real interpreter would instead send it for execution somewhere. |
|
756 | 756 | #src = isp.source; raise EOFError # dbg |
|
757 | 757 | src, raw = isp.source_raw_reset() |
|
758 | 758 | print 'Input source was:\n', src |
|
759 | 759 | print 'Raw source was:\n', raw |
|
760 | 760 | except EOFError: |
|
761 | 761 | print 'Bye' |
|
762 | 762 | |
|
763 | 763 | # Tests for cell magics support |
|
764 | 764 | |
|
765 | 765 | def test_last_blank(): |
|
766 | 766 | nt.assert_false(isp.last_blank('')) |
|
767 | 767 | nt.assert_false(isp.last_blank('abc')) |
|
768 | 768 | nt.assert_false(isp.last_blank('abc\n')) |
|
769 | 769 | nt.assert_false(isp.last_blank('abc\na')) |
|
770 | 770 | |
|
771 | 771 | nt.assert_true(isp.last_blank('\n')) |
|
772 | 772 | nt.assert_true(isp.last_blank('\n ')) |
|
773 | 773 | nt.assert_true(isp.last_blank('abc\n ')) |
|
774 | 774 | nt.assert_true(isp.last_blank('abc\n\n')) |
|
775 | 775 | nt.assert_true(isp.last_blank('abc\nd\n\n')) |
|
776 | 776 | nt.assert_true(isp.last_blank('abc\nd\ne\n\n')) |
|
777 | 777 | nt.assert_true(isp.last_blank('abc \n \n \n\n')) |
|
778 | 778 | |
|
779 | 779 | |
|
780 | 780 | def test_last_two_blanks(): |
|
781 | 781 | nt.assert_false(isp.last_two_blanks('')) |
|
782 | 782 | nt.assert_false(isp.last_two_blanks('abc')) |
|
783 | 783 | nt.assert_false(isp.last_two_blanks('abc\n')) |
|
784 | 784 | nt.assert_false(isp.last_two_blanks('abc\n\na')) |
|
785 | 785 | nt.assert_false(isp.last_two_blanks('abc\n \n')) |
|
786 | 786 | nt.assert_false(isp.last_two_blanks('abc\n\n')) |
|
787 | 787 | |
|
788 | 788 | nt.assert_true(isp.last_two_blanks('\n\n')) |
|
789 | 789 | nt.assert_true(isp.last_two_blanks('\n\n ')) |
|
790 | 790 | nt.assert_true(isp.last_two_blanks('\n \n')) |
|
791 | 791 | nt.assert_true(isp.last_two_blanks('abc\n\n ')) |
|
792 | 792 | nt.assert_true(isp.last_two_blanks('abc\n\n\n')) |
|
793 | 793 | nt.assert_true(isp.last_two_blanks('abc\n\n \n')) |
|
794 | 794 | nt.assert_true(isp.last_two_blanks('abc\n\n \n ')) |
|
795 | 795 | nt.assert_true(isp.last_two_blanks('abc\n\n \n \n')) |
|
796 | 796 | nt.assert_true(isp.last_two_blanks('abc\nd\n\n\n')) |
|
797 | 797 | nt.assert_true(isp.last_two_blanks('abc\nd\ne\nf\n\n\n')) |
|
798 | 798 | |
|
799 | 799 | |
|
800 | 800 | class CellMagicsCommon(object): |
|
801 | 801 | |
|
802 | 802 | def test_whole_cell(self): |
|
803 | 803 | src = "%%cellm line\nbody\n" |
|
804 | 804 | sp = self.sp |
|
805 | 805 | sp.push(src) |
|
806 | 806 | nt.assert_equal(sp.cell_magic_parts, ['body\n']) |
|
807 | 807 | out = sp.source |
|
808 | 808 | ref = u"get_ipython()._run_cached_cell_magic({u}'cellm', {u}'line')\n" |
|
809 | 809 | nt.assert_equal(out, py3compat.u_format(ref)) |
|
810 | 810 | |
|
811 | 811 | def tearDown(self): |
|
812 | 812 | self.sp.reset() |
|
813 | 813 | |
|
814 | 814 | |
|
815 | 815 | class CellModeCellMagics(CellMagicsCommon, unittest.TestCase): |
|
816 | 816 | sp = isp.IPythonInputSplitter(input_mode='cell') |
|
817 | 817 | |
|
818 | 818 | def test_incremental(self): |
|
819 | 819 | sp = self.sp |
|
820 | 820 | src = '%%cellm line2\n' |
|
821 | 821 | sp.push(src) |
|
822 | 822 | nt.assert_true(sp.push_accepts_more()) #1 |
|
823 | 823 | src += '\n' |
|
824 | 824 | sp.push(src) |
|
825 | 825 | # Note: if we ever change the logic to allow full blank lines (see |
|
826 | 826 | # _handle_cell_magic), then the following test should change to true |
|
827 | 827 | nt.assert_false(sp.push_accepts_more()) #2 |
|
828 | 828 | # By now, even with full blanks allowed, a second blank should signal |
|
829 | 829 | # the end. For now this test is only a redundancy safety, but don't |
|
830 | 830 | # delete it in case we change our mind and the previous one goes to |
|
831 | 831 | # true. |
|
832 | 832 | src += '\n' |
|
833 | 833 | sp.push(src) |
|
834 | 834 | nt.assert_false(sp.push_accepts_more()) #3 |
|
835 | 835 | |
|
836 | 836 | |
|
837 | 837 | class LineModeCellMagics(CellMagicsCommon, unittest.TestCase): |
|
838 | 838 | sp = isp.IPythonInputSplitter(input_mode='line') |
|
839 | 839 | |
|
840 | 840 | def test_incremental(self): |
|
841 | 841 | sp = self.sp |
|
842 | 842 | sp.push('%%cellm line2\n') |
|
843 | 843 | nt.assert_true(sp.push_accepts_more()) #1 |
|
844 | 844 | sp.push('\n') |
|
845 | 845 | nt.assert_false(sp.push_accepts_more()) #2 |
@@ -1,260 +1,260 | |||
|
1 | 1 | """Tests for the key interactiveshell module, where the main ipython class is defined. |
|
2 | 2 | """ |
|
3 | 3 | #----------------------------------------------------------------------------- |
|
4 | 4 | # Module imports |
|
5 | 5 | #----------------------------------------------------------------------------- |
|
6 | 6 | |
|
7 | 7 | # stdlib |
|
8 | 8 | import os |
|
9 | 9 | import shutil |
|
10 | 10 | import tempfile |
|
11 | 11 | |
|
12 | 12 | # third party |
|
13 | 13 | import nose.tools as nt |
|
14 | 14 | |
|
15 | 15 | # our own packages |
|
16 | 16 | from IPython.testing.globalipapp import get_ipython |
|
17 | 17 | from IPython.utils import py3compat |
|
18 | 18 | |
|
19 | 19 | #----------------------------------------------------------------------------- |
|
20 | 20 | # Globals |
|
21 | 21 | #----------------------------------------------------------------------------- |
|
22 | 22 | |
|
23 | 23 | # Get the public instance of IPython |
|
24 | 24 | ip = get_ipython() |
|
25 | 25 | |
|
26 | 26 | #----------------------------------------------------------------------------- |
|
27 | 27 | # Test functions |
|
28 | 28 | #----------------------------------------------------------------------------- |
|
29 | 29 | |
|
30 | 30 | def test_reset(): |
|
31 | 31 | """reset must clear most namespaces.""" |
|
32 | 32 | |
|
33 | 33 | # Check that reset runs without error |
|
34 | 34 | ip.reset() |
|
35 | 35 | |
|
36 | 36 | # Once we've reset it (to clear of any junk that might have been there from |
|
37 | 37 | # other tests, we can count how many variables are in the user's namespace |
|
38 | 38 | nvars_user_ns = len(ip.user_ns) |
|
39 | 39 | nvars_hidden = len(ip.user_ns_hidden) |
|
40 | 40 | |
|
41 | 41 | # Now add a few variables to user_ns, and check that reset clears them |
|
42 | 42 | ip.user_ns['x'] = 1 |
|
43 | 43 | ip.user_ns['y'] = 1 |
|
44 | 44 | ip.reset() |
|
45 | 45 | |
|
46 | 46 | # Finally, check that all namespaces have only as many variables as we |
|
47 | 47 | # expect to find in them: |
|
48 |
nt.assert_equal |
|
|
49 |
nt.assert_equal |
|
|
48 | nt.assert_equal(len(ip.user_ns), nvars_user_ns) | |
|
49 | nt.assert_equal(len(ip.user_ns_hidden), nvars_hidden) | |
|
50 | 50 | |
|
51 | 51 | |
|
52 | 52 | # Tests for reporting of exceptions in various modes, handling of SystemExit, |
|
53 | 53 | # and %tb functionality. This is really a mix of testing ultraTB and interactiveshell. |
|
54 | 54 | |
|
55 | 55 | def doctest_tb_plain(): |
|
56 | 56 | """ |
|
57 | 57 | In [18]: xmode plain |
|
58 | 58 | Exception reporting mode: Plain |
|
59 | 59 | |
|
60 | 60 | In [19]: run simpleerr.py |
|
61 | 61 | Traceback (most recent call last): |
|
62 | 62 | ...line 32, in <module> |
|
63 | 63 | bar(mode) |
|
64 | 64 | ...line 16, in bar |
|
65 | 65 | div0() |
|
66 | 66 | ...line 8, in div0 |
|
67 | 67 | x/y |
|
68 | 68 | ZeroDivisionError: ... |
|
69 | 69 | """ |
|
70 | 70 | |
|
71 | 71 | |
|
72 | 72 | def doctest_tb_context(): |
|
73 | 73 | """ |
|
74 | 74 | In [3]: xmode context |
|
75 | 75 | Exception reporting mode: Context |
|
76 | 76 | |
|
77 | 77 | In [4]: run simpleerr.py |
|
78 | 78 | --------------------------------------------------------------------------- |
|
79 | 79 | ZeroDivisionError Traceback (most recent call last) |
|
80 | 80 | <BLANKLINE> |
|
81 | 81 | ... in <module>() |
|
82 | 82 | 30 mode = 'div' |
|
83 | 83 | 31 |
|
84 | 84 | ---> 32 bar(mode) |
|
85 | 85 | <BLANKLINE> |
|
86 | 86 | ... in bar(mode) |
|
87 | 87 | 14 "bar" |
|
88 | 88 | 15 if mode=='div': |
|
89 | 89 | ---> 16 div0() |
|
90 | 90 | 17 elif mode=='exit': |
|
91 | 91 | 18 try: |
|
92 | 92 | <BLANKLINE> |
|
93 | 93 | ... in div0() |
|
94 | 94 | 6 x = 1 |
|
95 | 95 | 7 y = 0 |
|
96 | 96 | ----> 8 x/y |
|
97 | 97 | 9 |
|
98 | 98 | 10 def sysexit(stat, mode): |
|
99 | 99 | <BLANKLINE> |
|
100 | 100 | ZeroDivisionError: ... |
|
101 | 101 | """ |
|
102 | 102 | |
|
103 | 103 | |
|
104 | 104 | def doctest_tb_verbose(): |
|
105 | 105 | """ |
|
106 | 106 | In [5]: xmode verbose |
|
107 | 107 | Exception reporting mode: Verbose |
|
108 | 108 | |
|
109 | 109 | In [6]: run simpleerr.py |
|
110 | 110 | --------------------------------------------------------------------------- |
|
111 | 111 | ZeroDivisionError Traceback (most recent call last) |
|
112 | 112 | <BLANKLINE> |
|
113 | 113 | ... in <module>() |
|
114 | 114 | 30 mode = 'div' |
|
115 | 115 | 31 |
|
116 | 116 | ---> 32 bar(mode) |
|
117 | 117 | global bar = <function bar at ...> |
|
118 | 118 | global mode = 'div' |
|
119 | 119 | <BLANKLINE> |
|
120 | 120 | ... in bar(mode='div') |
|
121 | 121 | 14 "bar" |
|
122 | 122 | 15 if mode=='div': |
|
123 | 123 | ---> 16 div0() |
|
124 | 124 | global div0 = <function div0 at ...> |
|
125 | 125 | 17 elif mode=='exit': |
|
126 | 126 | 18 try: |
|
127 | 127 | <BLANKLINE> |
|
128 | 128 | ... in div0() |
|
129 | 129 | 6 x = 1 |
|
130 | 130 | 7 y = 0 |
|
131 | 131 | ----> 8 x/y |
|
132 | 132 | x = 1 |
|
133 | 133 | y = 0 |
|
134 | 134 | 9 |
|
135 | 135 | 10 def sysexit(stat, mode): |
|
136 | 136 | <BLANKLINE> |
|
137 | 137 | ZeroDivisionError: ... |
|
138 | 138 | """ |
|
139 | 139 | |
|
140 | 140 | def doctest_tb_sysexit(): |
|
141 | 141 | """ |
|
142 | 142 | In [17]: %xmode plain |
|
143 | 143 | Exception reporting mode: Plain |
|
144 | 144 | |
|
145 | 145 | In [18]: %run simpleerr.py exit |
|
146 | 146 | An exception has occurred, use %tb to see the full traceback. |
|
147 | 147 | SystemExit: (1, 'Mode = exit') |
|
148 | 148 | |
|
149 | 149 | In [19]: %run simpleerr.py exit 2 |
|
150 | 150 | An exception has occurred, use %tb to see the full traceback. |
|
151 | 151 | SystemExit: (2, 'Mode = exit') |
|
152 | 152 | |
|
153 | 153 | In [20]: %tb |
|
154 | 154 | Traceback (most recent call last): |
|
155 | 155 | File ... in <module> |
|
156 | 156 | bar(mode) |
|
157 | 157 | File ... line 22, in bar |
|
158 | 158 | sysexit(stat, mode) |
|
159 | 159 | File ... line 11, in sysexit |
|
160 | 160 | raise SystemExit(stat, 'Mode = %s' % mode) |
|
161 | 161 | SystemExit: (2, 'Mode = exit') |
|
162 | 162 | |
|
163 | 163 | In [21]: %xmode context |
|
164 | 164 | Exception reporting mode: Context |
|
165 | 165 | |
|
166 | 166 | In [22]: %tb |
|
167 | 167 | --------------------------------------------------------------------------- |
|
168 | 168 | SystemExit Traceback (most recent call last) |
|
169 | 169 | <BLANKLINE> |
|
170 | 170 | ...<module>() |
|
171 | 171 | 30 mode = 'div' |
|
172 | 172 | 31 |
|
173 | 173 | ---> 32 bar(mode) |
|
174 | 174 | <BLANKLINE> |
|
175 | 175 | ...bar(mode) |
|
176 | 176 | 20 except: |
|
177 | 177 | 21 stat = 1 |
|
178 | 178 | ---> 22 sysexit(stat, mode) |
|
179 | 179 | 23 else: |
|
180 | 180 | 24 raise ValueError('Unknown mode') |
|
181 | 181 | <BLANKLINE> |
|
182 | 182 | ...sysexit(stat, mode) |
|
183 | 183 | 9 |
|
184 | 184 | 10 def sysexit(stat, mode): |
|
185 | 185 | ---> 11 raise SystemExit(stat, 'Mode = %s' % mode) |
|
186 | 186 | 12 |
|
187 | 187 | 13 def bar(mode): |
|
188 | 188 | <BLANKLINE> |
|
189 | 189 | SystemExit: (2, 'Mode = exit') |
|
190 | 190 | |
|
191 | 191 | In [23]: %xmode verbose |
|
192 | 192 | Exception reporting mode: Verbose |
|
193 | 193 | |
|
194 | 194 | In [24]: %tb |
|
195 | 195 | --------------------------------------------------------------------------- |
|
196 | 196 | SystemExit Traceback (most recent call last) |
|
197 | 197 | <BLANKLINE> |
|
198 | 198 | ... in <module>() |
|
199 | 199 | 30 mode = 'div' |
|
200 | 200 | 31 |
|
201 | 201 | ---> 32 bar(mode) |
|
202 | 202 | global bar = <function bar at ...> |
|
203 | 203 | global mode = 'exit' |
|
204 | 204 | <BLANKLINE> |
|
205 | 205 | ... in bar(mode='exit') |
|
206 | 206 | 20 except: |
|
207 | 207 | 21 stat = 1 |
|
208 | 208 | ---> 22 sysexit(stat, mode) |
|
209 | 209 | global sysexit = <function sysexit at ...> |
|
210 | 210 | stat = 2 |
|
211 | 211 | mode = 'exit' |
|
212 | 212 | 23 else: |
|
213 | 213 | 24 raise ValueError('Unknown mode') |
|
214 | 214 | <BLANKLINE> |
|
215 | 215 | ... in sysexit(stat=2, mode='exit') |
|
216 | 216 | 9 |
|
217 | 217 | 10 def sysexit(stat, mode): |
|
218 | 218 | ---> 11 raise SystemExit(stat, 'Mode = %s' % mode) |
|
219 | 219 | global SystemExit = undefined |
|
220 | 220 | stat = 2 |
|
221 | 221 | mode = 'exit' |
|
222 | 222 | 12 |
|
223 | 223 | 13 def bar(mode): |
|
224 | 224 | <BLANKLINE> |
|
225 | 225 | SystemExit: (2, 'Mode = exit') |
|
226 | 226 | """ |
|
227 | 227 | |
|
228 | 228 | |
|
229 | 229 | def test_run_cell(): |
|
230 | 230 | import textwrap |
|
231 | 231 | ip.run_cell('a = 10\na+=1') |
|
232 | 232 | ip.run_cell('assert a == 11\nassert 1') |
|
233 | 233 | |
|
234 |
nt.assert_equal |
|
|
234 | nt.assert_equal(ip.user_ns['a'], 11) | |
|
235 | 235 | complex = textwrap.dedent(""" |
|
236 | 236 | if 1: |
|
237 | 237 | print "hello" |
|
238 | 238 | if 1: |
|
239 | 239 | print "world" |
|
240 | 240 | |
|
241 | 241 | if 2: |
|
242 | 242 | print "foo" |
|
243 | 243 | |
|
244 | 244 | if 3: |
|
245 | 245 | print "bar" |
|
246 | 246 | |
|
247 | 247 | if 4: |
|
248 | 248 | print "bar" |
|
249 | 249 | |
|
250 | 250 | """) |
|
251 | 251 | # Simply verifies that this kind of input is run |
|
252 | 252 | ip.run_cell(complex) |
|
253 | 253 | |
|
254 | 254 | |
|
255 | 255 | def test_db(): |
|
256 | 256 | """Test the internal database used for variable persistence.""" |
|
257 | 257 | ip.db['__unittest_'] = 12 |
|
258 |
nt.assert_equal |
|
|
258 | nt.assert_equal(ip.db['__unittest_'], 12) | |
|
259 | 259 | del ip.db['__unittest_'] |
|
260 | 260 | assert '__unittest_' not in ip.db |
@@ -1,764 +1,764 | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | """Tests for various magic functions. |
|
3 | 3 | |
|
4 | 4 | Needs to be run by nose (to make ipython session available). |
|
5 | 5 | """ |
|
6 | 6 | from __future__ import absolute_import |
|
7 | 7 | |
|
8 | 8 | #----------------------------------------------------------------------------- |
|
9 | 9 | # Imports |
|
10 | 10 | #----------------------------------------------------------------------------- |
|
11 | 11 | |
|
12 | 12 | import io |
|
13 | 13 | import os |
|
14 | 14 | import sys |
|
15 | 15 | from StringIO import StringIO |
|
16 | 16 | from unittest import TestCase |
|
17 | 17 | |
|
18 | 18 | try: |
|
19 | 19 | from importlib import invalidate_caches # Required from Python 3.3 |
|
20 | 20 | except ImportError: |
|
21 | 21 | def invalidate_caches(): |
|
22 | 22 | pass |
|
23 | 23 | |
|
24 | 24 | import nose.tools as nt |
|
25 | 25 | |
|
26 | 26 | from IPython.core import magic |
|
27 | 27 | from IPython.core.magic import (Magics, magics_class, line_magic, |
|
28 | 28 | cell_magic, line_cell_magic, |
|
29 | 29 | register_line_magic, register_cell_magic, |
|
30 | 30 | register_line_cell_magic) |
|
31 | 31 | from IPython.core.magics import execution, script |
|
32 | 32 | from IPython.nbformat.v3.tests.nbexamples import nb0 |
|
33 | 33 | from IPython.nbformat import current |
|
34 | 34 | from IPython.testing import decorators as dec |
|
35 | 35 | from IPython.testing import tools as tt |
|
36 | 36 | from IPython.utils import py3compat |
|
37 | 37 | from IPython.utils.tempdir import TemporaryDirectory |
|
38 | 38 | from IPython.utils.process import find_cmd |
|
39 | 39 | |
|
40 | 40 | #----------------------------------------------------------------------------- |
|
41 | 41 | # Test functions begin |
|
42 | 42 | #----------------------------------------------------------------------------- |
|
43 | 43 | |
|
44 | 44 | @magic.magics_class |
|
45 | 45 | class DummyMagics(magic.Magics): pass |
|
46 | 46 | |
|
47 | 47 | def test_rehashx(): |
|
48 | 48 | # clear up everything |
|
49 | 49 | _ip = get_ipython() |
|
50 | 50 | _ip.alias_manager.alias_table.clear() |
|
51 | 51 | del _ip.db['syscmdlist'] |
|
52 | 52 | |
|
53 | 53 | _ip.magic('rehashx') |
|
54 | 54 | # Practically ALL ipython development systems will have more than 10 aliases |
|
55 | 55 | |
|
56 | 56 | yield (nt.assert_true, len(_ip.alias_manager.alias_table) > 10) |
|
57 | 57 | for key, val in _ip.alias_manager.alias_table.iteritems(): |
|
58 | 58 | # we must strip dots from alias names |
|
59 | 59 | nt.assert_true('.' not in key) |
|
60 | 60 | |
|
61 | 61 | # rehashx must fill up syscmdlist |
|
62 | 62 | scoms = _ip.db['syscmdlist'] |
|
63 | 63 | yield (nt.assert_true, len(scoms) > 10) |
|
64 | 64 | |
|
65 | 65 | |
|
66 | 66 | def test_magic_parse_options(): |
|
67 | 67 | """Test that we don't mangle paths when parsing magic options.""" |
|
68 | 68 | ip = get_ipython() |
|
69 | 69 | path = 'c:\\x' |
|
70 | 70 | m = DummyMagics(ip) |
|
71 | 71 | opts = m.parse_options('-f %s' % path,'f:')[0] |
|
72 | 72 | # argv splitting is os-dependent |
|
73 | 73 | if os.name == 'posix': |
|
74 | 74 | expected = 'c:x' |
|
75 | 75 | else: |
|
76 | 76 | expected = path |
|
77 |
nt.assert_equal |
|
|
77 | nt.assert_equal(opts['f'], expected) | |
|
78 | 78 | |
|
79 | 79 | def test_magic_parse_long_options(): |
|
80 | 80 | """Magic.parse_options can handle --foo=bar long options""" |
|
81 | 81 | ip = get_ipython() |
|
82 | 82 | m = DummyMagics(ip) |
|
83 | 83 | opts, _ = m.parse_options('--foo --bar=bubble', 'a', 'foo', 'bar=') |
|
84 | 84 | nt.assert_true('foo' in opts) |
|
85 | 85 | nt.assert_true('bar' in opts) |
|
86 | 86 | nt.assert_true(opts['bar'], "bubble") |
|
87 | 87 | |
|
88 | 88 | |
|
89 | 89 | @dec.skip_without('sqlite3') |
|
90 | 90 | def doctest_hist_f(): |
|
91 | 91 | """Test %hist -f with temporary filename. |
|
92 | 92 | |
|
93 | 93 | In [9]: import tempfile |
|
94 | 94 | |
|
95 | 95 | In [10]: tfile = tempfile.mktemp('.py','tmp-ipython-') |
|
96 | 96 | |
|
97 | 97 | In [11]: %hist -nl -f $tfile 3 |
|
98 | 98 | |
|
99 | 99 | In [13]: import os; os.unlink(tfile) |
|
100 | 100 | """ |
|
101 | 101 | |
|
102 | 102 | |
|
103 | 103 | @dec.skip_without('sqlite3') |
|
104 | 104 | def doctest_hist_r(): |
|
105 | 105 | """Test %hist -r |
|
106 | 106 | |
|
107 | 107 | XXX - This test is not recording the output correctly. For some reason, in |
|
108 | 108 | testing mode the raw history isn't getting populated. No idea why. |
|
109 | 109 | Disabling the output checking for now, though at least we do run it. |
|
110 | 110 | |
|
111 | 111 | In [1]: 'hist' in _ip.lsmagic() |
|
112 | 112 | Out[1]: True |
|
113 | 113 | |
|
114 | 114 | In [2]: x=1 |
|
115 | 115 | |
|
116 | 116 | In [3]: %hist -rl 2 |
|
117 | 117 | x=1 # random |
|
118 | 118 | %hist -r 2 |
|
119 | 119 | """ |
|
120 | 120 | |
|
121 | 121 | |
|
122 | 122 | @dec.skip_without('sqlite3') |
|
123 | 123 | def doctest_hist_op(): |
|
124 | 124 | """Test %hist -op |
|
125 | 125 | |
|
126 | 126 | In [1]: class b(float): |
|
127 | 127 | ...: pass |
|
128 | 128 | ...: |
|
129 | 129 | |
|
130 | 130 | In [2]: class s(object): |
|
131 | 131 | ...: def __str__(self): |
|
132 | 132 | ...: return 's' |
|
133 | 133 | ...: |
|
134 | 134 | |
|
135 | 135 | In [3]: |
|
136 | 136 | |
|
137 | 137 | In [4]: class r(b): |
|
138 | 138 | ...: def __repr__(self): |
|
139 | 139 | ...: return 'r' |
|
140 | 140 | ...: |
|
141 | 141 | |
|
142 | 142 | In [5]: class sr(s,r): pass |
|
143 | 143 | ...: |
|
144 | 144 | |
|
145 | 145 | In [6]: |
|
146 | 146 | |
|
147 | 147 | In [7]: bb=b() |
|
148 | 148 | |
|
149 | 149 | In [8]: ss=s() |
|
150 | 150 | |
|
151 | 151 | In [9]: rr=r() |
|
152 | 152 | |
|
153 | 153 | In [10]: ssrr=sr() |
|
154 | 154 | |
|
155 | 155 | In [11]: 4.5 |
|
156 | 156 | Out[11]: 4.5 |
|
157 | 157 | |
|
158 | 158 | In [12]: str(ss) |
|
159 | 159 | Out[12]: 's' |
|
160 | 160 | |
|
161 | 161 | In [13]: |
|
162 | 162 | |
|
163 | 163 | In [14]: %hist -op |
|
164 | 164 | >>> class b: |
|
165 | 165 | ... pass |
|
166 | 166 | ... |
|
167 | 167 | >>> class s(b): |
|
168 | 168 | ... def __str__(self): |
|
169 | 169 | ... return 's' |
|
170 | 170 | ... |
|
171 | 171 | >>> |
|
172 | 172 | >>> class r(b): |
|
173 | 173 | ... def __repr__(self): |
|
174 | 174 | ... return 'r' |
|
175 | 175 | ... |
|
176 | 176 | >>> class sr(s,r): pass |
|
177 | 177 | >>> |
|
178 | 178 | >>> bb=b() |
|
179 | 179 | >>> ss=s() |
|
180 | 180 | >>> rr=r() |
|
181 | 181 | >>> ssrr=sr() |
|
182 | 182 | >>> 4.5 |
|
183 | 183 | 4.5 |
|
184 | 184 | >>> str(ss) |
|
185 | 185 | 's' |
|
186 | 186 | >>> |
|
187 | 187 | """ |
|
188 | 188 | |
|
189 | 189 | |
|
190 | 190 | @dec.skip_without('sqlite3') |
|
191 | 191 | def test_macro(): |
|
192 | 192 | ip = get_ipython() |
|
193 | 193 | ip.history_manager.reset() # Clear any existing history. |
|
194 | 194 | cmds = ["a=1", "def b():\n return a**2", "print(a,b())"] |
|
195 | 195 | for i, cmd in enumerate(cmds, start=1): |
|
196 | 196 | ip.history_manager.store_inputs(i, cmd) |
|
197 | 197 | ip.magic("macro test 1-3") |
|
198 | 198 | nt.assert_equal(ip.user_ns["test"].value, "\n".join(cmds)+"\n") |
|
199 | 199 | |
|
200 | 200 | # List macros. |
|
201 | 201 | assert "test" in ip.magic("macro") |
|
202 | 202 | |
|
203 | 203 | |
|
204 | 204 | @dec.skip_without('sqlite3') |
|
205 | 205 | def test_macro_run(): |
|
206 | 206 | """Test that we can run a multi-line macro successfully.""" |
|
207 | 207 | ip = get_ipython() |
|
208 | 208 | ip.history_manager.reset() |
|
209 | 209 | cmds = ["a=10", "a+=1", py3compat.doctest_refactor_print("print a"), |
|
210 | 210 | "%macro test 2-3"] |
|
211 | 211 | for cmd in cmds: |
|
212 | 212 | ip.run_cell(cmd, store_history=True) |
|
213 | 213 | nt.assert_equal(ip.user_ns["test"].value, |
|
214 | 214 | py3compat.doctest_refactor_print("a+=1\nprint a\n")) |
|
215 | 215 | with tt.AssertPrints("12"): |
|
216 | 216 | ip.run_cell("test") |
|
217 | 217 | with tt.AssertPrints("13"): |
|
218 | 218 | ip.run_cell("test") |
|
219 | 219 | |
|
220 | 220 | |
|
221 | 221 | @dec.skipif_not_numpy |
|
222 | 222 | def test_numpy_reset_array_undec(): |
|
223 | 223 | "Test '%reset array' functionality" |
|
224 | 224 | _ip.ex('import numpy as np') |
|
225 | 225 | _ip.ex('a = np.empty(2)') |
|
226 | 226 | yield (nt.assert_true, 'a' in _ip.user_ns) |
|
227 | 227 | _ip.magic('reset -f array') |
|
228 | 228 | yield (nt.assert_false, 'a' in _ip.user_ns) |
|
229 | 229 | |
|
230 | 230 | def test_reset_out(): |
|
231 | 231 | "Test '%reset out' magic" |
|
232 | 232 | _ip.run_cell("parrot = 'dead'", store_history=True) |
|
233 | 233 | # test '%reset -f out', make an Out prompt |
|
234 | 234 | _ip.run_cell("parrot", store_history=True) |
|
235 | 235 | nt.assert_true('dead' in [_ip.user_ns[x] for x in '_','__','___']) |
|
236 | 236 | _ip.magic('reset -f out') |
|
237 | 237 | nt.assert_false('dead' in [_ip.user_ns[x] for x in '_','__','___']) |
|
238 | 238 | nt.assert_true(len(_ip.user_ns['Out']) == 0) |
|
239 | 239 | |
|
240 | 240 | def test_reset_in(): |
|
241 | 241 | "Test '%reset in' magic" |
|
242 | 242 | # test '%reset -f in' |
|
243 | 243 | _ip.run_cell("parrot", store_history=True) |
|
244 | 244 | nt.assert_true('parrot' in [_ip.user_ns[x] for x in '_i','_ii','_iii']) |
|
245 | 245 | _ip.magic('%reset -f in') |
|
246 | 246 | nt.assert_false('parrot' in [_ip.user_ns[x] for x in '_i','_ii','_iii']) |
|
247 | 247 | nt.assert_true(len(set(_ip.user_ns['In'])) == 1) |
|
248 | 248 | |
|
249 | 249 | def test_reset_dhist(): |
|
250 | 250 | "Test '%reset dhist' magic" |
|
251 | 251 | _ip.run_cell("tmp = [d for d in _dh]") # copy before clearing |
|
252 | 252 | _ip.magic('cd ' + os.path.dirname(nt.__file__)) |
|
253 | 253 | _ip.magic('cd -') |
|
254 | 254 | nt.assert_true(len(_ip.user_ns['_dh']) > 0) |
|
255 | 255 | _ip.magic('reset -f dhist') |
|
256 | 256 | nt.assert_true(len(_ip.user_ns['_dh']) == 0) |
|
257 | 257 | _ip.run_cell("_dh = [d for d in tmp]") #restore |
|
258 | 258 | |
|
259 | 259 | def test_reset_in_length(): |
|
260 | 260 | "Test that '%reset in' preserves In[] length" |
|
261 | 261 | _ip.run_cell("print 'foo'") |
|
262 | 262 | _ip.run_cell("reset -f in") |
|
263 | 263 | nt.assert_true(len(_ip.user_ns['In']) == _ip.displayhook.prompt_count+1) |
|
264 | 264 | |
|
265 | 265 | def test_time(): |
|
266 | 266 | _ip.magic('time None') |
|
267 | 267 | |
|
268 | 268 | def test_tb_syntaxerror(): |
|
269 | 269 | """test %tb after a SyntaxError""" |
|
270 | 270 | ip = get_ipython() |
|
271 | 271 | ip.run_cell("for") |
|
272 | 272 | |
|
273 | 273 | # trap and validate stdout |
|
274 | 274 | save_stdout = sys.stdout |
|
275 | 275 | try: |
|
276 | 276 | sys.stdout = StringIO() |
|
277 | 277 | ip.run_cell("%tb") |
|
278 | 278 | out = sys.stdout.getvalue() |
|
279 | 279 | finally: |
|
280 | 280 | sys.stdout = save_stdout |
|
281 | 281 | # trim output, and only check the last line |
|
282 | 282 | last_line = out.rstrip().splitlines()[-1].strip() |
|
283 |
nt.assert_equal |
|
|
283 | nt.assert_equal(last_line, "SyntaxError: invalid syntax") | |
|
284 | 284 | |
|
285 | 285 | |
|
286 | 286 | @py3compat.doctest_refactor_print |
|
287 | 287 | def doctest_time(): |
|
288 | 288 | """ |
|
289 | 289 | In [10]: %time None |
|
290 | 290 | CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s |
|
291 | 291 | Wall time: 0.00 s |
|
292 | 292 | |
|
293 | 293 | In [11]: def f(kmjy): |
|
294 | 294 | ....: %time print 2*kmjy |
|
295 | 295 | |
|
296 | 296 | In [12]: f(3) |
|
297 | 297 | 6 |
|
298 | 298 | CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s |
|
299 | 299 | Wall time: 0.00 s |
|
300 | 300 | """ |
|
301 | 301 | |
|
302 | 302 | |
|
303 | 303 | def test_doctest_mode(): |
|
304 | 304 | "Toggle doctest_mode twice, it should be a no-op and run without error" |
|
305 | 305 | _ip.magic('doctest_mode') |
|
306 | 306 | _ip.magic('doctest_mode') |
|
307 | 307 | |
|
308 | 308 | |
|
309 | 309 | def test_parse_options(): |
|
310 | 310 | """Tests for basic options parsing in magics.""" |
|
311 | 311 | # These are only the most minimal of tests, more should be added later. At |
|
312 | 312 | # the very least we check that basic text/unicode calls work OK. |
|
313 | 313 | m = DummyMagics(_ip) |
|
314 | 314 | nt.assert_equal(m.parse_options('foo', '')[1], 'foo') |
|
315 | 315 | nt.assert_equal(m.parse_options(u'foo', '')[1], u'foo') |
|
316 | 316 | |
|
317 | 317 | |
|
318 | 318 | def test_dirops(): |
|
319 | 319 | """Test various directory handling operations.""" |
|
320 | 320 | # curpath = lambda :os.path.splitdrive(os.getcwdu())[1].replace('\\','/') |
|
321 | 321 | curpath = os.getcwdu |
|
322 | 322 | startdir = os.getcwdu() |
|
323 | 323 | ipdir = os.path.realpath(_ip.ipython_dir) |
|
324 | 324 | try: |
|
325 | 325 | _ip.magic('cd "%s"' % ipdir) |
|
326 | 326 | nt.assert_equal(curpath(), ipdir) |
|
327 | 327 | _ip.magic('cd -') |
|
328 | 328 | nt.assert_equal(curpath(), startdir) |
|
329 | 329 | _ip.magic('pushd "%s"' % ipdir) |
|
330 | 330 | nt.assert_equal(curpath(), ipdir) |
|
331 | 331 | _ip.magic('popd') |
|
332 | 332 | nt.assert_equal(curpath(), startdir) |
|
333 | 333 | finally: |
|
334 | 334 | os.chdir(startdir) |
|
335 | 335 | |
|
336 | 336 | |
|
337 | 337 | def test_xmode(): |
|
338 | 338 | # Calling xmode three times should be a no-op |
|
339 | 339 | xmode = _ip.InteractiveTB.mode |
|
340 | 340 | for i in range(3): |
|
341 | 341 | _ip.magic("xmode") |
|
342 | 342 | nt.assert_equal(_ip.InteractiveTB.mode, xmode) |
|
343 | 343 | |
|
344 | 344 | def test_reset_hard(): |
|
345 | 345 | monitor = [] |
|
346 | 346 | class A(object): |
|
347 | 347 | def __del__(self): |
|
348 | 348 | monitor.append(1) |
|
349 | 349 | def __repr__(self): |
|
350 | 350 | return "<A instance>" |
|
351 | 351 | |
|
352 | 352 | _ip.user_ns["a"] = A() |
|
353 | 353 | _ip.run_cell("a") |
|
354 | 354 | |
|
355 | 355 | nt.assert_equal(monitor, []) |
|
356 | 356 | _ip.magic("reset -f") |
|
357 | 357 | nt.assert_equal(monitor, [1]) |
|
358 | 358 | |
|
359 | 359 | class TestXdel(tt.TempFileMixin): |
|
360 | 360 | def test_xdel(self): |
|
361 | 361 | """Test that references from %run are cleared by xdel.""" |
|
362 | 362 | src = ("class A(object):\n" |
|
363 | 363 | " monitor = []\n" |
|
364 | 364 | " def __del__(self):\n" |
|
365 | 365 | " self.monitor.append(1)\n" |
|
366 | 366 | "a = A()\n") |
|
367 | 367 | self.mktmp(src) |
|
368 | 368 | # %run creates some hidden references... |
|
369 | 369 | _ip.magic("run %s" % self.fname) |
|
370 | 370 | # ... as does the displayhook. |
|
371 | 371 | _ip.run_cell("a") |
|
372 | 372 | |
|
373 | 373 | monitor = _ip.user_ns["A"].monitor |
|
374 | 374 | nt.assert_equal(monitor, []) |
|
375 | 375 | |
|
376 | 376 | _ip.magic("xdel a") |
|
377 | 377 | |
|
378 | 378 | # Check that a's __del__ method has been called. |
|
379 | 379 | nt.assert_equal(monitor, [1]) |
|
380 | 380 | |
|
381 | 381 | def doctest_who(): |
|
382 | 382 | """doctest for %who |
|
383 | 383 | |
|
384 | 384 | In [1]: %reset -f |
|
385 | 385 | |
|
386 | 386 | In [2]: alpha = 123 |
|
387 | 387 | |
|
388 | 388 | In [3]: beta = 'beta' |
|
389 | 389 | |
|
390 | 390 | In [4]: %who int |
|
391 | 391 | alpha |
|
392 | 392 | |
|
393 | 393 | In [5]: %who str |
|
394 | 394 | beta |
|
395 | 395 | |
|
396 | 396 | In [6]: %whos |
|
397 | 397 | Variable Type Data/Info |
|
398 | 398 | ---------------------------- |
|
399 | 399 | alpha int 123 |
|
400 | 400 | beta str beta |
|
401 | 401 | |
|
402 | 402 | In [7]: %who_ls |
|
403 | 403 | Out[7]: ['alpha', 'beta'] |
|
404 | 404 | """ |
|
405 | 405 | |
|
406 | 406 | def test_whos(): |
|
407 | 407 | """Check that whos is protected against objects where repr() fails.""" |
|
408 | 408 | class A(object): |
|
409 | 409 | def __repr__(self): |
|
410 | 410 | raise Exception() |
|
411 | 411 | _ip.user_ns['a'] = A() |
|
412 | 412 | _ip.magic("whos") |
|
413 | 413 | |
|
414 | 414 | @py3compat.u_format |
|
415 | 415 | def doctest_precision(): |
|
416 | 416 | """doctest for %precision |
|
417 | 417 | |
|
418 | 418 | In [1]: f = get_ipython().display_formatter.formatters['text/plain'] |
|
419 | 419 | |
|
420 | 420 | In [2]: %precision 5 |
|
421 | 421 | Out[2]: {u}'%.5f' |
|
422 | 422 | |
|
423 | 423 | In [3]: f.float_format |
|
424 | 424 | Out[3]: {u}'%.5f' |
|
425 | 425 | |
|
426 | 426 | In [4]: %precision %e |
|
427 | 427 | Out[4]: {u}'%e' |
|
428 | 428 | |
|
429 | 429 | In [5]: f(3.1415927) |
|
430 | 430 | Out[5]: {u}'3.141593e+00' |
|
431 | 431 | """ |
|
432 | 432 | |
|
433 | 433 | def test_psearch(): |
|
434 | 434 | with tt.AssertPrints("dict.fromkeys"): |
|
435 | 435 | _ip.run_cell("dict.fr*?") |
|
436 | 436 | |
|
437 | 437 | def test_timeit_shlex(): |
|
438 | 438 | """test shlex issues with timeit (#1109)""" |
|
439 | 439 | _ip.ex("def f(*a,**kw): pass") |
|
440 | 440 | _ip.magic('timeit -n1 "this is a bug".count(" ")') |
|
441 | 441 | _ip.magic('timeit -r1 -n1 f(" ", 1)') |
|
442 | 442 | _ip.magic('timeit -r1 -n1 f(" ", 1, " ", 2, " ")') |
|
443 | 443 | _ip.magic('timeit -r1 -n1 ("a " + "b")') |
|
444 | 444 | _ip.magic('timeit -r1 -n1 f("a " + "b")') |
|
445 | 445 | _ip.magic('timeit -r1 -n1 f("a " + "b ")') |
|
446 | 446 | |
|
447 | 447 | |
|
448 | 448 | def test_timeit_arguments(): |
|
449 | 449 | "Test valid timeit arguments, should not cause SyntaxError (GH #1269)" |
|
450 | 450 | _ip.magic("timeit ('#')") |
|
451 | 451 | |
|
452 | 452 | |
|
453 | 453 | def test_timeit_special_syntax(): |
|
454 | 454 | "Test %%timeit with IPython special syntax" |
|
455 | 455 | from IPython.core.magic import register_line_magic |
|
456 | 456 | |
|
457 | 457 | @register_line_magic |
|
458 | 458 | def lmagic(line): |
|
459 | 459 | ip = get_ipython() |
|
460 | 460 | ip.user_ns['lmagic_out'] = line |
|
461 | 461 | |
|
462 | 462 | # line mode test |
|
463 | 463 | _ip.run_line_magic('timeit', '-n1 -r1 %lmagic my line') |
|
464 | 464 | nt.assert_equal(_ip.user_ns['lmagic_out'], 'my line') |
|
465 | 465 | # cell mode test |
|
466 | 466 | _ip.run_cell_magic('timeit', '-n1 -r1', '%lmagic my line2') |
|
467 | 467 | nt.assert_equal(_ip.user_ns['lmagic_out'], 'my line2') |
|
468 | 468 | |
|
469 | 469 | |
|
470 | 470 | @dec.skipif(execution.profile is None) |
|
471 | 471 | def test_prun_quotes(): |
|
472 | 472 | "Test that prun does not clobber string escapes (GH #1302)" |
|
473 | 473 | _ip.magic(r"prun -q x = '\t'") |
|
474 | 474 | nt.assert_equal(_ip.user_ns['x'], '\t') |
|
475 | 475 | |
|
476 | 476 | def test_extension(): |
|
477 | 477 | tmpdir = TemporaryDirectory() |
|
478 | 478 | orig_ipython_dir = _ip.ipython_dir |
|
479 | 479 | try: |
|
480 | 480 | _ip.ipython_dir = tmpdir.name |
|
481 | 481 | nt.assert_raises(ImportError, _ip.magic, "load_ext daft_extension") |
|
482 | 482 | url = os.path.join(os.path.dirname(__file__), "daft_extension.py") |
|
483 | 483 | _ip.magic("install_ext %s" % url) |
|
484 | 484 | _ip.user_ns.pop('arq', None) |
|
485 | 485 | invalidate_caches() # Clear import caches |
|
486 | 486 | _ip.magic("load_ext daft_extension") |
|
487 | 487 | tt.assert_equal(_ip.user_ns['arq'], 185) |
|
488 | 488 | _ip.magic("unload_ext daft_extension") |
|
489 | 489 | assert 'arq' not in _ip.user_ns |
|
490 | 490 | finally: |
|
491 | 491 | _ip.ipython_dir = orig_ipython_dir |
|
492 | 492 | tmpdir.cleanup() |
|
493 | 493 | |
|
494 | 494 | def test_notebook_export_json(): |
|
495 | 495 | with TemporaryDirectory() as td: |
|
496 | 496 | outfile = os.path.join(td, "nb.ipynb") |
|
497 | 497 | _ip.ex(py3compat.u_format(u"u = {u}'hΓ©llo'")) |
|
498 | 498 | _ip.magic("notebook -e %s" % outfile) |
|
499 | 499 | |
|
500 | 500 | def test_notebook_export_py(): |
|
501 | 501 | with TemporaryDirectory() as td: |
|
502 | 502 | outfile = os.path.join(td, "nb.py") |
|
503 | 503 | _ip.ex(py3compat.u_format(u"u = {u}'hΓ©llo'")) |
|
504 | 504 | _ip.magic("notebook -e %s" % outfile) |
|
505 | 505 | |
|
506 | 506 | def test_notebook_reformat_py(): |
|
507 | 507 | with TemporaryDirectory() as td: |
|
508 | 508 | infile = os.path.join(td, "nb.ipynb") |
|
509 | 509 | with io.open(infile, 'w', encoding='utf-8') as f: |
|
510 | 510 | current.write(nb0, f, 'json') |
|
511 | 511 | |
|
512 | 512 | _ip.ex(py3compat.u_format(u"u = {u}'hΓ©llo'")) |
|
513 | 513 | _ip.magic("notebook -f py %s" % infile) |
|
514 | 514 | |
|
515 | 515 | def test_notebook_reformat_json(): |
|
516 | 516 | with TemporaryDirectory() as td: |
|
517 | 517 | infile = os.path.join(td, "nb.py") |
|
518 | 518 | with io.open(infile, 'w', encoding='utf-8') as f: |
|
519 | 519 | current.write(nb0, f, 'py') |
|
520 | 520 | |
|
521 | 521 | _ip.ex(py3compat.u_format(u"u = {u}'hΓ©llo'")) |
|
522 | 522 | _ip.magic("notebook -f ipynb %s" % infile) |
|
523 | 523 | _ip.magic("notebook -f json %s" % infile) |
|
524 | 524 | |
|
525 | 525 | def test_env(): |
|
526 | 526 | env = _ip.magic("env") |
|
527 | 527 | assert isinstance(env, dict), type(env) |
|
528 | 528 | |
|
529 | 529 | |
|
530 | 530 | class CellMagicTestCase(TestCase): |
|
531 | 531 | |
|
532 | 532 | def check_ident(self, magic): |
|
533 | 533 | # Manually called, we get the result |
|
534 | 534 | out = _ip.run_cell_magic(magic, 'a', 'b') |
|
535 |
nt.assert_equal |
|
|
535 | nt.assert_equal(out, ('a','b')) | |
|
536 | 536 | # Via run_cell, it goes into the user's namespace via displayhook |
|
537 | 537 | _ip.run_cell('%%' + magic +' c\nd') |
|
538 |
nt.assert_equal |
|
|
538 | nt.assert_equal(_ip.user_ns['_'], ('c','d')) | |
|
539 | 539 | |
|
540 | 540 | def test_cell_magic_func_deco(self): |
|
541 | 541 | "Cell magic using simple decorator" |
|
542 | 542 | @register_cell_magic |
|
543 | 543 | def cellm(line, cell): |
|
544 | 544 | return line, cell |
|
545 | 545 | |
|
546 | 546 | self.check_ident('cellm') |
|
547 | 547 | |
|
548 | 548 | def test_cell_magic_reg(self): |
|
549 | 549 | "Cell magic manually registered" |
|
550 | 550 | def cellm(line, cell): |
|
551 | 551 | return line, cell |
|
552 | 552 | |
|
553 | 553 | _ip.register_magic_function(cellm, 'cell', 'cellm2') |
|
554 | 554 | self.check_ident('cellm2') |
|
555 | 555 | |
|
556 | 556 | def test_cell_magic_class(self): |
|
557 | 557 | "Cell magics declared via a class" |
|
558 | 558 | @magics_class |
|
559 | 559 | class MyMagics(Magics): |
|
560 | 560 | |
|
561 | 561 | @cell_magic |
|
562 | 562 | def cellm3(self, line, cell): |
|
563 | 563 | return line, cell |
|
564 | 564 | |
|
565 | 565 | _ip.register_magics(MyMagics) |
|
566 | 566 | self.check_ident('cellm3') |
|
567 | 567 | |
|
568 | 568 | def test_cell_magic_class2(self): |
|
569 | 569 | "Cell magics declared via a class, #2" |
|
570 | 570 | @magics_class |
|
571 | 571 | class MyMagics2(Magics): |
|
572 | 572 | |
|
573 | 573 | @cell_magic('cellm4') |
|
574 | 574 | def cellm33(self, line, cell): |
|
575 | 575 | return line, cell |
|
576 | 576 | |
|
577 | 577 | _ip.register_magics(MyMagics2) |
|
578 | 578 | self.check_ident('cellm4') |
|
579 | 579 | # Check that nothing is registered as 'cellm33' |
|
580 | 580 | c33 = _ip.find_cell_magic('cellm33') |
|
581 |
nt.assert_equal |
|
|
581 | nt.assert_equal(c33, None) | |
|
582 | 582 | |
|
583 | 583 | def test_file(): |
|
584 | 584 | """Basic %%file""" |
|
585 | 585 | ip = get_ipython() |
|
586 | 586 | with TemporaryDirectory() as td: |
|
587 | 587 | fname = os.path.join(td, 'file1') |
|
588 | 588 | ip.run_cell_magic("file", fname, u'\n'.join([ |
|
589 | 589 | 'line1', |
|
590 | 590 | 'line2', |
|
591 | 591 | ])) |
|
592 | 592 | with open(fname) as f: |
|
593 | 593 | s = f.read() |
|
594 | 594 | nt.assert_in('line1\n', s) |
|
595 | 595 | nt.assert_in('line2', s) |
|
596 | 596 | |
|
597 | 597 | def test_file_unicode(): |
|
598 | 598 | """%%file with unicode cell""" |
|
599 | 599 | ip = get_ipython() |
|
600 | 600 | with TemporaryDirectory() as td: |
|
601 | 601 | fname = os.path.join(td, 'file1') |
|
602 | 602 | ip.run_cell_magic("file", fname, u'\n'.join([ |
|
603 | 603 | u'linΓ©1', |
|
604 | 604 | u'linΓ©2', |
|
605 | 605 | ])) |
|
606 | 606 | with io.open(fname, encoding='utf-8') as f: |
|
607 | 607 | s = f.read() |
|
608 | 608 | nt.assert_in(u'linΓ©1\n', s) |
|
609 | 609 | nt.assert_in(u'linΓ©2', s) |
|
610 | 610 | |
|
611 | 611 | def test_file_amend(): |
|
612 | 612 | """%%file -a amends files""" |
|
613 | 613 | ip = get_ipython() |
|
614 | 614 | with TemporaryDirectory() as td: |
|
615 | 615 | fname = os.path.join(td, 'file2') |
|
616 | 616 | ip.run_cell_magic("file", fname, u'\n'.join([ |
|
617 | 617 | 'line1', |
|
618 | 618 | 'line2', |
|
619 | 619 | ])) |
|
620 | 620 | ip.run_cell_magic("file", "-a %s" % fname, u'\n'.join([ |
|
621 | 621 | 'line3', |
|
622 | 622 | 'line4', |
|
623 | 623 | ])) |
|
624 | 624 | with open(fname) as f: |
|
625 | 625 | s = f.read() |
|
626 | 626 | nt.assert_in('line1\n', s) |
|
627 | 627 | nt.assert_in('line3\n', s) |
|
628 | 628 | |
|
629 | 629 | |
|
630 | 630 | def test_script_config(): |
|
631 | 631 | ip = get_ipython() |
|
632 | 632 | ip.config.ScriptMagics.script_magics = ['whoda'] |
|
633 | 633 | sm = script.ScriptMagics(shell=ip) |
|
634 | 634 | nt.assert_in('whoda', sm.magics['cell']) |
|
635 | 635 | |
|
636 | 636 | @dec.skip_win32 |
|
637 | 637 | def test_script_out(): |
|
638 | 638 | ip = get_ipython() |
|
639 | 639 | ip.run_cell_magic("script", "--out output sh", "echo 'hi'") |
|
640 |
nt.assert_equal |
|
|
640 | nt.assert_equal(ip.user_ns['output'], 'hi\n') | |
|
641 | 641 | |
|
642 | 642 | @dec.skip_win32 |
|
643 | 643 | def test_script_err(): |
|
644 | 644 | ip = get_ipython() |
|
645 | 645 | ip.run_cell_magic("script", "--err error sh", "echo 'hello' >&2") |
|
646 |
nt.assert_equal |
|
|
646 | nt.assert_equal(ip.user_ns['error'], 'hello\n') | |
|
647 | 647 | |
|
648 | 648 | @dec.skip_win32 |
|
649 | 649 | def test_script_out_err(): |
|
650 | 650 | ip = get_ipython() |
|
651 | 651 | ip.run_cell_magic("script", "--out output --err error sh", "echo 'hi'\necho 'hello' >&2") |
|
652 |
nt.assert_equal |
|
|
653 |
nt.assert_equal |
|
|
652 | nt.assert_equal(ip.user_ns['output'], 'hi\n') | |
|
653 | nt.assert_equal(ip.user_ns['error'], 'hello\n') | |
|
654 | 654 | |
|
655 | 655 | @dec.skip_win32 |
|
656 | 656 | def test_script_bg_out(): |
|
657 | 657 | ip = get_ipython() |
|
658 | 658 | ip.run_cell_magic("script", "--bg --out output sh", "echo 'hi'") |
|
659 |
nt.assert_equal |
|
|
659 | nt.assert_equal(ip.user_ns['output'].read(), b'hi\n') | |
|
660 | 660 | |
|
661 | 661 | @dec.skip_win32 |
|
662 | 662 | def test_script_bg_err(): |
|
663 | 663 | ip = get_ipython() |
|
664 | 664 | ip.run_cell_magic("script", "--bg --err error sh", "echo 'hello' >&2") |
|
665 |
nt.assert_equal |
|
|
665 | nt.assert_equal(ip.user_ns['error'].read(), b'hello\n') | |
|
666 | 666 | |
|
667 | 667 | @dec.skip_win32 |
|
668 | 668 | def test_script_bg_out_err(): |
|
669 | 669 | ip = get_ipython() |
|
670 | 670 | ip.run_cell_magic("script", "--bg --out output --err error sh", "echo 'hi'\necho 'hello' >&2") |
|
671 |
nt.assert_equal |
|
|
672 |
nt.assert_equal |
|
|
671 | nt.assert_equal(ip.user_ns['output'].read(), b'hi\n') | |
|
672 | nt.assert_equal(ip.user_ns['error'].read(), b'hello\n') | |
|
673 | 673 | |
|
674 | 674 | def test_script_defaults(): |
|
675 | 675 | ip = get_ipython() |
|
676 | 676 | for cmd in ['sh', 'bash', 'perl', 'ruby']: |
|
677 | 677 | try: |
|
678 | 678 | find_cmd(cmd) |
|
679 | 679 | except Exception: |
|
680 | 680 | pass |
|
681 | 681 | else: |
|
682 | 682 | nt.assert_in(cmd, ip.magics_manager.magics['cell']) |
|
683 | 683 | |
|
684 | 684 | |
|
685 | 685 | @magics_class |
|
686 | 686 | class FooFoo(Magics): |
|
687 | 687 | """class with both %foo and %%foo magics""" |
|
688 | 688 | @line_magic('foo') |
|
689 | 689 | def line_foo(self, line): |
|
690 | 690 | "I am line foo" |
|
691 | 691 | pass |
|
692 | 692 | |
|
693 | 693 | @cell_magic("foo") |
|
694 | 694 | def cell_foo(self, line, cell): |
|
695 | 695 | "I am cell foo, not line foo" |
|
696 | 696 | pass |
|
697 | 697 | |
|
698 | 698 | def test_line_cell_info(): |
|
699 | 699 | """%%foo and %foo magics are distinguishable to inspect""" |
|
700 | 700 | ip = get_ipython() |
|
701 | 701 | ip.magics_manager.register(FooFoo) |
|
702 | 702 | oinfo = ip.object_inspect('foo') |
|
703 | 703 | nt.assert_true(oinfo['found']) |
|
704 | 704 | nt.assert_true(oinfo['ismagic']) |
|
705 | 705 | |
|
706 | 706 | oinfo = ip.object_inspect('%%foo') |
|
707 | 707 | nt.assert_true(oinfo['found']) |
|
708 | 708 | nt.assert_true(oinfo['ismagic']) |
|
709 |
nt.assert_equal |
|
|
709 | nt.assert_equal(oinfo['docstring'], FooFoo.cell_foo.__doc__) | |
|
710 | 710 | |
|
711 | 711 | oinfo = ip.object_inspect('%foo') |
|
712 | 712 | nt.assert_true(oinfo['found']) |
|
713 | 713 | nt.assert_true(oinfo['ismagic']) |
|
714 |
nt.assert_equal |
|
|
714 | nt.assert_equal(oinfo['docstring'], FooFoo.line_foo.__doc__) | |
|
715 | 715 | |
|
716 | 716 | def test_multiple_magics(): |
|
717 | 717 | ip = get_ipython() |
|
718 | 718 | foo1 = FooFoo(ip) |
|
719 | 719 | foo2 = FooFoo(ip) |
|
720 | 720 | mm = ip.magics_manager |
|
721 | 721 | mm.register(foo1) |
|
722 | 722 | nt.assert_true(mm.magics['line']['foo'].im_self is foo1) |
|
723 | 723 | mm.register(foo2) |
|
724 | 724 | nt.assert_true(mm.magics['line']['foo'].im_self is foo2) |
|
725 | 725 | |
|
726 | 726 | def test_alias_magic(): |
|
727 | 727 | """Test %alias_magic.""" |
|
728 | 728 | ip = get_ipython() |
|
729 | 729 | mm = ip.magics_manager |
|
730 | 730 | |
|
731 | 731 | # Basic operation: both cell and line magics are created, if possible. |
|
732 | 732 | ip.run_line_magic('alias_magic', 'timeit_alias timeit') |
|
733 | 733 | nt.assert_true('timeit_alias' in mm.magics['line']) |
|
734 | 734 | nt.assert_true('timeit_alias' in mm.magics['cell']) |
|
735 | 735 | |
|
736 | 736 | # --cell is specified, line magic not created. |
|
737 | 737 | ip.run_line_magic('alias_magic', '--cell timeit_cell_alias timeit') |
|
738 | 738 | nt.assert_false('timeit_cell_alias' in mm.magics['line']) |
|
739 | 739 | nt.assert_true('timeit_cell_alias' in mm.magics['cell']) |
|
740 | 740 | |
|
741 | 741 | # Test that line alias is created successfully. |
|
742 | 742 | ip.run_line_magic('alias_magic', '--line env_alias env') |
|
743 | 743 | nt.assert_equal(ip.run_line_magic('env', ''), |
|
744 | 744 | ip.run_line_magic('env_alias', '')) |
|
745 | 745 | |
|
746 | 746 | def test_save(): |
|
747 | 747 | """Test %save.""" |
|
748 | 748 | ip = get_ipython() |
|
749 | 749 | ip.history_manager.reset() # Clear any existing history. |
|
750 | 750 | cmds = [u"a=1", u"def b():\n return a**2", u"print(a, b())"] |
|
751 | 751 | for i, cmd in enumerate(cmds, start=1): |
|
752 | 752 | ip.history_manager.store_inputs(i, cmd) |
|
753 | 753 | with TemporaryDirectory() as tmpdir: |
|
754 | 754 | file = os.path.join(tmpdir, "testsave.py") |
|
755 | 755 | ip.run_line_magic("save", "%s 1-10" % file) |
|
756 | 756 | with open(file) as f: |
|
757 | 757 | content = f.read() |
|
758 | 758 | nt.assert_equal(content.count(cmds[0]), 1) |
|
759 | 759 | nt.assert_true('coding: utf-8' in content) |
|
760 | 760 | ip.run_line_magic("save", "-a %s 1-10" % file) |
|
761 | 761 | with open(file) as f: |
|
762 | 762 | content = f.read() |
|
763 | 763 | nt.assert_equal(content.count(cmds[0]), 2) |
|
764 | 764 | nt.assert_true('coding: utf-8' in content) |
@@ -1,204 +1,204 | |||
|
1 | 1 | """Tests for various magic functions specific to the terminal frontend. |
|
2 | 2 | |
|
3 | 3 | Needs to be run by nose (to make ipython session available). |
|
4 | 4 | """ |
|
5 | 5 | from __future__ import absolute_import |
|
6 | 6 | |
|
7 | 7 | #----------------------------------------------------------------------------- |
|
8 | 8 | # Imports |
|
9 | 9 | #----------------------------------------------------------------------------- |
|
10 | 10 | |
|
11 | 11 | import sys |
|
12 | 12 | from StringIO import StringIO |
|
13 | 13 | from unittest import TestCase |
|
14 | 14 | |
|
15 | 15 | import nose.tools as nt |
|
16 | 16 | |
|
17 | 17 | from IPython.testing import tools as tt |
|
18 | 18 | |
|
19 | 19 | #----------------------------------------------------------------------------- |
|
20 | 20 | # Globals |
|
21 | 21 | #----------------------------------------------------------------------------- |
|
22 | 22 | ip = get_ipython() |
|
23 | 23 | |
|
24 | 24 | #----------------------------------------------------------------------------- |
|
25 | 25 | # Test functions begin |
|
26 | 26 | #----------------------------------------------------------------------------- |
|
27 | 27 | |
|
28 | 28 | def check_cpaste(code, should_fail=False): |
|
29 | 29 | """Execute code via 'cpaste' and ensure it was executed, unless |
|
30 | 30 | should_fail is set. |
|
31 | 31 | """ |
|
32 | 32 | ip.user_ns['code_ran'] = False |
|
33 | 33 | |
|
34 | 34 | src = StringIO() |
|
35 | 35 | if not hasattr(src, 'encoding'): |
|
36 | 36 | # IPython expects stdin to have an encoding attribute |
|
37 | 37 | src.encoding = None |
|
38 | 38 | src.write(code) |
|
39 | 39 | src.write('\n--\n') |
|
40 | 40 | src.seek(0) |
|
41 | 41 | |
|
42 | 42 | stdin_save = sys.stdin |
|
43 | 43 | sys.stdin = src |
|
44 | 44 | |
|
45 | 45 | try: |
|
46 | 46 | context = tt.AssertPrints if should_fail else tt.AssertNotPrints |
|
47 | 47 | with context("Traceback (most recent call last)"): |
|
48 | 48 | ip.magic('cpaste') |
|
49 | 49 | |
|
50 | 50 | if not should_fail: |
|
51 | 51 | assert ip.user_ns['code_ran'] |
|
52 | 52 | finally: |
|
53 | 53 | sys.stdin = stdin_save |
|
54 | 54 | |
|
55 | 55 | PY31 = sys.version_info[:2] == (3,1) |
|
56 | 56 | |
|
57 | 57 | def test_cpaste(): |
|
58 | 58 | """Test cpaste magic""" |
|
59 | 59 | |
|
60 | 60 | def runf(): |
|
61 | 61 | """Marker function: sets a flag when executed. |
|
62 | 62 | """ |
|
63 | 63 | ip.user_ns['code_ran'] = True |
|
64 | 64 | return 'runf' # return string so '+ runf()' doesn't result in success |
|
65 | 65 | |
|
66 | 66 | tests = {'pass': ["runf()", |
|
67 | 67 | "In [1]: runf()", |
|
68 | 68 | "In [1]: if 1:\n ...: runf()", |
|
69 | 69 | "> > > runf()", |
|
70 | 70 | ">>> runf()", |
|
71 | 71 | " >>> runf()", |
|
72 | 72 | ], |
|
73 | 73 | |
|
74 | 74 | 'fail': ["1 + runf()", |
|
75 | 75 | ]} |
|
76 | 76 | |
|
77 | 77 | # I don't know why this is failing specifically on Python 3.1. I've |
|
78 | 78 | # checked it manually interactively, but we don't care enough about 3.1 |
|
79 | 79 | # to spend time fiddling with the tests, so we just skip it. |
|
80 | 80 | if not PY31: |
|
81 | 81 | tests['fail'].append("++ runf()") |
|
82 | 82 | |
|
83 | 83 | ip.user_ns['runf'] = runf |
|
84 | 84 | |
|
85 | 85 | for code in tests['pass']: |
|
86 | 86 | check_cpaste(code) |
|
87 | 87 | |
|
88 | 88 | for code in tests['fail']: |
|
89 | 89 | check_cpaste(code, should_fail=True) |
|
90 | 90 | |
|
91 | 91 | |
|
92 | 92 | class PasteTestCase(TestCase): |
|
93 | 93 | """Multiple tests for clipboard pasting""" |
|
94 | 94 | |
|
95 | 95 | def paste(self, txt, flags='-q'): |
|
96 | 96 | """Paste input text, by default in quiet mode""" |
|
97 | 97 | ip.hooks.clipboard_get = lambda : txt |
|
98 | 98 | ip.magic('paste '+flags) |
|
99 | 99 | |
|
100 | 100 | def setUp(self): |
|
101 | 101 | # Inject fake clipboard hook but save original so we can restore it later |
|
102 | 102 | self.original_clip = ip.hooks.clipboard_get |
|
103 | 103 | |
|
104 | 104 | def tearDown(self): |
|
105 | 105 | # Restore original hook |
|
106 | 106 | ip.hooks.clipboard_get = self.original_clip |
|
107 | 107 | |
|
108 | 108 | def test_paste(self): |
|
109 | 109 | ip.user_ns.pop('x', None) |
|
110 | 110 | self.paste('x = 1') |
|
111 | 111 | nt.assert_equal(ip.user_ns['x'], 1) |
|
112 | 112 | ip.user_ns.pop('x') |
|
113 | 113 | |
|
114 | 114 | def test_paste_pyprompt(self): |
|
115 | 115 | ip.user_ns.pop('x', None) |
|
116 | 116 | self.paste('>>> x=2') |
|
117 | 117 | nt.assert_equal(ip.user_ns['x'], 2) |
|
118 | 118 | ip.user_ns.pop('x') |
|
119 | 119 | |
|
120 | 120 | def test_paste_py_multi(self): |
|
121 | 121 | self.paste(""" |
|
122 | 122 | >>> x = [1,2,3] |
|
123 | 123 | >>> y = [] |
|
124 | 124 | >>> for i in x: |
|
125 | 125 | ... y.append(i**2) |
|
126 | 126 | ... |
|
127 | 127 | """) |
|
128 | 128 | nt.assert_equal(ip.user_ns['x'], [1,2,3]) |
|
129 | 129 | nt.assert_equal(ip.user_ns['y'], [1,4,9]) |
|
130 | 130 | |
|
131 | 131 | def test_paste_py_multi_r(self): |
|
132 | 132 | "Now, test that self.paste -r works" |
|
133 | 133 | nt.assert_equal(ip.user_ns.pop('x'), [1,2,3]) |
|
134 | 134 | nt.assert_equal(ip.user_ns.pop('y'), [1,4,9]) |
|
135 | 135 | nt.assert_false('x' in ip.user_ns) |
|
136 | 136 | ip.magic('paste -r') |
|
137 | 137 | nt.assert_equal(ip.user_ns['x'], [1,2,3]) |
|
138 | 138 | nt.assert_equal(ip.user_ns['y'], [1,4,9]) |
|
139 | 139 | |
|
140 | 140 | def test_paste_email(self): |
|
141 | 141 | "Test pasting of email-quoted contents" |
|
142 | 142 | self.paste("""\ |
|
143 | 143 | >> def foo(x): |
|
144 | 144 | >> return x + 1 |
|
145 | 145 | >> xx = foo(1.1)""") |
|
146 | 146 | nt.assert_equal(ip.user_ns['xx'], 2.1) |
|
147 | 147 | |
|
148 | 148 | def test_paste_email2(self): |
|
149 | 149 | "Email again; some programs add a space also at each quoting level" |
|
150 | 150 | self.paste("""\ |
|
151 | 151 | > > def foo(x): |
|
152 | 152 | > > return x + 1 |
|
153 | 153 | > > yy = foo(2.1) """) |
|
154 | 154 | nt.assert_equal(ip.user_ns['yy'], 3.1) |
|
155 | 155 | |
|
156 | 156 | def test_paste_email_py(self): |
|
157 | 157 | "Email quoting of interactive input" |
|
158 | 158 | self.paste("""\ |
|
159 | 159 | >> >>> def f(x): |
|
160 | 160 | >> ... return x+1 |
|
161 | 161 | >> ... |
|
162 | 162 | >> >>> zz = f(2.5) """) |
|
163 | 163 | nt.assert_equal(ip.user_ns['zz'], 3.5) |
|
164 | 164 | |
|
165 | 165 | def test_paste_echo(self): |
|
166 | 166 | "Also test self.paste echoing, by temporarily faking the writer" |
|
167 | 167 | w = StringIO() |
|
168 | 168 | writer = ip.write |
|
169 | 169 | ip.write = w.write |
|
170 | 170 | code = """ |
|
171 | 171 | a = 100 |
|
172 | 172 | b = 200""" |
|
173 | 173 | try: |
|
174 | 174 | self.paste(code,'') |
|
175 | 175 | out = w.getvalue() |
|
176 | 176 | finally: |
|
177 | 177 | ip.write = writer |
|
178 | 178 | nt.assert_equal(ip.user_ns['a'], 100) |
|
179 | 179 | nt.assert_equal(ip.user_ns['b'], 200) |
|
180 | 180 | nt.assert_equal(out, code+"\n## -- End pasted text --\n") |
|
181 | 181 | |
|
182 | 182 | def test_paste_leading_commas(self): |
|
183 | 183 | "Test multiline strings with leading commas" |
|
184 | 184 | tm = ip.magics_manager.registry['TerminalMagics'] |
|
185 | 185 | s = '''\ |
|
186 | 186 | a = """ |
|
187 | 187 | ,1,2,3 |
|
188 | 188 | """''' |
|
189 | 189 | ip.user_ns.pop('foo', None) |
|
190 | 190 | tm.store_or_execute(s, 'foo') |
|
191 | 191 | nt.assert_in('foo', ip.user_ns) |
|
192 | 192 | |
|
193 | 193 | |
|
194 | 194 | def test_paste_trailing_question(self): |
|
195 | 195 | "Test pasting sources with trailing question marks" |
|
196 | 196 | tm = ip.magics_manager.registry['TerminalMagics'] |
|
197 | 197 | s = '''\ |
|
198 | 198 | def funcfoo(): |
|
199 | 199 | if True: #am i true? |
|
200 | 200 | return 'fooresult' |
|
201 | 201 | ''' |
|
202 | 202 | ip.user_ns.pop('funcfoo', None) |
|
203 | 203 | self.paste(s) |
|
204 |
nt.assert_equal |
|
|
204 | nt.assert_equal(ip.user_ns['funcfoo'](), 'fooresult') |
@@ -1,110 +1,110 | |||
|
1 | 1 | """Tests for input manipulation machinery.""" |
|
2 | 2 | |
|
3 | 3 | #----------------------------------------------------------------------------- |
|
4 | 4 | # Imports |
|
5 | 5 | #----------------------------------------------------------------------------- |
|
6 | 6 | import nose.tools as nt |
|
7 | 7 | |
|
8 | 8 | from IPython.core.prefilter import AutocallChecker |
|
9 | 9 | from IPython.testing import tools as tt, decorators as dec |
|
10 | 10 | from IPython.testing.globalipapp import get_ipython |
|
11 | 11 | |
|
12 | 12 | #----------------------------------------------------------------------------- |
|
13 | 13 | # Tests |
|
14 | 14 | #----------------------------------------------------------------------------- |
|
15 | 15 | ip = get_ipython() |
|
16 | 16 | |
|
17 | 17 | @dec.parametric |
|
18 | 18 | def test_prefilter(): |
|
19 | 19 | """Test user input conversions""" |
|
20 | 20 | |
|
21 | 21 | # pairs of (raw, expected correct) input |
|
22 | 22 | pairs = [ ('2+2','2+2'), |
|
23 | 23 | ('>>> 2+2','2+2'), |
|
24 | 24 | ('>>> # This is a comment\n' |
|
25 | 25 | '... 2+2', |
|
26 | 26 | '# This is a comment\n' |
|
27 | 27 | '2+2'), |
|
28 | 28 | # Some IPython input |
|
29 | 29 | ('In [1]: 1', '1'), |
|
30 | 30 | ('In [2]: for i in range(5):\n' |
|
31 | 31 | ' ...: print i,', |
|
32 | 32 | 'for i in range(5):\n' |
|
33 | 33 | ' print i,'), |
|
34 | 34 | ] |
|
35 | 35 | |
|
36 | 36 | for raw, correct in pairs: |
|
37 |
yield nt.assert_equal |
|
|
37 | yield nt.assert_equal(ip.prefilter(raw), correct) | |
|
38 | 38 | |
|
39 | 39 | |
|
40 | 40 | @dec.parametric |
|
41 | 41 | def test_autocall_binops(): |
|
42 | 42 | """See https://github.com/ipython/ipython/issues/81""" |
|
43 | 43 | ip.magic('autocall 2') |
|
44 | 44 | f = lambda x: x |
|
45 | 45 | ip.user_ns['f'] = f |
|
46 | 46 | try: |
|
47 |
yield nt.assert_equal |
|
|
47 | yield nt.assert_equal(ip.prefilter('f 1'),'f(1)') | |
|
48 | 48 | for t in ['f +1', 'f -1']: |
|
49 |
yield nt.assert_equal |
|
|
49 | yield nt.assert_equal(ip.prefilter(t), t) | |
|
50 | 50 | |
|
51 | 51 | # Run tests again with a more permissive exclude_regexp, which will |
|
52 | 52 | # allow transformation of binary operations ('f -1' -> 'f(-1)'). |
|
53 | 53 | pm = ip.prefilter_manager |
|
54 | 54 | ac = AutocallChecker(shell=pm.shell, prefilter_manager=pm, |
|
55 | 55 | config=pm.config) |
|
56 | 56 | try: |
|
57 | 57 | ac.priority = 1 |
|
58 | 58 | ac.exclude_regexp = r'^[,&^\|\*/]|^is |^not |^in |^and |^or ' |
|
59 | 59 | pm.sort_checkers() |
|
60 | 60 | |
|
61 |
yield nt.assert_equal |
|
|
62 |
yield nt.assert_equal |
|
|
61 | yield nt.assert_equal(ip.prefilter('f -1'), 'f(-1)') | |
|
62 | yield nt.assert_equal(ip.prefilter('f +1'), 'f(+1)') | |
|
63 | 63 | finally: |
|
64 | 64 | pm.unregister_checker(ac) |
|
65 | 65 | finally: |
|
66 | 66 | ip.magic('autocall 0') |
|
67 | 67 | del ip.user_ns['f'] |
|
68 | 68 | |
|
69 | 69 | |
|
70 | 70 | @dec.parametric |
|
71 | 71 | def test_issue_114(): |
|
72 | 72 | """Check that multiline string literals don't expand as magic |
|
73 | 73 | see http://github.com/ipython/ipython/issues/114""" |
|
74 | 74 | |
|
75 | 75 | template = '"""\n%s\n"""' |
|
76 | 76 | # Store the current value of multi_line_specials and turn it off before |
|
77 | 77 | # running test, since it could be true (case in which the test doesn't make |
|
78 | 78 | # sense, as multiline string literals *will* expand as magic in that case). |
|
79 | 79 | msp = ip.prefilter_manager.multi_line_specials |
|
80 | 80 | ip.prefilter_manager.multi_line_specials = False |
|
81 | 81 | try: |
|
82 | 82 | for mgk in ip.magics_manager.lsmagic()['line']: |
|
83 | 83 | raw = template % mgk |
|
84 |
yield nt.assert_equal |
|
|
84 | yield nt.assert_equal(ip.prefilter(raw), raw) | |
|
85 | 85 | finally: |
|
86 | 86 | ip.prefilter_manager.multi_line_specials = msp |
|
87 | 87 | |
|
88 | 88 | |
|
89 | 89 | def test_prefilter_attribute_errors(): |
|
90 | 90 | """Capture exceptions thrown by user objects on attribute access. |
|
91 | 91 | |
|
92 | 92 | See http://github.com/ipython/ipython/issues/988.""" |
|
93 | 93 | |
|
94 | 94 | class X(object): |
|
95 | 95 | def __getattr__(self, k): |
|
96 | 96 | raise ValueError('broken object') |
|
97 | 97 | def __call__(self, x): |
|
98 | 98 | return x |
|
99 | 99 | |
|
100 | 100 | # Create a callable broken object |
|
101 | 101 | ip.user_ns['x'] = X() |
|
102 | 102 | ip.magic('autocall 2') |
|
103 | 103 | try: |
|
104 | 104 | # Even if x throws an attribute error when looking at its rewrite |
|
105 | 105 | # attribute, we should not crash. So the test here is simply making |
|
106 | 106 | # the prefilter call and not having an exception. |
|
107 | 107 | ip.prefilter('x 1') |
|
108 | 108 | finally: |
|
109 | 109 | del ip.user_ns['x'] |
|
110 | 110 | ip.magic('autocall 0') |
@@ -1,151 +1,151 | |||
|
1 | 1 | # coding: utf-8 |
|
2 | 2 | """Tests for profile-related functions. |
|
3 | 3 | |
|
4 | 4 | Currently only the startup-dir functionality is tested, but more tests should |
|
5 | 5 | be added for: |
|
6 | 6 | |
|
7 | 7 | * ipython profile create |
|
8 | 8 | * ipython profile list |
|
9 | 9 | * ipython profile create --parallel |
|
10 | 10 | * security dir permissions |
|
11 | 11 | |
|
12 | 12 | Authors |
|
13 | 13 | ------- |
|
14 | 14 | |
|
15 | 15 | * MinRK |
|
16 | 16 | |
|
17 | 17 | """ |
|
18 | 18 | from __future__ import absolute_import |
|
19 | 19 | |
|
20 | 20 | #----------------------------------------------------------------------------- |
|
21 | 21 | # Imports |
|
22 | 22 | #----------------------------------------------------------------------------- |
|
23 | 23 | |
|
24 | 24 | import os |
|
25 | 25 | import shutil |
|
26 | 26 | import sys |
|
27 | 27 | import tempfile |
|
28 | 28 | |
|
29 | 29 | from unittest import TestCase |
|
30 | 30 | |
|
31 | 31 | import nose.tools as nt |
|
32 | 32 | from nose import SkipTest |
|
33 | 33 | |
|
34 | 34 | from IPython.core.profileapp import list_profiles_in, list_bundled_profiles |
|
35 | 35 | from IPython.core.profiledir import ProfileDir |
|
36 | 36 | |
|
37 | 37 | from IPython.testing import decorators as dec |
|
38 | 38 | from IPython.testing import tools as tt |
|
39 | 39 | from IPython.utils import py3compat |
|
40 | 40 | |
|
41 | 41 | |
|
42 | 42 | #----------------------------------------------------------------------------- |
|
43 | 43 | # Globals |
|
44 | 44 | #----------------------------------------------------------------------------- |
|
45 | 45 | TMP_TEST_DIR = tempfile.mkdtemp() |
|
46 | 46 | HOME_TEST_DIR = os.path.join(TMP_TEST_DIR, "home_test_dir") |
|
47 | 47 | IP_TEST_DIR = os.path.join(HOME_TEST_DIR,'.ipython') |
|
48 | 48 | |
|
49 | 49 | # |
|
50 | 50 | # Setup/teardown functions/decorators |
|
51 | 51 | # |
|
52 | 52 | |
|
53 | 53 | def setup(): |
|
54 | 54 | """Setup test environment for the module: |
|
55 | 55 | |
|
56 | 56 | - Adds dummy home dir tree |
|
57 | 57 | """ |
|
58 | 58 | # Do not mask exceptions here. In particular, catching WindowsError is a |
|
59 | 59 | # problem because that exception is only defined on Windows... |
|
60 | 60 | os.makedirs(IP_TEST_DIR) |
|
61 | 61 | |
|
62 | 62 | |
|
63 | 63 | def teardown(): |
|
64 | 64 | """Teardown test environment for the module: |
|
65 | 65 | |
|
66 | 66 | - Remove dummy home dir tree |
|
67 | 67 | """ |
|
68 | 68 | # Note: we remove the parent test dir, which is the root of all test |
|
69 | 69 | # subdirs we may have created. Use shutil instead of os.removedirs, so |
|
70 | 70 | # that non-empty directories are all recursively removed. |
|
71 | 71 | shutil.rmtree(TMP_TEST_DIR) |
|
72 | 72 | |
|
73 | 73 | |
|
74 | 74 | #----------------------------------------------------------------------------- |
|
75 | 75 | # Test functions |
|
76 | 76 | #----------------------------------------------------------------------------- |
|
77 | 77 | def win32_without_pywin32(): |
|
78 | 78 | if sys.platform == 'win32': |
|
79 | 79 | try: |
|
80 | 80 | import pywin32 |
|
81 | 81 | except ImportError: |
|
82 | 82 | return True |
|
83 | 83 | return False |
|
84 | 84 | |
|
85 | 85 | |
|
86 | 86 | class ProfileStartupTest(TestCase): |
|
87 | 87 | def setUp(self): |
|
88 | 88 | # create profile dir |
|
89 | 89 | self.pd = ProfileDir.create_profile_dir_by_name(IP_TEST_DIR, 'test') |
|
90 | 90 | self.options = ['--ipython-dir', IP_TEST_DIR, '--profile', 'test'] |
|
91 | 91 | self.fname = os.path.join(TMP_TEST_DIR, 'test.py') |
|
92 | 92 | |
|
93 | 93 | def tearDown(self): |
|
94 | 94 | # We must remove this profile right away so its presence doesn't |
|
95 | 95 | # confuse other tests. |
|
96 | 96 | shutil.rmtree(self.pd.location) |
|
97 | 97 | |
|
98 | 98 | def init(self, startup_file, startup, test): |
|
99 | 99 | # write startup python file |
|
100 | 100 | with open(os.path.join(self.pd.startup_dir, startup_file), 'w') as f: |
|
101 | 101 | f.write(startup) |
|
102 | 102 | # write simple test file, to check that the startup file was run |
|
103 | 103 | with open(self.fname, 'w') as f: |
|
104 | 104 | f.write(py3compat.doctest_refactor_print(test)) |
|
105 | 105 | |
|
106 | 106 | def validate(self, output): |
|
107 | 107 | tt.ipexec_validate(self.fname, output, '', options=self.options) |
|
108 | 108 | |
|
109 | 109 | @dec.skipif(win32_without_pywin32(), "Test requires pywin32 on Windows") |
|
110 | 110 | def test_startup_py(self): |
|
111 | 111 | self.init('00-start.py', 'zzz=123\n', |
|
112 | 112 | py3compat.doctest_refactor_print('print zzz\n')) |
|
113 | 113 | self.validate('123') |
|
114 | 114 | |
|
115 | 115 | @dec.skipif(win32_without_pywin32(), "Test requires pywin32 on Windows") |
|
116 | 116 | def test_startup_ipy(self): |
|
117 | 117 | self.init('00-start.ipy', '%profile\n', '') |
|
118 | 118 | self.validate('test') |
|
119 | 119 | |
|
120 | 120 | |
|
121 | 121 | def test_list_profiles_in(): |
|
122 | 122 | # No need to remove these directories and files, as they will get nuked in |
|
123 | 123 | # the module-level teardown. |
|
124 | 124 | td = tempfile.mkdtemp(dir=TMP_TEST_DIR) |
|
125 | 125 | td = py3compat.str_to_unicode(td) |
|
126 | 126 | for name in ('profile_foo', u'profile_ΓΌnicode', 'profile_hello', |
|
127 | 127 | 'not_a_profile'): |
|
128 | 128 | os.mkdir(os.path.join(td, name)) |
|
129 | 129 | with open(os.path.join(td, 'profile_file'), 'w') as f: |
|
130 | 130 | f.write("I am not a profile directory") |
|
131 | 131 | profiles = list_profiles_in(td) |
|
132 | 132 | |
|
133 | 133 | # unicode normalization can turn u'ΓΌnicode' into u'u\0308nicode', |
|
134 | 134 | # so only check for *nicode, and that creating a ProfileDir from the |
|
135 | 135 | # name remains valid |
|
136 | 136 | found_unicode = False |
|
137 | 137 | for p in list(profiles): |
|
138 | 138 | if p.endswith('nicode'): |
|
139 | 139 | pd = ProfileDir.find_profile_dir_by_name(td, p) |
|
140 | 140 | profiles.remove(p) |
|
141 | 141 | found_unicode = True |
|
142 | 142 | break |
|
143 | 143 | nt.assert_true(found_unicode) |
|
144 |
nt.assert_equal |
|
|
144 | nt.assert_equal(set(profiles), set(['foo', 'hello'])) | |
|
145 | 145 | |
|
146 | 146 | |
|
147 | 147 | def test_list_bundled_profiles(): |
|
148 | 148 | # This variable will need to be updated when a new profile gets bundled |
|
149 | 149 | bundled_true = [u'cluster', u'math', u'pysh', u'sympy'] |
|
150 | 150 | bundled = sorted(list_bundled_profiles()) |
|
151 |
nt.assert_equal |
|
|
151 | nt.assert_equal(bundled, bundled_true) |
@@ -1,250 +1,250 | |||
|
1 | 1 | # encoding: utf-8 |
|
2 | 2 | """Tests for code execution (%run and related), which is particularly tricky. |
|
3 | 3 | |
|
4 | 4 | Because of how %run manages namespaces, and the fact that we are trying here to |
|
5 | 5 | verify subtle object deletion and reference counting issues, the %run tests |
|
6 | 6 | will be kept in this separate file. This makes it easier to aggregate in one |
|
7 | 7 | place the tricks needed to handle it; most other magics are much easier to test |
|
8 | 8 | and we do so in a common test_magic file. |
|
9 | 9 | """ |
|
10 | 10 | from __future__ import absolute_import |
|
11 | 11 | |
|
12 | 12 | #----------------------------------------------------------------------------- |
|
13 | 13 | # Imports |
|
14 | 14 | #----------------------------------------------------------------------------- |
|
15 | 15 | |
|
16 | 16 | import os |
|
17 | 17 | import sys |
|
18 | 18 | import tempfile |
|
19 | 19 | |
|
20 | 20 | import nose.tools as nt |
|
21 | 21 | from nose import SkipTest |
|
22 | 22 | |
|
23 | 23 | from IPython.testing import decorators as dec |
|
24 | 24 | from IPython.testing import tools as tt |
|
25 | 25 | from IPython.utils import py3compat |
|
26 | 26 | |
|
27 | 27 | #----------------------------------------------------------------------------- |
|
28 | 28 | # Test functions begin |
|
29 | 29 | #----------------------------------------------------------------------------- |
|
30 | 30 | |
|
31 | 31 | def doctest_refbug(): |
|
32 | 32 | """Very nasty problem with references held by multiple runs of a script. |
|
33 | 33 | See: https://github.com/ipython/ipython/issues/141 |
|
34 | 34 | |
|
35 | 35 | In [1]: _ip.clear_main_mod_cache() |
|
36 | 36 | # random |
|
37 | 37 | |
|
38 | 38 | In [2]: %run refbug |
|
39 | 39 | |
|
40 | 40 | In [3]: call_f() |
|
41 | 41 | lowercased: hello |
|
42 | 42 | |
|
43 | 43 | In [4]: %run refbug |
|
44 | 44 | |
|
45 | 45 | In [5]: call_f() |
|
46 | 46 | lowercased: hello |
|
47 | 47 | lowercased: hello |
|
48 | 48 | """ |
|
49 | 49 | |
|
50 | 50 | |
|
51 | 51 | def doctest_run_builtins(): |
|
52 | 52 | r"""Check that %run doesn't damage __builtins__. |
|
53 | 53 | |
|
54 | 54 | In [1]: import tempfile |
|
55 | 55 | |
|
56 | 56 | In [2]: bid1 = id(__builtins__) |
|
57 | 57 | |
|
58 | 58 | In [3]: fname = tempfile.mkstemp('.py')[1] |
|
59 | 59 | |
|
60 | 60 | In [3]: f = open(fname,'w') |
|
61 | 61 | |
|
62 | 62 | In [4]: dummy= f.write('pass\n') |
|
63 | 63 | |
|
64 | 64 | In [5]: f.flush() |
|
65 | 65 | |
|
66 | 66 | In [6]: t1 = type(__builtins__) |
|
67 | 67 | |
|
68 | 68 | In [7]: %run $fname |
|
69 | 69 | |
|
70 | 70 | In [7]: f.close() |
|
71 | 71 | |
|
72 | 72 | In [8]: bid2 = id(__builtins__) |
|
73 | 73 | |
|
74 | 74 | In [9]: t2 = type(__builtins__) |
|
75 | 75 | |
|
76 | 76 | In [10]: t1 == t2 |
|
77 | 77 | Out[10]: True |
|
78 | 78 | |
|
79 | 79 | In [10]: bid1 == bid2 |
|
80 | 80 | Out[10]: True |
|
81 | 81 | |
|
82 | 82 | In [12]: try: |
|
83 | 83 | ....: os.unlink(fname) |
|
84 | 84 | ....: except: |
|
85 | 85 | ....: pass |
|
86 | 86 | ....: |
|
87 | 87 | """ |
|
88 | 88 | |
|
89 | 89 | @py3compat.doctest_refactor_print |
|
90 | 90 | def doctest_reset_del(): |
|
91 | 91 | """Test that resetting doesn't cause errors in __del__ methods. |
|
92 | 92 | |
|
93 | 93 | In [2]: class A(object): |
|
94 | 94 | ...: def __del__(self): |
|
95 | 95 | ...: print str("Hi") |
|
96 | 96 | ...: |
|
97 | 97 | |
|
98 | 98 | In [3]: a = A() |
|
99 | 99 | |
|
100 | 100 | In [4]: get_ipython().reset() |
|
101 | 101 | Hi |
|
102 | 102 | |
|
103 | 103 | In [5]: 1+1 |
|
104 | 104 | Out[5]: 2 |
|
105 | 105 | """ |
|
106 | 106 | |
|
107 | 107 | # For some tests, it will be handy to organize them in a class with a common |
|
108 | 108 | # setup that makes a temp file |
|
109 | 109 | |
|
110 | 110 | class TestMagicRunPass(tt.TempFileMixin): |
|
111 | 111 | |
|
112 | 112 | def setup(self): |
|
113 | 113 | """Make a valid python temp file.""" |
|
114 | 114 | self.mktmp('pass\n') |
|
115 | 115 | |
|
116 | 116 | def run_tmpfile(self): |
|
117 | 117 | _ip = get_ipython() |
|
118 | 118 | # This fails on Windows if self.tmpfile.name has spaces or "~" in it. |
|
119 | 119 | # See below and ticket https://bugs.launchpad.net/bugs/366353 |
|
120 | 120 | _ip.magic('run %s' % self.fname) |
|
121 | 121 | |
|
122 | 122 | def run_tmpfile_p(self): |
|
123 | 123 | _ip = get_ipython() |
|
124 | 124 | # This fails on Windows if self.tmpfile.name has spaces or "~" in it. |
|
125 | 125 | # See below and ticket https://bugs.launchpad.net/bugs/366353 |
|
126 | 126 | _ip.magic('run -p %s' % self.fname) |
|
127 | 127 | |
|
128 | 128 | def test_builtins_id(self): |
|
129 | 129 | """Check that %run doesn't damage __builtins__ """ |
|
130 | 130 | _ip = get_ipython() |
|
131 | 131 | # Test that the id of __builtins__ is not modified by %run |
|
132 | 132 | bid1 = id(_ip.user_ns['__builtins__']) |
|
133 | 133 | self.run_tmpfile() |
|
134 | 134 | bid2 = id(_ip.user_ns['__builtins__']) |
|
135 | 135 | tt.assert_equals(bid1, bid2) |
|
136 | 136 | |
|
137 | 137 | def test_builtins_type(self): |
|
138 | 138 | """Check that the type of __builtins__ doesn't change with %run. |
|
139 | 139 | |
|
140 | 140 | However, the above could pass if __builtins__ was already modified to |
|
141 | 141 | be a dict (it should be a module) by a previous use of %run. So we |
|
142 | 142 | also check explicitly that it really is a module: |
|
143 | 143 | """ |
|
144 | 144 | _ip = get_ipython() |
|
145 | 145 | self.run_tmpfile() |
|
146 | 146 | tt.assert_equals(type(_ip.user_ns['__builtins__']),type(sys)) |
|
147 | 147 | |
|
148 | 148 | def test_prompts(self): |
|
149 | 149 | """Test that prompts correctly generate after %run""" |
|
150 | 150 | self.run_tmpfile() |
|
151 | 151 | _ip = get_ipython() |
|
152 | 152 | p2 = _ip.prompt_manager.render('in2').strip() |
|
153 |
nt.assert_equal |
|
|
153 | nt.assert_equal(p2[:3], '...') | |
|
154 | 154 | |
|
155 | 155 | def test_run_profile( self ): |
|
156 | 156 | """Test that the option -p, which invokes the profiler, do not |
|
157 | 157 | crash by invoking execfile""" |
|
158 | 158 | _ip = get_ipython() |
|
159 | 159 | self.run_tmpfile_p() |
|
160 | 160 | |
|
161 | 161 | |
|
162 | 162 | class TestMagicRunSimple(tt.TempFileMixin): |
|
163 | 163 | |
|
164 | 164 | def test_simpledef(self): |
|
165 | 165 | """Test that simple class definitions work.""" |
|
166 | 166 | src = ("class foo: pass\n" |
|
167 | 167 | "def f(): return foo()") |
|
168 | 168 | self.mktmp(src) |
|
169 | 169 | _ip.magic('run %s' % self.fname) |
|
170 | 170 | _ip.run_cell('t = isinstance(f(), foo)') |
|
171 | 171 | nt.assert_true(_ip.user_ns['t']) |
|
172 | 172 | |
|
173 | 173 | def test_obj_del(self): |
|
174 | 174 | """Test that object's __del__ methods are called on exit.""" |
|
175 | 175 | if sys.platform == 'win32': |
|
176 | 176 | try: |
|
177 | 177 | import win32api |
|
178 | 178 | except ImportError: |
|
179 | 179 | raise SkipTest("Test requires pywin32") |
|
180 | 180 | src = ("class A(object):\n" |
|
181 | 181 | " def __del__(self):\n" |
|
182 | 182 | " print 'object A deleted'\n" |
|
183 | 183 | "a = A()\n") |
|
184 | 184 | self.mktmp(py3compat.doctest_refactor_print(src)) |
|
185 | 185 | if dec.module_not_available('sqlite3'): |
|
186 | 186 | err = 'WARNING: IPython History requires SQLite, your history will not be saved\n' |
|
187 | 187 | else: |
|
188 | 188 | err = None |
|
189 | 189 | tt.ipexec_validate(self.fname, 'object A deleted', err) |
|
190 | 190 | |
|
191 | 191 | @dec.skip_known_failure |
|
192 | 192 | def test_aggressive_namespace_cleanup(self): |
|
193 | 193 | """Test that namespace cleanup is not too aggressive GH-238 |
|
194 | 194 | |
|
195 | 195 | Returning from another run magic deletes the namespace""" |
|
196 | 196 | # see ticket https://github.com/ipython/ipython/issues/238 |
|
197 | 197 | class secondtmp(tt.TempFileMixin): pass |
|
198 | 198 | empty = secondtmp() |
|
199 | 199 | empty.mktmp('') |
|
200 | 200 | src = ("ip = get_ipython()\n" |
|
201 | 201 | "for i in range(5):\n" |
|
202 | 202 | " try:\n" |
|
203 | 203 | " ip.magic('run %s')\n" |
|
204 | 204 | " except NameError as e:\n" |
|
205 | 205 | " print i;break\n" % empty.fname) |
|
206 | 206 | self.mktmp(py3compat.doctest_refactor_print(src)) |
|
207 | 207 | _ip.magic('run %s' % self.fname) |
|
208 | 208 | _ip.run_cell('ip == get_ipython()') |
|
209 | 209 | tt.assert_equals(_ip.user_ns['i'], 5) |
|
210 | 210 | |
|
211 | 211 | @dec.skip_win32 |
|
212 | 212 | def test_tclass(self): |
|
213 | 213 | mydir = os.path.dirname(__file__) |
|
214 | 214 | tc = os.path.join(mydir, 'tclass') |
|
215 | 215 | src = ("%%run '%s' C-first\n" |
|
216 | 216 | "%%run '%s' C-second\n" |
|
217 | 217 | "%%run '%s' C-third\n") % (tc, tc, tc) |
|
218 | 218 | self.mktmp(src, '.ipy') |
|
219 | 219 | out = """\ |
|
220 | 220 | ARGV 1-: ['C-first'] |
|
221 | 221 | ARGV 1-: ['C-second'] |
|
222 | 222 | tclass.py: deleting object: C-first |
|
223 | 223 | ARGV 1-: ['C-third'] |
|
224 | 224 | tclass.py: deleting object: C-second |
|
225 | 225 | tclass.py: deleting object: C-third |
|
226 | 226 | """ |
|
227 | 227 | if dec.module_not_available('sqlite3'): |
|
228 | 228 | err = 'WARNING: IPython History requires SQLite, your history will not be saved\n' |
|
229 | 229 | else: |
|
230 | 230 | err = None |
|
231 | 231 | tt.ipexec_validate(self.fname, out, err) |
|
232 | 232 | |
|
233 | 233 | def test_run_i_after_reset(self): |
|
234 | 234 | """Check that %run -i still works after %reset (gh-693)""" |
|
235 | 235 | src = "yy = zz\n" |
|
236 | 236 | self.mktmp(src) |
|
237 | 237 | _ip.run_cell("zz = 23") |
|
238 | 238 | _ip.magic('run -i %s' % self.fname) |
|
239 | 239 | tt.assert_equals(_ip.user_ns['yy'], 23) |
|
240 | 240 | _ip.magic('reset -f') |
|
241 | 241 | _ip.run_cell("zz = 23") |
|
242 | 242 | _ip.magic('run -i %s' % self.fname) |
|
243 | 243 | tt.assert_equals(_ip.user_ns['yy'], 23) |
|
244 | 244 | |
|
245 | 245 | def test_unicode(self): |
|
246 | 246 | """Check that files in odd encodings are accepted.""" |
|
247 | 247 | mydir = os.path.dirname(__file__) |
|
248 | 248 | na = os.path.join(mydir, 'nonascii.py') |
|
249 | 249 | _ip.magic('run "%s"' % na) |
|
250 | 250 | tt.assert_equals(_ip.user_ns['u'], u'ΠΡβΠ€') |
@@ -1,58 +1,58 | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | """Tests for the Cython magics extension.""" |
|
3 | 3 | |
|
4 | 4 | import os |
|
5 | 5 | import nose.tools as nt |
|
6 | 6 | |
|
7 | 7 | from IPython.testing import decorators as dec |
|
8 | 8 | from IPython.utils import py3compat |
|
9 | 9 | |
|
10 | 10 | code = py3compat.str_to_unicode("""def f(x): |
|
11 | 11 | return 2*x |
|
12 | 12 | """) |
|
13 | 13 | |
|
14 | 14 | try: |
|
15 | 15 | import Cython |
|
16 | 16 | except: |
|
17 | 17 | __test__ = False |
|
18 | 18 | |
|
19 | 19 | ip = get_ipython() |
|
20 | 20 | |
|
21 | 21 | |
|
22 | 22 | def setup(): |
|
23 | 23 | ip.extension_manager.load_extension('cythonmagic') |
|
24 | 24 | |
|
25 | 25 | |
|
26 | 26 | def test_cython_inline(): |
|
27 | 27 | ip.ex('a=10; b=20') |
|
28 | 28 | result = ip.run_cell_magic('cython_inline','','return a+b') |
|
29 |
nt.assert_equal |
|
|
29 | nt.assert_equal(result, 30) | |
|
30 | 30 | |
|
31 | 31 | |
|
32 | 32 | def test_cython_pyximport(): |
|
33 | 33 | module_name = '_test_cython_pyximport' |
|
34 | 34 | ip.run_cell_magic('cython_pyximport', module_name, code) |
|
35 | 35 | ip.ex('g = f(10)') |
|
36 |
nt.assert_equal |
|
|
36 | nt.assert_equal(ip.user_ns['g'], 20.0) | |
|
37 | 37 | try: |
|
38 | 38 | os.remove(module_name+'.pyx') |
|
39 | 39 | except OSError: |
|
40 | 40 | pass |
|
41 | 41 | |
|
42 | 42 | |
|
43 | 43 | def test_cython(): |
|
44 | 44 | ip.run_cell_magic('cython', '', code) |
|
45 | 45 | ip.ex('g = f(10)') |
|
46 |
nt.assert_equal |
|
|
46 | nt.assert_equal(ip.user_ns['g'], 20.0) | |
|
47 | 47 | |
|
48 | 48 | |
|
49 | 49 | @dec.skip_win32 |
|
50 | 50 | def test_extlibs(): |
|
51 | 51 | code = py3compat.str_to_unicode(""" |
|
52 | 52 | from libc.math cimport sin |
|
53 | 53 | x = sin(0.0) |
|
54 | 54 | """) |
|
55 | 55 | ip.user_ns['x'] = 1 |
|
56 | 56 | ip.run_cell_magic('cython', '-l m', code) |
|
57 |
nt.assert_equal |
|
|
57 | nt.assert_equal(ip.user_ns['x'], 0) | |
|
58 | 58 |
@@ -1,59 +1,59 | |||
|
1 | 1 | """Tests for two-process terminal frontend |
|
2 | 2 | |
|
3 | 3 | Currenlty only has the most simple test possible, starting a console and running |
|
4 | 4 | a single command. |
|
5 | 5 | |
|
6 | 6 | Authors: |
|
7 | 7 | |
|
8 | 8 | * Min RK |
|
9 | 9 | """ |
|
10 | 10 | |
|
11 | 11 | #----------------------------------------------------------------------------- |
|
12 | 12 | # Imports |
|
13 | 13 | #----------------------------------------------------------------------------- |
|
14 | 14 | |
|
15 | 15 | import time |
|
16 | 16 | |
|
17 | 17 | import nose.tools as nt |
|
18 | 18 | from nose import SkipTest |
|
19 | 19 | |
|
20 | 20 | from IPython.testing import decorators as dec |
|
21 | 21 | from IPython.testing import tools as tt |
|
22 | 22 | from IPython.utils import py3compat |
|
23 | 23 | from IPython.utils.process import find_cmd |
|
24 | 24 | |
|
25 | 25 | #----------------------------------------------------------------------------- |
|
26 | 26 | # Test functions begin |
|
27 | 27 | #----------------------------------------------------------------------------- |
|
28 | 28 | |
|
29 | 29 | @dec.skip_win32 |
|
30 | 30 | def test_console_starts(): |
|
31 | 31 | """test that `ipython console` starts a terminal""" |
|
32 | 32 | from IPython.external import pexpect |
|
33 | 33 | |
|
34 | 34 | # weird IOErrors prevent this from firing sometimes: |
|
35 | 35 | ipython_cmd = None |
|
36 | 36 | for i in range(5): |
|
37 | 37 | try: |
|
38 | 38 | ipython_cmd = find_cmd('ipython3' if py3compat.PY3 else 'ipython') |
|
39 | 39 | except IOError: |
|
40 | 40 | time.sleep(0.1) |
|
41 | 41 | else: |
|
42 | 42 | break |
|
43 | 43 | if ipython_cmd is None: |
|
44 | 44 | raise SkipTest("Could not determine ipython command") |
|
45 | 45 | |
|
46 | 46 | p = pexpect.spawn(ipython_cmd, args=['console', '--colors=NoColor']) |
|
47 | 47 | idx = p.expect([r'In \[\d+\]', pexpect.EOF], timeout=15) |
|
48 |
nt.assert_equal |
|
|
48 | nt.assert_equal(idx, 0, "expected in prompt") | |
|
49 | 49 | p.sendline('5') |
|
50 | 50 | idx = p.expect([r'Out\[\d+\]: 5', pexpect.EOF], timeout=5) |
|
51 |
nt.assert_equal |
|
|
51 | nt.assert_equal(idx, 0, "expected out prompt") | |
|
52 | 52 | idx = p.expect([r'In \[\d+\]', pexpect.EOF], timeout=5) |
|
53 |
nt.assert_equal |
|
|
53 | nt.assert_equal(idx, 0, "expected second in prompt") | |
|
54 | 54 | # send ctrl-D;ctrl-D to exit |
|
55 | 55 | p.sendeof() |
|
56 | 56 | p.sendeof() |
|
57 | 57 | p.expect([pexpect.EOF, pexpect.TIMEOUT], timeout=5) |
|
58 | 58 | if p.isalive(): |
|
59 | 59 | p.terminate() |
@@ -1,91 +1,91 | |||
|
1 | 1 | """Tests for pylab tools module. |
|
2 | 2 | """ |
|
3 | 3 | #----------------------------------------------------------------------------- |
|
4 | 4 | # Copyright (c) 2011, the IPython Development Team. |
|
5 | 5 | # |
|
6 | 6 | # Distributed under the terms of the Modified BSD License. |
|
7 | 7 | # |
|
8 | 8 | # The full license is in the file COPYING.txt, distributed with this software. |
|
9 | 9 | #----------------------------------------------------------------------------- |
|
10 | 10 | |
|
11 | 11 | #----------------------------------------------------------------------------- |
|
12 | 12 | # Imports |
|
13 | 13 | #----------------------------------------------------------------------------- |
|
14 | 14 | from __future__ import print_function |
|
15 | 15 | |
|
16 | 16 | # Stdlib imports |
|
17 | 17 | import sys |
|
18 | 18 | import time |
|
19 | 19 | |
|
20 | 20 | # Third-party imports |
|
21 | 21 | import nose.tools as nt |
|
22 | 22 | |
|
23 | 23 | # Our own imports |
|
24 | 24 | from IPython.lib import backgroundjobs as bg |
|
25 | 25 | from IPython.testing import decorators as dec |
|
26 | 26 | |
|
27 | 27 | #----------------------------------------------------------------------------- |
|
28 | 28 | # Globals and constants |
|
29 | 29 | #----------------------------------------------------------------------------- |
|
30 | 30 | t_short = 0.0001 # very short interval to wait on jobs |
|
31 | 31 | |
|
32 | 32 | #----------------------------------------------------------------------------- |
|
33 | 33 | # Local utilities |
|
34 | 34 | #----------------------------------------------------------------------------- |
|
35 | 35 | def sleeper(interval=t_short, *a, **kw): |
|
36 | 36 | args = dict(interval=interval, |
|
37 | 37 | other_args=a, |
|
38 | 38 | kw_args=kw) |
|
39 | 39 | time.sleep(interval) |
|
40 | 40 | return args |
|
41 | 41 | |
|
42 | 42 | def crasher(interval=t_short, *a, **kw): |
|
43 | 43 | time.sleep(interval) |
|
44 | 44 | raise Exception("Dead job with interval %s" % interval) |
|
45 | 45 | |
|
46 | 46 | #----------------------------------------------------------------------------- |
|
47 | 47 | # Classes and functions |
|
48 | 48 | #----------------------------------------------------------------------------- |
|
49 | 49 | |
|
50 | 50 | def test_result(): |
|
51 | 51 | """Test job submission and result retrieval""" |
|
52 | 52 | jobs = bg.BackgroundJobManager() |
|
53 | 53 | j = jobs.new(sleeper) |
|
54 | 54 | j.join() |
|
55 |
nt.assert_equal |
|
|
55 | nt.assert_equal(j.result['interval'], t_short) | |
|
56 | 56 | |
|
57 | 57 | |
|
58 | 58 | def test_flush(): |
|
59 | 59 | """Test job control""" |
|
60 | 60 | jobs = bg.BackgroundJobManager() |
|
61 | 61 | j = jobs.new(sleeper) |
|
62 | 62 | j.join() |
|
63 |
nt.assert_equal |
|
|
64 |
nt.assert_equal |
|
|
63 | nt.assert_equal(len(jobs.completed), 1) | |
|
64 | nt.assert_equal(len(jobs.dead), 0) | |
|
65 | 65 | jobs.flush() |
|
66 |
nt.assert_equal |
|
|
66 | nt.assert_equal(len(jobs.completed), 0) | |
|
67 | 67 | |
|
68 | 68 | |
|
69 | 69 | def test_dead(): |
|
70 | 70 | """Test control of dead jobs""" |
|
71 | 71 | jobs = bg.BackgroundJobManager() |
|
72 | 72 | j = jobs.new(crasher) |
|
73 | 73 | j.join() |
|
74 |
nt.assert_equal |
|
|
75 |
nt.assert_equal |
|
|
74 | nt.assert_equal(len(jobs.completed), 0) | |
|
75 | nt.assert_equal(len(jobs.dead), 1) | |
|
76 | 76 | jobs.flush() |
|
77 |
nt.assert_equal |
|
|
77 | nt.assert_equal(len(jobs.dead), 0) | |
|
78 | 78 | |
|
79 | 79 | |
|
80 | 80 | def test_longer(): |
|
81 | 81 | """Test control of longer-running jobs""" |
|
82 | 82 | jobs = bg.BackgroundJobManager() |
|
83 | 83 | # Sleep for long enough for the following two checks to still report the |
|
84 | 84 | # job as running, but not so long that it makes the test suite noticeably |
|
85 | 85 | # slower. |
|
86 | 86 | j = jobs.new(sleeper, 0.1) |
|
87 |
nt.assert_equal |
|
|
88 |
nt.assert_equal |
|
|
87 | nt.assert_equal(len(jobs.running), 1) | |
|
88 | nt.assert_equal(len(jobs.completed), 0) | |
|
89 | 89 | j.join() |
|
90 |
nt.assert_equal |
|
|
91 |
nt.assert_equal |
|
|
90 | nt.assert_equal(len(jobs.running), 0) | |
|
91 | nt.assert_equal(len(jobs.completed), 1) |
@@ -1,84 +1,84 | |||
|
1 | 1 | """Tests for IPython.lib.pretty. |
|
2 | 2 | """ |
|
3 | 3 | #----------------------------------------------------------------------------- |
|
4 | 4 | # Copyright (c) 2011, the IPython Development Team. |
|
5 | 5 | # |
|
6 | 6 | # Distributed under the terms of the Modified BSD License. |
|
7 | 7 | # |
|
8 | 8 | # The full license is in the file COPYING.txt, distributed with this software. |
|
9 | 9 | #----------------------------------------------------------------------------- |
|
10 | 10 | |
|
11 | 11 | #----------------------------------------------------------------------------- |
|
12 | 12 | # Imports |
|
13 | 13 | #----------------------------------------------------------------------------- |
|
14 | 14 | from __future__ import print_function |
|
15 | 15 | |
|
16 | 16 | # Third-party imports |
|
17 | 17 | import nose.tools as nt |
|
18 | 18 | |
|
19 | 19 | # Our own imports |
|
20 | 20 | from IPython.lib import pretty |
|
21 | 21 | |
|
22 | 22 | #----------------------------------------------------------------------------- |
|
23 | 23 | # Classes and functions |
|
24 | 24 | #----------------------------------------------------------------------------- |
|
25 | 25 | |
|
26 | 26 | class MyList(object): |
|
27 | 27 | def __init__(self, content): |
|
28 | 28 | self.content = content |
|
29 | 29 | def _repr_pretty_(self, p, cycle): |
|
30 | 30 | if cycle: |
|
31 | 31 | p.text("MyList(...)") |
|
32 | 32 | else: |
|
33 | 33 | with p.group(3, "MyList(", ")"): |
|
34 | 34 | for (i, child) in enumerate(self.content): |
|
35 | 35 | if i: |
|
36 | 36 | p.text(",") |
|
37 | 37 | p.breakable() |
|
38 | 38 | else: |
|
39 | 39 | p.breakable("") |
|
40 | 40 | p.pretty(child) |
|
41 | 41 | |
|
42 | 42 | |
|
43 | 43 | class MyDict(dict): |
|
44 | 44 | def _repr_pretty_(self, p, cycle): |
|
45 | 45 | p.text("MyDict(...)") |
|
46 | 46 | |
|
47 | 47 | |
|
48 | 48 | class Dummy1(object): |
|
49 | 49 | def _repr_pretty_(self, p, cycle): |
|
50 | 50 | p.text("Dummy1(...)") |
|
51 | 51 | |
|
52 | 52 | class Dummy2(Dummy1): |
|
53 | 53 | _repr_pretty_ = None |
|
54 | 54 | |
|
55 | 55 | |
|
56 | 56 | def test_indentation(): |
|
57 | 57 | """Test correct indentation in groups""" |
|
58 | 58 | count = 40 |
|
59 | 59 | gotoutput = pretty.pretty(MyList(range(count))) |
|
60 | 60 | expectedoutput = "MyList(\n" + ",\n".join(" %d" % i for i in range(count)) + ")" |
|
61 | 61 | |
|
62 |
nt.assert_equal |
|
|
62 | nt.assert_equal(gotoutput, expectedoutput) | |
|
63 | 63 | |
|
64 | 64 | |
|
65 | 65 | def test_dispatch(): |
|
66 | 66 | """ |
|
67 | 67 | Test correct dispatching: The _repr_pretty_ method for MyDict |
|
68 | 68 | must be found before the registered printer for dict. |
|
69 | 69 | """ |
|
70 | 70 | gotoutput = pretty.pretty(MyDict()) |
|
71 | 71 | expectedoutput = "MyDict(...)" |
|
72 | 72 | |
|
73 |
nt.assert_equal |
|
|
73 | nt.assert_equal(gotoutput, expectedoutput) | |
|
74 | 74 | |
|
75 | 75 | |
|
76 | 76 | def test_callability_checking(): |
|
77 | 77 | """ |
|
78 | 78 | Test that the _repr_pretty_ method is tested for callability and skipped if |
|
79 | 79 | not. |
|
80 | 80 | """ |
|
81 | 81 | gotoutput = pretty.pretty(Dummy2()) |
|
82 | 82 | expectedoutput = "Dummy1(...)" |
|
83 | 83 | |
|
84 |
nt.assert_equal |
|
|
84 | nt.assert_equal(gotoutput, expectedoutput) |
@@ -1,21 +1,21 | |||
|
1 | 1 | from IPython.lib import passwd |
|
2 | 2 | from IPython.lib.security import passwd_check, salt_len |
|
3 | 3 | import nose.tools as nt |
|
4 | 4 | |
|
5 | 5 | def test_passwd_structure(): |
|
6 | 6 | p = passwd('passphrase') |
|
7 | 7 | algorithm, salt, hashed = p.split(':') |
|
8 |
nt.assert_equal |
|
|
9 |
nt.assert_equal |
|
|
10 |
nt.assert_equal |
|
|
8 | nt.assert_equal(algorithm, 'sha1') | |
|
9 | nt.assert_equal(len(salt), salt_len) | |
|
10 | nt.assert_equal(len(hashed), 40) | |
|
11 | 11 | |
|
12 | 12 | def test_roundtrip(): |
|
13 | 13 | p = passwd('passphrase') |
|
14 |
nt.assert_equal |
|
|
14 | nt.assert_equal(passwd_check(p, 'passphrase'), True) | |
|
15 | 15 | |
|
16 | 16 | def test_bad(): |
|
17 | 17 | p = passwd('passphrase') |
|
18 |
nt.assert_equal |
|
|
19 |
nt.assert_equal |
|
|
20 |
nt.assert_equal |
|
|
18 | nt.assert_equal(passwd_check(p, p), False) | |
|
19 | nt.assert_equal(passwd_check(p, 'a:b:c:d'), False) | |
|
20 | nt.assert_equal(passwd_check(p, 'a:b'), False) | |
|
21 | 21 |
@@ -1,392 +1,392 | |||
|
1 | 1 | """Generic testing tools. |
|
2 | 2 | |
|
3 | 3 | In particular, this module exposes a set of top-level assert* functions that |
|
4 | 4 | can be used in place of nose.tools.assert* in method generators (the ones in |
|
5 | 5 | nose can not, at least as of nose 0.10.4). |
|
6 | 6 | |
|
7 | 7 | |
|
8 | 8 | Authors |
|
9 | 9 | ------- |
|
10 | 10 | - Fernando Perez <Fernando.Perez@berkeley.edu> |
|
11 | 11 | """ |
|
12 | 12 | |
|
13 | 13 | from __future__ import absolute_import |
|
14 | 14 | |
|
15 | 15 | #----------------------------------------------------------------------------- |
|
16 | 16 | # Copyright (C) 2009-2011 The IPython Development Team |
|
17 | 17 | # |
|
18 | 18 | # Distributed under the terms of the BSD License. The full license is in |
|
19 | 19 | # the file COPYING, distributed as part of this software. |
|
20 | 20 | #----------------------------------------------------------------------------- |
|
21 | 21 | |
|
22 | 22 | #----------------------------------------------------------------------------- |
|
23 | 23 | # Imports |
|
24 | 24 | #----------------------------------------------------------------------------- |
|
25 | 25 | |
|
26 | 26 | import os |
|
27 | 27 | import re |
|
28 | 28 | import sys |
|
29 | 29 | import tempfile |
|
30 | 30 | |
|
31 | 31 | from contextlib import contextmanager |
|
32 | 32 | from io import StringIO |
|
33 | 33 | |
|
34 | 34 | try: |
|
35 | 35 | # These tools are used by parts of the runtime, so we make the nose |
|
36 | 36 | # dependency optional at this point. Nose is a hard dependency to run the |
|
37 | 37 | # test suite, but NOT to use ipython itself. |
|
38 | 38 | import nose.tools as nt |
|
39 | 39 | has_nose = True |
|
40 | 40 | except ImportError: |
|
41 | 41 | has_nose = False |
|
42 | 42 | |
|
43 | 43 | from IPython.config.loader import Config |
|
44 | 44 | from IPython.utils.process import find_cmd, getoutputerror |
|
45 | 45 | from IPython.utils.text import list_strings |
|
46 | 46 | from IPython.utils.io import temp_pyfile, Tee |
|
47 | 47 | from IPython.utils import py3compat |
|
48 | 48 | from IPython.utils.encoding import DEFAULT_ENCODING |
|
49 | 49 | |
|
50 | 50 | from . import decorators as dec |
|
51 | 51 | from . import skipdoctest |
|
52 | 52 | |
|
53 | 53 | #----------------------------------------------------------------------------- |
|
54 | 54 | # Globals |
|
55 | 55 | #----------------------------------------------------------------------------- |
|
56 | 56 | |
|
57 | 57 | # Make a bunch of nose.tools assert wrappers that can be used in test |
|
58 | 58 | # generators. This will expose an assert* function for each one in nose.tools. |
|
59 | 59 | |
|
60 | 60 | _tpl = """ |
|
61 | 61 | def %(name)s(*a,**kw): |
|
62 | 62 | return nt.%(name)s(*a,**kw) |
|
63 | 63 | """ |
|
64 | 64 | |
|
65 | 65 | if has_nose: |
|
66 | 66 | for _x in [a for a in dir(nt) if a.startswith('assert')]: |
|
67 | 67 | exec _tpl % dict(name=_x) |
|
68 | 68 | |
|
69 | 69 | #----------------------------------------------------------------------------- |
|
70 | 70 | # Functions and classes |
|
71 | 71 | #----------------------------------------------------------------------------- |
|
72 | 72 | |
|
73 | 73 | # The docstring for full_path doctests differently on win32 (different path |
|
74 | 74 | # separator) so just skip the doctest there. The example remains informative. |
|
75 | 75 | doctest_deco = skipdoctest.skip_doctest if sys.platform == 'win32' else dec.null_deco |
|
76 | 76 | |
|
77 | 77 | @doctest_deco |
|
78 | 78 | def full_path(startPath,files): |
|
79 | 79 | """Make full paths for all the listed files, based on startPath. |
|
80 | 80 | |
|
81 | 81 | Only the base part of startPath is kept, since this routine is typically |
|
82 | 82 | used with a script's __file__ variable as startPath. The base of startPath |
|
83 | 83 | is then prepended to all the listed files, forming the output list. |
|
84 | 84 | |
|
85 | 85 | Parameters |
|
86 | 86 | ---------- |
|
87 | 87 | startPath : string |
|
88 | 88 | Initial path to use as the base for the results. This path is split |
|
89 | 89 | using os.path.split() and only its first component is kept. |
|
90 | 90 | |
|
91 | 91 | files : string or list |
|
92 | 92 | One or more files. |
|
93 | 93 | |
|
94 | 94 | Examples |
|
95 | 95 | -------- |
|
96 | 96 | |
|
97 | 97 | >>> full_path('/foo/bar.py',['a.txt','b.txt']) |
|
98 | 98 | ['/foo/a.txt', '/foo/b.txt'] |
|
99 | 99 | |
|
100 | 100 | >>> full_path('/foo',['a.txt','b.txt']) |
|
101 | 101 | ['/a.txt', '/b.txt'] |
|
102 | 102 | |
|
103 | 103 | If a single file is given, the output is still a list: |
|
104 | 104 | >>> full_path('/foo','a.txt') |
|
105 | 105 | ['/a.txt'] |
|
106 | 106 | """ |
|
107 | 107 | |
|
108 | 108 | files = list_strings(files) |
|
109 | 109 | base = os.path.split(startPath)[0] |
|
110 | 110 | return [ os.path.join(base,f) for f in files ] |
|
111 | 111 | |
|
112 | 112 | |
|
113 | 113 | def parse_test_output(txt): |
|
114 | 114 | """Parse the output of a test run and return errors, failures. |
|
115 | 115 | |
|
116 | 116 | Parameters |
|
117 | 117 | ---------- |
|
118 | 118 | txt : str |
|
119 | 119 | Text output of a test run, assumed to contain a line of one of the |
|
120 | 120 | following forms:: |
|
121 | 121 | 'FAILED (errors=1)' |
|
122 | 122 | 'FAILED (failures=1)' |
|
123 | 123 | 'FAILED (errors=1, failures=1)' |
|
124 | 124 | |
|
125 | 125 | Returns |
|
126 | 126 | ------- |
|
127 | 127 | nerr, nfail: number of errors and failures. |
|
128 | 128 | """ |
|
129 | 129 | |
|
130 | 130 | err_m = re.search(r'^FAILED \(errors=(\d+)\)', txt, re.MULTILINE) |
|
131 | 131 | if err_m: |
|
132 | 132 | nerr = int(err_m.group(1)) |
|
133 | 133 | nfail = 0 |
|
134 | 134 | return nerr, nfail |
|
135 | 135 | |
|
136 | 136 | fail_m = re.search(r'^FAILED \(failures=(\d+)\)', txt, re.MULTILINE) |
|
137 | 137 | if fail_m: |
|
138 | 138 | nerr = 0 |
|
139 | 139 | nfail = int(fail_m.group(1)) |
|
140 | 140 | return nerr, nfail |
|
141 | 141 | |
|
142 | 142 | both_m = re.search(r'^FAILED \(errors=(\d+), failures=(\d+)\)', txt, |
|
143 | 143 | re.MULTILINE) |
|
144 | 144 | if both_m: |
|
145 | 145 | nerr = int(both_m.group(1)) |
|
146 | 146 | nfail = int(both_m.group(2)) |
|
147 | 147 | return nerr, nfail |
|
148 | 148 | |
|
149 | 149 | # If the input didn't match any of these forms, assume no error/failures |
|
150 | 150 | return 0, 0 |
|
151 | 151 | |
|
152 | 152 | |
|
153 | 153 | # So nose doesn't think this is a test |
|
154 | 154 | parse_test_output.__test__ = False |
|
155 | 155 | |
|
156 | 156 | |
|
157 | 157 | def default_argv(): |
|
158 | 158 | """Return a valid default argv for creating testing instances of ipython""" |
|
159 | 159 | |
|
160 | 160 | return ['--quick', # so no config file is loaded |
|
161 | 161 | # Other defaults to minimize side effects on stdout |
|
162 | 162 | '--colors=NoColor', '--no-term-title','--no-banner', |
|
163 | 163 | '--autocall=0'] |
|
164 | 164 | |
|
165 | 165 | |
|
166 | 166 | def default_config(): |
|
167 | 167 | """Return a config object with good defaults for testing.""" |
|
168 | 168 | config = Config() |
|
169 | 169 | config.TerminalInteractiveShell.colors = 'NoColor' |
|
170 | 170 | config.TerminalTerminalInteractiveShell.term_title = False, |
|
171 | 171 | config.TerminalInteractiveShell.autocall = 0 |
|
172 | 172 | config.HistoryManager.hist_file = tempfile.mktemp(u'test_hist.sqlite') |
|
173 | 173 | config.HistoryManager.db_cache_size = 10000 |
|
174 | 174 | return config |
|
175 | 175 | |
|
176 | 176 | |
|
177 | 177 | def ipexec(fname, options=None): |
|
178 | 178 | """Utility to call 'ipython filename'. |
|
179 | 179 | |
|
180 | 180 | Starts IPython witha minimal and safe configuration to make startup as fast |
|
181 | 181 | as possible. |
|
182 | 182 | |
|
183 | 183 | Note that this starts IPython in a subprocess! |
|
184 | 184 | |
|
185 | 185 | Parameters |
|
186 | 186 | ---------- |
|
187 | 187 | fname : str |
|
188 | 188 | Name of file to be executed (should have .py or .ipy extension). |
|
189 | 189 | |
|
190 | 190 | options : optional, list |
|
191 | 191 | Extra command-line flags to be passed to IPython. |
|
192 | 192 | |
|
193 | 193 | Returns |
|
194 | 194 | ------- |
|
195 | 195 | (stdout, stderr) of ipython subprocess. |
|
196 | 196 | """ |
|
197 | 197 | if options is None: options = [] |
|
198 | 198 | |
|
199 | 199 | # For these subprocess calls, eliminate all prompt printing so we only see |
|
200 | 200 | # output from script execution |
|
201 | 201 | prompt_opts = [ '--PromptManager.in_template=""', |
|
202 | 202 | '--PromptManager.in2_template=""', |
|
203 | 203 | '--PromptManager.out_template=""' |
|
204 | 204 | ] |
|
205 | 205 | cmdargs = ' '.join(default_argv() + prompt_opts + options) |
|
206 | 206 | |
|
207 | 207 | _ip = get_ipython() |
|
208 | 208 | test_dir = os.path.dirname(__file__) |
|
209 | 209 | |
|
210 | 210 | ipython_cmd = find_cmd('ipython3' if py3compat.PY3 else 'ipython') |
|
211 | 211 | # Absolute path for filename |
|
212 | 212 | full_fname = os.path.join(test_dir, fname) |
|
213 | 213 | full_cmd = '%s %s %s' % (ipython_cmd, cmdargs, full_fname) |
|
214 | 214 | #print >> sys.stderr, 'FULL CMD:', full_cmd # dbg |
|
215 | 215 | out, err = getoutputerror(full_cmd) |
|
216 | 216 | # `import readline` causes 'ESC[?1034h' to be output sometimes, |
|
217 | 217 | # so strip that out before doing comparisons |
|
218 | 218 | if out: |
|
219 | 219 | out = re.sub(r'\x1b\[[^h]+h', '', out) |
|
220 | 220 | return out, err |
|
221 | 221 | |
|
222 | 222 | |
|
223 | 223 | def ipexec_validate(fname, expected_out, expected_err='', |
|
224 | 224 | options=None): |
|
225 | 225 | """Utility to call 'ipython filename' and validate output/error. |
|
226 | 226 | |
|
227 | 227 | This function raises an AssertionError if the validation fails. |
|
228 | 228 | |
|
229 | 229 | Note that this starts IPython in a subprocess! |
|
230 | 230 | |
|
231 | 231 | Parameters |
|
232 | 232 | ---------- |
|
233 | 233 | fname : str |
|
234 | 234 | Name of the file to be executed (should have .py or .ipy extension). |
|
235 | 235 | |
|
236 | 236 | expected_out : str |
|
237 | 237 | Expected stdout of the process. |
|
238 | 238 | |
|
239 | 239 | expected_err : optional, str |
|
240 | 240 | Expected stderr of the process. |
|
241 | 241 | |
|
242 | 242 | options : optional, list |
|
243 | 243 | Extra command-line flags to be passed to IPython. |
|
244 | 244 | |
|
245 | 245 | Returns |
|
246 | 246 | ------- |
|
247 | 247 | None |
|
248 | 248 | """ |
|
249 | 249 | |
|
250 | 250 | import nose.tools as nt |
|
251 | 251 | |
|
252 | 252 | out, err = ipexec(fname, options) |
|
253 | 253 | #print 'OUT', out # dbg |
|
254 | 254 | #print 'ERR', err # dbg |
|
255 | 255 | # If there are any errors, we must check those befor stdout, as they may be |
|
256 | 256 | # more informative than simply having an empty stdout. |
|
257 | 257 | if err: |
|
258 | 258 | if expected_err: |
|
259 |
nt.assert_equal |
|
|
259 | nt.assert_equal(err.strip(), expected_err.strip()) | |
|
260 | 260 | else: |
|
261 | 261 | raise ValueError('Running file %r produced error: %r' % |
|
262 | 262 | (fname, err)) |
|
263 | 263 | # If no errors or output on stderr was expected, match stdout |
|
264 |
nt.assert_equal |
|
|
264 | nt.assert_equal(out.strip(), expected_out.strip()) | |
|
265 | 265 | |
|
266 | 266 | |
|
267 | 267 | class TempFileMixin(object): |
|
268 | 268 | """Utility class to create temporary Python/IPython files. |
|
269 | 269 | |
|
270 | 270 | Meant as a mixin class for test cases.""" |
|
271 | 271 | |
|
272 | 272 | def mktmp(self, src, ext='.py'): |
|
273 | 273 | """Make a valid python temp file.""" |
|
274 | 274 | fname, f = temp_pyfile(src, ext) |
|
275 | 275 | self.tmpfile = f |
|
276 | 276 | self.fname = fname |
|
277 | 277 | |
|
278 | 278 | def tearDown(self): |
|
279 | 279 | if hasattr(self, 'tmpfile'): |
|
280 | 280 | # If the tmpfile wasn't made because of skipped tests, like in |
|
281 | 281 | # win32, there's nothing to cleanup. |
|
282 | 282 | self.tmpfile.close() |
|
283 | 283 | try: |
|
284 | 284 | os.unlink(self.fname) |
|
285 | 285 | except: |
|
286 | 286 | # On Windows, even though we close the file, we still can't |
|
287 | 287 | # delete it. I have no clue why |
|
288 | 288 | pass |
|
289 | 289 | |
|
290 | 290 | pair_fail_msg = ("Testing {0}\n\n" |
|
291 | 291 | "In:\n" |
|
292 | 292 | " {1!r}\n" |
|
293 | 293 | "Expected:\n" |
|
294 | 294 | " {2!r}\n" |
|
295 | 295 | "Got:\n" |
|
296 | 296 | " {3!r}\n") |
|
297 | 297 | def check_pairs(func, pairs): |
|
298 | 298 | """Utility function for the common case of checking a function with a |
|
299 | 299 | sequence of input/output pairs. |
|
300 | 300 | |
|
301 | 301 | Parameters |
|
302 | 302 | ---------- |
|
303 | 303 | func : callable |
|
304 | 304 | The function to be tested. Should accept a single argument. |
|
305 | 305 | pairs : iterable |
|
306 | 306 | A list of (input, expected_output) tuples. |
|
307 | 307 | |
|
308 | 308 | Returns |
|
309 | 309 | ------- |
|
310 | 310 | None. Raises an AssertionError if any output does not match the expected |
|
311 | 311 | value. |
|
312 | 312 | """ |
|
313 | 313 | name = getattr(func, "func_name", getattr(func, "__name__", "<unknown>")) |
|
314 | 314 | for inp, expected in pairs: |
|
315 | 315 | out = func(inp) |
|
316 | 316 | assert out == expected, pair_fail_msg.format(name, inp, expected, out) |
|
317 | 317 | |
|
318 | 318 | |
|
319 | 319 | if py3compat.PY3: |
|
320 | 320 | MyStringIO = StringIO |
|
321 | 321 | else: |
|
322 | 322 | # In Python 2, stdout/stderr can have either bytes or unicode written to them, |
|
323 | 323 | # so we need a class that can handle both. |
|
324 | 324 | class MyStringIO(StringIO): |
|
325 | 325 | def write(self, s): |
|
326 | 326 | s = py3compat.cast_unicode(s, encoding=DEFAULT_ENCODING) |
|
327 | 327 | super(MyStringIO, self).write(s) |
|
328 | 328 | |
|
329 | 329 | notprinted_msg = """Did not find {0!r} in printed output (on {1}): |
|
330 | 330 | {2!r}""" |
|
331 | 331 | |
|
332 | 332 | class AssertPrints(object): |
|
333 | 333 | """Context manager for testing that code prints certain text. |
|
334 | 334 | |
|
335 | 335 | Examples |
|
336 | 336 | -------- |
|
337 | 337 | >>> with AssertPrints("abc", suppress=False): |
|
338 | 338 | ... print "abcd" |
|
339 | 339 | ... print "def" |
|
340 | 340 | ... |
|
341 | 341 | abcd |
|
342 | 342 | def |
|
343 | 343 | """ |
|
344 | 344 | def __init__(self, s, channel='stdout', suppress=True): |
|
345 | 345 | self.s = s |
|
346 | 346 | self.channel = channel |
|
347 | 347 | self.suppress = suppress |
|
348 | 348 | |
|
349 | 349 | def __enter__(self): |
|
350 | 350 | self.orig_stream = getattr(sys, self.channel) |
|
351 | 351 | self.buffer = MyStringIO() |
|
352 | 352 | self.tee = Tee(self.buffer, channel=self.channel) |
|
353 | 353 | setattr(sys, self.channel, self.buffer if self.suppress else self.tee) |
|
354 | 354 | |
|
355 | 355 | def __exit__(self, etype, value, traceback): |
|
356 | 356 | self.tee.flush() |
|
357 | 357 | setattr(sys, self.channel, self.orig_stream) |
|
358 | 358 | printed = self.buffer.getvalue() |
|
359 | 359 | assert self.s in printed, notprinted_msg.format(self.s, self.channel, printed) |
|
360 | 360 | return False |
|
361 | 361 | |
|
362 | 362 | class AssertNotPrints(AssertPrints): |
|
363 | 363 | """Context manager for checking that certain output *isn't* produced. |
|
364 | 364 | |
|
365 | 365 | Counterpart of AssertPrints""" |
|
366 | 366 | def __exit__(self, etype, value, traceback): |
|
367 | 367 | self.tee.flush() |
|
368 | 368 | setattr(sys, self.channel, self.orig_stream) |
|
369 | 369 | printed = self.buffer.getvalue() |
|
370 | 370 | assert self.s not in printed, notprinted_msg.format(self.s, self.channel, printed) |
|
371 | 371 | return False |
|
372 | 372 | |
|
373 | 373 | @contextmanager |
|
374 | 374 | def mute_warn(): |
|
375 | 375 | from IPython.utils import warn |
|
376 | 376 | save_warn = warn.warn |
|
377 | 377 | warn.warn = lambda *a, **kw: None |
|
378 | 378 | try: |
|
379 | 379 | yield |
|
380 | 380 | finally: |
|
381 | 381 | warn.warn = save_warn |
|
382 | 382 | |
|
383 | 383 | @contextmanager |
|
384 | 384 | def make_tempfile(name): |
|
385 | 385 | """ Create an empty, named, temporary file for the duration of the context. |
|
386 | 386 | """ |
|
387 | 387 | f = open(name, 'w') |
|
388 | 388 | f.close() |
|
389 | 389 | try: |
|
390 | 390 | yield |
|
391 | 391 | finally: |
|
392 | 392 | os.unlink(name) |
@@ -1,86 +1,86 | |||
|
1 | 1 | # encoding: utf-8 |
|
2 | 2 | """Tests for io.py""" |
|
3 | 3 | |
|
4 | 4 | #----------------------------------------------------------------------------- |
|
5 | 5 | # Copyright (C) 2008-2011 The IPython Development Team |
|
6 | 6 | # |
|
7 | 7 | # Distributed under the terms of the BSD License. The full license is in |
|
8 | 8 | # the file COPYING, distributed as part of this software. |
|
9 | 9 | #----------------------------------------------------------------------------- |
|
10 | 10 | |
|
11 | 11 | #----------------------------------------------------------------------------- |
|
12 | 12 | # Imports |
|
13 | 13 | #----------------------------------------------------------------------------- |
|
14 | 14 | from __future__ import print_function |
|
15 | 15 | |
|
16 | 16 | import sys |
|
17 | 17 | |
|
18 | 18 | from StringIO import StringIO |
|
19 | 19 | from subprocess import Popen, PIPE |
|
20 | 20 | |
|
21 | 21 | import nose.tools as nt |
|
22 | 22 | |
|
23 | 23 | from IPython.testing import decorators as dec |
|
24 | 24 | from IPython.utils.io import Tee, capture_output |
|
25 | 25 | from IPython.utils.py3compat import doctest_refactor_print |
|
26 | 26 | |
|
27 | 27 | #----------------------------------------------------------------------------- |
|
28 | 28 | # Tests |
|
29 | 29 | #----------------------------------------------------------------------------- |
|
30 | 30 | |
|
31 | 31 | |
|
32 | 32 | def test_tee_simple(): |
|
33 | 33 | "Very simple check with stdout only" |
|
34 | 34 | chan = StringIO() |
|
35 | 35 | text = 'Hello' |
|
36 | 36 | tee = Tee(chan, channel='stdout') |
|
37 | 37 | print(text, file=chan) |
|
38 | 38 | nt.assert_equal(chan.getvalue(), text+"\n") |
|
39 | 39 | |
|
40 | 40 | |
|
41 | 41 | class TeeTestCase(dec.ParametricTestCase): |
|
42 | 42 | |
|
43 | 43 | def tchan(self, channel, check='close'): |
|
44 | 44 | trap = StringIO() |
|
45 | 45 | chan = StringIO() |
|
46 | 46 | text = 'Hello' |
|
47 | 47 | |
|
48 | 48 | std_ori = getattr(sys, channel) |
|
49 | 49 | setattr(sys, channel, trap) |
|
50 | 50 | |
|
51 | 51 | tee = Tee(chan, channel=channel) |
|
52 | 52 | print(text, end='', file=chan) |
|
53 | 53 | setattr(sys, channel, std_ori) |
|
54 | 54 | trap_val = trap.getvalue() |
|
55 |
nt.assert_equal |
|
|
55 | nt.assert_equal(chan.getvalue(), text) | |
|
56 | 56 | if check=='close': |
|
57 | 57 | tee.close() |
|
58 | 58 | else: |
|
59 | 59 | del tee |
|
60 | 60 | |
|
61 | 61 | def test(self): |
|
62 | 62 | for chan in ['stdout', 'stderr']: |
|
63 | 63 | for check in ['close', 'del']: |
|
64 | 64 | yield self.tchan(chan, check) |
|
65 | 65 | |
|
66 | 66 | def test_io_init(): |
|
67 | 67 | """Test that io.stdin/out/err exist at startup""" |
|
68 | 68 | for name in ('stdin', 'stdout', 'stderr'): |
|
69 | 69 | cmd = doctest_refactor_print("from IPython.utils import io;print io.%s.__class__"%name) |
|
70 | 70 | p = Popen([sys.executable, '-c', cmd], |
|
71 | 71 | stdout=PIPE) |
|
72 | 72 | p.wait() |
|
73 | 73 | classname = p.stdout.read().strip().decode('ascii') |
|
74 | 74 | # __class__ is a reference to the class object in Python 3, so we can't |
|
75 | 75 | # just test for string equality. |
|
76 | 76 | assert 'IPython.utils.io.IOStream' in classname, classname |
|
77 | 77 | |
|
78 | 78 | def test_capture_output(): |
|
79 | 79 | """capture_output() context works""" |
|
80 | 80 | |
|
81 | 81 | with capture_output() as io: |
|
82 | 82 | print('hi, stdout') |
|
83 | 83 | print('hi, stderr', file=sys.stderr) |
|
84 | 84 | |
|
85 |
nt.assert_equal |
|
|
86 |
nt.assert_equal |
|
|
85 | nt.assert_equal(io.stdout, 'hi, stdout\n') | |
|
86 | nt.assert_equal(io.stderr, 'hi, stderr\n') |
@@ -1,446 +1,446 | |||
|
1 | 1 | # encoding: utf-8 |
|
2 | 2 | """Tests for IPython.utils.path.py""" |
|
3 | 3 | |
|
4 | 4 | #----------------------------------------------------------------------------- |
|
5 | 5 | # Copyright (C) 2008-2011 The IPython Development Team |
|
6 | 6 | # |
|
7 | 7 | # Distributed under the terms of the BSD License. The full license is in |
|
8 | 8 | # the file COPYING, distributed as part of this software. |
|
9 | 9 | #----------------------------------------------------------------------------- |
|
10 | 10 | |
|
11 | 11 | #----------------------------------------------------------------------------- |
|
12 | 12 | # Imports |
|
13 | 13 | #----------------------------------------------------------------------------- |
|
14 | 14 | |
|
15 | 15 | from __future__ import with_statement |
|
16 | 16 | |
|
17 | 17 | import os |
|
18 | 18 | import shutil |
|
19 | 19 | import sys |
|
20 | 20 | import tempfile |
|
21 | 21 | from io import StringIO |
|
22 | 22 | |
|
23 | 23 | from os.path import join, abspath, split |
|
24 | 24 | |
|
25 | 25 | import nose.tools as nt |
|
26 | 26 | |
|
27 | 27 | from nose import with_setup |
|
28 | 28 | |
|
29 | 29 | import IPython |
|
30 | 30 | from IPython.testing import decorators as dec |
|
31 | 31 | from IPython.testing.decorators import skip_if_not_win32, skip_win32 |
|
32 | 32 | from IPython.testing.tools import make_tempfile, AssertPrints |
|
33 | 33 | from IPython.utils import path, io |
|
34 | 34 | from IPython.utils import py3compat |
|
35 | 35 | |
|
36 | 36 | # Platform-dependent imports |
|
37 | 37 | try: |
|
38 | 38 | import _winreg as wreg |
|
39 | 39 | except ImportError: |
|
40 | 40 | #Fake _winreg module on none windows platforms |
|
41 | 41 | import types |
|
42 | 42 | wr_name = "winreg" if py3compat.PY3 else "_winreg" |
|
43 | 43 | sys.modules[wr_name] = types.ModuleType(wr_name) |
|
44 | 44 | import _winreg as wreg |
|
45 | 45 | #Add entries that needs to be stubbed by the testing code |
|
46 | 46 | (wreg.OpenKey, wreg.QueryValueEx,) = (None, None) |
|
47 | 47 | |
|
48 | 48 | try: |
|
49 | 49 | reload |
|
50 | 50 | except NameError: # Python 3 |
|
51 | 51 | from imp import reload |
|
52 | 52 | |
|
53 | 53 | #----------------------------------------------------------------------------- |
|
54 | 54 | # Globals |
|
55 | 55 | #----------------------------------------------------------------------------- |
|
56 | 56 | env = os.environ |
|
57 | 57 | TEST_FILE_PATH = split(abspath(__file__))[0] |
|
58 | 58 | TMP_TEST_DIR = tempfile.mkdtemp() |
|
59 | 59 | HOME_TEST_DIR = join(TMP_TEST_DIR, "home_test_dir") |
|
60 | 60 | XDG_TEST_DIR = join(HOME_TEST_DIR, "xdg_test_dir") |
|
61 | 61 | IP_TEST_DIR = join(HOME_TEST_DIR,'.ipython') |
|
62 | 62 | # |
|
63 | 63 | # Setup/teardown functions/decorators |
|
64 | 64 | # |
|
65 | 65 | |
|
66 | 66 | def setup(): |
|
67 | 67 | """Setup testenvironment for the module: |
|
68 | 68 | |
|
69 | 69 | - Adds dummy home dir tree |
|
70 | 70 | """ |
|
71 | 71 | # Do not mask exceptions here. In particular, catching WindowsError is a |
|
72 | 72 | # problem because that exception is only defined on Windows... |
|
73 | 73 | os.makedirs(IP_TEST_DIR) |
|
74 | 74 | os.makedirs(os.path.join(XDG_TEST_DIR, 'ipython')) |
|
75 | 75 | |
|
76 | 76 | |
|
77 | 77 | def teardown(): |
|
78 | 78 | """Teardown testenvironment for the module: |
|
79 | 79 | |
|
80 | 80 | - Remove dummy home dir tree |
|
81 | 81 | """ |
|
82 | 82 | # Note: we remove the parent test dir, which is the root of all test |
|
83 | 83 | # subdirs we may have created. Use shutil instead of os.removedirs, so |
|
84 | 84 | # that non-empty directories are all recursively removed. |
|
85 | 85 | shutil.rmtree(TMP_TEST_DIR) |
|
86 | 86 | |
|
87 | 87 | |
|
88 | 88 | def setup_environment(): |
|
89 | 89 | """Setup testenvironment for some functions that are tested |
|
90 | 90 | in this module. In particular this functions stores attributes |
|
91 | 91 | and other things that we need to stub in some test functions. |
|
92 | 92 | This needs to be done on a function level and not module level because |
|
93 | 93 | each testfunction needs a pristine environment. |
|
94 | 94 | """ |
|
95 | 95 | global oldstuff, platformstuff |
|
96 | 96 | oldstuff = (env.copy(), os.name, sys.platform, path.get_home_dir, IPython.__file__, os.getcwd()) |
|
97 | 97 | |
|
98 | 98 | if os.name == 'nt': |
|
99 | 99 | platformstuff = (wreg.OpenKey, wreg.QueryValueEx,) |
|
100 | 100 | |
|
101 | 101 | |
|
102 | 102 | def teardown_environment(): |
|
103 | 103 | """Restore things that were remebered by the setup_environment function |
|
104 | 104 | """ |
|
105 | 105 | (oldenv, os.name, sys.platform, path.get_home_dir, IPython.__file__, old_wd) = oldstuff |
|
106 | 106 | os.chdir(old_wd) |
|
107 | 107 | reload(path) |
|
108 | 108 | |
|
109 | 109 | for key in env.keys(): |
|
110 | 110 | if key not in oldenv: |
|
111 | 111 | del env[key] |
|
112 | 112 | env.update(oldenv) |
|
113 | 113 | if hasattr(sys, 'frozen'): |
|
114 | 114 | del sys.frozen |
|
115 | 115 | if os.name == 'nt': |
|
116 | 116 | (wreg.OpenKey, wreg.QueryValueEx,) = platformstuff |
|
117 | 117 | |
|
118 | 118 | # Build decorator that uses the setup_environment/setup_environment |
|
119 | 119 | with_environment = with_setup(setup_environment, teardown_environment) |
|
120 | 120 | |
|
121 | 121 | @skip_if_not_win32 |
|
122 | 122 | @with_environment |
|
123 | 123 | def test_get_home_dir_1(): |
|
124 | 124 | """Testcase for py2exe logic, un-compressed lib |
|
125 | 125 | """ |
|
126 | 126 | sys.frozen = True |
|
127 | 127 | |
|
128 | 128 | #fake filename for IPython.__init__ |
|
129 | 129 | IPython.__file__ = abspath(join(HOME_TEST_DIR, "Lib/IPython/__init__.py")) |
|
130 | 130 | |
|
131 | 131 | home_dir = path.get_home_dir() |
|
132 | 132 | nt.assert_equal(home_dir, abspath(HOME_TEST_DIR)) |
|
133 | 133 | |
|
134 | 134 | |
|
135 | 135 | @skip_if_not_win32 |
|
136 | 136 | @with_environment |
|
137 | 137 | def test_get_home_dir_2(): |
|
138 | 138 | """Testcase for py2exe logic, compressed lib |
|
139 | 139 | """ |
|
140 | 140 | sys.frozen = True |
|
141 | 141 | #fake filename for IPython.__init__ |
|
142 | 142 | IPython.__file__ = abspath(join(HOME_TEST_DIR, "Library.zip/IPython/__init__.py")).lower() |
|
143 | 143 | |
|
144 | 144 | home_dir = path.get_home_dir(True) |
|
145 | 145 | nt.assert_equal(home_dir, abspath(HOME_TEST_DIR).lower()) |
|
146 | 146 | |
|
147 | 147 | |
|
148 | 148 | @with_environment |
|
149 | 149 | def test_get_home_dir_3(): |
|
150 | 150 | """get_home_dir() uses $HOME if set""" |
|
151 | 151 | env["HOME"] = HOME_TEST_DIR |
|
152 | 152 | home_dir = path.get_home_dir(True) |
|
153 | 153 | # get_home_dir expands symlinks |
|
154 | 154 | nt.assert_equal(home_dir, os.path.realpath(env["HOME"])) |
|
155 | 155 | |
|
156 | 156 | |
|
157 | 157 | @with_environment |
|
158 | 158 | def test_get_home_dir_4(): |
|
159 | 159 | """get_home_dir() still works if $HOME is not set""" |
|
160 | 160 | |
|
161 | 161 | if 'HOME' in env: del env['HOME'] |
|
162 | 162 | # this should still succeed, but we don't care what the answer is |
|
163 | 163 | home = path.get_home_dir(False) |
|
164 | 164 | |
|
165 | 165 | @with_environment |
|
166 | 166 | def test_get_home_dir_5(): |
|
167 | 167 | """raise HomeDirError if $HOME is specified, but not a writable dir""" |
|
168 | 168 | env['HOME'] = abspath(HOME_TEST_DIR+'garbage') |
|
169 | 169 | # set os.name = posix, to prevent My Documents fallback on Windows |
|
170 | 170 | os.name = 'posix' |
|
171 | 171 | nt.assert_raises(path.HomeDirError, path.get_home_dir, True) |
|
172 | 172 | |
|
173 | 173 | |
|
174 | 174 | # Should we stub wreg fully so we can run the test on all platforms? |
|
175 | 175 | @skip_if_not_win32 |
|
176 | 176 | @with_environment |
|
177 | 177 | def test_get_home_dir_8(): |
|
178 | 178 | """Using registry hack for 'My Documents', os=='nt' |
|
179 | 179 | |
|
180 | 180 | HOMESHARE, HOMEDRIVE, HOMEPATH, USERPROFILE and others are missing. |
|
181 | 181 | """ |
|
182 | 182 | os.name = 'nt' |
|
183 | 183 | # Remove from stub environment all keys that may be set |
|
184 | 184 | for key in ['HOME', 'HOMESHARE', 'HOMEDRIVE', 'HOMEPATH', 'USERPROFILE']: |
|
185 | 185 | env.pop(key, None) |
|
186 | 186 | |
|
187 | 187 | #Stub windows registry functions |
|
188 | 188 | def OpenKey(x, y): |
|
189 | 189 | class key: |
|
190 | 190 | def Close(self): |
|
191 | 191 | pass |
|
192 | 192 | return key() |
|
193 | 193 | def QueryValueEx(x, y): |
|
194 | 194 | return [abspath(HOME_TEST_DIR)] |
|
195 | 195 | |
|
196 | 196 | wreg.OpenKey = OpenKey |
|
197 | 197 | wreg.QueryValueEx = QueryValueEx |
|
198 | 198 | |
|
199 | 199 | home_dir = path.get_home_dir() |
|
200 | 200 | nt.assert_equal(home_dir, abspath(HOME_TEST_DIR)) |
|
201 | 201 | |
|
202 | 202 | |
|
203 | 203 | @with_environment |
|
204 | 204 | def test_get_ipython_dir_1(): |
|
205 | 205 | """test_get_ipython_dir_1, Testcase to see if we can call get_ipython_dir without Exceptions.""" |
|
206 | 206 | env_ipdir = os.path.join("someplace", ".ipython") |
|
207 | 207 | path._writable_dir = lambda path: True |
|
208 | 208 | env['IPYTHONDIR'] = env_ipdir |
|
209 | 209 | ipdir = path.get_ipython_dir() |
|
210 | 210 | nt.assert_equal(ipdir, env_ipdir) |
|
211 | 211 | |
|
212 | 212 | |
|
213 | 213 | @with_environment |
|
214 | 214 | def test_get_ipython_dir_2(): |
|
215 | 215 | """test_get_ipython_dir_2, Testcase to see if we can call get_ipython_dir without Exceptions.""" |
|
216 | 216 | path.get_home_dir = lambda : "someplace" |
|
217 | 217 | path.get_xdg_dir = lambda : None |
|
218 | 218 | path._writable_dir = lambda path: True |
|
219 | 219 | os.name = "posix" |
|
220 | 220 | env.pop('IPYTHON_DIR', None) |
|
221 | 221 | env.pop('IPYTHONDIR', None) |
|
222 | 222 | env.pop('XDG_CONFIG_HOME', None) |
|
223 | 223 | ipdir = path.get_ipython_dir() |
|
224 | 224 | nt.assert_equal(ipdir, os.path.join("someplace", ".ipython")) |
|
225 | 225 | |
|
226 | 226 | @with_environment |
|
227 | 227 | def test_get_ipython_dir_3(): |
|
228 | 228 | """test_get_ipython_dir_3, use XDG if defined, and .ipython doesn't exist.""" |
|
229 | 229 | path.get_home_dir = lambda : "someplace" |
|
230 | 230 | path._writable_dir = lambda path: True |
|
231 | 231 | os.name = "posix" |
|
232 | 232 | env.pop('IPYTHON_DIR', None) |
|
233 | 233 | env.pop('IPYTHONDIR', None) |
|
234 | 234 | env['XDG_CONFIG_HOME'] = XDG_TEST_DIR |
|
235 | 235 | ipdir = path.get_ipython_dir() |
|
236 | 236 | if sys.platform == "darwin": |
|
237 | 237 | expected = os.path.join("someplace", ".ipython") |
|
238 | 238 | else: |
|
239 | 239 | expected = os.path.join(XDG_TEST_DIR, "ipython") |
|
240 | 240 | nt.assert_equal(ipdir, expected) |
|
241 | 241 | |
|
242 | 242 | @with_environment |
|
243 | 243 | def test_get_ipython_dir_4(): |
|
244 | 244 | """test_get_ipython_dir_4, use XDG if both exist.""" |
|
245 | 245 | path.get_home_dir = lambda : HOME_TEST_DIR |
|
246 | 246 | os.name = "posix" |
|
247 | 247 | env.pop('IPYTHON_DIR', None) |
|
248 | 248 | env.pop('IPYTHONDIR', None) |
|
249 | 249 | env['XDG_CONFIG_HOME'] = XDG_TEST_DIR |
|
250 | 250 | ipdir = path.get_ipython_dir() |
|
251 | 251 | if sys.platform == "darwin": |
|
252 | 252 | expected = os.path.join(HOME_TEST_DIR, ".ipython") |
|
253 | 253 | else: |
|
254 | 254 | expected = os.path.join(XDG_TEST_DIR, "ipython") |
|
255 | 255 | nt.assert_equal(ipdir, expected) |
|
256 | 256 | |
|
257 | 257 | @with_environment |
|
258 | 258 | def test_get_ipython_dir_5(): |
|
259 | 259 | """test_get_ipython_dir_5, use .ipython if exists and XDG defined, but doesn't exist.""" |
|
260 | 260 | path.get_home_dir = lambda : HOME_TEST_DIR |
|
261 | 261 | os.name = "posix" |
|
262 | 262 | env.pop('IPYTHON_DIR', None) |
|
263 | 263 | env.pop('IPYTHONDIR', None) |
|
264 | 264 | env['XDG_CONFIG_HOME'] = XDG_TEST_DIR |
|
265 | 265 | os.rmdir(os.path.join(XDG_TEST_DIR, 'ipython')) |
|
266 | 266 | ipdir = path.get_ipython_dir() |
|
267 | 267 | nt.assert_equal(ipdir, IP_TEST_DIR) |
|
268 | 268 | |
|
269 | 269 | @with_environment |
|
270 | 270 | def test_get_ipython_dir_6(): |
|
271 | 271 | """test_get_ipython_dir_6, use XDG if defined and neither exist.""" |
|
272 | 272 | xdg = os.path.join(HOME_TEST_DIR, 'somexdg') |
|
273 | 273 | os.mkdir(xdg) |
|
274 | 274 | shutil.rmtree(os.path.join(HOME_TEST_DIR, '.ipython')) |
|
275 | 275 | path.get_home_dir = lambda : HOME_TEST_DIR |
|
276 | 276 | path.get_xdg_dir = lambda : xdg |
|
277 | 277 | os.name = "posix" |
|
278 | 278 | env.pop('IPYTHON_DIR', None) |
|
279 | 279 | env.pop('IPYTHONDIR', None) |
|
280 | 280 | env.pop('XDG_CONFIG_HOME', None) |
|
281 | 281 | xdg_ipdir = os.path.join(xdg, "ipython") |
|
282 | 282 | ipdir = path.get_ipython_dir() |
|
283 | 283 | nt.assert_equal(ipdir, xdg_ipdir) |
|
284 | 284 | |
|
285 | 285 | @with_environment |
|
286 | 286 | def test_get_ipython_dir_7(): |
|
287 | 287 | """test_get_ipython_dir_7, test home directory expansion on IPYTHONDIR""" |
|
288 | 288 | path._writable_dir = lambda path: True |
|
289 | 289 | home_dir = os.path.normpath(os.path.expanduser('~')) |
|
290 | 290 | env['IPYTHONDIR'] = os.path.join('~', 'somewhere') |
|
291 | 291 | ipdir = path.get_ipython_dir() |
|
292 | 292 | nt.assert_equal(ipdir, os.path.join(home_dir, 'somewhere')) |
|
293 | 293 | |
|
294 | 294 | |
|
295 | 295 | @with_environment |
|
296 | 296 | def test_get_xdg_dir_0(): |
|
297 | 297 | """test_get_xdg_dir_0, check xdg_dir""" |
|
298 | 298 | reload(path) |
|
299 | 299 | path._writable_dir = lambda path: True |
|
300 | 300 | path.get_home_dir = lambda : 'somewhere' |
|
301 | 301 | os.name = "posix" |
|
302 | 302 | sys.platform = "linux2" |
|
303 | 303 | env.pop('IPYTHON_DIR', None) |
|
304 | 304 | env.pop('IPYTHONDIR', None) |
|
305 | 305 | env.pop('XDG_CONFIG_HOME', None) |
|
306 | 306 | |
|
307 | 307 | nt.assert_equal(path.get_xdg_dir(), os.path.join('somewhere', '.config')) |
|
308 | 308 | |
|
309 | 309 | |
|
310 | 310 | @with_environment |
|
311 | 311 | def test_get_xdg_dir_1(): |
|
312 | 312 | """test_get_xdg_dir_1, check nonexistant xdg_dir""" |
|
313 | 313 | reload(path) |
|
314 | 314 | path.get_home_dir = lambda : HOME_TEST_DIR |
|
315 | 315 | os.name = "posix" |
|
316 | 316 | sys.platform = "linux2" |
|
317 | 317 | env.pop('IPYTHON_DIR', None) |
|
318 | 318 | env.pop('IPYTHONDIR', None) |
|
319 | 319 | env.pop('XDG_CONFIG_HOME', None) |
|
320 | 320 | nt.assert_equal(path.get_xdg_dir(), None) |
|
321 | 321 | |
|
322 | 322 | @with_environment |
|
323 | 323 | def test_get_xdg_dir_2(): |
|
324 | 324 | """test_get_xdg_dir_2, check xdg_dir default to ~/.config""" |
|
325 | 325 | reload(path) |
|
326 | 326 | path.get_home_dir = lambda : HOME_TEST_DIR |
|
327 | 327 | os.name = "posix" |
|
328 | 328 | sys.platform = "linux2" |
|
329 | 329 | env.pop('IPYTHON_DIR', None) |
|
330 | 330 | env.pop('IPYTHONDIR', None) |
|
331 | 331 | env.pop('XDG_CONFIG_HOME', None) |
|
332 | 332 | cfgdir=os.path.join(path.get_home_dir(), '.config') |
|
333 | 333 | if not os.path.exists(cfgdir): |
|
334 | 334 | os.makedirs(cfgdir) |
|
335 | 335 | |
|
336 | 336 | nt.assert_equal(path.get_xdg_dir(), cfgdir) |
|
337 | 337 | |
|
338 | 338 | @with_environment |
|
339 | 339 | def test_get_xdg_dir_3(): |
|
340 | 340 | """test_get_xdg_dir_3, check xdg_dir not used on OS X""" |
|
341 | 341 | reload(path) |
|
342 | 342 | path.get_home_dir = lambda : HOME_TEST_DIR |
|
343 | 343 | os.name = "posix" |
|
344 | 344 | sys.platform = "darwin" |
|
345 | 345 | env.pop('IPYTHON_DIR', None) |
|
346 | 346 | env.pop('IPYTHONDIR', None) |
|
347 | 347 | env.pop('XDG_CONFIG_HOME', None) |
|
348 | 348 | cfgdir=os.path.join(path.get_home_dir(), '.config') |
|
349 | 349 | if not os.path.exists(cfgdir): |
|
350 | 350 | os.makedirs(cfgdir) |
|
351 | 351 | |
|
352 | 352 | nt.assert_equal(path.get_xdg_dir(), None) |
|
353 | 353 | |
|
354 | 354 | def test_filefind(): |
|
355 | 355 | """Various tests for filefind""" |
|
356 | 356 | f = tempfile.NamedTemporaryFile() |
|
357 | 357 | # print 'fname:',f.name |
|
358 | 358 | alt_dirs = path.get_ipython_dir() |
|
359 | 359 | t = path.filefind(f.name, alt_dirs) |
|
360 | 360 | # print 'found:',t |
|
361 | 361 | |
|
362 | 362 | |
|
363 | 363 | def test_get_ipython_package_dir(): |
|
364 | 364 | ipdir = path.get_ipython_package_dir() |
|
365 | 365 | nt.assert_true(os.path.isdir(ipdir)) |
|
366 | 366 | |
|
367 | 367 | |
|
368 | 368 | def test_get_ipython_module_path(): |
|
369 | 369 | ipapp_path = path.get_ipython_module_path('IPython.frontend.terminal.ipapp') |
|
370 | 370 | nt.assert_true(os.path.isfile(ipapp_path)) |
|
371 | 371 | |
|
372 | 372 | |
|
373 | 373 | @dec.skip_if_not_win32 |
|
374 | 374 | def test_get_long_path_name_win32(): |
|
375 | 375 | p = path.get_long_path_name('c:\\docume~1') |
|
376 |
nt.assert_equal |
|
|
376 | nt.assert_equal(p,u'c:\\Documents and Settings') | |
|
377 | 377 | |
|
378 | 378 | |
|
379 | 379 | @dec.skip_win32 |
|
380 | 380 | def test_get_long_path_name(): |
|
381 | 381 | p = path.get_long_path_name('/usr/local') |
|
382 |
nt.assert_equal |
|
|
382 | nt.assert_equal(p,'/usr/local') | |
|
383 | 383 | |
|
384 | 384 | @dec.skip_win32 # can't create not-user-writable dir on win |
|
385 | 385 | @with_environment |
|
386 | 386 | def test_not_writable_ipdir(): |
|
387 | 387 | tmpdir = tempfile.mkdtemp() |
|
388 | 388 | os.name = "posix" |
|
389 | 389 | env.pop('IPYTHON_DIR', None) |
|
390 | 390 | env.pop('IPYTHONDIR', None) |
|
391 | 391 | env.pop('XDG_CONFIG_HOME', None) |
|
392 | 392 | env['HOME'] = tmpdir |
|
393 | 393 | ipdir = os.path.join(tmpdir, '.ipython') |
|
394 | 394 | os.mkdir(ipdir) |
|
395 | 395 | os.chmod(ipdir, 600) |
|
396 | 396 | with AssertPrints('is not a writable location', channel='stderr'): |
|
397 | 397 | ipdir = path.get_ipython_dir() |
|
398 | 398 | env.pop('IPYTHON_DIR', None) |
|
399 | 399 | |
|
400 | 400 | def test_unquote_filename(): |
|
401 | 401 | for win32 in (True, False): |
|
402 |
nt.assert_equal |
|
|
403 |
nt.assert_equal |
|
|
404 |
nt.assert_equal |
|
|
405 |
nt.assert_equal |
|
|
406 |
nt.assert_equal |
|
|
407 |
nt.assert_equal |
|
|
408 |
nt.assert_equal |
|
|
409 |
nt.assert_equal |
|
|
410 |
nt.assert_equal |
|
|
411 |
nt.assert_equal |
|
|
402 | nt.assert_equal(path.unquote_filename('foo.py', win32=win32), 'foo.py') | |
|
403 | nt.assert_equal(path.unquote_filename('foo bar.py', win32=win32), 'foo bar.py') | |
|
404 | nt.assert_equal(path.unquote_filename('"foo.py"', win32=True), 'foo.py') | |
|
405 | nt.assert_equal(path.unquote_filename('"foo bar.py"', win32=True), 'foo bar.py') | |
|
406 | nt.assert_equal(path.unquote_filename("'foo.py'", win32=True), 'foo.py') | |
|
407 | nt.assert_equal(path.unquote_filename("'foo bar.py'", win32=True), 'foo bar.py') | |
|
408 | nt.assert_equal(path.unquote_filename('"foo.py"', win32=False), '"foo.py"') | |
|
409 | nt.assert_equal(path.unquote_filename('"foo bar.py"', win32=False), '"foo bar.py"') | |
|
410 | nt.assert_equal(path.unquote_filename("'foo.py'", win32=False), "'foo.py'") | |
|
411 | nt.assert_equal(path.unquote_filename("'foo bar.py'", win32=False), "'foo bar.py'") | |
|
412 | 412 | |
|
413 | 413 | @with_environment |
|
414 | 414 | def test_get_py_filename(): |
|
415 | 415 | os.chdir(TMP_TEST_DIR) |
|
416 | 416 | for win32 in (True, False): |
|
417 | 417 | with make_tempfile('foo.py'): |
|
418 |
nt.assert_equal |
|
|
419 |
nt.assert_equal |
|
|
418 | nt.assert_equal(path.get_py_filename('foo.py', force_win32=win32), 'foo.py') | |
|
419 | nt.assert_equal(path.get_py_filename('foo', force_win32=win32), 'foo.py') | |
|
420 | 420 | with make_tempfile('foo'): |
|
421 |
nt.assert_equal |
|
|
421 | nt.assert_equal(path.get_py_filename('foo', force_win32=win32), 'foo') | |
|
422 | 422 | nt.assert_raises(IOError, path.get_py_filename, 'foo.py', force_win32=win32) |
|
423 | 423 | nt.assert_raises(IOError, path.get_py_filename, 'foo', force_win32=win32) |
|
424 | 424 | nt.assert_raises(IOError, path.get_py_filename, 'foo.py', force_win32=win32) |
|
425 | 425 | true_fn = 'foo with spaces.py' |
|
426 | 426 | with make_tempfile(true_fn): |
|
427 |
nt.assert_equal |
|
|
428 |
nt.assert_equal |
|
|
427 | nt.assert_equal(path.get_py_filename('foo with spaces', force_win32=win32), true_fn) | |
|
428 | nt.assert_equal(path.get_py_filename('foo with spaces.py', force_win32=win32), true_fn) | |
|
429 | 429 | if win32: |
|
430 |
nt.assert_equal |
|
|
431 |
nt.assert_equal |
|
|
430 | nt.assert_equal(path.get_py_filename('"foo with spaces.py"', force_win32=True), true_fn) | |
|
431 | nt.assert_equal(path.get_py_filename("'foo with spaces.py'", force_win32=True), true_fn) | |
|
432 | 432 | else: |
|
433 | 433 | nt.assert_raises(IOError, path.get_py_filename, '"foo with spaces.py"', force_win32=False) |
|
434 | 434 | nt.assert_raises(IOError, path.get_py_filename, "'foo with spaces.py'", force_win32=False) |
|
435 | 435 | |
|
436 | 436 | def test_unicode_in_filename(): |
|
437 | 437 | """When a file doesn't exist, the exception raised should be safe to call |
|
438 | 438 | str() on - i.e. in Python 2 it must only have ASCII characters. |
|
439 | 439 | |
|
440 | 440 | https://github.com/ipython/ipython/issues/875 |
|
441 | 441 | """ |
|
442 | 442 | try: |
|
443 | 443 | # these calls should not throw unicode encode exceptions |
|
444 | 444 | path.get_py_filename(u'fooéè.py', force_win32=False) |
|
445 | 445 | except IOError as ex: |
|
446 | 446 | str(ex) |
@@ -1,131 +1,131 | |||
|
1 | 1 | # encoding: utf-8 |
|
2 | 2 | """ |
|
3 | 3 | Tests for platutils.py |
|
4 | 4 | """ |
|
5 | 5 | |
|
6 | 6 | #----------------------------------------------------------------------------- |
|
7 | 7 | # Copyright (C) 2008-2011 The IPython Development Team |
|
8 | 8 | # |
|
9 | 9 | # Distributed under the terms of the BSD License. The full license is in |
|
10 | 10 | # the file COPYING, distributed as part of this software. |
|
11 | 11 | #----------------------------------------------------------------------------- |
|
12 | 12 | |
|
13 | 13 | #----------------------------------------------------------------------------- |
|
14 | 14 | # Imports |
|
15 | 15 | #----------------------------------------------------------------------------- |
|
16 | 16 | |
|
17 | 17 | import sys |
|
18 | 18 | from unittest import TestCase |
|
19 | 19 | |
|
20 | 20 | import nose.tools as nt |
|
21 | 21 | |
|
22 | 22 | from IPython.utils.process import (find_cmd, FindCmdError, arg_split, |
|
23 | 23 | system, getoutput, getoutputerror) |
|
24 | 24 | from IPython.testing import decorators as dec |
|
25 | 25 | from IPython.testing import tools as tt |
|
26 | 26 | |
|
27 | 27 | #----------------------------------------------------------------------------- |
|
28 | 28 | # Tests |
|
29 | 29 | #----------------------------------------------------------------------------- |
|
30 | 30 | |
|
31 | 31 | def test_find_cmd_python(): |
|
32 | 32 | """Make sure we find sys.exectable for python.""" |
|
33 |
nt.assert_equal |
|
|
33 | nt.assert_equal(find_cmd('python'), sys.executable) | |
|
34 | 34 | |
|
35 | 35 | |
|
36 | 36 | @dec.skip_win32 |
|
37 | 37 | def test_find_cmd_ls(): |
|
38 | 38 | """Make sure we can find the full path to ls.""" |
|
39 | 39 | path = find_cmd('ls') |
|
40 | 40 | nt.assert_true(path.endswith('ls')) |
|
41 | 41 | |
|
42 | 42 | |
|
43 | 43 | def has_pywin32(): |
|
44 | 44 | try: |
|
45 | 45 | import win32api |
|
46 | 46 | except ImportError: |
|
47 | 47 | return False |
|
48 | 48 | return True |
|
49 | 49 | |
|
50 | 50 | |
|
51 | 51 | @dec.onlyif(has_pywin32, "This test requires win32api to run") |
|
52 | 52 | def test_find_cmd_pythonw(): |
|
53 | 53 | """Try to find pythonw on Windows.""" |
|
54 | 54 | path = find_cmd('pythonw') |
|
55 | 55 | nt.assert_true(path.endswith('pythonw.exe')) |
|
56 | 56 | |
|
57 | 57 | |
|
58 | 58 | @dec.onlyif(lambda : sys.platform != 'win32' or has_pywin32(), |
|
59 | 59 | "This test runs on posix or in win32 with win32api installed") |
|
60 | 60 | def test_find_cmd_fail(): |
|
61 | 61 | """Make sure that FindCmdError is raised if we can't find the cmd.""" |
|
62 | 62 | nt.assert_raises(FindCmdError,find_cmd,'asdfasdf') |
|
63 | 63 | |
|
64 | 64 | |
|
65 | 65 | @dec.skip_win32 |
|
66 | 66 | def test_arg_split(): |
|
67 | 67 | """Ensure that argument lines are correctly split like in a shell.""" |
|
68 | 68 | tests = [['hi', ['hi']], |
|
69 | 69 | [u'hi', [u'hi']], |
|
70 | 70 | ['hello there', ['hello', 'there']], |
|
71 | 71 | # \u01ce == \N{LATIN SMALL LETTER A WITH CARON} |
|
72 | 72 | # Do not use \N because the tests crash with syntax error in |
|
73 | 73 | # some cases, for example windows python2.6. |
|
74 | 74 | [u'h\u01cello', [u'h\u01cello']], |
|
75 | 75 | ['something "with quotes"', ['something', '"with quotes"']], |
|
76 | 76 | ] |
|
77 | 77 | for argstr, argv in tests: |
|
78 | 78 | nt.assert_equal(arg_split(argstr), argv) |
|
79 | 79 | |
|
80 | 80 | @dec.skip_if_not_win32 |
|
81 | 81 | def test_arg_split_win32(): |
|
82 | 82 | """Ensure that argument lines are correctly split like in a shell.""" |
|
83 | 83 | tests = [['hi', ['hi']], |
|
84 | 84 | [u'hi', [u'hi']], |
|
85 | 85 | ['hello there', ['hello', 'there']], |
|
86 | 86 | [u'h\u01cello', [u'h\u01cello']], |
|
87 | 87 | ['something "with quotes"', ['something', 'with quotes']], |
|
88 | 88 | ] |
|
89 | 89 | for argstr, argv in tests: |
|
90 | 90 | nt.assert_equal(arg_split(argstr), argv) |
|
91 | 91 | |
|
92 | 92 | |
|
93 | 93 | class SubProcessTestCase(TestCase, tt.TempFileMixin): |
|
94 | 94 | def setUp(self): |
|
95 | 95 | """Make a valid python temp file.""" |
|
96 | 96 | lines = ["from __future__ import print_function", |
|
97 | 97 | "import sys", |
|
98 | 98 | "print('on stdout', end='', file=sys.stdout)", |
|
99 | 99 | "print('on stderr', end='', file=sys.stderr)", |
|
100 | 100 | "sys.stdout.flush()", |
|
101 | 101 | "sys.stderr.flush()"] |
|
102 | 102 | self.mktmp('\n'.join(lines)) |
|
103 | 103 | |
|
104 | 104 | def test_system(self): |
|
105 | 105 | status = system('python "%s"' % self.fname) |
|
106 | 106 | self.assertEqual(status, 0) |
|
107 | 107 | |
|
108 | 108 | def test_system_quotes(self): |
|
109 | 109 | status = system('python -c "import sys"') |
|
110 | 110 | self.assertEqual(status, 0) |
|
111 | 111 | |
|
112 | 112 | def test_getoutput(self): |
|
113 | 113 | out = getoutput('python "%s"' % self.fname) |
|
114 | 114 | self.assertEqual(out, 'on stdout') |
|
115 | 115 | |
|
116 | 116 | def test_getoutput_quoted(self): |
|
117 | 117 | out = getoutput('python -c "print (1)"') |
|
118 | 118 | self.assertEqual(out.strip(), '1') |
|
119 | 119 | |
|
120 | 120 | #Invalid quoting on windows |
|
121 | 121 | @dec.skip_win32 |
|
122 | 122 | def test_getoutput_quoted2(self): |
|
123 | 123 | out = getoutput("python -c 'print (1)'") |
|
124 | 124 | self.assertEqual(out.strip(), '1') |
|
125 | 125 | out = getoutput("python -c 'print (\"1\")'") |
|
126 | 126 | self.assertEqual(out.strip(), '1') |
|
127 | 127 | |
|
128 | 128 | def test_getoutput(self): |
|
129 | 129 | out, err = getoutputerror('python "%s"' % self.fname) |
|
130 | 130 | self.assertEqual(out, 'on stdout') |
|
131 | 131 | self.assertEqual(err, 'on stderr') |
@@ -1,34 +1,34 | |||
|
1 | 1 | """Test suite for our color utilities. |
|
2 | 2 | |
|
3 | 3 | Authors |
|
4 | 4 | ------- |
|
5 | 5 | |
|
6 | 6 | * Min RK |
|
7 | 7 | """ |
|
8 | 8 | #----------------------------------------------------------------------------- |
|
9 | 9 | # Copyright (C) 2011 The IPython Development Team |
|
10 | 10 | # |
|
11 | 11 | # Distributed under the terms of the BSD License. The full license is in |
|
12 | 12 | # the file COPYING.txt, distributed as part of this software. |
|
13 | 13 | #----------------------------------------------------------------------------- |
|
14 | 14 | |
|
15 | 15 | #----------------------------------------------------------------------------- |
|
16 | 16 | # Imports |
|
17 | 17 | #----------------------------------------------------------------------------- |
|
18 | 18 | |
|
19 | 19 | # third party |
|
20 | 20 | import nose.tools as nt |
|
21 | 21 | |
|
22 | 22 | # our own |
|
23 | 23 | from IPython.utils.PyColorize import Parser |
|
24 | 24 | |
|
25 | 25 | #----------------------------------------------------------------------------- |
|
26 | 26 | # Test functions |
|
27 | 27 | #----------------------------------------------------------------------------- |
|
28 | 28 | |
|
29 | 29 | def test_unicode_colorize(): |
|
30 | 30 | p = Parser() |
|
31 | 31 | f1 = p.format('1/0', 'str') |
|
32 | 32 | f2 = p.format(u'1/0', 'str') |
|
33 |
nt.assert_equal |
|
|
33 | nt.assert_equal(f1, f2) | |
|
34 | 34 |
@@ -1,170 +1,170 | |||
|
1 | 1 | # encoding: utf-8 |
|
2 | 2 | """Tests for IPython.utils.text""" |
|
3 | 3 | |
|
4 | 4 | #----------------------------------------------------------------------------- |
|
5 | 5 | # Copyright (C) 2011 The IPython Development Team |
|
6 | 6 | # |
|
7 | 7 | # Distributed under the terms of the BSD License. The full license is in |
|
8 | 8 | # the file COPYING, distributed as part of this software. |
|
9 | 9 | #----------------------------------------------------------------------------- |
|
10 | 10 | |
|
11 | 11 | #----------------------------------------------------------------------------- |
|
12 | 12 | # Imports |
|
13 | 13 | #----------------------------------------------------------------------------- |
|
14 | 14 | |
|
15 | 15 | import os |
|
16 | 16 | import math |
|
17 | 17 | import random |
|
18 | 18 | |
|
19 | 19 | import nose.tools as nt |
|
20 | 20 | |
|
21 | 21 | from nose import with_setup |
|
22 | 22 | |
|
23 | 23 | from IPython.testing import decorators as dec |
|
24 | 24 | from IPython.utils import text |
|
25 | 25 | |
|
26 | 26 | #----------------------------------------------------------------------------- |
|
27 | 27 | # Globals |
|
28 | 28 | #----------------------------------------------------------------------------- |
|
29 | 29 | |
|
30 | 30 | def test_columnize(): |
|
31 | 31 | """Basic columnize tests.""" |
|
32 | 32 | size = 5 |
|
33 | 33 | items = [l*size for l in 'abc'] |
|
34 | 34 | out = text.columnize(items, displaywidth=80) |
|
35 |
nt.assert_equal |
|
|
35 | nt.assert_equal(out, 'aaaaa bbbbb ccccc\n') | |
|
36 | 36 | out = text.columnize(items, displaywidth=12) |
|
37 |
nt.assert_equal |
|
|
37 | nt.assert_equal(out, 'aaaaa ccccc\nbbbbb\n') | |
|
38 | 38 | out = text.columnize(items, displaywidth=10) |
|
39 |
nt.assert_equal |
|
|
39 | nt.assert_equal(out, 'aaaaa\nbbbbb\nccccc\n') | |
|
40 | 40 | |
|
41 | 41 | def test_columnize_random(): |
|
42 | 42 | """Test with random input to hopfully catch edge case """ |
|
43 | 43 | for nitems in [random.randint(2,70) for i in range(2,20)]: |
|
44 | 44 | displaywidth = random.randint(20,200) |
|
45 | 45 | rand_len = [random.randint(2,displaywidth) for i in range(nitems)] |
|
46 | 46 | items = ['x'*l for l in rand_len] |
|
47 | 47 | out = text.columnize(items, displaywidth=displaywidth) |
|
48 | 48 | longer_line = max([len(x) for x in out.split('\n')]) |
|
49 | 49 | longer_element = max(rand_len) |
|
50 | 50 | if longer_line > displaywidth: |
|
51 | 51 | print "Columnize displayed something lager than displaywidth : %s " % longer_line |
|
52 | 52 | print "longer element : %s " % longer_element |
|
53 | 53 | print "displaywidth : %s " % displaywidth |
|
54 | 54 | print "number of element : %s " % nitems |
|
55 | 55 | print "size of each element :\n %s" % rand_len |
|
56 | 56 | assert False |
|
57 | 57 | |
|
58 | 58 | def test_columnize_medium(): |
|
59 | 59 | """Test with inputs than shouldn't be wider tahn 80 """ |
|
60 | 60 | size = 40 |
|
61 | 61 | items = [l*size for l in 'abc'] |
|
62 | 62 | out = text.columnize(items, displaywidth=80) |
|
63 |
nt.assert_equal |
|
|
63 | nt.assert_equal(out, '\n'.join(items+[''])) | |
|
64 | 64 | |
|
65 | 65 | def test_columnize_long(): |
|
66 | 66 | """Test columnize with inputs longer than the display window""" |
|
67 | 67 | size = 11 |
|
68 | 68 | items = [l*size for l in 'abc'] |
|
69 | 69 | out = text.columnize(items, displaywidth=size-1) |
|
70 |
nt.assert_equal |
|
|
70 | nt.assert_equal(out, '\n'.join(items+[''])) | |
|
71 | 71 | |
|
72 | 72 | def eval_formatter_check(f): |
|
73 | 73 | ns = dict(n=12, pi=math.pi, stuff='hello there', os=os, u=u"cafΓ©", b="cafΓ©") |
|
74 | 74 | s = f.format("{n} {n//4} {stuff.split()[0]}", **ns) |
|
75 |
nt.assert_equal |
|
|
75 | nt.assert_equal(s, "12 3 hello") | |
|
76 | 76 | s = f.format(' '.join(['{n//%i}'%i for i in range(1,8)]), **ns) |
|
77 |
nt.assert_equal |
|
|
77 | nt.assert_equal(s, "12 6 4 3 2 2 1") | |
|
78 | 78 | s = f.format('{[n//i for i in range(1,8)]}', **ns) |
|
79 |
nt.assert_equal |
|
|
79 | nt.assert_equal(s, "[12, 6, 4, 3, 2, 2, 1]") | |
|
80 | 80 | s = f.format("{stuff!s}", **ns) |
|
81 |
nt.assert_equal |
|
|
81 | nt.assert_equal(s, ns['stuff']) | |
|
82 | 82 | s = f.format("{stuff!r}", **ns) |
|
83 |
nt.assert_equal |
|
|
83 | nt.assert_equal(s, repr(ns['stuff'])) | |
|
84 | 84 | |
|
85 | 85 | # Check with unicode: |
|
86 | 86 | s = f.format("{u}", **ns) |
|
87 |
nt.assert_equal |
|
|
87 | nt.assert_equal(s, ns['u']) | |
|
88 | 88 | # This decodes in a platform dependent manner, but it shouldn't error out |
|
89 | 89 | s = f.format("{b}", **ns) |
|
90 | 90 | |
|
91 | 91 | nt.assert_raises(NameError, f.format, '{dne}', **ns) |
|
92 | 92 | |
|
93 | 93 | def eval_formatter_slicing_check(f): |
|
94 | 94 | ns = dict(n=12, pi=math.pi, stuff='hello there', os=os) |
|
95 | 95 | s = f.format(" {stuff.split()[:]} ", **ns) |
|
96 |
nt.assert_equal |
|
|
96 | nt.assert_equal(s, " ['hello', 'there'] ") | |
|
97 | 97 | s = f.format(" {stuff.split()[::-1]} ", **ns) |
|
98 |
nt.assert_equal |
|
|
98 | nt.assert_equal(s, " ['there', 'hello'] ") | |
|
99 | 99 | s = f.format("{stuff[::2]}", **ns) |
|
100 |
nt.assert_equal |
|
|
100 | nt.assert_equal(s, ns['stuff'][::2]) | |
|
101 | 101 | |
|
102 | 102 | nt.assert_raises(SyntaxError, f.format, "{n:x}", **ns) |
|
103 | 103 | |
|
104 | 104 | def eval_formatter_no_slicing_check(f): |
|
105 | 105 | ns = dict(n=12, pi=math.pi, stuff='hello there', os=os) |
|
106 | 106 | |
|
107 | 107 | s = f.format('{n:x} {pi**2:+f}', **ns) |
|
108 |
nt.assert_equal |
|
|
108 | nt.assert_equal(s, "c +9.869604") | |
|
109 | 109 | |
|
110 | 110 | s = f.format('{stuff[slice(1,4)]}', **ns) |
|
111 |
nt.assert_equal |
|
|
111 | nt.assert_equal(s, 'ell') | |
|
112 | 112 | |
|
113 | 113 | nt.assert_raises(SyntaxError, f.format, "{a[:]}") |
|
114 | 114 | |
|
115 | 115 | def test_eval_formatter(): |
|
116 | 116 | f = text.EvalFormatter() |
|
117 | 117 | eval_formatter_check(f) |
|
118 | 118 | eval_formatter_no_slicing_check(f) |
|
119 | 119 | |
|
120 | 120 | def test_full_eval_formatter(): |
|
121 | 121 | f = text.FullEvalFormatter() |
|
122 | 122 | eval_formatter_check(f) |
|
123 | 123 | eval_formatter_slicing_check(f) |
|
124 | 124 | |
|
125 | 125 | def test_dollar_formatter(): |
|
126 | 126 | f = text.DollarFormatter() |
|
127 | 127 | eval_formatter_check(f) |
|
128 | 128 | eval_formatter_slicing_check(f) |
|
129 | 129 | |
|
130 | 130 | ns = dict(n=12, pi=math.pi, stuff='hello there', os=os) |
|
131 | 131 | s = f.format("$n", **ns) |
|
132 |
nt.assert_equal |
|
|
132 | nt.assert_equal(s, "12") | |
|
133 | 133 | s = f.format("$n.real", **ns) |
|
134 |
nt.assert_equal |
|
|
134 | nt.assert_equal(s, "12") | |
|
135 | 135 | s = f.format("$n/{stuff[:5]}", **ns) |
|
136 |
nt.assert_equal |
|
|
136 | nt.assert_equal(s, "12/hello") | |
|
137 | 137 | s = f.format("$n $$HOME", **ns) |
|
138 |
nt.assert_equal |
|
|
138 | nt.assert_equal(s, "12 $HOME") | |
|
139 | 139 | s = f.format("${foo}", foo="HOME") |
|
140 |
nt.assert_equal |
|
|
140 | nt.assert_equal(s, "$HOME") | |
|
141 | 141 | |
|
142 | 142 | |
|
143 | 143 | def test_long_substr(): |
|
144 | 144 | data = ['hi'] |
|
145 |
nt.assert_equal |
|
|
145 | nt.assert_equal(text.long_substr(data), 'hi') | |
|
146 | 146 | |
|
147 | 147 | |
|
148 | 148 | def test_long_substr2(): |
|
149 | 149 | data = ['abc', 'abd', 'abf', 'ab'] |
|
150 |
nt.assert_equal |
|
|
150 | nt.assert_equal(text.long_substr(data), 'ab') | |
|
151 | 151 | |
|
152 | 152 | |
|
153 | 153 | def test_strip_email(): |
|
154 | 154 | src = """\ |
|
155 | 155 | >> >>> def f(x): |
|
156 | 156 | >> ... return x+1 |
|
157 | 157 | >> ... |
|
158 | 158 | >> >>> zz = f(2.5)""" |
|
159 | 159 | cln = """\ |
|
160 | 160 | >>> def f(x): |
|
161 | 161 | ... return x+1 |
|
162 | 162 | ... |
|
163 | 163 | >>> zz = f(2.5)""" |
|
164 |
nt.assert_equal |
|
|
164 | nt.assert_equal(text.strip_email_quotes(src), cln) | |
|
165 | 165 | |
|
166 | 166 | |
|
167 | 167 | def test_strip_email2(): |
|
168 | 168 | src = '> > > list()' |
|
169 | 169 | cln = 'list()' |
|
170 |
nt.assert_equal |
|
|
170 | nt.assert_equal(text.strip_email_quotes(src), cln) |
@@ -1,193 +1,193 | |||
|
1 | 1 | """test IPython.embed_kernel()""" |
|
2 | 2 | |
|
3 | 3 | #------------------------------------------------------------------------------- |
|
4 | 4 | # Copyright (C) 2012 The IPython Development Team |
|
5 | 5 | # |
|
6 | 6 | # Distributed under the terms of the BSD License. The full license is in |
|
7 | 7 | # the file COPYING, distributed as part of this software. |
|
8 | 8 | #------------------------------------------------------------------------------- |
|
9 | 9 | |
|
10 | 10 | #------------------------------------------------------------------------------- |
|
11 | 11 | # Imports |
|
12 | 12 | #------------------------------------------------------------------------------- |
|
13 | 13 | |
|
14 | 14 | import os |
|
15 | 15 | import shutil |
|
16 | 16 | import sys |
|
17 | 17 | import tempfile |
|
18 | 18 | import time |
|
19 | 19 | |
|
20 | 20 | from contextlib import contextmanager |
|
21 | 21 | from subprocess import Popen, PIPE |
|
22 | 22 | |
|
23 | 23 | import nose.tools as nt |
|
24 | 24 | |
|
25 | 25 | from IPython.zmq.blockingkernelmanager import BlockingKernelManager |
|
26 | 26 | from IPython.utils import path, py3compat |
|
27 | 27 | |
|
28 | 28 | #------------------------------------------------------------------------------- |
|
29 | 29 | # Tests |
|
30 | 30 | #------------------------------------------------------------------------------- |
|
31 | 31 | |
|
32 | 32 | def setup(): |
|
33 | 33 | """setup temporary IPYTHONDIR for tests""" |
|
34 | 34 | global IPYTHONDIR |
|
35 | 35 | global env |
|
36 | 36 | global save_get_ipython_dir |
|
37 | 37 | |
|
38 | 38 | IPYTHONDIR = tempfile.mkdtemp() |
|
39 | 39 | |
|
40 | 40 | env = os.environ.copy() |
|
41 | 41 | env["IPYTHONDIR"] = IPYTHONDIR |
|
42 | 42 | |
|
43 | 43 | save_get_ipython_dir = path.get_ipython_dir |
|
44 | 44 | path.get_ipython_dir = lambda : IPYTHONDIR |
|
45 | 45 | |
|
46 | 46 | |
|
47 | 47 | def teardown(): |
|
48 | 48 | path.get_ipython_dir = save_get_ipython_dir |
|
49 | 49 | |
|
50 | 50 | try: |
|
51 | 51 | shutil.rmtree(IPYTHONDIR) |
|
52 | 52 | except (OSError, IOError): |
|
53 | 53 | # no such file |
|
54 | 54 | pass |
|
55 | 55 | |
|
56 | 56 | |
|
57 | 57 | @contextmanager |
|
58 | 58 | def setup_kernel(cmd): |
|
59 | 59 | """start an embedded kernel in a subprocess, and wait for it to be ready |
|
60 | 60 | |
|
61 | 61 | Returns |
|
62 | 62 | ------- |
|
63 | 63 | kernel_manager: connected KernelManager instance |
|
64 | 64 | """ |
|
65 | 65 | kernel = Popen([sys.executable, '-c', cmd], stdout=PIPE, stderr=PIPE, env=env) |
|
66 | 66 | connection_file = os.path.join(IPYTHONDIR, |
|
67 | 67 | 'profile_default', |
|
68 | 68 | 'security', |
|
69 | 69 | 'kernel-%i.json' % kernel.pid |
|
70 | 70 | ) |
|
71 | 71 | # wait for connection file to exist, timeout after 5s |
|
72 | 72 | tic = time.time() |
|
73 | 73 | while not os.path.exists(connection_file) and kernel.poll() is None and time.time() < tic + 10: |
|
74 | 74 | time.sleep(0.1) |
|
75 | 75 | |
|
76 | 76 | if kernel.poll() is not None: |
|
77 | 77 | o,e = kernel.communicate() |
|
78 | 78 | e = py3compat.cast_unicode(e) |
|
79 | 79 | raise IOError("Kernel failed to start:\n%s" % e) |
|
80 | 80 | |
|
81 | 81 | if not os.path.exists(connection_file): |
|
82 | 82 | if kernel.poll() is None: |
|
83 | 83 | kernel.terminate() |
|
84 | 84 | raise IOError("Connection file %r never arrived" % connection_file) |
|
85 | 85 | |
|
86 | 86 | km = BlockingKernelManager(connection_file=connection_file) |
|
87 | 87 | km.load_connection_file() |
|
88 | 88 | km.start_channels() |
|
89 | 89 | |
|
90 | 90 | try: |
|
91 | 91 | yield km |
|
92 | 92 | finally: |
|
93 | 93 | km.stop_channels() |
|
94 | 94 | kernel.terminate() |
|
95 | 95 | |
|
96 | 96 | def test_embed_kernel_basic(): |
|
97 | 97 | """IPython.embed_kernel() is basically functional""" |
|
98 | 98 | cmd = '\n'.join([ |
|
99 | 99 | 'from IPython import embed_kernel', |
|
100 | 100 | 'def go():', |
|
101 | 101 | ' a=5', |
|
102 | 102 | ' b="hi there"', |
|
103 | 103 | ' embed_kernel()', |
|
104 | 104 | 'go()', |
|
105 | 105 | '', |
|
106 | 106 | ]) |
|
107 | 107 | |
|
108 | 108 | with setup_kernel(cmd) as km: |
|
109 | 109 | shell = km.shell_channel |
|
110 | 110 | |
|
111 | 111 | # oinfo a (int) |
|
112 | 112 | msg_id = shell.object_info('a') |
|
113 | 113 | msg = shell.get_msg(block=True, timeout=2) |
|
114 | 114 | content = msg['content'] |
|
115 | 115 | nt.assert_true(content['found']) |
|
116 | 116 | |
|
117 | 117 | msg_id = shell.execute("c=a*2") |
|
118 | 118 | msg = shell.get_msg(block=True, timeout=2) |
|
119 | 119 | content = msg['content'] |
|
120 |
nt.assert_equal |
|
|
120 | nt.assert_equal(content['status'], u'ok') | |
|
121 | 121 | |
|
122 | 122 | # oinfo c (should be 10) |
|
123 | 123 | msg_id = shell.object_info('c') |
|
124 | 124 | msg = shell.get_msg(block=True, timeout=2) |
|
125 | 125 | content = msg['content'] |
|
126 | 126 | nt.assert_true(content['found']) |
|
127 |
nt.assert_equal |
|
|
127 | nt.assert_equal(content['string_form'], u'10') | |
|
128 | 128 | |
|
129 | 129 | def test_embed_kernel_namespace(): |
|
130 | 130 | """IPython.embed_kernel() inherits calling namespace""" |
|
131 | 131 | cmd = '\n'.join([ |
|
132 | 132 | 'from IPython import embed_kernel', |
|
133 | 133 | 'def go():', |
|
134 | 134 | ' a=5', |
|
135 | 135 | ' b="hi there"', |
|
136 | 136 | ' embed_kernel()', |
|
137 | 137 | 'go()', |
|
138 | 138 | '', |
|
139 | 139 | ]) |
|
140 | 140 | |
|
141 | 141 | with setup_kernel(cmd) as km: |
|
142 | 142 | shell = km.shell_channel |
|
143 | 143 | |
|
144 | 144 | # oinfo a (int) |
|
145 | 145 | msg_id = shell.object_info('a') |
|
146 | 146 | msg = shell.get_msg(block=True, timeout=2) |
|
147 | 147 | content = msg['content'] |
|
148 | 148 | nt.assert_true(content['found']) |
|
149 |
nt.assert_equal |
|
|
149 | nt.assert_equal(content['string_form'], u'5') | |
|
150 | 150 | |
|
151 | 151 | # oinfo b (str) |
|
152 | 152 | msg_id = shell.object_info('b') |
|
153 | 153 | msg = shell.get_msg(block=True, timeout=2) |
|
154 | 154 | content = msg['content'] |
|
155 | 155 | nt.assert_true(content['found']) |
|
156 |
nt.assert_equal |
|
|
156 | nt.assert_equal(content['string_form'], u'hi there') | |
|
157 | 157 | |
|
158 | 158 | # oinfo c (undefined) |
|
159 | 159 | msg_id = shell.object_info('c') |
|
160 | 160 | msg = shell.get_msg(block=True, timeout=2) |
|
161 | 161 | content = msg['content'] |
|
162 | 162 | nt.assert_false(content['found']) |
|
163 | 163 | |
|
164 | 164 | def test_embed_kernel_reentrant(): |
|
165 | 165 | """IPython.embed_kernel() can be called multiple times""" |
|
166 | 166 | cmd = '\n'.join([ |
|
167 | 167 | 'from IPython import embed_kernel', |
|
168 | 168 | 'count = 0', |
|
169 | 169 | 'def go():', |
|
170 | 170 | ' global count', |
|
171 | 171 | ' embed_kernel()', |
|
172 | 172 | ' count = count + 1', |
|
173 | 173 | '', |
|
174 | 174 | 'while True:' |
|
175 | 175 | ' go()', |
|
176 | 176 | '', |
|
177 | 177 | ]) |
|
178 | 178 | |
|
179 | 179 | with setup_kernel(cmd) as km: |
|
180 | 180 | shell = km.shell_channel |
|
181 | 181 | for i in range(5): |
|
182 | 182 | msg_id = shell.object_info('count') |
|
183 | 183 | msg = shell.get_msg(block=True, timeout=2) |
|
184 | 184 | content = msg['content'] |
|
185 | 185 | nt.assert_true(content['found']) |
|
186 |
nt.assert_equal |
|
|
186 | nt.assert_equal(content['string_form'], unicode(i)) | |
|
187 | 187 | |
|
188 | 188 | # exit from embed_kernel |
|
189 | 189 | shell.execute("get_ipython().exit_now = True") |
|
190 | 190 | msg = shell.get_msg(block=True, timeout=2) |
|
191 | 191 | time.sleep(0.2) |
|
192 | 192 | |
|
193 | 193 |
@@ -1,452 +1,452 | |||
|
1 | 1 | """Test suite for our zeromq-based messaging specification. |
|
2 | 2 | """ |
|
3 | 3 | #----------------------------------------------------------------------------- |
|
4 | 4 | # Copyright (C) 2010-2011 The IPython Development Team |
|
5 | 5 | # |
|
6 | 6 | # Distributed under the terms of the BSD License. The full license is in |
|
7 | 7 | # the file COPYING.txt, distributed as part of this software. |
|
8 | 8 | #----------------------------------------------------------------------------- |
|
9 | 9 | |
|
10 | 10 | import re |
|
11 | 11 | import sys |
|
12 | 12 | import time |
|
13 | 13 | from subprocess import PIPE |
|
14 | 14 | from Queue import Empty |
|
15 | 15 | |
|
16 | 16 | import nose.tools as nt |
|
17 | 17 | |
|
18 | 18 | from ..blockingkernelmanager import BlockingKernelManager |
|
19 | 19 | |
|
20 | 20 | |
|
21 | 21 | from IPython.testing import decorators as dec |
|
22 | 22 | from IPython.utils import io |
|
23 | 23 | from IPython.utils.traitlets import ( |
|
24 | 24 | HasTraits, TraitError, Bool, Unicode, Dict, Integer, List, Enum, |
|
25 | 25 | ) |
|
26 | 26 | |
|
27 | 27 | #----------------------------------------------------------------------------- |
|
28 | 28 | # Global setup and utilities |
|
29 | 29 | #----------------------------------------------------------------------------- |
|
30 | 30 | |
|
31 | 31 | def setup(): |
|
32 | 32 | global KM |
|
33 | 33 | KM = BlockingKernelManager() |
|
34 | 34 | |
|
35 | 35 | KM.start_kernel(stdout=PIPE, stderr=PIPE) |
|
36 | 36 | KM.start_channels() |
|
37 | 37 | |
|
38 | 38 | # wait for kernel to be ready |
|
39 | 39 | KM.shell_channel.execute("pass") |
|
40 | 40 | KM.shell_channel.get_msg(block=True, timeout=5) |
|
41 | 41 | flush_channels() |
|
42 | 42 | |
|
43 | 43 | |
|
44 | 44 | def teardown(): |
|
45 | 45 | KM.stop_channels() |
|
46 | 46 | KM.shutdown_kernel() |
|
47 | 47 | |
|
48 | 48 | |
|
49 | 49 | def flush_channels(): |
|
50 | 50 | """flush any messages waiting on the queue""" |
|
51 | 51 | for channel in (KM.shell_channel, KM.sub_channel): |
|
52 | 52 | while True: |
|
53 | 53 | try: |
|
54 | 54 | msg = channel.get_msg(block=True, timeout=0.1) |
|
55 | 55 | except Empty: |
|
56 | 56 | break |
|
57 | 57 | else: |
|
58 | 58 | list(validate_message(msg)) |
|
59 | 59 | |
|
60 | 60 | |
|
61 | 61 | def execute(code='', **kwargs): |
|
62 | 62 | """wrapper for doing common steps for validating an execution request""" |
|
63 | 63 | shell = KM.shell_channel |
|
64 | 64 | sub = KM.sub_channel |
|
65 | 65 | |
|
66 | 66 | msg_id = shell.execute(code=code, **kwargs) |
|
67 | 67 | reply = shell.get_msg(timeout=2) |
|
68 | 68 | list(validate_message(reply, 'execute_reply', msg_id)) |
|
69 | 69 | busy = sub.get_msg(timeout=2) |
|
70 | 70 | list(validate_message(busy, 'status', msg_id)) |
|
71 |
nt.assert_equal |
|
|
71 | nt.assert_equal(busy['content']['execution_state'], 'busy') | |
|
72 | 72 | |
|
73 | 73 | if not kwargs.get('silent'): |
|
74 | 74 | pyin = sub.get_msg(timeout=2) |
|
75 | 75 | list(validate_message(pyin, 'pyin', msg_id)) |
|
76 |
nt.assert_equal |
|
|
76 | nt.assert_equal(pyin['content']['code'], code) | |
|
77 | 77 | |
|
78 | 78 | return msg_id, reply['content'] |
|
79 | 79 | |
|
80 | 80 | #----------------------------------------------------------------------------- |
|
81 | 81 | # MSG Spec References |
|
82 | 82 | #----------------------------------------------------------------------------- |
|
83 | 83 | |
|
84 | 84 | |
|
85 | 85 | class Reference(HasTraits): |
|
86 | 86 | |
|
87 | 87 | def check(self, d): |
|
88 | 88 | """validate a dict against our traits""" |
|
89 | 89 | for key in self.trait_names(): |
|
90 | 90 | yield nt.assert_true(key in d, "Missing key: %r, should be found in %s" % (key, d)) |
|
91 | 91 | # FIXME: always allow None, probably not a good idea |
|
92 | 92 | if d[key] is None: |
|
93 | 93 | continue |
|
94 | 94 | try: |
|
95 | 95 | setattr(self, key, d[key]) |
|
96 | 96 | except TraitError as e: |
|
97 | 97 | yield nt.assert_true(False, str(e)) |
|
98 | 98 | |
|
99 | 99 | |
|
100 | 100 | class RMessage(Reference): |
|
101 | 101 | msg_id = Unicode() |
|
102 | 102 | msg_type = Unicode() |
|
103 | 103 | header = Dict() |
|
104 | 104 | parent_header = Dict() |
|
105 | 105 | content = Dict() |
|
106 | 106 | |
|
107 | 107 | class RHeader(Reference): |
|
108 | 108 | msg_id = Unicode() |
|
109 | 109 | msg_type = Unicode() |
|
110 | 110 | session = Unicode() |
|
111 | 111 | username = Unicode() |
|
112 | 112 | |
|
113 | 113 | class RContent(Reference): |
|
114 | 114 | status = Enum((u'ok', u'error')) |
|
115 | 115 | |
|
116 | 116 | |
|
117 | 117 | class ExecuteReply(Reference): |
|
118 | 118 | execution_count = Integer() |
|
119 | 119 | status = Enum((u'ok', u'error')) |
|
120 | 120 | |
|
121 | 121 | def check(self, d): |
|
122 | 122 | for tst in Reference.check(self, d): |
|
123 | 123 | yield tst |
|
124 | 124 | if d['status'] == 'ok': |
|
125 | 125 | for tst in ExecuteReplyOkay().check(d): |
|
126 | 126 | yield tst |
|
127 | 127 | elif d['status'] == 'error': |
|
128 | 128 | for tst in ExecuteReplyError().check(d): |
|
129 | 129 | yield tst |
|
130 | 130 | |
|
131 | 131 | |
|
132 | 132 | class ExecuteReplyOkay(Reference): |
|
133 | 133 | payload = List(Dict) |
|
134 | 134 | user_variables = Dict() |
|
135 | 135 | user_expressions = Dict() |
|
136 | 136 | |
|
137 | 137 | |
|
138 | 138 | class ExecuteReplyError(Reference): |
|
139 | 139 | ename = Unicode() |
|
140 | 140 | evalue = Unicode() |
|
141 | 141 | traceback = List(Unicode) |
|
142 | 142 | |
|
143 | 143 | |
|
144 | 144 | class OInfoReply(Reference): |
|
145 | 145 | name = Unicode() |
|
146 | 146 | found = Bool() |
|
147 | 147 | ismagic = Bool() |
|
148 | 148 | isalias = Bool() |
|
149 | 149 | namespace = Enum((u'builtin', u'magics', u'alias', u'Interactive')) |
|
150 | 150 | type_name = Unicode() |
|
151 | 151 | string_form = Unicode() |
|
152 | 152 | base_class = Unicode() |
|
153 | 153 | length = Integer() |
|
154 | 154 | file = Unicode() |
|
155 | 155 | definition = Unicode() |
|
156 | 156 | argspec = Dict() |
|
157 | 157 | init_definition = Unicode() |
|
158 | 158 | docstring = Unicode() |
|
159 | 159 | init_docstring = Unicode() |
|
160 | 160 | class_docstring = Unicode() |
|
161 | 161 | call_def = Unicode() |
|
162 | 162 | call_docstring = Unicode() |
|
163 | 163 | source = Unicode() |
|
164 | 164 | |
|
165 | 165 | def check(self, d): |
|
166 | 166 | for tst in Reference.check(self, d): |
|
167 | 167 | yield tst |
|
168 | 168 | if d['argspec'] is not None: |
|
169 | 169 | for tst in ArgSpec().check(d['argspec']): |
|
170 | 170 | yield tst |
|
171 | 171 | |
|
172 | 172 | |
|
173 | 173 | class ArgSpec(Reference): |
|
174 | 174 | args = List(Unicode) |
|
175 | 175 | varargs = Unicode() |
|
176 | 176 | varkw = Unicode() |
|
177 | 177 | defaults = List() |
|
178 | 178 | |
|
179 | 179 | |
|
180 | 180 | class Status(Reference): |
|
181 | 181 | execution_state = Enum((u'busy', u'idle')) |
|
182 | 182 | |
|
183 | 183 | |
|
184 | 184 | class CompleteReply(Reference): |
|
185 | 185 | matches = List(Unicode) |
|
186 | 186 | |
|
187 | 187 | |
|
188 | 188 | # IOPub messages |
|
189 | 189 | |
|
190 | 190 | class PyIn(Reference): |
|
191 | 191 | code = Unicode() |
|
192 | 192 | execution_count = Integer() |
|
193 | 193 | |
|
194 | 194 | |
|
195 | 195 | PyErr = ExecuteReplyError |
|
196 | 196 | |
|
197 | 197 | |
|
198 | 198 | class Stream(Reference): |
|
199 | 199 | name = Enum((u'stdout', u'stderr')) |
|
200 | 200 | data = Unicode() |
|
201 | 201 | |
|
202 | 202 | |
|
203 | 203 | mime_pat = re.compile(r'\w+/\w+') |
|
204 | 204 | |
|
205 | 205 | class DisplayData(Reference): |
|
206 | 206 | source = Unicode() |
|
207 | 207 | metadata = Dict() |
|
208 | 208 | data = Dict() |
|
209 | 209 | def _data_changed(self, name, old, new): |
|
210 | 210 | for k,v in new.iteritems(): |
|
211 | 211 | nt.assert_true(mime_pat.match(k)) |
|
212 | 212 | nt.assert_true(isinstance(v, basestring), "expected string data, got %r" % v) |
|
213 | 213 | |
|
214 | 214 | |
|
215 | 215 | class PyOut(Reference): |
|
216 | 216 | execution_count = Integer() |
|
217 | 217 | data = Dict() |
|
218 | 218 | def _data_changed(self, name, old, new): |
|
219 | 219 | for k,v in new.iteritems(): |
|
220 | 220 | nt.assert_true(mime_pat.match(k)) |
|
221 | 221 | nt.assert_true(isinstance(v, basestring), "expected string data, got %r" % v) |
|
222 | 222 | |
|
223 | 223 | |
|
224 | 224 | references = { |
|
225 | 225 | 'execute_reply' : ExecuteReply(), |
|
226 | 226 | 'object_info_reply' : OInfoReply(), |
|
227 | 227 | 'status' : Status(), |
|
228 | 228 | 'complete_reply' : CompleteReply(), |
|
229 | 229 | 'pyin' : PyIn(), |
|
230 | 230 | 'pyout' : PyOut(), |
|
231 | 231 | 'pyerr' : PyErr(), |
|
232 | 232 | 'stream' : Stream(), |
|
233 | 233 | 'display_data' : DisplayData(), |
|
234 | 234 | } |
|
235 | 235 | |
|
236 | 236 | |
|
237 | 237 | def validate_message(msg, msg_type=None, parent=None): |
|
238 | 238 | """validate a message |
|
239 | 239 | |
|
240 | 240 | This is a generator, and must be iterated through to actually |
|
241 | 241 | trigger each test. |
|
242 | 242 | |
|
243 | 243 | If msg_type and/or parent are given, the msg_type and/or parent msg_id |
|
244 | 244 | are compared with the given values. |
|
245 | 245 | """ |
|
246 | 246 | RMessage().check(msg) |
|
247 | 247 | if msg_type: |
|
248 |
yield nt.assert_equal |
|
|
248 | yield nt.assert_equal(msg['msg_type'], msg_type) | |
|
249 | 249 | if parent: |
|
250 | 250 | yield nt.assert_equal(msg['parent_header']['msg_id'], parent) |
|
251 | 251 | content = msg['content'] |
|
252 | 252 | ref = references[msg['msg_type']] |
|
253 | 253 | for tst in ref.check(content): |
|
254 | 254 | yield tst |
|
255 | 255 | |
|
256 | 256 | |
|
257 | 257 | #----------------------------------------------------------------------------- |
|
258 | 258 | # Tests |
|
259 | 259 | #----------------------------------------------------------------------------- |
|
260 | 260 | |
|
261 | 261 | # Shell channel |
|
262 | 262 | |
|
263 | 263 | @dec.parametric |
|
264 | 264 | def test_execute(): |
|
265 | 265 | flush_channels() |
|
266 | 266 | |
|
267 | 267 | shell = KM.shell_channel |
|
268 | 268 | msg_id = shell.execute(code='x=1') |
|
269 | 269 | reply = shell.get_msg(timeout=2) |
|
270 | 270 | for tst in validate_message(reply, 'execute_reply', msg_id): |
|
271 | 271 | yield tst |
|
272 | 272 | |
|
273 | 273 | |
|
274 | 274 | @dec.parametric |
|
275 | 275 | def test_execute_silent(): |
|
276 | 276 | flush_channels() |
|
277 | 277 | msg_id, reply = execute(code='x=1', silent=True) |
|
278 | 278 | |
|
279 | 279 | # flush status=idle |
|
280 | 280 | status = KM.sub_channel.get_msg(timeout=2) |
|
281 | 281 | for tst in validate_message(status, 'status', msg_id): |
|
282 | 282 | yield tst |
|
283 |
nt.assert_equal |
|
|
283 | nt.assert_equal(status['content']['execution_state'], 'idle') | |
|
284 | 284 | |
|
285 | 285 | yield nt.assert_raises(Empty, KM.sub_channel.get_msg, timeout=0.1) |
|
286 | 286 | count = reply['execution_count'] |
|
287 | 287 | |
|
288 | 288 | msg_id, reply = execute(code='x=2', silent=True) |
|
289 | 289 | |
|
290 | 290 | # flush status=idle |
|
291 | 291 | status = KM.sub_channel.get_msg(timeout=2) |
|
292 | 292 | for tst in validate_message(status, 'status', msg_id): |
|
293 | 293 | yield tst |
|
294 |
yield nt.assert_equal |
|
|
294 | yield nt.assert_equal(status['content']['execution_state'], 'idle') | |
|
295 | 295 | |
|
296 | 296 | yield nt.assert_raises(Empty, KM.sub_channel.get_msg, timeout=0.1) |
|
297 | 297 | count_2 = reply['execution_count'] |
|
298 |
yield nt.assert_equal |
|
|
298 | yield nt.assert_equal(count_2, count) | |
|
299 | 299 | |
|
300 | 300 | |
|
301 | 301 | @dec.parametric |
|
302 | 302 | def test_execute_error(): |
|
303 | 303 | flush_channels() |
|
304 | 304 | |
|
305 | 305 | msg_id, reply = execute(code='1/0') |
|
306 |
yield nt.assert_equal |
|
|
307 |
yield nt.assert_equal |
|
|
306 | yield nt.assert_equal(reply['status'], 'error') | |
|
307 | yield nt.assert_equal(reply['ename'], 'ZeroDivisionError') | |
|
308 | 308 | |
|
309 | 309 | pyerr = KM.sub_channel.get_msg(timeout=2) |
|
310 | 310 | for tst in validate_message(pyerr, 'pyerr', msg_id): |
|
311 | 311 | yield tst |
|
312 | 312 | |
|
313 | 313 | |
|
314 | 314 | def test_execute_inc(): |
|
315 | 315 | """execute request should increment execution_count""" |
|
316 | 316 | flush_channels() |
|
317 | 317 | |
|
318 | 318 | msg_id, reply = execute(code='x=1') |
|
319 | 319 | count = reply['execution_count'] |
|
320 | 320 | |
|
321 | 321 | flush_channels() |
|
322 | 322 | |
|
323 | 323 | msg_id, reply = execute(code='x=2') |
|
324 | 324 | count_2 = reply['execution_count'] |
|
325 |
nt.assert_equal |
|
|
325 | nt.assert_equal(count_2, count+1) | |
|
326 | 326 | |
|
327 | 327 | |
|
328 | 328 | def test_user_variables(): |
|
329 | 329 | flush_channels() |
|
330 | 330 | |
|
331 | 331 | msg_id, reply = execute(code='x=1', user_variables=['x']) |
|
332 | 332 | user_variables = reply['user_variables'] |
|
333 |
nt.assert_equal |
|
|
333 | nt.assert_equal(user_variables, {u'x' : u'1'}) | |
|
334 | 334 | |
|
335 | 335 | |
|
336 | 336 | def test_user_expressions(): |
|
337 | 337 | flush_channels() |
|
338 | 338 | |
|
339 | 339 | msg_id, reply = execute(code='x=1', user_expressions=dict(foo='x+1')) |
|
340 | 340 | user_expressions = reply['user_expressions'] |
|
341 |
nt.assert_equal |
|
|
341 | nt.assert_equal(user_expressions, {u'foo' : u'2'}) | |
|
342 | 342 | |
|
343 | 343 | |
|
344 | 344 | @dec.parametric |
|
345 | 345 | def test_oinfo(): |
|
346 | 346 | flush_channels() |
|
347 | 347 | |
|
348 | 348 | shell = KM.shell_channel |
|
349 | 349 | |
|
350 | 350 | msg_id = shell.object_info('a') |
|
351 | 351 | reply = shell.get_msg(timeout=2) |
|
352 | 352 | for tst in validate_message(reply, 'object_info_reply', msg_id): |
|
353 | 353 | yield tst |
|
354 | 354 | |
|
355 | 355 | |
|
356 | 356 | @dec.parametric |
|
357 | 357 | def test_oinfo_found(): |
|
358 | 358 | flush_channels() |
|
359 | 359 | |
|
360 | 360 | shell = KM.shell_channel |
|
361 | 361 | |
|
362 | 362 | msg_id, reply = execute(code='a=5') |
|
363 | 363 | |
|
364 | 364 | msg_id = shell.object_info('a') |
|
365 | 365 | reply = shell.get_msg(timeout=2) |
|
366 | 366 | for tst in validate_message(reply, 'object_info_reply', msg_id): |
|
367 | 367 | yield tst |
|
368 | 368 | content = reply['content'] |
|
369 | 369 | yield nt.assert_true(content['found']) |
|
370 | 370 | argspec = content['argspec'] |
|
371 | 371 | yield nt.assert_true(argspec is None, "didn't expect argspec dict, got %r" % argspec) |
|
372 | 372 | |
|
373 | 373 | |
|
374 | 374 | @dec.parametric |
|
375 | 375 | def test_oinfo_detail(): |
|
376 | 376 | flush_channels() |
|
377 | 377 | |
|
378 | 378 | shell = KM.shell_channel |
|
379 | 379 | |
|
380 | 380 | msg_id, reply = execute(code='ip=get_ipython()') |
|
381 | 381 | |
|
382 | 382 | msg_id = shell.object_info('ip.object_inspect', detail_level=2) |
|
383 | 383 | reply = shell.get_msg(timeout=2) |
|
384 | 384 | for tst in validate_message(reply, 'object_info_reply', msg_id): |
|
385 | 385 | yield tst |
|
386 | 386 | content = reply['content'] |
|
387 | 387 | yield nt.assert_true(content['found']) |
|
388 | 388 | argspec = content['argspec'] |
|
389 | 389 | yield nt.assert_true(isinstance(argspec, dict), "expected non-empty argspec dict, got %r" % argspec) |
|
390 |
yield nt.assert_equal |
|
|
390 | yield nt.assert_equal(argspec['defaults'], [0]) | |
|
391 | 391 | |
|
392 | 392 | |
|
393 | 393 | @dec.parametric |
|
394 | 394 | def test_oinfo_not_found(): |
|
395 | 395 | flush_channels() |
|
396 | 396 | |
|
397 | 397 | shell = KM.shell_channel |
|
398 | 398 | |
|
399 | 399 | msg_id = shell.object_info('dne') |
|
400 | 400 | reply = shell.get_msg(timeout=2) |
|
401 | 401 | for tst in validate_message(reply, 'object_info_reply', msg_id): |
|
402 | 402 | yield tst |
|
403 | 403 | content = reply['content'] |
|
404 | 404 | yield nt.assert_false(content['found']) |
|
405 | 405 | |
|
406 | 406 | |
|
407 | 407 | @dec.parametric |
|
408 | 408 | def test_complete(): |
|
409 | 409 | flush_channels() |
|
410 | 410 | |
|
411 | 411 | shell = KM.shell_channel |
|
412 | 412 | |
|
413 | 413 | msg_id, reply = execute(code="alpha = albert = 5") |
|
414 | 414 | |
|
415 | 415 | msg_id = shell.complete('al', 'al', 2) |
|
416 | 416 | reply = shell.get_msg(timeout=2) |
|
417 | 417 | for tst in validate_message(reply, 'complete_reply', msg_id): |
|
418 | 418 | yield tst |
|
419 | 419 | matches = reply['content']['matches'] |
|
420 | 420 | for name in ('alpha', 'albert'): |
|
421 | 421 | yield nt.assert_true(name in matches, "Missing match: %r" % name) |
|
422 | 422 | |
|
423 | 423 | |
|
424 | 424 | # IOPub channel |
|
425 | 425 | |
|
426 | 426 | |
|
427 | 427 | @dec.parametric |
|
428 | 428 | def test_stream(): |
|
429 | 429 | flush_channels() |
|
430 | 430 | |
|
431 | 431 | msg_id, reply = execute("print('hi')") |
|
432 | 432 | |
|
433 | 433 | stdout = KM.sub_channel.get_msg(timeout=2) |
|
434 | 434 | for tst in validate_message(stdout, 'stream', msg_id): |
|
435 | 435 | yield tst |
|
436 | 436 | content = stdout['content'] |
|
437 |
yield nt.assert_equal |
|
|
438 |
yield nt.assert_equal |
|
|
437 | yield nt.assert_equal(content['name'], u'stdout') | |
|
438 | yield nt.assert_equal(content['data'], u'hi\n') | |
|
439 | 439 | |
|
440 | 440 | |
|
441 | 441 | @dec.parametric |
|
442 | 442 | def test_display_data(): |
|
443 | 443 | flush_channels() |
|
444 | 444 | |
|
445 | 445 | msg_id, reply = execute("from IPython.core.display import display; display(1)") |
|
446 | 446 | |
|
447 | 447 | display = KM.sub_channel.get_msg(timeout=2) |
|
448 | 448 | for tst in validate_message(display, 'display_data', parent=msg_id): |
|
449 | 449 | yield tst |
|
450 | 450 | data = display['content']['data'] |
|
451 |
yield nt.assert_equal |
|
|
451 | yield nt.assert_equal(data['text/plain'], u'1') | |
|
452 | 452 |
General Comments 0
You need to be logged in to leave comments.
Login now