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