1"""
2Tests for using special type codes in struct definitions
3
4TODO:
5* _C_UNICHAR, _C_CHAR_AS_INT, _C_CHAR_AS_TEXT
6"""
7import weakref
8from PyObjCTools.TestSupport import *
9from PyObjCTest.fnd import NSObject
10
11from PyObjCTest.specialtypecodes import *
12
13
14EmbeddedBoolStruct = objc.createStructType(
15        "EmbeddedBoolStruct",
16        b"{_EmbeddedBool=" + objc._C_INT + objc._C_NSBOOL + b"}",
17        [
18            "count",
19            "isValid"
20        ])
21
22EmbeddedBoolArrayStruct = objc.createStructType(
23        "EmbeddedBoolArrayStruct",
24        b"{_EmbeddedBoolArray=" + objc._C_INT + b"[4" + objc._C_NSBOOL + b"]}",
25        [
26            "count",
27            "valid"
28        ])
29
30
31
32class TestRecode (TestCase):
33    # Use recode to test to/from Objective-C.
34    #
35    # This has limited because in 'real life' we'd encode/decode based on a
36    # typestring from the Objective-C runtime and those don't include our
37    # special type codes.
38    #
39    # This does make sure that the basic encoding machinery works.
40    def testBoolStruct(self):
41        v = (55, True)
42        w = objc.repythonify(v, EmbeddedBoolStruct.__typestr__)
43        self.assertIsInstance(w, EmbeddedBoolStruct)
44        self.assertEqual(w.count, 55)
45        self.assertIs(w.isValid, True)
46
47    def testBoolArrayStruct(self):
48        v = (42, [0, 1, 1, 0])
49        w = objc.repythonify(v, EmbeddedBoolArrayStruct.__typestr__)
50        self.assertIsInstance(w, EmbeddedBoolArrayStruct)
51        self.assertEqual(w.count, 42)
52        self.assertIs(w.valid[0], False)
53        self.assertIs(w.valid[1], True)
54        self.assertIs(w.valid[2], True)
55        self.assertIs(w.valid[3], False)
56
57
58class TestObjectiveC (TestCase):
59    # Use an Objective-C class to test to/from Objective-C.
60    #
61    def testBoolStruct(self):
62        o = OC_TestSpecialTypeCode.alloc().init()
63
64        v = (55, True)
65        w = o.identicalEmbeddedBoolStruct_(v)
66        self.assertIsInstance(w, EmbeddedBoolStruct)
67        self.assertEqual(w.count, 55)
68        self.assertIs(w.isValid, True)
69
70    def testBoolArrayStruct(self):
71        o = OC_TestSpecialTypeCode.alloc().init()
72
73        v = (42, [0, 1, 1, 0])
74        w = o.identicalEmbeddedBoolArrayStruct_(v)
75        self.assertIsInstance(w, EmbeddedBoolArrayStruct)
76        self.assertEqual(w.count, 42)
77        self.assertIs(w.valid[0], False)
78        self.assertIs(w.valid[1], True)
79        self.assertIs(w.valid[2], True)
80        self.assertIs(w.valid[3], False)
81
82if __name__ == "__main__":
83    main()
84