def test_modified_module(self): import clr test_name = "test_modified_module.py" try: #create the file with open(test_name, "w") as f: f.writelines('''A=1; print "First Run"''') with path_modifier('.') as p: with stdout_trapper() as output: new_module = clr.Use(test_name.split(".py")[0]) #run checks self.assertEqual(output.messages, ["First Run"]) self.assertEqual(new_module.A, 1) #--Do everything again with different values... #recreate the file with open(test_name, "w") as f: f.writelines('''A=2; print "Second Run"''') with path_modifier('.') as p: with stdout_trapper() as output: new_module = clr.Use(test_name.split(".py")[0]) #run checks self.assertEqual(output.messages, []) self.assertEqual(new_module.A, 1) finally: #cleanup try: os.remove(test_name) except: pass
def test_z_cli_tests(self): # runs last to prevent tainting the module w/ CLR names import clr import System self.load_iron_python_test() from IronPythonTest import WriteOnly if is_netcoreapp: clr.AddReference("System.IO.Compression") with stdout_trapper() as output: help(WriteOnly) help(System.IO.Compression) help(System.Int64) x = self.run_help(System.String.Format) self.assertTrue(x.find('Format(format: str, arg0: object) -> str') != -1) x = self.run_help('u.u'.Split('u')) # requires std lib self.assertTrue('Help on Array[str] object' in x, x) # https://github.com/IronLanguages/ironpython2/issues/359 if not is_mono: from System.IO import MemoryStream x_class = self.run_help(MemoryStream.Write) x_instance = self.run_help(MemoryStream().Write) self.assertEqual(x_class, x_instance.replace("built-in function Write", "method_descriptor")) #http://ironpython.codeplex.com/WorkItem/View.aspx?WorkItemId=11883 self.assertEqual(dir(System).count('Action'), 1) with stdout_trapper() as output: help(System.Action) self.assertEqual(dir(System).count('Action'), 1)
def test_nodoc(self): class foo(object): pass with stdout_trapper() as output: help(foo) class foo: pass with stdout_trapper() as output: help(foo) def foo(): pass with stdout_trapper() as output: help(foo)
def test_cp23555(self): with stdout_trapper() as trapper: class Base(object): pass class Real(Base, float): def __new__(cls, *args, **kwargs): print('real new') result = Stub.__new__(cls, *args, **kwargs) return result def __init__(self, *args, **kwargs): print('real init') def __del__(self): print('real del') class Stub(Real): def __new__(cls, *args, **kwargs): print('stub new') return float.__new__(Stub, args[0]) def __init__(self, *args, **kwargs): print('stub init') def __del__(self): print("this should never happen; it's just here to ensure I get registered for GC") def ConstructReal(x): f = Real(x) f.__class__ = Real return f f = ConstructReal(1.0) del f self.assertEqual(trapper.messages[0:3], ['real new', 'stub new', 'stub init']) #'real del']) => CLR GC
def test_cp23555(self): with stdout_trapper() as trapper: class Base(object): pass class Real(Base, float): def __new__(cls, *args, **kwargs): print 'real new' result = Stub.__new__(cls, *args, **kwargs) return result def __init__(self, *args, **kwargs): print 'real init' def __del__(self): print 'real del' class Stub(Real): def __new__(cls, *args, **kwargs): print 'stub new' return float.__new__(Stub, args[0]) def __init__(self, *args, **kwargs): print 'stub init' def __del__(self): print "this should never happen; it's just here to ensure I get registered for GC" def ConstructReal(x): f = Real(x) f.__class__ = Real return f f = ConstructReal(1.0) del f self.assertEqual(trapper.messages[0:3], ['real new', 'stub new', 'stub init']) #'real del']) => CLR GC
def simpleTester(self, a, b, c): global SIMPLE_TEST_COUNT import clr test_name = "clrusetest{}.py".format(SIMPLE_TEST_COUNT) SIMPLE_TEST_COUNT += 1 expected_stdout = '''OK...''' #create the file test_text = SIMPLE_TEST % (str(a), str(c)) with open(test_name, "w") as f: f.writelines(test_text) try: with path_modifier('.') as p: with stdout_trapper() as output: new_module = clr.Use(test_name.split(".py")[0]) #run a few easy checks self.assertEqual(len(output.messages), 3) self.assertEqual(output.messages[0], expected_stdout) self.assertEqual(output.messages[2], "b= 42") self.assertEqual(new_module.A, a) self.assertEqual(new_module.aFunc(None), c) self.assertTrue(isinstance(new_module.K, new_module.Klass)) self.assertEqual(new_module.K.Member, 72) new_module.K.Member = "foo" self.assertEqual(new_module.K.Member, "foo") new_module.K.NewMember = 33 self.assertEqual(new_module.K.NewMember, 33) new_module.K = None self.assertEqual(new_module.K, None) #negative checks self.assertRaises(TypeError, new_module.aFunc) self.assertRaises(TypeError, new_module.aFunc, 1, 2) self.assertRaises(TypeError, new_module.aFunc, 1, 2, 3) self.assertTrue(not hasattr(new_module, "a")) self.assertTrue(not hasattr(new_module, "simpleTester")) try: aFunc(7) self.fail("Should never get this far") except: pass #hard test real_module = __import__(test_name.split(".py")[0]) #for key in dir(real_module): self.assertEqual(real_module.__dict__[key], new_module.__dict__[key]) finally: #cleanup try: os.remove(test_name) except: pass
def test_cp23914(self): class C(object): def __init__(self, x, y, z): print x, y, z m = type.__call__ with stdout_trapper() as trapper: try: l = m(C, 1, 2, 3) l = m(C, z=3, y=2, x=1) except Exception, e: print e.message
def simpleTester(self, a, b, c): global SIMPLE_TEST_COUNT import clr test_name = "clrusetest{}.py".format(SIMPLE_TEST_COUNT) SIMPLE_TEST_COUNT += 1 expected_stdout = '''OK...''' #create the file test_text = SIMPLE_TEST % (str(a), str(c)) with open(test_name, "w") as f: f.writelines(test_text) try: with path_modifier('.') as p: with stdout_trapper() as output: new_module = clr.Use(test_name.split(".py")[0]) #run a few easy checks self.assertEqual(len(output.messages), 3) self.assertEqual(output.messages[0], expected_stdout) self.assertEqual(output.messages[2], "b= 42") self.assertEqual(new_module.A, a) self.assertEqual(new_module.aFunc(None), c) self.assertTrue(isinstance(new_module.K, new_module.Klass)) self.assertEqual(new_module.K.Member, 72) new_module.K.Member = "foo" self.assertEqual(new_module.K.Member, "foo") new_module.K.NewMember = 33 self.assertEqual(new_module.K.NewMember, 33) new_module.K = None self.assertEqual(new_module.K, None) #negative checks self.assertRaises(TypeError, new_module.aFunc) self.assertRaises(TypeError, new_module.aFunc, 1, 2) self.assertRaises(TypeError, new_module.aFunc, 1, 2, 3) self.assertTrue(not hasattr(new_module, "a")) self.assertTrue(not hasattr(new_module, "simpleTester")) try: aFunc(7) self.fail("Should never get this far") except: pass #hard test real_module = __import__(test_name.split(".py")[0]) #for key in dir(real_module): self.assertEqual(real_module.__dict__[key], new_module.__dict__[key]) finally: pass
def test_user_class(self): class foo(object): """this documentation is going to make the world a better place""" class TClass(object): ''' Some TClass doc... ''' def __init__(self, p1, *args, **argkw): ''' Some constructor doc... p1 does something args does something argkw does something ''' self.member = 1 def plainMethod(self): ''' Doc here ''' pass def plainMethodEmpty(self): ''' ''' pass def plainMethodNone(self): pass with stdout_trapper() as output: help(foo) #sanity checks. just make sure no execeptions #are thrown help(TClass) help(TClass.__init__) help(TClass.plainMethod) help(TClass.plainMethodEmpty) help(TClass.plainMethodNone) x = os.linesep.join(output.messages) self.assertTrue( x.find( 'this documentation is going to make the world a better place') != -1)
def test_cp23914(self): class C(object): def __init__(self,x,y,z): print x,y,z m = type.__call__ with stdout_trapper() as trapper: try: l = m(C,1,2,3) l = m(C,z=3,y=2,x=1) except Exception as e: print(e.args[0]) self.assertEqual(trapper.messages[0:2], ['1 2 3', '1 2 3'])
def test_cp23914(self): class C(object): def __init__(self, x, y, z): print x, y, z m = type.__call__ with stdout_trapper() as trapper: try: l = m(C, 1, 2, 3) l = m(C, z=3, y=2, x=1) except Exception as e: print(e.args[0]) self.assertEqual(trapper.messages[0:2], ['1 2 3', '1 2 3'])
def test_user_class(self): class foo(object): """this documentation is going to make the world a better place""" class TClass(object): ''' Some TClass doc... ''' def __init__(self, p1, *args, **argkw): ''' Some constructor doc... p1 does something args does something argkw does something ''' self.member = 1 def plainMethod(self): ''' Doc here ''' pass def plainMethodEmpty(self): ''' ''' pass def plainMethodNone(self): pass with stdout_trapper() as output: help(foo) #sanity checks. just make sure no execeptions #are thrown help(TClass) help(TClass.__init__) help(TClass.plainMethod) help(TClass.plainMethodEmpty) help(TClass.plainMethodNone) x = os.linesep.join(output.messages) self.assertTrue(x.find('this documentation is going to make the world a better place') != -1)
def test_cp23555(self): with stdout_trapper() as trapper: class Base(object): pass class Real(Base, float): def __new__(cls, *args, **kwargs): print('real new') result = Stub.__new__(cls, *args, **kwargs) return result def __init__(self, *args, **kwargs): print('real init') def __del__(self): print('real del') class Stub(Real): def __new__(cls, *args, **kwargs): print('stub new') return float.__new__(Stub, args[0]) def __init__(self, *args, **kwargs): print('stub init') def __del__(self): print( "this should never happen; it's just here to ensure I get registered for GC" ) def ConstructReal(x): f = Real(x) f.__class__ = Real return f f = ConstructReal(1.0) del f # ensure __del__ is called import gc gc.collect() self.assertEqual(trapper.messages, ['real new', 'stub new', 'stub init', 'real del'])
def test_gh1435(self): import clr code = """ using System; /// <summary> /// Some description1. /// </summary> public class gh1435 { /// <summary> /// Some description2. /// </summary> public static String strFoo= "foo"; /// <summary> /// Some description3. /// </summary> public gh1435() { } /// <summary> /// Some description4. /// </summary> public int someMethod1() { return 8; } /// <summary> /// Some description5. /// </summary> public int someMethod2(string strSome) { return 8; } /// <summary> /// Some description6. /// </summary> public int someMethod3(out string strSome) { strSome = "Some string."; return 8; } /// <summary> /// Another description1 /// </summary> public int someMethod4(out string strSome, ref int foo) { strSome = "Another string"; foo = 10; return 5; } } """ tmp = self.temporary_dir test_cs, test_dll, test_xml = os.path.join( tmp, 'gh1435.cs'), os.path.join(tmp, 'gh1435.dll'), os.path.join( tmp, 'gh1435.xml') self.write_to_file(test_cs, code) self.assertEqual( self.run_csc( '/nologo /doc:{0} /target:library /out:{1} {2}'.format( test_xml, test_dll, test_cs)), 0) expected = """Help on method_descriptor: someMethod4(...) someMethod4(self: clsBar, foo: int) -> (int, str, int) Another description1""".replace('\r', '') clr.AddReferenceToFileAndPath(test_dll) import gh1435 with stdout_trapper() as trapper: help(gh1435.someMethod4) self.assertTrue('\n'.join(trapper.messages), expected)
def run_help(self, o): with stdout_trapper() as output: help(o) return os.linesep.join(output.messages)
def test_gh1435(self): import clr code = """ using System; /// <summary> /// Some description1. /// </summary> public class gh1435 { /// <summary> /// Some description2. /// </summary> public static String strFoo= "foo"; /// <summary> /// Some description3. /// </summary> public gh1435() { } /// <summary> /// Some description4. /// </summary> public int someMethod1() { return 8; } /// <summary> /// Some description5. /// </summary> public int someMethod2(string strSome) { return 8; } /// <summary> /// Some description6. /// </summary> public int someMethod3(out string strSome) { strSome = "Some string."; return 8; } /// <summary> /// Another description1 /// </summary> public int someMethod4(out string strSome, ref int foo) { strSome = "Another string"; foo = 10; return 5; } } """ tmp = self.temporary_dir test_cs, test_dll, test_xml = os.path.join(tmp, 'gh1435.cs'), os.path.join(tmp, 'gh1435.dll'), os.path.join(tmp, 'gh1435.xml') self.write_to_file(test_cs, code) self.assertEqual(self.run_csc('/nologo /doc:{0} /target:library /out:{1} {2}'.format(test_xml, test_dll, test_cs)), 0) expected = """Help on method_descriptor: someMethod4(...) someMethod4(self: clsBar, foo: int) -> (int, str, int) Another description1""".replace('\r', '') clr.AddReferenceToFileAndPath(test_dll) import gh1435 with stdout_trapper() as trapper: help(gh1435.someMethod4) self.assertTrue('\n'.join(trapper.messages), expected)