1""" 2Testcases for the CoreFoundation wrappers introduced in 1.5 3""" 4import objc 5from PyObjCTools.TestSupport import * 6import re 7import sys 8 9from PyObjCTest.corefoundation import * 10 11# registerCFSignature(name, encoding, typeId [, tollfreeName]) -> type 12 13CFUUIDRef = objc.registerCFSignature( 14 "CFUUIDRef", 15 OC_TestCoreFoundation.signatureForCFUUIDRef(), 16 OC_TestCoreFoundation.typeidForCFUUIDRef(), 17 ) 18 19CFDateRef = objc.registerCFSignature( 20 "CFDateRef", 21 OC_TestCoreFoundation.signatureForCFDateRef(), 22 OC_TestCoreFoundation.typeidForCFDateRef(), 23 "NSDate", 24 ) 25 26if sys.version_info[0] == 3: 27 unicode = str 28 29class TestCoreFoundation (TestCase): 30 def testTollFree(self): 31 obj = OC_TestCoreFoundation.today() 32 33 self.assertIs(CFDateRef, objc.lookUpClass("NSDate")) 34 self.assertIsInstance(obj, CFDateRef) 35 36 v = OC_TestCoreFoundation.formatDate_(obj) 37 self.assertIsInstance(v, unicode) 38 39 formatter = objc.lookUpClass("NSDateFormatter").new() 40 formatter.setDateStyle_(OC_TestCoreFoundation.shortStyle()) 41 formatter.setTimeStyle_(OC_TestCoreFoundation.shortStyle()) 42 formatter.setLocale_(objc.lookUpClass("NSLocale").currentLocale()) 43 v2 = formatter.stringForObjectValue_(obj) 44 45 # Arggh, I'm an idiot: the code above doesn't calculate the same 46 # string as the C code in corefoundation.m. 47 #print v , v2 48 49 def testBridged(self): 50 51 obj = OC_TestCoreFoundation.createUUID() 52 53 self.assertIsInstance(obj, CFUUIDRef) 54 55 formatted = OC_TestCoreFoundation.formatUUID_(obj) 56 57 self.assertIsInstance(formatted, unicode) 58 self.assertTrue( re.match( 59 r'[A-Z0-9]{8}-[A-Z0-9]{4}-[A-Z0-9]{4}-[A-Z0-9]{4}-[A-Z0-9]{12}', 60 formatted) ) 61 62 self.assertRaises(objc.error, objc.lookUpClass, "CFUUIDRef") 63 64 65 # AnotherUUID claims to return an Object (objc._C_ID), check that 66 # we correctly return an object of the right type in that case as well. 67 obj = OC_TestCoreFoundation.anotherUUID() 68 69 self.assertIsInstance(obj, CFUUIDRef) 70 71 formatted = OC_TestCoreFoundation.formatUUID_(obj) 72 73 self.assertIsInstance(formatted, unicode) 74 self.assertTrue( re.match( 75 r'[A-Z0-9]{8}-[A-Z0-9]{4}-[A-Z0-9]{4}-[A-Z0-9]{4}-[A-Z0-9]{12}', 76 formatted) ) 77 78 # TODO: testcases that check that 79 # 1) you cannot delete selectors 80 # 2) or even add them 81 # 3) but can add/update/delete new Python methods or other attributes 82 # 83 84 def testMutableTypes(self): 85 cftype = objc.lookUpClass('NSCFType') 86 87 def myMethod(self, arg): 88 return '%s %s'%(self.__class__.__name__, arg) 89 90 self.assertNotHasAttr(CFUUIDRef, 'myMethod') 91 92 CFUUIDRef.myMethod = myMethod 93 94 self.assertHasAttr(CFUUIDRef, 'myMethod') 95 self.assertNotHasAttr(CFDateRef, 'myMethod') 96 self.assertNotHasAttr(cftype, 'myMethod') 97 98if __name__ == "__main__": 99 main() 100