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 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 = types.MethodType(myExists, simplefile, 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.assert_(self.duck_method_called) simplefile.exists = None
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 testCallingStaticMethodWithClass(self): '''Call static method using class.''' self.assertTrue(SimpleFile.exists(self.existing_filename)) self.assertFalse(SimpleFile.exists(self.non_existing_filename))
def testDuckPunchingStaticNonStaticMethod(self): f = SimpleFile(self.existing_filename) f.exists = lambda: "Meee" self.assertEqual(f.exists(), "Meee")
def testDuckPunchingStaticNonStaticMethod(self): f = SimpleFile(self.existing_filename) f.exists = lambda : "Meee" self.assertEqual(f.exists(), "Meee")
def testCallingInstanceMethod(self): '''Call instance method.''' f1 = SimpleFile(self.non_existing_filename) self.assertFalse(f1.exists()) f2 = SimpleFile(self.existing_filename) self.assert_(f2.exists())
def testCallingStaticMethodWithInstance(self): '''Call static method using instance of class.''' f = SimpleFile(self.non_existing_filename) self.assert_(f.exists(self.existing_filename)) self.assertFalse(f.exists(self.non_existing_filename))
def testCallingStaticMethodWithClass(self): '''Call static method using class.''' self.assert_(SimpleFile.exists(self.existing_filename)) self.assertFalse(SimpleFile.exists(self.non_existing_filename))