def test_protocol_caching(self): """ObjCProtocol instances are cached.""" ExampleProtocol1 = ObjCProtocol('ExampleProtocol') ExampleProtocol2 = ObjCProtocol('ExampleProtocol') self.assertIs(ExampleProtocol1, ExampleProtocol2)
def test_objcprotocol_protocols(self): """An ObjCProtocol's protocols can be looked up.""" DerivedProtocol = ObjCProtocol('DerivedProtocol') BaseProtocolOne = ObjCProtocol('BaseProtocolOne') BaseProtocolTwo = ObjCProtocol('BaseProtocolTwo') self.assertEqual(DerivedProtocol.protocols, (BaseProtocolOne, BaseProtocolTwo))
def test_objcclass_protocols(self): """An ObjCClass's protocols can be looked up.""" BaseExample = ObjCClass('BaseExample') ExampleProtocol = ObjCProtocol('ExampleProtocol') DerivedProtocol = ObjCProtocol('DerivedProtocol') self.assertEqual(BaseExample.protocols, (ExampleProtocol, DerivedProtocol))
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_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_objcinstance_can_produce_objcprotocol(self): """Creating an ObjCInstance for a protocol pointer gives an ObjCProtocol.""" example_protocol_ptr = libobjc.objc_getProtocol(b'ExampleProtocol') ExampleProtocol = ObjCInstance(example_protocol_ptr) self.assertEqual(ExampleProtocol, ObjCProtocol('ExampleProtocol')) self.assertIsInstance(ExampleProtocol, ObjCProtocol)
def test_protocol_def_extends(self): """An ObjCProtocol that extends other protocols can be defined.""" ExampleProtocol = ObjCProtocol('ExampleProtocol') class ProtocolExtendsProtocols(NSObjectProtocol, ExampleProtocol): pass self.assertSequenceEqual(ProtocolExtendsProtocols.protocols, [NSObjectProtocol, ExampleProtocol])
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)
def test_interface(self): "An ObjC protocol implementation can be defined in Python." Callback = ObjCProtocol('Callback') results = {} class Handler(NSObject, protocols=[Callback]): @objc_method def initWithValue_(self, value: int): self.value = value return self @objc_method def peek_withValue_(self, example, value: int) -> None: results['string'] = example.toString() + " peeked" results['int'] = value + self.value @objc_method def poke_withValue_(self, example, value: int) -> None: results['string'] = example.toString() + " poked" results['int'] = value + self.value @objc_method def reverse_(self, input): return ''.join(reversed(input)) @objc_method def message(self): return "Alea iacta est." @objc_classmethod def fiddle_(cls, value: int) -> None: results['string'] = "Fiddled with it" results['int'] = value # Check that the protocol is adopted. self.assertSequenceEqual(Handler.protocols, (Callback, )) # Create two handler instances so we can check the right one # is being invoked. handler1 = Handler.alloc().initWithValue_(5) handler2 = Handler.alloc().initWithValue_(10) # Create an Example object, and register a handler with it. Example = ObjCClass('Example') example = Example.alloc().init() example.callback = handler2 # Check some Python-side attributes self.assertEqual(handler1.value, 5) self.assertEqual(handler2.value, 10) # Invoke the callback; check that the results have been peeked as expected example.testPeek_(42) self.assertEqual(results['string'], 'This is an ObjC Example object peeked') self.assertEqual(results['int'], 52) example.testPoke_(37) self.assertEqual(results['string'], 'This is an ObjC Example object poked') self.assertEqual(results['int'], 47) self.assertEqual(example.getMessage(), 'Alea iacta est.') self.assertEqual(example.reverseIt_('Alea iacta est.'), '.tse atcai aelA') Handler.fiddle_(99) self.assertEqual(results['string'], 'Fiddled with it') self.assertEqual(results['int'], 99)
def test_nonexistant_protocol(self): """A NameError is raised if a protocol doesn't exist.""" with self.assertRaises(NameError): ObjCProtocol('DoesNotExist')
def test_protocol_by_pointer(self): """An Objective-C protocol can be created from a pointer.""" example_protocol_ptr = libobjc.objc_getProtocol(b'ExampleProtocol') ExampleProtocol = ObjCProtocol(example_protocol_ptr) self.assertEqual(ExampleProtocol, ObjCProtocol('ExampleProtocol'))
def test_protocol_by_name(self): """An Objective-C protocol can be looked up by name.""" ExampleProtocol = ObjCProtocol('ExampleProtocol') self.assertEqual(ExampleProtocol.name, 'ExampleProtocol')