Show More
@@ -1,191 +1,205 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 | _ip.user_ns['code_ran'] = False |
|
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('\n') |
|
38 | src.write('\n') | |
39 | src.write(code) |
|
39 | src.write(code) | |
40 | src.write('\n--\n') |
|
40 | src.write('\n--\n') | |
41 | src.seek(0) |
|
41 | src.seek(0) | |
42 |
|
42 | |||
43 | stdin_save = sys.stdin |
|
43 | stdin_save = sys.stdin | |
44 | sys.stdin = src |
|
44 | sys.stdin = src | |
45 |
|
45 | |||
46 | try: |
|
46 | try: | |
47 | context = tt.AssertPrints if should_fail else tt.AssertNotPrints |
|
47 | context = tt.AssertPrints if should_fail else tt.AssertNotPrints | |
48 | with context("Traceback (most recent call last)"): |
|
48 | with context("Traceback (most recent call last)"): | |
49 | _ip.magic('cpaste') |
|
49 | _ip.magic('cpaste') | |
50 |
|
50 | |||
51 | if not should_fail: |
|
51 | if not should_fail: | |
52 | assert _ip.user_ns['code_ran'] |
|
52 | assert _ip.user_ns['code_ran'] | |
53 | finally: |
|
53 | finally: | |
54 | sys.stdin = stdin_save |
|
54 | sys.stdin = stdin_save | |
55 |
|
55 | |||
56 | PY31 = sys.version_info[:2] == (3,1) |
|
56 | PY31 = sys.version_info[:2] == (3,1) | |
57 |
|
57 | |||
58 | def test_cpaste(): |
|
58 | def test_cpaste(): | |
59 | """Test cpaste magic""" |
|
59 | """Test cpaste magic""" | |
60 |
|
60 | |||
61 | def run(): |
|
61 | def run(): | |
62 | """Marker function: sets a flag when executed. |
|
62 | """Marker function: sets a flag when executed. | |
63 | """ |
|
63 | """ | |
64 | _ip.user_ns['code_ran'] = True |
|
64 | _ip.user_ns['code_ran'] = True | |
65 | return 'run' # return string so '+ run()' doesn't result in success |
|
65 | return 'run' # return string so '+ run()' doesn't result in success | |
66 |
|
66 | |||
67 | tests = {'pass': ["run()", |
|
67 | tests = {'pass': ["run()", | |
68 | "In [1]: run()", |
|
68 | "In [1]: run()", | |
69 | "In [1]: if 1:\n ...: run()", |
|
69 | "In [1]: if 1:\n ...: run()", | |
70 | "> > > run()", |
|
70 | "> > > run()", | |
71 | ">>> run()", |
|
71 | ">>> run()", | |
72 | " >>> run()", |
|
72 | " >>> run()", | |
73 | ], |
|
73 | ], | |
74 |
|
74 | |||
75 | 'fail': ["1 + run()", |
|
75 | 'fail': ["1 + run()", | |
76 | ]} |
|
76 | ]} | |
77 |
|
77 | |||
78 | # I don't know why this is failing specifically on Python 3.1. I've |
|
78 | # I don't know why this is failing specifically on Python 3.1. I've | |
79 | # checked it manually interactively, but we don't care enough about 3.1 |
|
79 | # checked it manually interactively, but we don't care enough about 3.1 | |
80 | # to spend time fiddling with the tests, so we just skip it. |
|
80 | # to spend time fiddling with the tests, so we just skip it. | |
81 | if not PY31: |
|
81 | if not PY31: | |
82 | tests['fail'].append("++ run()") |
|
82 | tests['fail'].append("++ run()") | |
83 |
|
83 | |||
84 | _ip.user_ns['run'] = run |
|
84 | _ip.user_ns['run'] = run | |
85 |
|
85 | |||
86 | for code in tests['pass']: |
|
86 | for code in tests['pass']: | |
87 | check_cpaste(code) |
|
87 | check_cpaste(code) | |
88 |
|
88 | |||
89 | for code in tests['fail']: |
|
89 | for code in tests['fail']: | |
90 | check_cpaste(code, should_fail=True) |
|
90 | check_cpaste(code, should_fail=True) | |
91 |
|
91 | |||
92 |
|
92 | |||
93 | class PasteTestCase(TestCase): |
|
93 | class PasteTestCase(TestCase): | |
94 | """Multiple tests for clipboard pasting""" |
|
94 | """Multiple tests for clipboard pasting""" | |
95 |
|
95 | |||
96 | def paste(self, txt, flags='-q'): |
|
96 | def paste(self, txt, flags='-q'): | |
97 | """Paste input text, by default in quiet mode""" |
|
97 | """Paste input text, by default in quiet mode""" | |
98 | ip.hooks.clipboard_get = lambda : txt |
|
98 | ip.hooks.clipboard_get = lambda : txt | |
99 | ip.magic('paste '+flags) |
|
99 | ip.magic('paste '+flags) | |
100 |
|
100 | |||
101 | def setUp(self): |
|
101 | def setUp(self): | |
102 | # Inject fake clipboard hook but save original so we can restore it later |
|
102 | # Inject fake clipboard hook but save original so we can restore it later | |
103 | self.original_clip = ip.hooks.clipboard_get |
|
103 | self.original_clip = ip.hooks.clipboard_get | |
104 |
|
104 | |||
105 | def tearDown(self): |
|
105 | def tearDown(self): | |
106 | # Restore original hook |
|
106 | # Restore original hook | |
107 | ip.hooks.clipboard_get = self.original_clip |
|
107 | ip.hooks.clipboard_get = self.original_clip | |
108 |
|
108 | |||
109 | def test_paste(self): |
|
109 | def test_paste(self): | |
110 | ip.user_ns.pop('x', None) |
|
110 | ip.user_ns.pop('x', None) | |
111 | self.paste('x=1') |
|
111 | self.paste('x=1') | |
112 | nt.assert_equal(ip.user_ns['x'], 1) |
|
112 | nt.assert_equal(ip.user_ns['x'], 1) | |
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 |
|
118 | |||
119 | def test_paste_py_multi(self): |
|
119 | def test_paste_py_multi(self): | |
120 | self.paste(""" |
|
120 | self.paste(""" | |
121 | >>> x = [1,2,3] |
|
121 | >>> x = [1,2,3] | |
122 | >>> y = [] |
|
122 | >>> y = [] | |
123 | >>> for i in x: |
|
123 | >>> for i in x: | |
124 | ... y.append(i**2) |
|
124 | ... y.append(i**2) | |
125 | ... |
|
125 | ... | |
126 | """) |
|
126 | """) | |
127 | nt.assert_equal(ip.user_ns['x'], [1,2,3]) |
|
127 | nt.assert_equal(ip.user_ns['x'], [1,2,3]) | |
128 | nt.assert_equal(ip.user_ns['y'], [1,4,9]) |
|
128 | nt.assert_equal(ip.user_ns['y'], [1,4,9]) | |
129 |
|
129 | |||
130 | def test_paste_py_multi_r(self): |
|
130 | def test_paste_py_multi_r(self): | |
131 | "Now, test that self.paste -r works" |
|
131 | "Now, test that self.paste -r works" | |
132 | ip.user_ns.pop('x', None) |
|
132 | ip.user_ns.pop('x', None) | |
133 | nt.assert_false('x' in ip.user_ns) |
|
133 | nt.assert_false('x' in ip.user_ns) | |
134 | ip.magic('paste -r') |
|
134 | ip.magic('paste -r') | |
135 | nt.assert_equal(ip.user_ns['x'], [1,2,3]) |
|
135 | nt.assert_equal(ip.user_ns['x'], [1,2,3]) | |
136 |
|
136 | |||
137 | def test_paste_email(self): |
|
137 | def test_paste_email(self): | |
138 | "Test pasting of email-quoted contents" |
|
138 | "Test pasting of email-quoted contents" | |
139 | self.paste(""" |
|
139 | self.paste(""" | |
140 | >> def foo(x): |
|
140 | >> def foo(x): | |
141 | >> return x + 1 |
|
141 | >> return x + 1 | |
142 | >> x = foo(1.1) |
|
142 | >> x = foo(1.1) | |
143 | """) |
|
143 | """) | |
144 | nt.assert_equal(ip.user_ns['x'], 2.1) |
|
144 | nt.assert_equal(ip.user_ns['x'], 2.1) | |
145 |
|
145 | |||
146 | def test_paste_email2(self): |
|
146 | def test_paste_email2(self): | |
147 | "Email again; some programs add a space also at each quoting level" |
|
147 | "Email again; some programs add a space also at each quoting level" | |
148 | self.paste(""" |
|
148 | self.paste(""" | |
149 | > > def foo(x): |
|
149 | > > def foo(x): | |
150 | > > return x + 1 |
|
150 | > > return x + 1 | |
151 | > > x = foo(2.1) |
|
151 | > > x = foo(2.1) | |
152 | """) |
|
152 | """) | |
153 | nt.assert_equal(ip.user_ns['x'], 3.1) |
|
153 | nt.assert_equal(ip.user_ns['x'], 3.1) | |
154 |
|
154 | |||
155 | def test_paste_email_py(self): |
|
155 | def test_paste_email_py(self): | |
156 | "Email quoting of interactive input" |
|
156 | "Email quoting of interactive input" | |
157 | self.paste(""" |
|
157 | self.paste(""" | |
158 | >> >>> def f(x): |
|
158 | >> >>> def f(x): | |
159 | >> ... return x+1 |
|
159 | >> ... return x+1 | |
160 | >> ... |
|
160 | >> ... | |
161 | >> >>> x = f(2.5) |
|
161 | >> >>> x = f(2.5) | |
162 | """) |
|
162 | """) | |
163 | nt.assert_equal(ip.user_ns['x'], 3.5) |
|
163 | nt.assert_equal(ip.user_ns['x'], 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 | ||||
|
193 | ||||
|
194 | def test_paste_trailing_question(self): | |||
|
195 | "Test pasting sources with trailing question marks" | |||
|
196 | tm = ip.magics_manager.registry['TerminalMagics'] | |||
|
197 | s = '''\ | |||
|
198 | def funcfoo(): | |||
|
199 | if True: #am i true? | |||
|
200 | return 'fooresult' | |||
|
201 | ''' | |||
|
202 | ip.user_ns.pop('funcfoo', None) | |||
|
203 | tm.store_or_execute(s, 'foosrc') | |||
|
204 | nt.assert_in('foosrc', ip.user_ns) | |||
|
205 | nt.assert_equals(ip.user_ns['funcfoo'](), 'fooresult') |
General Comments 0
You need to be logged in to leave comments.
Login now