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