]> git.saurik.com Git - apple/objc4.git/blob - test/property.m
objc4-756.2.tar.gz
[apple/objc4.git] / test / property.m
1 /*
2 TEST_BUILD_OUTPUT
3 .*property.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\](\n.* note: expanded from macro 'testassert')?
4 .*property.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\](\n.* note: expanded from macro 'testassert')?
5 .*property.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\](\n.* note: expanded from macro 'testassert')?
6 .*property.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\](\n.* note: expanded from macro 'testassert')?
7 .*property.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\](\n.* note: expanded from macro 'testassert')?
8 .*property.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\](\n.* note: expanded from macro 'testassert')?
9 END
10 */
11
12 #include "test.h"
13 #include "testroot.i"
14 #include <stdint.h>
15 #include <string.h>
16 #include <objc/objc-runtime.h>
17
18
19 @protocol SuperProto
20 @property(readonly) char superProtoProp;
21 @property(class,readonly) char superProtoProp;
22 @end
23
24 @protocol SubProto <SuperProto>
25 @property(readonly) uintptr_t subProtoProp;
26 @property(class,readonly) uintptr_t subProtoProp;
27 @property(readonly) uintptr_t subInstanceOnlyProtoProp;
28 @property(class,readonly) uintptr_t subClassOnlyProtoProp;
29 @end
30
31 @interface Super : TestRoot <SuperProto> {
32 @public
33 char superIvar;
34 }
35
36 @property(readonly) char superProp;
37 @property(class,readonly) char superProp;
38 @end
39
40 @implementation Super
41 @synthesize superProp = superIvar;
42 +(char)superProp { return 'a'; }
43
44 -(char)superProtoProp { return 'a'; }
45 +(char)superProtoProp { return 'a'; }
46 @end
47
48
49 @interface Sub : Super <SubProto> {
50 @public
51 uintptr_t subIvar;
52 }
53 @property(readonly) uintptr_t subProp;
54 @property(class,readonly) uintptr_t subProp;
55 @property(readonly) uintptr_t subInstanceOnlyProp;
56 @property(class,readonly) uintptr_t subClassOnlyProp;
57 @end
58
59 @implementation Sub
60 @synthesize subProp = subIvar;
61 +(uintptr_t)subProp { return 'a'; }
62 +(uintptr_t)subClassOnlyProp { return 'a'; }
63 -(uintptr_t)subInstanceOnlyProp { return 'a'; }
64
65 -(uintptr_t)subProtoProp { return 'a'; }
66 +(uintptr_t)subProtoProp { return 'a'; }
67 +(uintptr_t)subClassOnlyProtoProp { return 'a'; }
68 -(uintptr_t)subInstanceOnlyProtoProp { return 'a'; }
69 @end
70
71
72 void test(Class subcls)
73 {
74 objc_property_t prop;
75
76 Class supercls = class_getSuperclass(subcls);
77
78 prop = class_getProperty(subcls, "subProp");
79 testassert(prop);
80
81 prop = class_getProperty(subcls, "subProtoProp");
82 testassert(prop);
83
84 prop = class_getProperty(supercls, "superProp");
85 testassert(prop);
86 testassert(prop == class_getProperty(subcls, "superProp"));
87
88 prop = class_getProperty(supercls, "superProtoProp");
89 testassert(prop);
90 // These are distinct because Sub adopts SuperProto itself
91 // in addition to Super's adoption of SuperProto.
92 testassert(prop != class_getProperty(subcls, "superProtoProp"));
93
94 prop = class_getProperty(supercls, "subProp");
95 testassert(!prop);
96
97 prop = class_getProperty(supercls, "subProtoProp");
98 testassert(!prop);
99
100 testassert(nil == class_getProperty(nil, "foo"));
101 testassert(nil == class_getProperty(subcls, nil));
102 testassert(nil == class_getProperty(nil, nil));
103 }
104
105
106 int main()
107 {
108 Class subcls = [Sub class];
109 Class submeta = object_getClass(subcls);
110 objc_property_t prop;
111
112 // instance properties
113 test(subcls);
114
115 // class properties
116 test(submeta);
117
118 // properties must not appear on the wrong side
119 testassert(nil == class_getProperty(subcls, "subClassOnlyProp"));
120 testassert(nil == class_getProperty(submeta, "subInstanceOnlyProp"));
121 testassert(nil == class_getProperty(subcls, "subClassOnlyProtoProp"));
122 testassert(nil == class_getProperty(submeta, "subInstanceOnlyProtoProp"));
123
124 // properties with the same name on both sides are distinct
125 testassert(class_getProperty(subcls, "subProp") != class_getProperty(submeta, "subProp"));
126 testassert(class_getProperty(subcls, "superProp") != class_getProperty(submeta, "superProp"));
127 testassert(class_getProperty(subcls, "subProtoProp") != class_getProperty(submeta, "subProtoProp"));
128 testassert(class_getProperty(subcls, "superProtoProp") != class_getProperty(submeta, "superProtoProp"));
129
130 // protocol properties
131
132 prop = protocol_getProperty(@protocol(SubProto), "subProtoProp", YES, YES);
133 testassert(prop);
134
135 prop = protocol_getProperty(@protocol(SuperProto), "superProtoProp", YES, YES);
136 testassert(prop == protocol_getProperty(@protocol(SubProto), "superProtoProp", YES, YES));
137
138 prop = protocol_getProperty(@protocol(SuperProto), "subProtoProp", YES, YES);
139 testassert(!prop);
140
141 // protocol properties must not appear on the wrong side
142 testassert(nil == protocol_getProperty(@protocol(SubProto), "subClassOnlyProtoProp", YES, YES));
143 testassert(nil == protocol_getProperty(@protocol(SubProto), "subInstanceOnlyProtoProp", YES, NO));
144
145 // protocol properties with the same name on both sides are distinct
146 testassert(protocol_getProperty(@protocol(SubProto), "subProtoProp", YES, YES) != protocol_getProperty(@protocol(SubProto), "subProtoProp", YES, NO));
147 testassert(protocol_getProperty(@protocol(SubProto), "superProtoProp", YES, YES) != protocol_getProperty(@protocol(SubProto), "superProtoProp", YES, NO));
148
149 testassert(nil == protocol_getProperty(nil, "foo", YES, YES));
150 testassert(nil == protocol_getProperty(@protocol(SuperProto), nil, YES, YES));
151 testassert(nil == protocol_getProperty(nil, nil, YES, YES));
152 testassert(nil == protocol_getProperty(@protocol(SuperProto), "superProtoProp", NO, YES));
153 testassert(nil == protocol_getProperty(@protocol(SuperProto), "superProtoProp", NO, NO));
154
155 succeed(__FILE__);
156 return 0;
157 }