##// END OF EJS Templates
Refactored paste tests into proper class with setup/teardown.
Fernando Perez -
Show More
@@ -1,170 +1,181 b''
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 from unittest import TestCase
13 14
14 15 import nose.tools as nt
15 16
16 17 from IPython.testing import tools as tt
17 18
18 19 #-----------------------------------------------------------------------------
20 # Globals
21 #-----------------------------------------------------------------------------
22 ip = get_ipython()
23
24 #-----------------------------------------------------------------------------
19 25 # Test functions begin
20 26 #-----------------------------------------------------------------------------
21 27
22 28 def check_cpaste(code, should_fail=False):
23 29 """Execute code via 'cpaste' and ensure it was executed, unless
24 30 should_fail is set.
25 31 """
26 32 _ip.user_ns['code_ran'] = False
27 33
28 34 src = StringIO()
29 35 if not hasattr(src, 'encoding'):
30 36 # IPython expects stdin to have an encoding attribute
31 37 src.encoding = None
32 38 src.write('\n')
33 39 src.write(code)
34 40 src.write('\n--\n')
35 41 src.seek(0)
36 42
37 43 stdin_save = sys.stdin
38 44 sys.stdin = src
39 45
40 46 try:
41 47 context = tt.AssertPrints if should_fail else tt.AssertNotPrints
42 48 with context("Traceback (most recent call last)"):
43 49 _ip.magic('cpaste')
44 50
45 51 if not should_fail:
46 52 assert _ip.user_ns['code_ran']
47 53 finally:
48 54 sys.stdin = stdin_save
49 55
50 56 PY31 = sys.version_info[:2] == (3,1)
51 57
52 58 def test_cpaste():
53 59 """Test cpaste magic"""
54 60
55 61 def run():
56 62 """Marker function: sets a flag when executed.
57 63 """
58 64 _ip.user_ns['code_ran'] = True
59 65 return 'run' # return string so '+ run()' doesn't result in success
60 66
61 67 tests = {'pass': ["run()",
62 68 "In [1]: run()",
63 69 "In [1]: if 1:\n ...: run()",
64 70 "> > > run()",
65 71 ">>> run()",
66 72 " >>> run()",
67 73 ],
68 74
69 75 'fail': ["1 + run()",
70 76 ]}
71 77
72 78 # I don't know why this is failing specifically on Python 3.1. I've
73 79 # checked it manually interactively, but we don't care enough about 3.1
74 80 # to spend time fiddling with the tests, so we just skip it.
75 81 if not PY31:
76 82 tests['fail'].append("++ run()")
77 83
78 84 _ip.user_ns['run'] = run
79 85
80 86 for code in tests['pass']:
81 87 check_cpaste(code)
82 88
83 89 for code in tests['fail']:
84 90 check_cpaste(code, should_fail=True)
85 91
86 92
87 # Multiple tests for clipboard pasting
88 def test_paste():
89 _ip = get_ipython()
93 class PasteTestCase(TestCase):
94 """Multiple tests for clipboard pasting"""
90 95
91 def paste(txt, flags='-q'):
96 def paste(self, txt, flags='-q'):
92 97 """Paste input text, by default in quiet mode"""
93 hooks.clipboard_get = lambda : txt
94 _ip.magic('paste '+flags)
95
96 # Inject fake clipboard hook but save original so we can restore it later
97 hooks = _ip.hooks
98 user_ns = _ip.user_ns
99 original_clip = hooks.clipboard_get
100
101 try:
102 # Run tests with fake clipboard function
103 user_ns.pop('x', None)
104 paste('x=1')
105 nt.assert_equal(user_ns['x'], 1)
98 ip.hooks.clipboard_get = lambda : txt
99 ip.magic('paste '+flags)
106 100
107 user_ns.pop('x', None)
108 paste('>>> x=2')
109 nt.assert_equal(user_ns['x'], 2)
101 def setUp(self):
102 # Inject fake clipboard hook but save original so we can restore it later
103 self.original_clip = ip.hooks.clipboard_get
110 104
111 paste("""
105 def tearDown(self):
106 # Restore original hook
107 ip.hooks.clipboard_get = self.original_clip
108
109 def test_paste(self):
110 ip.user_ns.pop('x', None)
111 self.paste('x=1')
112 nt.assert_equal(ip.user_ns['x'], 1)
113
114 def test_paste_pyprompt(self):
115 ip.user_ns.pop('x', None)
116 self.paste('>>> x=2')
117 nt.assert_equal(ip.user_ns['x'], 2)
118
119 def test_paste_py_multi(self):
120 self.paste("""
112 121 >>> x = [1,2,3]
113 122 >>> y = []
114 123 >>> for i in x:
115 124 ... y.append(i**2)
116 125 ...
117 126 """)
118 nt.assert_equal(user_ns['x'], [1,2,3])
119 nt.assert_equal(user_ns['y'], [1,4,9])
120
121 # Now, test that paste -r works
122 user_ns.pop('x', None)
123 nt.assert_false('x' in user_ns)
124 _ip.magic('paste -r')
125 nt.assert_equal(user_ns['x'], [1,2,3])
126
127 # Test pasting of email-quoted contents
128 paste("""
127 nt.assert_equal(ip.user_ns['x'], [1,2,3])
128 nt.assert_equal(ip.user_ns['y'], [1,4,9])
129
130 def test_paste_py_multi_r(self):
131 "Now, test that self.paste -r works"
132 ip.user_ns.pop('x', None)
133 nt.assert_false('x' in ip.user_ns)
134 ip.magic('paste -r')
135 nt.assert_equal(ip.user_ns['x'], [1,2,3])
136
137 def test_paste_email(self):
138 "Test pasting of email-quoted contents"
139 self.paste("""
129 140 >> def foo(x):
130 141 >> return x + 1
131 142 >> x = foo(1.1)
132 143 """)
133 nt.assert_equal(user_ns['x'], 2.1)
144 nt.assert_equal(ip.user_ns['x'], 2.1)
134 145
135 # Email again; some programs add a space also at each quoting level
136 paste("""
146 def test_paste_email2(self):
147 "Email again; some programs add a space also at each quoting level"
148 self.paste("""
137 149 > > def foo(x):
138 150 > > return x + 1
139 151 > > x = foo(2.1)
140 152 """)
141 nt.assert_equal(user_ns['x'], 3.1)
153 nt.assert_equal(ip.user_ns['x'], 3.1)
142 154
143 # Email quoting of interactive input
144 paste("""
155 def test_paste_email_py(self):
156 "Email quoting of interactive input"
157 self.paste("""
145 158 >> >>> def f(x):
146 159 >> ... return x+1
147 160 >> ...
148 161 >> >>> x = f(2.5)
149 162 """)
150 nt.assert_equal(user_ns['x'], 3.5)
163 nt.assert_equal(ip.user_ns['x'], 3.5)
151 164
152 # Also test paste echoing, by temporarily faking the writer
165 def test_paste_echo(self):
166 "Also test self.paste echoing, by temporarily faking the writer"
153 167 w = StringIO()
154 writer = _ip.write
155 _ip.write = w.write
168 writer = ip.write
169 ip.write = w.write
156 170 code = """
157 171 a = 100
158 172 b = 200"""
159 173 try:
160 paste(code,'')
174 self.paste(code,'')
161 175 out = w.getvalue()
162 176 finally:
163 _ip.write = writer
164 nt.assert_equal(user_ns['a'], 100)
165 nt.assert_equal(user_ns['b'], 200)
177 ip.write = writer
178 nt.assert_equal(ip.user_ns['a'], 100)
179 nt.assert_equal(ip.user_ns['b'], 200)
166 180 nt.assert_equal(out, code+"\n## -- End pasted text --\n")
167 181
168 finally:
169 # Restore original hook
170 hooks.clipboard_get = original_clip
General Comments 0
You need to be logged in to leave comments. Login now