def test_objcclass_instancecheck(self): """isinstance works with an ObjCClass as the second argument.""" NSArray = ObjCClass('NSArray') NSString = ObjCClass('NSString') self.assertIsInstance(NSObject.new(), NSObject) self.assertIsInstance(at(''), NSString) self.assertIsInstance(at(''), NSObject) self.assertIsInstance(NSObject, NSObject) self.assertIsInstance(NSObject, NSObject.objc_class) self.assertNotIsInstance(object(), NSObject) self.assertNotIsInstance(NSObject.new(), NSString) self.assertNotIsInstance(NSArray.array, NSString)
def test_objcprotocol_requires_protocol(self): """ObjCProtocol only accepts protocol pointers.""" random_obj = NSObject.alloc().init() with self.assertRaises(ValueError): ObjCProtocol(random_obj.ptr) random_obj.release()
def test_objcclass_requires_class(self): """ObjCClass only accepts class pointers.""" random_obj = NSObject.alloc().init() with self.assertRaises(ValueError): ObjCClass(random_obj.ptr) random_obj.release()
def test_objcmetaclass_requires_metaclass(self): """ObjCMetaClass only accepts metaclass pointers.""" random_obj = NSObject.alloc().init() with self.assertRaises(ValueError): ObjCMetaClass(random_obj.ptr) with self.assertRaises(ValueError): ObjCMetaClass(NSObject.ptr)
def test_objcprotocol_instancecheck(self): """isinstance works with an ObjCProtocol as the second argument.""" NSCoding = ObjCProtocol('NSCoding') NSSecureCoding = ObjCProtocol('NSSecureCoding') self.assertIsInstance(at(''), NSSecureCoding) self.assertIsInstance(at(''), NSCoding) self.assertNotIsInstance(object(), NSSecureCoding) self.assertNotIsInstance(NSObject.new(), NSSecureCoding)
def test_objcinstance_release_owned(self): # Create an object which we own. obj = NSObject.alloc().init() # Check that it is marked for release. self.assertTrue(obj._needs_release) # Explicitly release the object. obj.release() # Check that we no longer need to release it. self.assertFalse(obj._needs_release) # Delete it and make sure that we don't segfault on garbage collection. del obj gc.collect()
def test_objcclass_subclasscheck(self): """issubclass works with an ObjCClass as the second argument.""" NSArray = ObjCClass('NSArray') NSString = ObjCClass('NSString') self.assertTrue(issubclass(NSObject, NSObject)) self.assertTrue(issubclass(NSString, NSObject)) self.assertTrue(issubclass(NSObject.objc_class, NSObject)) self.assertTrue(issubclass(NSObject.objc_class, NSObject.objc_class)) self.assertFalse(issubclass(NSObject, NSString)) self.assertFalse(issubclass(NSArray, NSString)) with self.assertRaises(TypeError): issubclass(object(), NSObject) with self.assertRaises(TypeError): issubclass(object, NSObject) with self.assertRaises(TypeError): issubclass(NSObject.new(), NSObject) with self.assertRaises(TypeError): issubclass(NSObjectProtocol, NSObject)
def test_objcprotocol_subclasscheck(self): """issubclass works with an ObjCProtocol as the second argument.""" NSString = ObjCClass('NSString') NSCopying = ObjCProtocol('NSCopying') NSCoding = ObjCProtocol('NSCoding') NSSecureCoding = ObjCProtocol('NSSecureCoding') self.assertTrue(issubclass(NSObject, NSObjectProtocol)) self.assertTrue(issubclass(NSString, NSObjectProtocol)) self.assertTrue(issubclass(NSSecureCoding, NSSecureCoding)) self.assertTrue(issubclass(NSSecureCoding, NSCoding)) self.assertFalse(issubclass(NSObject, NSSecureCoding)) self.assertFalse(issubclass(NSCoding, NSSecureCoding)) self.assertFalse(issubclass(NSCopying, NSSecureCoding)) with self.assertRaises(TypeError): issubclass(object(), NSSecureCoding) with self.assertRaises(TypeError): issubclass(object, NSSecureCoding) with self.assertRaises(TypeError): issubclass(NSObject.new(), NSSecureCoding)