4 // Test basic +initialize behavior
5 // * +initialize before class method
6 // * superclass +initialize before subclass +initialize
7 // * subclass inheritance of superclass implementation
8 // * messaging during +initialize
13 @interface Super0 { } @end
14 @implementation Super0
16 fail("objc_getClass() must not trigger +initialize");
20 @interface Super {} @end
23 testprintf("in [Super initialize]\n");
24 testassert(state == 0);
28 fail("[Super method] shouldn't be called");
32 @interface Sub : Super { } @end
35 testprintf("in [Sub initialize]\n");
36 testassert(state == 1);
40 testprintf("in [Sub method]\n");
41 testassert(state == 2);
47 @interface Super2 { } @end
48 @interface Sub2 : Super2 { } @end
50 @implementation Super2
51 +(id)class { return self; }
53 if (self == objc_getClass("Sub2")) {
54 testprintf("in [Super2 initialize] of Sub2\n");
55 testassert(state == 1);
57 } else if (self == objc_getClass("Super2")) {
58 testprintf("in [Super2 initialize] of Super2\n");
59 testassert(state == 0);
62 fail("in [Super2 initialize] of unknown class");
66 testprintf("in [Super2 method]\n");
67 testassert(state == 2);
77 @interface Super3 { } @end
78 @interface Sub3 : Super3 { } @end
80 @implementation Super3
81 +(id)class { return self; }
83 if (self == [Sub3 class]) { // this message triggers [Sub3 initialize]
84 testprintf("in [Super3 initialize] of Sub3\n");
85 testassert(state == 0);
87 } else if (self == [Super3 class]) {
88 testprintf("in [Super3 initialize] of Super3\n");
89 testassert(state == 1);
92 fail("in [Super3 initialize] of unknown class");
96 testprintf("in [Super3 method]\n");
97 testassert(state == 2);
108 // objc_getClass() must not +initialize anything
110 objc_getClass("Super0");
111 testassert(state == 0);
113 // initialize superclass, then subclass
116 testassert(state == 3);
118 // check subclass's inheritance of superclass initialize
121 testassert(state == 3);
123 // check subclass method called from superclass initialize
126 testassert(state == 3);