22# The .NET Foundation licenses this file to you under the Apache 2.0 License.
33# See the LICENSE file in the project root for more information.
44
5- from iptest .assert_util import *
5+ import os
6+
7+ from iptest import IronPythonTestCase , is_cli , run_test
8+
9+ tc = IronPythonTestCase ('__init__' )
10+ tc .setUp ()
611
712##
813## to test how exec related to globals/locals
914##
1015
1116def _contains (large , small ):
12- for (key , value ) in list ( small .items () ):
13- Assert (large [key ] == value )
17+ for (key , value ) in small .items ():
18+ tc . assertEqual (large [key ], value )
1419
1520def _not_contains (dict , * keylist ):
1621 for key in keylist :
17- Assert (key not in dict )
22+ tc . assertTrue (key not in dict )
1823
1924## exec without in something
2025x = 1
@@ -54,7 +59,7 @@ def _not_contains(dict, *keylist):
5459try :
5560 exec ("x, y" , g2 )
5661except NameError : pass
57- else : Assert (False , "should throw NameError exception" )
62+ else : tc . assertTrue (False , "should throw NameError exception" )
5863
5964exec ("y = 'ironpython'" , g2 )
6065_contains (g2 , {"x" :3 , "y" :"ironpython" })
@@ -138,7 +143,7 @@ class C:
138143 exec ('a = unique_global_name' )
139144 exec ("if unique_global_name != 987654321: raise AssertionError('cannott see unique_global_name')" )
140145
141- AreEqual (C .a , 987654321 )
146+ tc . assertEqual (C .a , 987654321 )
142147
143148def f ():
144149 exec ("if unique_global_name != 987654321: raise AssertionError('cannot see unique_global_name')" )
@@ -155,28 +160,12 @@ def g(): exec("if unique_global_name != 987654321: raise AssertionError('cannot
155160try :
156161 exec (3 )
157162except TypeError : pass
158- else : Fail ("Should already thrown (3)" )
163+ else : tc . fail ("Should already thrown (3)" )
159164
160165# verify exec(...) takes a code object
161166codeobj = compile ('1+1' , '<compiled code>' , 'exec' )
162167exec (codeobj )
163168
164- # verify exec(...) takes a file...
165- fn = path_combine (testpath .temporary_dir , 'testfile.tmp' )
166- write_to_file (fn , "x = [1,2,3,4,5]\n x.reverse()\n Assert(x == [5,4,3,2,1])\n " )
167-
168- f = file (fn , "r" )
169- exec (f )
170- Assert (x == [5 ,4 ,3 ,2 ,1 ])
171- f .close ()
172-
173- # and now verify it'll take a .NET Stream as well...
174- if is_cli :
175- import System
176- f = System .IO .FileStream (fn , System .IO .FileMode .Open )
177- exec (f )
178- f .Close ()
179-
180169# verify that exec'd code has access to existing locals
181170qqq = 3
182171exec ('qqq+1' )
@@ -185,7 +174,7 @@ def g(): exec("if unique_global_name != 987654321: raise AssertionError('cannot
185174del qqq
186175try : exec ('qqq+1' )
187176except NameError : pass
188- else : Fail ("should already thrown (qqq+1)" )
177+ else : tc . fail ("should already thrown (qqq+1)" )
189178
190179exec ('qqq+1' , {'qqq' :99 })
191180
@@ -197,19 +186,19 @@ def g(): exec("if unique_global_name != 987654321: raise AssertionError('cannot
197186myloc = {}
198187myglob = {}
199188exec ("a = 1; global b; b = 1" , myglob , myloc )
200- Assert ("a" in myloc )
201- Assert ("a" not in myglob )
202- Assert ("b" in myglob )
203- Assert ("b" not in myloc )
189+ tc . assertTrue ("a" in myloc )
190+ tc . assertTrue ("a" not in myglob )
191+ tc . assertTrue ("b" in myglob )
192+ tc . assertTrue ("b" not in myloc )
204193
205194# Statement form of exec.
206195myloc = {}
207196myglob = {}
208197exec ("a = 1; global b; b = 1" , myglob , myloc )
209- Assert ("a" in myloc )
210- Assert ("a" not in myglob )
211- Assert ("b" in myglob )
212- Assert ("b" not in myloc )
198+ tc . assertTrue ("a" in myloc )
199+ tc . assertTrue ("a" not in myglob )
200+ tc . assertTrue ("b" in myglob )
201+ tc . assertTrue ("b" not in myloc )
213202
214203
215204# Explicit global scope implies the same local scope.
@@ -218,19 +207,19 @@ def g(): exec("if unique_global_name != 987654321: raise AssertionError('cannot
218207myloc = {}
219208myglob = {}
220209exec ("a = 1; global b; b = 1" , myglob )
221- Assert ("a" in myglob )
222- Assert ("a" not in myloc )
223- Assert ("b" in myglob )
224- Assert ("b" not in myloc )
210+ tc . assertTrue ("a" in myglob )
211+ tc . assertTrue ("a" not in myloc )
212+ tc . assertTrue ("b" in myglob )
213+ tc . assertTrue ("b" not in myloc )
225214
226215# Statement form of exec.
227216myloc = {}
228217myglob = {}
229218exec ("a = 1; global b; b = 1" , myglob )
230- Assert ("a" in myglob )
231- Assert ("a" not in myloc )
232- Assert ("b" in myglob )
233- Assert ("b" not in myloc )
219+ tc . assertTrue ("a" in myglob )
220+ tc . assertTrue ("a" not in myloc )
221+ tc . assertTrue ("b" in myglob )
222+ tc . assertTrue ("b" not in myloc )
234223
235224# Testing interesting exec cases
236225
@@ -246,7 +235,7 @@ class Nothing:
246235 pass
247236
248237def MakeDict (value ):
249- return { 'AreEqual' : AreEqual , 'AssertError' : AssertError , 'x' : value , 'str' : str }
238+ return { 'AreEqual' : tc . assertEqual , 'AssertError' : tc . assertRaises , 'x' : value , 'str' : str }
250239
251240class Mapping :
252241 def __init__ (self , value = None ):
@@ -260,14 +249,14 @@ def __init__(self, value = None):
260249 def __getitem__ (self , item ):
261250 return self .values [item ]
262251
263- TryExecG ("AreEqual (x, 'global_x')" , None )
264- TryExecGL ("AreEqual (x, 'global_x')" , None , None )
252+ TryExecG ("tc.assertEqual (x, 'global_x')" , None )
253+ TryExecGL ("tc.assertEqual (x, 'global_x')" , None , None )
265254
266- AssertError (TypeError , TryExecG , "print x " , Nothing ())
267- AssertError (TypeError , TryExecGL , "print x " , Nothing (), None )
255+ tc . assertRaises (TypeError , TryExecG , "print(x) " , Nothing ())
256+ tc . assertRaises (TypeError , TryExecGL , "print(x) " , Nothing (), None )
268257
269- AssertError (TypeError , TryExecG , "print x " , Mapping ())
270- AssertError (TypeError , TryExecGL , "print x " , Mapping (), None )
258+ tc . assertRaises (TypeError , TryExecG , "print(x) " , Mapping ())
259+ tc . assertRaises (TypeError , TryExecGL , "print(x) " , Mapping (), None )
271260
272261TryExecG ("AreEqual(x, 17)" , MakeDict (17 ))
273262TryExecGL ("AreEqual(x, 19)" , MakeDict (19 ), None )
@@ -276,7 +265,7 @@ def __getitem__(self, item):
276265#TryExecGL("AreEqual(x, 29)", MyDict(29), None)
277266
278267TryExecGL ("AreEqual(x, 31)" , None , MakeDict (31 ))
279- AssertError (TypeError , TryExecGL , "print x " , None , Nothing ())
268+ tc . assertRaises (TypeError , TryExecGL , "print(x) " , None , Nothing ())
280269
281270TryExecGL ("AreEqual(x, 37)" , None , Mapping (37 ))
282271#TryExecGL("AreEqual(x, 41)", None, MyDict(41))
@@ -289,55 +278,58 @@ def f(l):
289278
290279l = []
291280exec ("pass" , f (l ))
292- AreEqual (l , ["called f" ])
281+ tc . assertEqual (l , ["called f" ])
293282
294283def g (l ):
295284 l .append ("called g" )
296285 return {}
297286
298287l = []
299288exec ("pass" , f (l ), g (l ))
300- AreEqual (l , ["called f" , "called g" ])
301-
302- # testing exec accepts \n eolns only
303- def test_eolns ():
304- def f1 (sep ): exec ('x = 2$y=4$' .replace ('$' , sep ))
305- def f2 (sep ): exec ('''x = 3$y = 5$''' .replace ('$' , sep ))
306- def f3 (sep ): exec ("exec '''x = 3$y = 5$'''" .replace ('$' , sep ))
307-
308- for x in [f1 , f2 , f3 ]:
309- if is_cli : #http://ironpython.codeplex.com/workitem/27991
310- AssertError (SyntaxError , x , '\r \n ' )
311- AssertError (SyntaxError , x , '\r ' )
312- else :
313- temp = x ('\r \n ' )
314- temp = x ('\r ' )
315-
316- AssertError (SyntaxError , x , '\a ' )
317- x ('\n ' )
318-
319- def test_set_builtins ():
320- g = {}
321- exec ("" , g , None )
322- Assert ('__builtins__' in list (g .keys ()))
323-
324- def test_builtins_type ():
325- x , y = {}, {}
326- exec ('abc = 42' , x , y )
327- AreEqual (type (x ['__builtins__' ]), dict )
328-
329- def test_exec_locals ():
330- exec ("""
289+ tc .assertEqual (l , ["called f" , "called g" ])
290+
291+ class ExecTestCase (IronPythonTestCase ):
292+
293+ # testing exec accepts \n eolns only
294+ def test_eolns (self ):
295+ def f1 (sep ): exec ('x = 2$y=4$' .replace ('$' , sep ))
296+ def f2 (sep ): exec ('''x = 3$y = 5$''' .replace ('$' , sep ))
297+ def f3 (sep ): exec ("exec('''x = 3$y = 5$''')" .replace ('$' , sep ))
298+
299+ for x in [f1 , f2 , f3 ]:
300+ if is_cli : #http://ironpython.codeplex.com/workitem/27991
301+ self .assertRaises (SyntaxError , x , '\r \n ' )
302+ self .assertRaises (SyntaxError , x , '\r ' )
303+ else :
304+ temp = x ('\r \n ' )
305+ temp = x ('\r ' )
306+
307+ self .assertRaises (SyntaxError , x , '\a ' )
308+ x ('\n ' )
309+
310+ def test_set_builtins (self ):
311+ g = {}
312+ exec ("" , g , None )
313+ self .assertTrue ('__builtins__' in g .keys ())
314+
315+ def test_builtins_type (self ):
316+ x , y = {}, {}
317+ exec ('abc = 42' , x , y )
318+ self .assertEqual (type (x ['__builtins__' ]), dict )
319+
320+ def test_exec_locals (self ):
321+ tc = self
322+ exec ("""
331323def body():
332- AreEqual ('anythingatall' in locals(), False)
324+ tc.assertEqual ('anythingatall' in locals(), False)
333325
334326body()
335327foozbab = 2
336328def body():
337- AreEqual ('foozbab' in locals(), False)
329+ tc.assertEqual ('foozbab' in locals(), False)
338330
339331body()
340-
332+
341333""" )
342334
343335run_test (__name__ )
0 commit comments