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