Show More
@@ -10,12 +10,18 b' from __future__ import absolute_import' | |||
|
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 | |
@@ -84,87 +90,92 b' def test_cpaste():' | |||
|
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 |
|
|
|
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 = |
|
|
155 |
|
|
|
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 |
|
|
|
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