##// END OF EJS Templates
Refactored paste tests into proper class with setup/teardown.
Fernando Perez -
Show More
@@ -10,12 +10,18 b' from __future__ import absolute_import'
10
10
11 import sys
11 import sys
12 from StringIO import StringIO
12 from StringIO import StringIO
13 from unittest import TestCase
13
14
14 import nose.tools as nt
15 import nose.tools as nt
15
16
16 from IPython.testing import tools as tt
17 from IPython.testing import tools as tt
17
18
18 #-----------------------------------------------------------------------------
19 #-----------------------------------------------------------------------------
20 # Globals
21 #-----------------------------------------------------------------------------
22 ip = get_ipython()
23
24 #-----------------------------------------------------------------------------
19 # Test functions begin
25 # Test functions begin
20 #-----------------------------------------------------------------------------
26 #-----------------------------------------------------------------------------
21
27
@@ -84,87 +90,92 b' def test_cpaste():'
84 check_cpaste(code, should_fail=True)
90 check_cpaste(code, should_fail=True)
85
91
86
92
87 # Multiple tests for clipboard pasting
93 class PasteTestCase(TestCase):
88 def test_paste():
94 """Multiple tests for clipboard pasting"""
89 _ip = get_ipython()
90
95
91 def paste(txt, flags='-q'):
96 def paste(self, txt, flags='-q'):
92 """Paste input text, by default in quiet mode"""
97 """Paste input text, by default in quiet mode"""
93 hooks.clipboard_get = lambda : txt
98 ip.hooks.clipboard_get = lambda : txt
94 _ip.magic('paste '+flags)
99 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)
106
100
107 user_ns.pop('x', None)
101 def setUp(self):
108 paste('>>> x=2')
102 # Inject fake clipboard hook but save original so we can restore it later
109 nt.assert_equal(user_ns['x'], 2)
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 >>> x = [1,2,3]
121 >>> x = [1,2,3]
113 >>> y = []
122 >>> y = []
114 >>> for i in x:
123 >>> for i in x:
115 ... y.append(i**2)
124 ... y.append(i**2)
116 ...
125 ...
117 """)
126 """)
118 nt.assert_equal(user_ns['x'], [1,2,3])
127 nt.assert_equal(ip.user_ns['x'], [1,2,3])
119 nt.assert_equal(user_ns['y'], [1,4,9])
128 nt.assert_equal(ip.user_ns['y'], [1,4,9])
120
129
121 # Now, test that paste -r works
130 def test_paste_py_multi_r(self):
122 user_ns.pop('x', None)
131 "Now, test that self.paste -r works"
123 nt.assert_false('x' in user_ns)
132 ip.user_ns.pop('x', None)
124 _ip.magic('paste -r')
133 nt.assert_false('x' in ip.user_ns)
125 nt.assert_equal(user_ns['x'], [1,2,3])
134 ip.magic('paste -r')
126
135 nt.assert_equal(ip.user_ns['x'], [1,2,3])
127 # Test pasting of email-quoted contents
136
128 paste("""
137 def test_paste_email(self):
138 "Test pasting of email-quoted contents"
139 self.paste("""
129 >> def foo(x):
140 >> def foo(x):
130 >> return x + 1
141 >> return x + 1
131 >> x = foo(1.1)
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
146 def test_paste_email2(self):
136 paste("""
147 "Email again; some programs add a space also at each quoting level"
148 self.paste("""
137 > > def foo(x):
149 > > def foo(x):
138 > > return x + 1
150 > > return x + 1
139 > > x = foo(2.1)
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
155 def test_paste_email_py(self):
144 paste("""
156 "Email quoting of interactive input"
157 self.paste("""
145 >> >>> def f(x):
158 >> >>> def f(x):
146 >> ... return x+1
159 >> ... return x+1
147 >> ...
160 >> ...
148 >> >>> x = f(2.5)
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 w = StringIO()
167 w = StringIO()
154 writer = _ip.write
168 writer = ip.write
155 _ip.write = w.write
169 ip.write = w.write
156 code = """
170 code = """
157 a = 100
171 a = 100
158 b = 200"""
172 b = 200"""
159 try:
173 try:
160 paste(code,'')
174 self.paste(code,'')
161 out = w.getvalue()
175 out = w.getvalue()
162 finally:
176 finally:
163 _ip.write = writer
177 ip.write = writer
164 nt.assert_equal(user_ns['a'], 100)
178 nt.assert_equal(ip.user_ns['a'], 100)
165 nt.assert_equal(user_ns['b'], 200)
179 nt.assert_equal(ip.user_ns['b'], 200)
166 nt.assert_equal(out, code+"\n## -- End pasted text --\n")
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