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