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