def testExistingFile(self): '''Test SimpleFile class with existing file.''' f = SimpleFile(self.existing_filename) self.assertEqual(f.filename(), self.existing_filename) f.open() self.assertNotEqual(f.size(), 0) f.close()
def testMonkeyPatchOnMethodWithStaticAndNonStaticOverloads(self): '''Injects new 'exists' on a SimpleFile instance and makes C++ call it.''' simplefile = SimpleFile('foobar') # Static 'exists' simplefile.exists('sbrubbles') self.assertFalse(self.duck_method_called) # Non-static 'exists' simplefile.exists() self.assertFalse(self.duck_method_called) def myExists(obj): self.duck_method_called = True return False simplefile.exists = MethodTypeCompat(myExists, simplefile) # Static 'exists' was overridden by the monkey patch, which accepts 0 arguments self.assertRaises(TypeError, simplefile.exists, 'sbrubbles') # Monkey patched exists simplefile.exists() self.assertTrue(self.duck_method_called) simplefile.exists = None
def testNonExistingFile(self): '''Test SimpleFile class with non-existing file.''' f = SimpleFile(self.non_existing_filename) self.assertEqual(f.filename(), self.non_existing_filename) self.assertRaises(IOError, f.open) self.assertEqual(f.size(), 0)
def testCallingInstanceMethod(self): '''Call instance method.''' f1 = SimpleFile(self.non_existing_filename) self.assertFalse(f1.exists()) f2 = SimpleFile(self.existing_filename) self.assertTrue(f2.exists())
def testCallingStaticMethodWithInstance(self): '''Call static method using instance of class.''' f = SimpleFile(self.non_existing_filename) self.assertTrue(f.exists(self.existing_filename)) self.assertFalse(f.exists(self.non_existing_filename))
def testDuckPunchingStaticNonStaticMethod(self): f = SimpleFile(self.existing_filename) f.exists = lambda: "Meee" self.assertEqual(f.exists(), "Meee")