##// END OF EJS Templates
Move tests for magics that are terminal-specific to their own file.
Fernando Perez -
Show More
@@ -0,0 +1,164 b''
1 """Tests for various magic functions specific to the terminal frontend.
2
3 Needs to be run by nose (to make ipython session available).
4 """
5 from __future__ import absolute_import
6
7 #-----------------------------------------------------------------------------
8 # Imports
9 #-----------------------------------------------------------------------------
10
11 import sys
12 from StringIO import StringIO
13
14 import nose.tools as nt
15
16 from IPython.testing import decorators as dec
17 from IPython.testing import tools as tt
18
19 #-----------------------------------------------------------------------------
20 # Test functions begin
21 #-----------------------------------------------------------------------------
22
23 def check_cpaste(code, should_fail=False):
24 """Execute code via 'cpaste' and ensure it was executed, unless
25 should_fail is set.
26 """
27 _ip.user_ns['code_ran'] = False
28
29 src = StringIO()
30 if not hasattr(src, 'encoding'):
31 # IPython expects stdin to have an encoding attribute
32 src.encoding = None
33 src.write('\n')
34 src.write(code)
35 src.write('\n--\n')
36 src.seek(0)
37
38 stdin_save = sys.stdin
39 sys.stdin = src
40
41 try:
42 context = tt.AssertPrints if should_fail else tt.AssertNotPrints
43 with context("Traceback (most recent call last)"):
44 _ip.magic('cpaste')
45
46 if not should_fail:
47 assert _ip.user_ns['code_ran']
48 finally:
49 sys.stdin = stdin_save
50
51
52 def test_cpaste():
53 """Test cpaste magic"""
54
55 def run():
56 """Marker function: sets a flag when executed.
57 """
58 _ip.user_ns['code_ran'] = True
59 return 'run' # return string so '+ run()' doesn't result in success
60
61 tests = {'pass': ["run()",
62 "In [1]: run()",
63 "In [1]: if 1:\n ...: run()",
64 "> > > run()",
65 ">>> run()",
66 " >>> run()",
67 ],
68
69 'fail': ["1 + run()",
70 "++ run()"]}
71
72 _ip.user_ns['run'] = run
73
74 for code in tests['pass']:
75 check_cpaste(code)
76
77 for code in tests['fail']:
78 check_cpaste(code, should_fail=True)
79
80
81 # Multiple tests for clipboard pasting
82 def test_paste():
83 _ip = get_ipython()
84
85 def paste(txt, flags='-q'):
86 """Paste input text, by default in quiet mode"""
87 hooks.clipboard_get = lambda : txt
88 _ip.magic('paste '+flags)
89
90 # Inject fake clipboard hook but save original so we can restore it later
91 hooks = _ip.hooks
92 user_ns = _ip.user_ns
93 original_clip = hooks.clipboard_get
94
95 try:
96 # Run tests with fake clipboard function
97 user_ns.pop('x', None)
98 paste('x=1')
99 nt.assert_equal(user_ns['x'], 1)
100
101 user_ns.pop('x', None)
102 paste('>>> x=2')
103 nt.assert_equal(user_ns['x'], 2)
104
105 paste("""
106 >>> x = [1,2,3]
107 >>> y = []
108 >>> for i in x:
109 ... y.append(i**2)
110 ...
111 """)
112 nt.assert_equal(user_ns['x'], [1,2,3])
113 nt.assert_equal(user_ns['y'], [1,4,9])
114
115 # Now, test that paste -r works
116 user_ns.pop('x', None)
117 nt.assert_false('x' in user_ns)
118 _ip.magic('paste -r')
119 nt.assert_equal(user_ns['x'], [1,2,3])
120
121 # Test pasting of email-quoted contents
122 paste("""
123 >> def foo(x):
124 >> return x + 1
125 >> x = foo(1.1)
126 """)
127 nt.assert_equal(user_ns['x'], 2.1)
128
129 # Email again; some programs add a space also at each quoting level
130 paste("""
131 > > def foo(x):
132 > > return x + 1
133 > > x = foo(2.1)
134 """)
135 nt.assert_equal(user_ns['x'], 3.1)
136
137 # Email quoting of interactive input
138 paste("""
139 >> >>> def f(x):
140 >> ... return x+1
141 >> ...
142 >> >>> x = f(2.5)
143 """)
144 nt.assert_equal(user_ns['x'], 3.5)
145
146 # Also test paste echoing, by temporarily faking the writer
147 w = StringIO()
148 writer = _ip.write
149 _ip.write = w.write
150 code = """
151 a = 100
152 b = 200"""
153 try:
154 paste(code,'')
155 out = w.getvalue()
156 finally:
157 _ip.write = writer
158 nt.assert_equal(user_ns['a'], 100)
159 nt.assert_equal(user_ns['b'], 200)
160 nt.assert_equal(out, code+"\n## -- End pasted text --\n")
161
162 finally:
163 # Restore original hook
164 hooks.clipboard_get = original_clip
@@ -9,14 +9,9 b' from __future__ import absolute_import'
9 #-----------------------------------------------------------------------------
9 #-----------------------------------------------------------------------------
10
10
11 import os
11 import os
12 import sys
13 import tempfile
14 import types
15 from StringIO import StringIO
16
12
17 import nose.tools as nt
13 import nose.tools as nt
18
14
19 from IPython.utils.path import get_long_path_name
20 from IPython.testing import decorators as dec
15 from IPython.testing import decorators as dec
21 from IPython.testing import tools as tt
16 from IPython.testing import tools as tt
22 from IPython.utils import py3compat
17 from IPython.utils import py3compat
@@ -24,6 +19,7 b' from IPython.utils import py3compat'
24 #-----------------------------------------------------------------------------
19 #-----------------------------------------------------------------------------
25 # Test functions begin
20 # Test functions begin
26 #-----------------------------------------------------------------------------
21 #-----------------------------------------------------------------------------
22
27 def test_rehashx():
23 def test_rehashx():
28 # clear up everything
24 # clear up everything
29 _ip = get_ipython()
25 _ip = get_ipython()
@@ -202,67 +198,6 b' def test_numpy_clear_array_undec():'
202 yield (nt.assert_false, 'a' in _ip.user_ns)
198 yield (nt.assert_false, 'a' in _ip.user_ns)
203
199
204
200
205 # Multiple tests for clipboard pasting
206 @dec.parametric
207 def test_paste():
208 _ip = get_ipython()
209 def paste(txt, flags='-q'):
210 """Paste input text, by default in quiet mode"""
211 hooks.clipboard_get = lambda : txt
212 _ip.magic('paste '+flags)
213
214 # Inject fake clipboard hook but save original so we can restore it later
215 hooks = _ip.hooks
216 user_ns = _ip.user_ns
217 original_clip = hooks.clipboard_get
218
219 try:
220 # Run tests with fake clipboard function
221 user_ns.pop('x', None)
222 paste('x=1')
223 yield nt.assert_equal(user_ns['x'], 1)
224
225 user_ns.pop('x', None)
226 paste('>>> x=2')
227 yield nt.assert_equal(user_ns['x'], 2)
228
229 paste("""
230 >>> x = [1,2,3]
231 >>> y = []
232 >>> for i in x:
233 ... y.append(i**2)
234 ...
235 """)
236 yield nt.assert_equal(user_ns['x'], [1,2,3])
237 yield nt.assert_equal(user_ns['y'], [1,4,9])
238
239 # Now, test that paste -r works
240 user_ns.pop('x', None)
241 yield nt.assert_false('x' in user_ns)
242 _ip.magic('paste -r')
243 yield nt.assert_equal(user_ns['x'], [1,2,3])
244
245 # Also test paste echoing, by temporarily faking the writer
246 w = StringIO()
247 writer = _ip.write
248 _ip.write = w.write
249 code = """
250 a = 100
251 b = 200"""
252 try:
253 paste(code,'')
254 out = w.getvalue()
255 finally:
256 _ip.write = writer
257 yield nt.assert_equal(user_ns['a'], 100)
258 yield nt.assert_equal(user_ns['b'], 200)
259 yield nt.assert_equal(out, code+"\n## -- End pasted text --\n")
260
261 finally:
262 # Restore original hook
263 hooks.clipboard_get = original_clip
264
265
266 def test_time():
201 def test_time():
267 _ip.magic('time None')
202 _ip.magic('time None')
268
203
@@ -317,62 +252,6 b' def test_dirops():'
317 os.chdir(startdir)
252 os.chdir(startdir)
318
253
319
254
320 def check_cpaste(code, should_fail=False):
321 """Execute code via 'cpaste' and ensure it was executed, unless
322 should_fail is set.
323 """
324 _ip.user_ns['code_ran'] = False
325
326 src = StringIO()
327 if not hasattr(src, 'encoding'):
328 # IPython expects stdin to have an encoding attribute
329 src.encoding = None
330 src.write('\n')
331 src.write(code)
332 src.write('\n--\n')
333 src.seek(0)
334
335 stdin_save = sys.stdin
336 sys.stdin = src
337
338 try:
339 context = tt.AssertPrints if should_fail else tt.AssertNotPrints
340 with context("Traceback (most recent call last)"):
341 _ip.magic('cpaste')
342
343 if not should_fail:
344 assert _ip.user_ns['code_ran']
345 finally:
346 sys.stdin = stdin_save
347
348
349 def test_cpaste():
350 """Test cpaste magic"""
351
352 def run():
353 """Marker function: sets a flag when executed.
354 """
355 _ip.user_ns['code_ran'] = True
356 return 'run' # return string so '+ run()' doesn't result in success
357
358 tests = {'pass': ["run()",
359 "In [1]: run()",
360 "In [1]: if 1:\n ...: run()",
361 ">>> run()",
362 " >>> run()",
363 ],
364
365 'fail': ["1 + run()",
366 "++ run()"]}
367
368 _ip.user_ns['run'] = run
369
370 for code in tests['pass']:
371 check_cpaste(code)
372
373 for code in tests['fail']:
374 check_cpaste(code, should_fail=True)
375
376 def test_xmode():
255 def test_xmode():
377 # Calling xmode three times should be a no-op
256 # Calling xmode three times should be a no-op
378 xmode = _ip.InteractiveTB.mode
257 xmode = _ip.InteractiveTB.mode
General Comments 0
You need to be logged in to leave comments. Login now