Show More
@@ -1,170 +1,181 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 |
|
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 | |||
22 | def check_cpaste(code, should_fail=False): |
|
28 | def check_cpaste(code, should_fail=False): | |
23 | """Execute code via 'cpaste' and ensure it was executed, unless |
|
29 | """Execute code via 'cpaste' and ensure it was executed, unless | |
24 | should_fail is set. |
|
30 | should_fail is set. | |
25 | """ |
|
31 | """ | |
26 | _ip.user_ns['code_ran'] = False |
|
32 | _ip.user_ns['code_ran'] = False | |
27 |
|
33 | |||
28 | src = StringIO() |
|
34 | src = StringIO() | |
29 | if not hasattr(src, 'encoding'): |
|
35 | if not hasattr(src, 'encoding'): | |
30 | # IPython expects stdin to have an encoding attribute |
|
36 | # IPython expects stdin to have an encoding attribute | |
31 | src.encoding = None |
|
37 | src.encoding = None | |
32 | src.write('\n') |
|
38 | src.write('\n') | |
33 | src.write(code) |
|
39 | src.write(code) | |
34 | src.write('\n--\n') |
|
40 | src.write('\n--\n') | |
35 | src.seek(0) |
|
41 | src.seek(0) | |
36 |
|
42 | |||
37 | stdin_save = sys.stdin |
|
43 | stdin_save = sys.stdin | |
38 | sys.stdin = src |
|
44 | sys.stdin = src | |
39 |
|
45 | |||
40 | try: |
|
46 | try: | |
41 | context = tt.AssertPrints if should_fail else tt.AssertNotPrints |
|
47 | context = tt.AssertPrints if should_fail else tt.AssertNotPrints | |
42 | with context("Traceback (most recent call last)"): |
|
48 | with context("Traceback (most recent call last)"): | |
43 | _ip.magic('cpaste') |
|
49 | _ip.magic('cpaste') | |
44 |
|
50 | |||
45 | if not should_fail: |
|
51 | if not should_fail: | |
46 | assert _ip.user_ns['code_ran'] |
|
52 | assert _ip.user_ns['code_ran'] | |
47 | finally: |
|
53 | finally: | |
48 | sys.stdin = stdin_save |
|
54 | sys.stdin = stdin_save | |
49 |
|
55 | |||
50 | PY31 = sys.version_info[:2] == (3,1) |
|
56 | PY31 = sys.version_info[:2] == (3,1) | |
51 |
|
57 | |||
52 | def test_cpaste(): |
|
58 | def test_cpaste(): | |
53 | """Test cpaste magic""" |
|
59 | """Test cpaste magic""" | |
54 |
|
60 | |||
55 | def run(): |
|
61 | def run(): | |
56 | """Marker function: sets a flag when executed. |
|
62 | """Marker function: sets a flag when executed. | |
57 | """ |
|
63 | """ | |
58 | _ip.user_ns['code_ran'] = True |
|
64 | _ip.user_ns['code_ran'] = True | |
59 | return 'run' # return string so '+ run()' doesn't result in success |
|
65 | return 'run' # return string so '+ run()' doesn't result in success | |
60 |
|
66 | |||
61 | tests = {'pass': ["run()", |
|
67 | tests = {'pass': ["run()", | |
62 | "In [1]: run()", |
|
68 | "In [1]: run()", | |
63 | "In [1]: if 1:\n ...: run()", |
|
69 | "In [1]: if 1:\n ...: run()", | |
64 | "> > > run()", |
|
70 | "> > > run()", | |
65 | ">>> run()", |
|
71 | ">>> run()", | |
66 | " >>> run()", |
|
72 | " >>> run()", | |
67 | ], |
|
73 | ], | |
68 |
|
74 | |||
69 | 'fail': ["1 + run()", |
|
75 | 'fail': ["1 + run()", | |
70 | ]} |
|
76 | ]} | |
71 |
|
77 | |||
72 | # 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 | |
73 | # 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 | |
74 | # 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. | |
75 | if not PY31: |
|
81 | if not PY31: | |
76 | tests['fail'].append("++ run()") |
|
82 | tests['fail'].append("++ run()") | |
77 |
|
83 | |||
78 | _ip.user_ns['run'] = run |
|
84 | _ip.user_ns['run'] = run | |
79 |
|
85 | |||
80 | for code in tests['pass']: |
|
86 | for code in tests['pass']: | |
81 | check_cpaste(code) |
|
87 | check_cpaste(code) | |
82 |
|
88 | |||
83 | for code in tests['fail']: |
|
89 | for code in tests['fail']: | |
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 |
|
|
99 | ip.magic('paste '+flags) | |
95 |
|
100 | |||
|
101 | def setUp(self): | |||
96 | # 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 | |
97 | hooks = _ip.hooks |
|
103 | self.original_clip = ip.hooks.clipboard_get | |
98 | user_ns = _ip.user_ns |
|
|||
99 | original_clip = hooks.clipboard_get |
|
|||
100 |
|
104 | |||
101 | try: |
|
105 | def tearDown(self): | |
102 | # Run tests with fake clipboard function |
|
106 | # Restore original hook | |
103 | user_ns.pop('x', None) |
|
107 | ip.hooks.clipboard_get = self.original_clip | |
104 | paste('x=1') |
|
|||
105 | nt.assert_equal(user_ns['x'], 1) |
|
|||
106 |
|
108 | |||
107 | user_ns.pop('x', None) |
|
109 | def test_paste(self): | |
108 | paste('>>> x=2') |
|
110 | ip.user_ns.pop('x', None) | |
109 | nt.assert_equal(user_ns['x'], 2) |
|
111 | self.paste('x=1') | |
|
112 | nt.assert_equal(ip.user_ns['x'], 1) | |||
110 |
|
113 | |||
111 | paste(""" |
|
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 = |
|
168 | writer = ip.write | |
155 |
|
|
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 |
|
|
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