Show More
@@ -1,647 +1,653 b'' | |||
|
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 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 | |
|
29 | 29 | #----------------------------------------------------------------------------- |
|
30 | 30 | # Semi-complete examples (also used as tests) |
|
31 | 31 | #----------------------------------------------------------------------------- |
|
32 | 32 | |
|
33 | 33 | # Note: at the bottom, there's a slightly more complete version of this that |
|
34 | 34 | # can be useful during development of code here. |
|
35 | 35 | |
|
36 | 36 | def mini_interactive_loop(input_func): |
|
37 | 37 | """Minimal example of the logic of an interactive interpreter loop. |
|
38 | 38 | |
|
39 | 39 | This serves as an example, and it is used by the test system with a fake |
|
40 | 40 | raw_input that simulates interactive input.""" |
|
41 | 41 | |
|
42 | 42 | from IPython.core.inputsplitter import InputSplitter |
|
43 | 43 | |
|
44 | 44 | isp = InputSplitter() |
|
45 | 45 | # In practice, this input loop would be wrapped in an outside loop to read |
|
46 | 46 | # input indefinitely, until some exit/quit command was issued. Here we |
|
47 | 47 | # only illustrate the basic inner loop. |
|
48 | 48 | while isp.push_accepts_more(): |
|
49 | 49 | indent = ' '*isp.indent_spaces |
|
50 | 50 | prompt = '>>> ' + indent |
|
51 | 51 | line = indent + input_func(prompt) |
|
52 | 52 | isp.push(line) |
|
53 | 53 | |
|
54 | 54 | # Here we just return input so we can use it in a test suite, but a real |
|
55 | 55 | # interpreter would instead send it for execution somewhere. |
|
56 | 56 | src = isp.source_reset() |
|
57 | 57 | #print 'Input source was:\n', src # dbg |
|
58 | 58 | return src |
|
59 | 59 | |
|
60 | 60 | #----------------------------------------------------------------------------- |
|
61 | 61 | # Test utilities, just for local use |
|
62 | 62 | #----------------------------------------------------------------------------- |
|
63 | 63 | |
|
64 | 64 | def assemble(block): |
|
65 | 65 | """Assemble a block into multi-line sub-blocks.""" |
|
66 | 66 | return ['\n'.join(sub_block)+'\n' for sub_block in block] |
|
67 | 67 | |
|
68 | 68 | |
|
69 | 69 | def pseudo_input(lines): |
|
70 | 70 | """Return a function that acts like raw_input but feeds the input list.""" |
|
71 | 71 | ilines = iter(lines) |
|
72 | 72 | def raw_in(prompt): |
|
73 | 73 | try: |
|
74 | 74 | return next(ilines) |
|
75 | 75 | except StopIteration: |
|
76 | 76 | return '' |
|
77 | 77 | return raw_in |
|
78 | 78 | |
|
79 | 79 | #----------------------------------------------------------------------------- |
|
80 | 80 | # Tests |
|
81 | 81 | #----------------------------------------------------------------------------- |
|
82 | 82 | def test_spaces(): |
|
83 | 83 | tests = [('', 0), |
|
84 | 84 | (' ', 1), |
|
85 | 85 | ('\n', 0), |
|
86 | 86 | (' \n', 1), |
|
87 | 87 | ('x', 0), |
|
88 | 88 | (' x', 1), |
|
89 | 89 | (' x',2), |
|
90 | 90 | (' x',4), |
|
91 | 91 | # Note: tabs are counted as a single whitespace! |
|
92 | 92 | ('\tx', 1), |
|
93 | 93 | ('\t x', 2), |
|
94 | 94 | ] |
|
95 | 95 | |
|
96 | 96 | for s, nsp in tests: |
|
97 | 97 | nt.assert_equal(isp.num_ini_spaces(s), nsp) |
|
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 | |
|
110 | 110 | for inp, out in tests: |
|
111 | 111 | nt.assert_equal(isp.remove_comments(inp), out) |
|
112 | 112 | |
|
113 | 113 | |
|
114 | 114 | def test_get_input_encoding(): |
|
115 | 115 | encoding = isp.get_input_encoding() |
|
116 | 116 | nt.assert_true(isinstance(encoding, basestring)) |
|
117 | 117 | # simple-minded check that at least encoding a simple string works with the |
|
118 | 118 | # encoding we got. |
|
119 | 119 | nt.assert_equal('test'.encode(encoding), 'test') |
|
120 | 120 | |
|
121 | 121 | |
|
122 | 122 | class NoInputEncodingTestCase(unittest.TestCase): |
|
123 | 123 | def setUp(self): |
|
124 | 124 | self.old_stdin = sys.stdin |
|
125 | 125 | class X: pass |
|
126 | 126 | fake_stdin = X() |
|
127 | 127 | sys.stdin = fake_stdin |
|
128 | 128 | |
|
129 | 129 | def test(self): |
|
130 | 130 | # Verify that if sys.stdin has no 'encoding' attribute we do the right |
|
131 | 131 | # thing |
|
132 | 132 | enc = isp.get_input_encoding() |
|
133 | 133 | self.assertEqual(enc, 'ascii') |
|
134 | 134 | |
|
135 | 135 | def tearDown(self): |
|
136 | 136 | sys.stdin = self.old_stdin |
|
137 | 137 | |
|
138 | 138 | |
|
139 | 139 | class InputSplitterTestCase(unittest.TestCase): |
|
140 | 140 | def setUp(self): |
|
141 | 141 | self.isp = isp.InputSplitter() |
|
142 | 142 | |
|
143 | 143 | def test_reset(self): |
|
144 | 144 | isp = self.isp |
|
145 | 145 | isp.push('x=1') |
|
146 | 146 | isp.reset() |
|
147 | 147 | self.assertEqual(isp._buffer, []) |
|
148 | 148 | self.assertEqual(isp.indent_spaces, 0) |
|
149 | 149 | self.assertEqual(isp.source, '') |
|
150 | 150 | self.assertEqual(isp.code, None) |
|
151 | 151 | self.assertEqual(isp._is_complete, False) |
|
152 | 152 | |
|
153 | 153 | def test_source(self): |
|
154 | 154 | self.isp._store('1') |
|
155 | 155 | self.isp._store('2') |
|
156 | 156 | self.assertEqual(self.isp.source, '1\n2\n') |
|
157 | 157 | self.assertTrue(len(self.isp._buffer)>0) |
|
158 | 158 | self.assertEqual(self.isp.source_reset(), '1\n2\n') |
|
159 | 159 | self.assertEqual(self.isp._buffer, []) |
|
160 | 160 | self.assertEqual(self.isp.source, '') |
|
161 | 161 | |
|
162 | 162 | def test_indent(self): |
|
163 | 163 | isp = self.isp # shorthand |
|
164 | 164 | isp.push('x=1') |
|
165 | 165 | self.assertEqual(isp.indent_spaces, 0) |
|
166 | 166 | isp.push('if 1:\n x=1') |
|
167 | 167 | self.assertEqual(isp.indent_spaces, 4) |
|
168 | 168 | isp.push('y=2\n') |
|
169 | 169 | self.assertEqual(isp.indent_spaces, 0) |
|
170 | 170 | |
|
171 | 171 | def test_indent2(self): |
|
172 | 172 | # In cell mode, inputs must be fed in whole blocks, so skip this test |
|
173 | 173 | if self.isp.input_mode == 'cell': return |
|
174 | 174 | |
|
175 | 175 | isp = self.isp |
|
176 | 176 | isp.push('if 1:') |
|
177 | 177 | self.assertEqual(isp.indent_spaces, 4) |
|
178 | 178 | isp.push(' x=1') |
|
179 | 179 | self.assertEqual(isp.indent_spaces, 4) |
|
180 | 180 | # Blank lines shouldn't change the indent level |
|
181 | 181 | isp.push(' '*2) |
|
182 | 182 | self.assertEqual(isp.indent_spaces, 4) |
|
183 | 183 | |
|
184 | 184 | def test_indent3(self): |
|
185 | 185 | # In cell mode, inputs must be fed in whole blocks, so skip this test |
|
186 | 186 | if self.isp.input_mode == 'cell': return |
|
187 | 187 | |
|
188 | 188 | isp = self.isp |
|
189 | 189 | # When a multiline statement contains parens or multiline strings, we |
|
190 | 190 | # shouldn't get confused. |
|
191 | 191 | isp.push("if 1:") |
|
192 | 192 | isp.push(" x = (1+\n 2)") |
|
193 | 193 | self.assertEqual(isp.indent_spaces, 4) |
|
194 | 194 | |
|
195 | 195 | def test_dedent_pass(self): |
|
196 | 196 | isp = self.isp # shorthand |
|
197 | 197 | # should NOT cause dedent |
|
198 | 198 | isp.push('if 1:\n passes = 5') |
|
199 | 199 | self.assertEqual(isp.indent_spaces, 4) |
|
200 | 200 | isp.push('if 1:\n pass') |
|
201 | 201 | self.assertEqual(isp.indent_spaces, 0) |
|
202 | 202 | isp.push('if 1:\n pass ') |
|
203 | 203 | self.assertEqual(isp.indent_spaces, 0) |
|
204 | 204 | |
|
205 | 205 | def test_dedent_raise(self): |
|
206 | 206 | isp = self.isp # shorthand |
|
207 | 207 | # should NOT cause dedent |
|
208 | 208 | isp.push('if 1:\n raised = 4') |
|
209 | 209 | self.assertEqual(isp.indent_spaces, 4) |
|
210 | 210 | isp.push('if 1:\n raise TypeError()') |
|
211 | 211 | self.assertEqual(isp.indent_spaces, 0) |
|
212 | 212 | isp.push('if 1:\n raise') |
|
213 | 213 | self.assertEqual(isp.indent_spaces, 0) |
|
214 | 214 | isp.push('if 1:\n raise ') |
|
215 | 215 | self.assertEqual(isp.indent_spaces, 0) |
|
216 | 216 | |
|
217 | 217 | def test_dedent_return(self): |
|
218 | 218 | isp = self.isp # shorthand |
|
219 | 219 | # should NOT cause dedent |
|
220 | 220 | isp.push('if 1:\n returning = 4') |
|
221 | 221 | self.assertEqual(isp.indent_spaces, 4) |
|
222 | 222 | isp.push('if 1:\n return 5 + 493') |
|
223 | 223 | self.assertEqual(isp.indent_spaces, 0) |
|
224 | 224 | isp.push('if 1:\n return') |
|
225 | 225 | self.assertEqual(isp.indent_spaces, 0) |
|
226 | 226 | isp.push('if 1:\n return ') |
|
227 | 227 | self.assertEqual(isp.indent_spaces, 0) |
|
228 | 228 | isp.push('if 1:\n return(0)') |
|
229 | 229 | self.assertEqual(isp.indent_spaces, 0) |
|
230 | 230 | |
|
231 | 231 | def test_push(self): |
|
232 | 232 | isp = self.isp |
|
233 | 233 | self.assertTrue(isp.push('x=1')) |
|
234 | 234 | |
|
235 | 235 | def test_push2(self): |
|
236 | 236 | isp = self.isp |
|
237 | 237 | self.assertFalse(isp.push('if 1:')) |
|
238 | 238 | for line in [' x=1', '# a comment', ' y=2']: |
|
239 | 239 | self.assertTrue(isp.push(line)) |
|
240 | 240 | |
|
241 | def test_push3(self): | |
|
242 | isp = self.isp | |
|
243 | isp.push('if True:') | |
|
244 | isp.push(' a = 1') | |
|
245 | self.assertFalse(isp.push('b = [1,')) | |
|
246 | ||
|
241 | 247 | def test_replace_mode(self): |
|
242 | 248 | isp = self.isp |
|
243 | 249 | isp.input_mode = 'cell' |
|
244 | 250 | isp.push('x=1') |
|
245 | 251 | self.assertEqual(isp.source, 'x=1\n') |
|
246 | 252 | isp.push('x=2') |
|
247 | 253 | self.assertEqual(isp.source, 'x=2\n') |
|
248 | 254 | |
|
249 | 255 | def test_push_accepts_more(self): |
|
250 | 256 | isp = self.isp |
|
251 | 257 | isp.push('x=1') |
|
252 | 258 | self.assertFalse(isp.push_accepts_more()) |
|
253 | 259 | |
|
254 | 260 | def test_push_accepts_more2(self): |
|
255 | 261 | # In cell mode, inputs must be fed in whole blocks, so skip this test |
|
256 | 262 | if self.isp.input_mode == 'cell': return |
|
257 | 263 | |
|
258 | 264 | isp = self.isp |
|
259 | 265 | isp.push('if 1:') |
|
260 | 266 | self.assertTrue(isp.push_accepts_more()) |
|
261 | 267 | isp.push(' x=1') |
|
262 | 268 | self.assertTrue(isp.push_accepts_more()) |
|
263 | 269 | isp.push('') |
|
264 | 270 | self.assertFalse(isp.push_accepts_more()) |
|
265 | 271 | |
|
266 | 272 | def test_push_accepts_more3(self): |
|
267 | 273 | isp = self.isp |
|
268 | 274 | isp.push("x = (2+\n3)") |
|
269 | 275 | self.assertFalse(isp.push_accepts_more()) |
|
270 | 276 | |
|
271 | 277 | def test_push_accepts_more4(self): |
|
272 | 278 | # In cell mode, inputs must be fed in whole blocks, so skip this test |
|
273 | 279 | if self.isp.input_mode == 'cell': return |
|
274 | 280 | |
|
275 | 281 | isp = self.isp |
|
276 | 282 | # When a multiline statement contains parens or multiline strings, we |
|
277 | 283 | # shouldn't get confused. |
|
278 | 284 | # FIXME: we should be able to better handle de-dents in statements like |
|
279 | 285 | # multiline strings and multiline expressions (continued with \ or |
|
280 | 286 | # parens). Right now we aren't handling the indentation tracking quite |
|
281 | 287 | # correctly with this, though in practice it may not be too much of a |
|
282 | 288 | # problem. We'll need to see. |
|
283 | 289 | isp.push("if 1:") |
|
284 | 290 | isp.push(" x = (2+") |
|
285 | 291 | isp.push(" 3)") |
|
286 | 292 | self.assertTrue(isp.push_accepts_more()) |
|
287 | 293 | isp.push(" y = 3") |
|
288 | 294 | self.assertTrue(isp.push_accepts_more()) |
|
289 | 295 | isp.push('') |
|
290 | 296 | self.assertFalse(isp.push_accepts_more()) |
|
291 | 297 | |
|
292 | 298 | def test_push_accepts_more5(self): |
|
293 | 299 | # In cell mode, inputs must be fed in whole blocks, so skip this test |
|
294 | 300 | if self.isp.input_mode == 'cell': return |
|
295 | 301 | |
|
296 | 302 | isp = self.isp |
|
297 | 303 | isp.push('try:') |
|
298 | 304 | isp.push(' a = 5') |
|
299 | 305 | isp.push('except:') |
|
300 | 306 | isp.push(' raise') |
|
301 | 307 | self.assertTrue(isp.push_accepts_more()) |
|
302 | 308 | |
|
303 | 309 | def test_continuation(self): |
|
304 | 310 | isp = self.isp |
|
305 | 311 | isp.push("import os, \\") |
|
306 | 312 | self.assertTrue(isp.push_accepts_more()) |
|
307 | 313 | isp.push("sys") |
|
308 | 314 | self.assertFalse(isp.push_accepts_more()) |
|
309 | 315 | |
|
310 | 316 | def test_syntax_error(self): |
|
311 | 317 | isp = self.isp |
|
312 | 318 | # Syntax errors immediately produce a 'ready' block, so the invalid |
|
313 | 319 | # Python can be sent to the kernel for evaluation with possible ipython |
|
314 | 320 | # special-syntax conversion. |
|
315 | 321 | isp.push('run foo') |
|
316 | 322 | self.assertFalse(isp.push_accepts_more()) |
|
317 | 323 | |
|
318 | 324 | def test_unicode(self): |
|
319 | 325 | self.isp.push(u"PΓ©rez") |
|
320 | 326 | self.isp.push(u'\xc3\xa9') |
|
321 | 327 | self.isp.push(u"u'\xc3\xa9'") |
|
322 | 328 | |
|
323 | 329 | class InteractiveLoopTestCase(unittest.TestCase): |
|
324 | 330 | """Tests for an interactive loop like a python shell. |
|
325 | 331 | """ |
|
326 | 332 | def check_ns(self, lines, ns): |
|
327 | 333 | """Validate that the given input lines produce the resulting namespace. |
|
328 | 334 | |
|
329 | 335 | Note: the input lines are given exactly as they would be typed in an |
|
330 | 336 | auto-indenting environment, as mini_interactive_loop above already does |
|
331 | 337 | auto-indenting and prepends spaces to the input. |
|
332 | 338 | """ |
|
333 | 339 | src = mini_interactive_loop(pseudo_input(lines)) |
|
334 | 340 | test_ns = {} |
|
335 | 341 | exec src in test_ns |
|
336 | 342 | # We can't check that the provided ns is identical to the test_ns, |
|
337 | 343 | # because Python fills test_ns with extra keys (copyright, etc). But |
|
338 | 344 | # we can check that the given dict is *contained* in test_ns |
|
339 | 345 | for k,v in ns.iteritems(): |
|
340 | 346 | self.assertEqual(test_ns[k], v) |
|
341 | 347 | |
|
342 | 348 | def test_simple(self): |
|
343 | 349 | self.check_ns(['x=1'], dict(x=1)) |
|
344 | 350 | |
|
345 | 351 | def test_simple2(self): |
|
346 | 352 | self.check_ns(['if 1:', 'x=2'], dict(x=2)) |
|
347 | 353 | |
|
348 | 354 | def test_xy(self): |
|
349 | 355 | self.check_ns(['x=1; y=2'], dict(x=1, y=2)) |
|
350 | 356 | |
|
351 | 357 | def test_abc(self): |
|
352 | 358 | self.check_ns(['if 1:','a=1','b=2','c=3'], dict(a=1, b=2, c=3)) |
|
353 | 359 | |
|
354 | 360 | def test_multi(self): |
|
355 | 361 | self.check_ns(['x =(1+','1+','2)'], dict(x=4)) |
|
356 | 362 | |
|
357 | 363 | |
|
358 | 364 | def test_LineInfo(): |
|
359 | 365 | """Simple test for LineInfo construction and str()""" |
|
360 | 366 | linfo = isp.LineInfo(' %cd /home') |
|
361 | 367 | nt.assert_equals(str(linfo), 'LineInfo [ |%|cd|/home]') |
|
362 | 368 | |
|
363 | 369 | |
|
364 | 370 | def test_split_user_input(): |
|
365 | 371 | """Unicode test - split_user_input already has good doctests""" |
|
366 | 372 | line = u"PΓ©rez Fernando" |
|
367 | 373 | parts = isp.split_user_input(line) |
|
368 | 374 | parts_expected = (u'', u'', u'', line) |
|
369 | 375 | nt.assert_equal(parts, parts_expected) |
|
370 | 376 | |
|
371 | 377 | |
|
372 | 378 | # Transformer tests |
|
373 | 379 | def transform_checker(tests, func): |
|
374 | 380 | """Utility to loop over test inputs""" |
|
375 | 381 | for inp, tr in tests: |
|
376 | 382 | nt.assert_equals(func(inp), tr) |
|
377 | 383 | |
|
378 | 384 | # Data for all the syntax tests in the form of lists of pairs of |
|
379 | 385 | # raw/transformed input. We store it here as a global dict so that we can use |
|
380 | 386 | # it both within single-function tests and also to validate the behavior of the |
|
381 | 387 | # larger objects |
|
382 | 388 | |
|
383 | 389 | syntax = \ |
|
384 | 390 | dict(assign_system = |
|
385 | 391 | [('a =! ls', 'a = get_ipython().getoutput(u"ls")'), |
|
386 | 392 | ('b = !ls', 'b = get_ipython().getoutput(u"ls")'), |
|
387 | 393 | ('x=1', 'x=1'), # normal input is unmodified |
|
388 | 394 | (' ',' '), # blank lines are kept intact |
|
389 | 395 | ], |
|
390 | 396 | |
|
391 | 397 | assign_magic = |
|
392 | 398 | [('a =% who', 'a = get_ipython().magic(u"who")'), |
|
393 | 399 | ('b = %who', 'b = get_ipython().magic(u"who")'), |
|
394 | 400 | ('x=1', 'x=1'), # normal input is unmodified |
|
395 | 401 | (' ',' '), # blank lines are kept intact |
|
396 | 402 | ], |
|
397 | 403 | |
|
398 | 404 | classic_prompt = |
|
399 | 405 | [('>>> x=1', 'x=1'), |
|
400 | 406 | ('x=1', 'x=1'), # normal input is unmodified |
|
401 | 407 | (' ', ' '), # blank lines are kept intact |
|
402 | 408 | ('... ', ''), # continuation prompts |
|
403 | 409 | ], |
|
404 | 410 | |
|
405 | 411 | ipy_prompt = |
|
406 | 412 | [('In [1]: x=1', 'x=1'), |
|
407 | 413 | ('x=1', 'x=1'), # normal input is unmodified |
|
408 | 414 | (' ',' '), # blank lines are kept intact |
|
409 | 415 | (' ....: ', ''), # continuation prompts |
|
410 | 416 | ], |
|
411 | 417 | |
|
412 | 418 | # Tests for the escape transformer to leave normal code alone |
|
413 | 419 | escaped_noesc = |
|
414 | 420 | [ (' ', ' '), |
|
415 | 421 | ('x=1', 'x=1'), |
|
416 | 422 | ], |
|
417 | 423 | |
|
418 | 424 | # System calls |
|
419 | 425 | escaped_shell = |
|
420 | 426 | [ ('!ls', 'get_ipython().system(u"ls")'), |
|
421 | 427 | # Double-escape shell, this means to capture the output of the |
|
422 | 428 | # subprocess and return it |
|
423 | 429 | ('!!ls', 'get_ipython().getoutput(u"ls")'), |
|
424 | 430 | ], |
|
425 | 431 | |
|
426 | 432 | # Help/object info |
|
427 | 433 | escaped_help = |
|
428 | 434 | [ ('?', 'get_ipython().show_usage()'), |
|
429 | 435 | ('?x1', 'get_ipython().magic(u"pinfo x1")'), |
|
430 | 436 | ('??x2', 'get_ipython().magic(u"pinfo2 x2")'), |
|
431 | 437 | ('x3?', 'get_ipython().magic(u"pinfo x3")'), |
|
432 | 438 | ('x4??', 'get_ipython().magic(u"pinfo2 x4")'), |
|
433 | 439 | ('%hist?', 'get_ipython().magic(u"pinfo %hist")'), |
|
434 | 440 | ('f*?', 'get_ipython().magic(u"psearch f*")'), |
|
435 | 441 | ('ax.*aspe*?', 'get_ipython().magic(u"psearch ax.*aspe*")'), |
|
436 | 442 | ], |
|
437 | 443 | |
|
438 | 444 | # Explicit magic calls |
|
439 | 445 | escaped_magic = |
|
440 | 446 | [ ('%cd', 'get_ipython().magic(u"cd")'), |
|
441 | 447 | ('%cd /home', 'get_ipython().magic(u"cd /home")'), |
|
442 | 448 | (' %magic', ' get_ipython().magic(u"magic")'), |
|
443 | 449 | ], |
|
444 | 450 | |
|
445 | 451 | # Quoting with separate arguments |
|
446 | 452 | escaped_quote = |
|
447 | 453 | [ (',f', 'f("")'), |
|
448 | 454 | (',f x', 'f("x")'), |
|
449 | 455 | (' ,f y', ' f("y")'), |
|
450 | 456 | (',f a b', 'f("a", "b")'), |
|
451 | 457 | ], |
|
452 | 458 | |
|
453 | 459 | # Quoting with single argument |
|
454 | 460 | escaped_quote2 = |
|
455 | 461 | [ (';f', 'f("")'), |
|
456 | 462 | (';f x', 'f("x")'), |
|
457 | 463 | (' ;f y', ' f("y")'), |
|
458 | 464 | (';f a b', 'f("a b")'), |
|
459 | 465 | ], |
|
460 | 466 | |
|
461 | 467 | # Simply apply parens |
|
462 | 468 | escaped_paren = |
|
463 | 469 | [ ('/f', 'f()'), |
|
464 | 470 | ('/f x', 'f(x)'), |
|
465 | 471 | (' /f y', ' f(y)'), |
|
466 | 472 | ('/f a b', 'f(a, b)'), |
|
467 | 473 | ], |
|
468 | 474 | |
|
469 | 475 | ) |
|
470 | 476 | |
|
471 | 477 | # multiline syntax examples. Each of these should be a list of lists, with |
|
472 | 478 | # each entry itself having pairs of raw/transformed input. The union (with |
|
473 | 479 | # '\n'.join() of the transformed inputs is what the splitter should produce |
|
474 | 480 | # when fed the raw lines one at a time via push. |
|
475 | 481 | syntax_ml = \ |
|
476 | 482 | dict(classic_prompt = |
|
477 | 483 | [ [('>>> for i in range(10):','for i in range(10):'), |
|
478 | 484 | ('... print i',' print i'), |
|
479 | 485 | ('... ', ''), |
|
480 | 486 | ], |
|
481 | 487 | ], |
|
482 | 488 | |
|
483 | 489 | ipy_prompt = |
|
484 | 490 | [ [('In [24]: for i in range(10):','for i in range(10):'), |
|
485 | 491 | (' ....: print i',' print i'), |
|
486 | 492 | (' ....: ', ''), |
|
487 | 493 | ], |
|
488 | 494 | ], |
|
489 | 495 | ) |
|
490 | 496 | |
|
491 | 497 | |
|
492 | 498 | def test_assign_system(): |
|
493 | 499 | transform_checker(syntax['assign_system'], isp.transform_assign_system) |
|
494 | 500 | |
|
495 | 501 | |
|
496 | 502 | def test_assign_magic(): |
|
497 | 503 | transform_checker(syntax['assign_magic'], isp.transform_assign_magic) |
|
498 | 504 | |
|
499 | 505 | |
|
500 | 506 | def test_classic_prompt(): |
|
501 | 507 | transform_checker(syntax['classic_prompt'], isp.transform_classic_prompt) |
|
502 | 508 | for example in syntax_ml['classic_prompt']: |
|
503 | 509 | transform_checker(example, isp.transform_classic_prompt) |
|
504 | 510 | |
|
505 | 511 | |
|
506 | 512 | def test_ipy_prompt(): |
|
507 | 513 | transform_checker(syntax['ipy_prompt'], isp.transform_ipy_prompt) |
|
508 | 514 | for example in syntax_ml['ipy_prompt']: |
|
509 | 515 | transform_checker(example, isp.transform_ipy_prompt) |
|
510 | 516 | |
|
511 | 517 | |
|
512 | 518 | def test_escaped_noesc(): |
|
513 | 519 | transform_checker(syntax['escaped_noesc'], isp.transform_escaped) |
|
514 | 520 | |
|
515 | 521 | |
|
516 | 522 | def test_escaped_shell(): |
|
517 | 523 | transform_checker(syntax['escaped_shell'], isp.transform_escaped) |
|
518 | 524 | |
|
519 | 525 | |
|
520 | 526 | def test_escaped_help(): |
|
521 | 527 | transform_checker(syntax['escaped_help'], isp.transform_escaped) |
|
522 | 528 | |
|
523 | 529 | |
|
524 | 530 | def test_escaped_magic(): |
|
525 | 531 | transform_checker(syntax['escaped_magic'], isp.transform_escaped) |
|
526 | 532 | |
|
527 | 533 | |
|
528 | 534 | def test_escaped_quote(): |
|
529 | 535 | transform_checker(syntax['escaped_quote'], isp.transform_escaped) |
|
530 | 536 | |
|
531 | 537 | |
|
532 | 538 | def test_escaped_quote2(): |
|
533 | 539 | transform_checker(syntax['escaped_quote2'], isp.transform_escaped) |
|
534 | 540 | |
|
535 | 541 | |
|
536 | 542 | def test_escaped_paren(): |
|
537 | 543 | transform_checker(syntax['escaped_paren'], isp.transform_escaped) |
|
538 | 544 | |
|
539 | 545 | |
|
540 | 546 | class IPythonInputTestCase(InputSplitterTestCase): |
|
541 | 547 | """By just creating a new class whose .isp is a different instance, we |
|
542 | 548 | re-run the same test battery on the new input splitter. |
|
543 | 549 | |
|
544 | 550 | In addition, this runs the tests over the syntax and syntax_ml dicts that |
|
545 | 551 | were tested by individual functions, as part of the OO interface. |
|
546 | 552 | |
|
547 | 553 | It also makes some checks on the raw buffer storage. |
|
548 | 554 | """ |
|
549 | 555 | |
|
550 | 556 | def setUp(self): |
|
551 | 557 | self.isp = isp.IPythonInputSplitter(input_mode='line') |
|
552 | 558 | |
|
553 | 559 | def test_syntax(self): |
|
554 | 560 | """Call all single-line syntax tests from the main object""" |
|
555 | 561 | isp = self.isp |
|
556 | 562 | for example in syntax.itervalues(): |
|
557 | 563 | for raw, out_t in example: |
|
558 | 564 | if raw.startswith(' '): |
|
559 | 565 | continue |
|
560 | 566 | |
|
561 | 567 | isp.push(raw) |
|
562 | 568 | out, out_raw = isp.source_raw_reset() |
|
563 | 569 | self.assertEqual(out.rstrip(), out_t) |
|
564 | 570 | self.assertEqual(out_raw.rstrip(), raw.rstrip()) |
|
565 | 571 | |
|
566 | 572 | def test_syntax_multiline(self): |
|
567 | 573 | isp = self.isp |
|
568 | 574 | for example in syntax_ml.itervalues(): |
|
569 | 575 | out_t_parts = [] |
|
570 | 576 | raw_parts = [] |
|
571 | 577 | for line_pairs in example: |
|
572 | 578 | for lraw, out_t_part in line_pairs: |
|
573 | 579 | isp.push(lraw) |
|
574 | 580 | out_t_parts.append(out_t_part) |
|
575 | 581 | raw_parts.append(lraw) |
|
576 | 582 | |
|
577 | 583 | out, out_raw = isp.source_raw_reset() |
|
578 | 584 | out_t = '\n'.join(out_t_parts).rstrip() |
|
579 | 585 | raw = '\n'.join(raw_parts).rstrip() |
|
580 | 586 | self.assertEqual(out.rstrip(), out_t) |
|
581 | 587 | self.assertEqual(out_raw.rstrip(), raw) |
|
582 | 588 | |
|
583 | 589 | |
|
584 | 590 | class BlockIPythonInputTestCase(IPythonInputTestCase): |
|
585 | 591 | |
|
586 | 592 | # Deactivate tests that don't make sense for the block mode |
|
587 | 593 | test_push3 = test_split = lambda s: None |
|
588 | 594 | |
|
589 | 595 | def setUp(self): |
|
590 | 596 | self.isp = isp.IPythonInputSplitter(input_mode='cell') |
|
591 | 597 | |
|
592 | 598 | def test_syntax_multiline(self): |
|
593 | 599 | isp = self.isp |
|
594 | 600 | for example in syntax_ml.itervalues(): |
|
595 | 601 | raw_parts = [] |
|
596 | 602 | out_t_parts = [] |
|
597 | 603 | for line_pairs in example: |
|
598 | 604 | for raw, out_t_part in line_pairs: |
|
599 | 605 | raw_parts.append(raw) |
|
600 | 606 | out_t_parts.append(out_t_part) |
|
601 | 607 | |
|
602 | 608 | raw = '\n'.join(raw_parts) |
|
603 | 609 | out_t = '\n'.join(out_t_parts) |
|
604 | 610 | |
|
605 | 611 | isp.push(raw) |
|
606 | 612 | out, out_raw = isp.source_raw_reset() |
|
607 | 613 | # Match ignoring trailing whitespace |
|
608 | 614 | self.assertEqual(out.rstrip(), out_t.rstrip()) |
|
609 | 615 | self.assertEqual(out_raw.rstrip(), raw.rstrip()) |
|
610 | 616 | |
|
611 | 617 | |
|
612 | 618 | #----------------------------------------------------------------------------- |
|
613 | 619 | # Main - use as a script, mostly for developer experiments |
|
614 | 620 | #----------------------------------------------------------------------------- |
|
615 | 621 | |
|
616 | 622 | if __name__ == '__main__': |
|
617 | 623 | # A simple demo for interactive experimentation. This code will not get |
|
618 | 624 | # picked up by any test suite. |
|
619 | 625 | from IPython.core.inputsplitter import InputSplitter, IPythonInputSplitter |
|
620 | 626 | |
|
621 | 627 | # configure here the syntax to use, prompt and whether to autoindent |
|
622 | 628 | #isp, start_prompt = InputSplitter(), '>>> ' |
|
623 | 629 | isp, start_prompt = IPythonInputSplitter(), 'In> ' |
|
624 | 630 | |
|
625 | 631 | autoindent = True |
|
626 | 632 | #autoindent = False |
|
627 | 633 | |
|
628 | 634 | try: |
|
629 | 635 | while True: |
|
630 | 636 | prompt = start_prompt |
|
631 | 637 | while isp.push_accepts_more(): |
|
632 | 638 | indent = ' '*isp.indent_spaces |
|
633 | 639 | if autoindent: |
|
634 | 640 | line = indent + raw_input(prompt+indent) |
|
635 | 641 | else: |
|
636 | 642 | line = raw_input(prompt) |
|
637 | 643 | isp.push(line) |
|
638 | 644 | prompt = '... ' |
|
639 | 645 | |
|
640 | 646 | # Here we just return input so we can use it in a test suite, but a |
|
641 | 647 | # real interpreter would instead send it for execution somewhere. |
|
642 | 648 | #src = isp.source; raise EOFError # dbg |
|
643 | 649 | src, raw = isp.source_raw_reset() |
|
644 | 650 | print 'Input source was:\n', src |
|
645 | 651 | print 'Raw source was:\n', raw |
|
646 | 652 | except EOFError: |
|
647 | 653 | print 'Bye' |
General Comments 0
You need to be logged in to leave comments.
Login now