1. Ronald Oussoren
  2. pyobjc

Commits

Ronald Oussoren  committed 8666b19

- Implement some unittests
- Raise TypeError when inheriting from something that is not a Protocol
(instead of objc.error)

  • Participants
  • Parent commits 9722d40
  • Branches pyobjc-ancient

Comments (0)

Files changed (2)

File Lib/objc/test/test_protocol.py

View file
     objc.selector(None, selector="testMethod2:", signature="v@:i", isRequired=0)
 ))
 
-class TestProtocols(unittest.TestCase):
+class TestInformalProtocols(unittest.TestCase):
 
 
     def testMissingProto(self):
             def testMethod(self):
                 pass
 
+class TestFormalProtocols (unittest.TestCase):
+    # Implement unittests for formal protocols here.
+    #
+
+    def testImplementFormalProtocol(self):
+
+        NSLocking = objc.protocolNamed('NSLocking')
+
+        class MyClassNotImplementingLocking(NSObject):
+            pass
+
+        self.assert_(not MyClassNotImplementingLocking.pyobjc_classMethods.conformsToProtocol_(NSLocking))
+
+        class MyClassImplementingLocking(NSObject, NSLocking):
+            pass
+
+        self.assert_(MyClassImplementingLocking.pyobjc_classMethods.conformsToProtocol_(NSLocking))
+
+    def testImplementAnotherObject(self):
+        anObject = NSObject.alloc().init()
+
+        try:
+            class MyClassImplementingAnotherObject(NSObject, anObject):
+                    pass
+            self.fail()
+        except TypeError: 
+            pass
+
+        try:
+            class MyClassImplementingAnotherObject(NSObject, 10):
+                    pass
+            self.fail()
+        except TypeError: 
+            pass
+
+        try:
+            class MyClassImplementingAnotherObject(NSObject, int):
+                    pass
+            self.fail()
+        except TypeError: 
+            pass
+
 
 if __name__ == '__main__':
     unittest.main()

File Modules/objc/class-builder.m

View file
 				continue;
 			}
 			if (!PyObjCObject_Check(wrapped_protocol)) {
-				PyErr_Format(PyObjCExc_Error,
+				PyErr_Format(PyExc_TypeError,
 					"protocol must be a 'Protocol', not an '%s'",
 					wrapped_protocol->ob_type->tp_name);
 				goto error_cleanup;
 			}
 			protocol = (Protocol *)PyObjCObject_GetObject(wrapped_protocol);
 			if (!PyObjCClass_IsSubClass(GETISA((id)protocol), [Protocol class])) {
-				PyErr_Format(PyObjCExc_Error,
+				PyErr_Format(PyExc_TypeError,
 					"protocol must be a 'Protocol', not an '%s'",
 					wrapped_protocol->ob_type->tp_name);
 				goto error_cleanup;