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