Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def testAssignAMethod(self):
MEClass.doSomethingElse = lambda self: 2*2
MEClass.doDuplicate_ = lambda self, x: 2*x
self.assertTrue(MEClass.instancesRespondToSelector_("doSomethingElse"))
self.assertTrue(MEClass.instancesRespondToSelector_("doDuplicate:"))
o = MEClass.alloc().init()
self.assertEquals(4, o.doSomethingElse())
self.assertEquals(8, o.doDuplicate_(4))
@onlyPython2
def testPythonSourcedMethods(self):
# 20031227, Ronald: Assigning the methods works alright, but actually
# using them won't because the new methods are actually still methods
# of a different class and will therefore complain about the type
# of 'self'.
objc.classAddMethods(MEClass, [PurePython.description,
PurePython.newMethod,
PurePython.purePythonMethod])
self.assertTrue(MEClass.instancesRespondToSelector_("description"))
self.assertTrue(MEClass.instancesRespondToSelector_("newMethod"))
self.assertTrue(MEClass.instancesRespondToSelector_("purePythonMethod"))
newInstance = MEClass.new()
# This is bogus, see above:
#self.assertEquals(newInstance.description(), u"")
#self.assertEquals(newInstance.newMethod(), u"")
#self.assertEquals(newInstance.purePythonMethod(), u"")
#self.assertEquals(preEverythingInstance.description(), u"")
#self.assertEquals(preEverythingInstance.newMethod(), u"")
#self.assertEquals(preEverythingInstance.purePythonMethod(), u"")
self.assertRaises(TypeError, newInstance.description)
self.assertRaises(TypeError, newInstance.newMethod)
self.assertRaises(TypeError, newInstance.purePythonMethod)
self.assertRaises(TypeError, preEverythingInstance.description)
@onlyPython2
def testPythonSourcedMethods(self):
# 20031227, Ronald: Assigning the methods works alright, but actually
# using them won't because the new methods are actually still methods
# of a different class and will therefore complain about the type
# of 'self'.
objc.classAddMethods(MEClass, [PurePython.description,
PurePython.newMethod,
PurePython.purePythonMethod])
self.assertTrue(MEClass.instancesRespondToSelector_("description"))
self.assertTrue(MEClass.instancesRespondToSelector_("newMethod"))
self.assertTrue(MEClass.instancesRespondToSelector_("purePythonMethod"))
newInstance = MEClass.new()
# This is bogus, see above:
#self.assertEquals(newInstance.description(), u"")
#self.assertEquals(newInstance.newMethod(), u"")
#self.assertEquals(newInstance.purePythonMethod(), u"")
#self.assertEquals(preEverythingInstance.description(), u"")
#self.assertEquals(preEverythingInstance.newMethod(), u"")
#self.assertEquals(preEverythingInstance.purePythonMethod(), u"")
self.assertRaises(TypeError, newInstance.description)
self.assertRaises(TypeError, newInstance.newMethod)
if sys.version_info[0] == 2:
objc.classAddMethods(MEClass, [
PurePython.description.im_func,
PurePython.newMethod.im_func,
PurePython.purePythonMethod.im_func,
])
else:
objc.classAddMethods(MEClass, [
PurePython.description,
PurePython.newMethod,
PurePython.purePythonMethod,
])
self.assertTrue(MEClass.instancesRespondToSelector_("description"))
self.assertTrue(MEClass.instancesRespondToSelector_("newMethod"))
self.assertTrue(MEClass.instancesRespondToSelector_("purePythonMethod"))
newInstance = MEClass.new()
self.assertEquals(newInstance.description(), u"")
self.assertEquals(newInstance.newMethod(), u"")
self.assertEquals(newInstance.purePythonMethod(), u"")
self.assertEquals(preEverythingInstance.description(), u"")
self.assertEquals(preEverythingInstance.newMethod(), u"")
self.assertEquals(preEverythingInstance.purePythonMethod(), u"")
def testSubNewMethod(self):
objc.classAddMethods(MEClass, [MethodsSub.newMethod, MethodsSub.newSubMethod])
self.assertTrue(MEClass.instancesRespondToSelector_("newMethod"))
self.assertTrue(MEClass.instancesRespondToSelector_("newSubMethod"))
newInstance = MEClass.new()
self.assertEquals(newInstance.newMethod(), u"")
self.assertEquals(preEverythingInstance.newMethod(), u"")
self.assertEquals(newInstance.newSubMethod(), u"")
self.assertEquals(preEverythingInstance.newSubMethod(), u"")
def testNewMethod(self):
objc.classAddMethods(MEClass, [Methods.pyobjc_instanceMethods.newMethod])
self.assertTrue(MEClass.instancesRespondToSelector_("newMethod"))
newInstance = MEClass.new()
self.assertEquals(newInstance.newMethod(), u"")
self.assertEquals(preEverythingInstance.newMethod(), u"")
def testSubDescriptionOverride(self):
objc.classAddMethods(MEClass, [MethodsSub.pyobjc_instanceMethods.description])
self.assertTrue(MEClass.instancesRespondToSelector_("description"))
newInstance = MEClass.new()
self.assertEquals(newInstance.description(), u"")
self.assertEquals(preEverythingInstance.description(), u"")
@onlyPython2
def testPythonSourcedMethods(self):
# 20031227, Ronald: Assigning the methods works alright, but actually
# using them won't because the new methods are actually still methods
# of a different class and will therefore complain about the type
# of 'self'.
objc.classAddMethods(MEClass, [PurePython.description,
PurePython.newMethod,
PurePython.purePythonMethod])
self.assertTrue(MEClass.instancesRespondToSelector_("description"))
self.assertTrue(MEClass.instancesRespondToSelector_("newMethod"))
self.assertTrue(MEClass.instancesRespondToSelector_("purePythonMethod"))
newInstance = MEClass.new()
# This is bogus, see above:
#self.assertEquals(newInstance.description(), u"")
#self.assertEquals(newInstance.newMethod(), u"")
#self.assertEquals(newInstance.purePythonMethod(), u"")
#self.assertEquals(preEverythingInstance.description(), u"")
#self.assertEquals(preEverythingInstance.newMethod(), u"")
#self.assertEquals(preEverythingInstance.purePythonMethod(), u"")
self.assertRaises(TypeError, newInstance.description)
self.assertRaises(TypeError, newInstance.newMethod)
self.assertRaises(TypeError, newInstance.purePythonMethod)
if sys.version_info[0] == 2:
objc.classAddMethods(MEClass, [
PurePython.description.im_func,
PurePython.newMethod.im_func,
PurePython.purePythonMethod.im_func,
])
else:
objc.classAddMethods(MEClass, [
PurePython.description,
PurePython.newMethod,
PurePython.purePythonMethod,
])
self.assertTrue(MEClass.instancesRespondToSelector_("description"))
self.assertTrue(MEClass.instancesRespondToSelector_("newMethod"))
self.assertTrue(MEClass.instancesRespondToSelector_("purePythonMethod"))
newInstance = MEClass.new()
self.assertEquals(newInstance.description(), u"")
self.assertEquals(newInstance.newMethod(), u"")
self.assertEquals(newInstance.purePythonMethod(), u"")
self.assertEquals(preEverythingInstance.description(), u"")
self.assertEquals(preEverythingInstance.newMethod(), u"")
self.assertEquals(preEverythingInstance.purePythonMethod(), u"")