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_modified_module(): test_name = "test_modified_module.py" new_stdout_name = "new_stdout.log" #create the file f = open(test_name, "w") f.writelines('''A=1; print "First Run"''') f.close() #take control of stdout old_stdout = sys.stdout new_stdout = open(new_stdout_name, "w") sys.stdout = new_stdout #clr.Use new_module = clr.Use(test_name.split(".py")[0]) #give stdout back sys.stdout = old_stdout new_stdout.close() new_stdout = open(new_stdout_name, "r") new_stdout_lines = new_stdout.readlines() new_stdout.close() #run checks AreEqual(new_stdout_lines, ["First Run\n"]) AreEqual(new_module.A, 1) #--Do everything again with different values... #recreate the file f = open(test_name, "w") f.writelines('''A=2; print "Second Run"''') f.close() #take control of stdout old_stdout = sys.stdout new_stdout = open(new_stdout_name, "w") sys.stdout = new_stdout #clr.Use new_module = clr.Use(test_name.split(".py")[0]) #give stdout back sys.stdout = old_stdout new_stdout.close() new_stdout = open(new_stdout_name, "r") new_stdout_lines = new_stdout.readlines() new_stdout.close() #run checks AreEqual(new_stdout_lines, []) AreEqual(new_module.A, 1) #cleanup os.remove(test_name) os.remove(new_stdout_name)
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 simpleTester(a, b, c): global SIMPLE_TEST global SIMPLE_TEST_COUNT import os test_name = "clrusetest" + str(SIMPLE_TEST_COUNT) + ".py" SIMPLE_TEST_COUNT = SIMPLE_TEST_COUNT + 1 new_stdout_name = "new_stdout.log" expected_stdout = '''OK...\n''' #create the file test_text = SIMPLE_TEST % (str(a), str(c)) f = open(test_name, "w") f.writelines(test_text) f.close() #take control of stdout old_stdout = sys.stdout new_stdout = open(new_stdout_name, "w") sys.stdout = new_stdout #clr.Use name = test_name.split(".py")[0] new_module = clr.Use(name) #give stdout back sys.stdout = old_stdout new_stdout.close() new_stdout = open(new_stdout_name, "r") new_stdout_lines = new_stdout.readlines() new_stdout.close() #run a few easy checks AreEqual(len(new_stdout_lines), 3) AreEqual(new_stdout_lines[0], expected_stdout) AreEqual(new_stdout_lines[2], "b= 42\n") AreEqual(new_module.A, a) AreEqual(new_module.aFunc(None), c) Assert(isinstance(new_module.K, new_module.Klass)) AreEqual(new_module.K.Member, 72) new_module.K.Member = "foo" AreEqual(new_module.K.Member, "foo") new_module.K.NewMember = 33 AreEqual(new_module.K.NewMember, 33) new_module.K = None AreEqual(new_module.K, None) #negative checks AssertError(TypeError, new_module.aFunc) AssertError(TypeError, new_module.aFunc, 1, 2) AssertError(TypeError, new_module.aFunc, 1, 2, 3) Assert(not hasattr(new_module, "a")) Assert(not hasattr(new_module, "simpleTester")) try: aFunc(7) raise "Should never get this far" except: pass #hard test real_module = __import__(test_name.split(".py")[0]) #for key in dir(real_module): AreEqual(real_module.__dict__[key], new_module.__dict__[key]) #cleanup os.remove(test_name) os.remove(new_stdout_name)