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))
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))
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))
def testDescriptionOverride(self):
objc.classAddMethods(MEClass, [Methods.pyobjc_instanceMethods.description])
self.assertTrue(MEClass.instancesRespondToSelector_("description"))
newInstance = MEClass.new()
self.assertEquals(newInstance.description(), u"")
self.assertEquals(preEverythingInstance.description(), u"")
def testAddedMethodType(self):
def anotherNewClassMethod(cls):
"CLS DOC STRING"
return "BAR CLS"
anotherNewClassMethod = classmethod(anotherNewClassMethod)
def anotherNewMethod(self):
"INST DOC STRING"
return "BAR SELF"
self.assertTrue(not MEClass.pyobjc_classMethods.respondsToSelector_("anotherNewClassMethod"))
self.assertTrue(not MEClass.pyobjc_classMethods.instancesRespondToSelector_("anotherNewMethod"))
objc.classAddMethods(MEClass, [anotherNewClassMethod, anotherNewMethod])
self.assertTrue(MEClass.pyobjc_classMethods.respondsToSelector_("anotherNewClassMethod"))
self.assertTrue(MEClass.pyobjc_classMethods.instancesRespondToSelector_("anotherNewMethod"))
self.assertEquals(MEClass.anotherNewClassMethod.__doc__, "CLS DOC STRING")
self.assertEquals(MEClass.anotherNewMethod.__doc__, "INST DOC STRING")
def testAssignAClassMethod(self):
MEClass.classSomethingElse = classmethod(lambda self: 2*2)
MEClass.classDuplicate_ = classmethod(lambda self, x: 2*x)
self.assertTrue(MEClass.pyobjc_classMethods.respondsToSelector_(b"classSomethingElse"))
self.assertTrue(MEClass.pyobjc_classMethods.respondsToSelector_(b"classDuplicate:"))
self.assertEquals(4, MEClass.classSomethingElse())
self.assertEquals(8, MEClass.classDuplicate_(4))
# FIXME: This test suite seems to polute it's environment, other tests fail
# when this test suite is active!
from PyObjCTools.TestSupport import *
import sys
from PyObjCTools.TestSupport import onlyPython2
import objc
NSObject = objc.lookUpClass('NSObject')
class MEClass(NSObject):
pass
preEverythingInstance = MEClass.new()
class Methods(NSObject):
def description(self):
return u""
def newMethod(self):
return u""
class MethodsSub(NSObject):
def description(self):
return u""
def newMethod(self):
return u""
def newSubMethod(self):
return "BAR CLS"
anotherNewClassMethod = classmethod(anotherNewClassMethod)
def anotherNewMethod(self):
"INST DOC STRING"
return "BAR SELF"
self.assertTrue(not MEClass.pyobjc_classMethods.respondsToSelector_("anotherNewClassMethod"))
self.assertTrue(not MEClass.pyobjc_classMethods.instancesRespondToSelector_("anotherNewMethod"))
objc.classAddMethods(MEClass, [anotherNewClassMethod, anotherNewMethod])
self.assertTrue(MEClass.pyobjc_classMethods.respondsToSelector_("anotherNewClassMethod"))
self.assertTrue(MEClass.pyobjc_classMethods.instancesRespondToSelector_("anotherNewMethod"))
self.assertEquals(MEClass.anotherNewClassMethod.__doc__, "CLS DOC STRING")
self.assertEquals(MEClass.anotherNewMethod.__doc__, "INST DOC STRING")
"CLS DOC STRING"
return "BAR CLS"
anotherNewClassMethod = classmethod(anotherNewClassMethod)
def anotherNewMethod(self):
"INST DOC STRING"
return "BAR SELF"
self.assertTrue(not MEClass.pyobjc_classMethods.respondsToSelector_("anotherNewClassMethod"))
self.assertTrue(not MEClass.pyobjc_classMethods.instancesRespondToSelector_("anotherNewMethod"))
objc.classAddMethods(MEClass, [anotherNewClassMethod, anotherNewMethod])
self.assertTrue(MEClass.pyobjc_classMethods.respondsToSelector_("anotherNewClassMethod"))
self.assertTrue(MEClass.pyobjc_classMethods.instancesRespondToSelector_("anotherNewMethod"))
self.assertEquals(MEClass.anotherNewClassMethod.__doc__, "CLS DOC STRING")
self.assertEquals(MEClass.anotherNewMethod.__doc__, "INST DOC STRING")
def testNewClassMethod(self):
def aNewClassMethod(cls):
return "Foo cls"
aNewClassMethod = classmethod(aNewClassMethod)
self.assertTrue(not MEClass.pyobjc_classMethods.respondsToSelector_("aNewClassMethod"))
objc.classAddMethods(MEClass, [aNewClassMethod])
self.assertTrue(MEClass.pyobjc_classMethods.respondsToSelector_("aNewClassMethod"))
self.assertTrue(MEClass.aNewClassMethod.isClassMethod)
self.assertEquals(MEClass.aNewClassMethod(), 'Foo cls')