This repository has been archived by the owner on Dec 5, 2019. It is now read-only.
/
EXTRuntimeExtensionsTest.m
186 lines (136 loc) · 7.43 KB
/
EXTRuntimeExtensionsTest.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
//
// EXTRuntimeExtensionsTest.m
// extobjc
//
// Created by Justin Spahr-Summers on 2011-03-06.
// Copyright (C) 2012 Justin Spahr-Summers.
// Released under the MIT license.
//
#import "EXTRuntimeExtensionsTest.h"
@interface RuntimeTestClass : NSObject {
}
@property (nonatomic, assign, getter = isNormalBool, readonly) BOOL normalBool;
@property (nonatomic, strong, getter = whoopsWhatArray, setter = setThatArray:) NSArray *array;
@property (copy) NSString *normalString;
@property (unsafe_unretained) id untypedObject;
@property (nonatomic, weak) NSObject *weakObject;
@end
@implementation RuntimeTestClass
@synthesize normalBool = _normalBool;
@synthesize array = m_array;
@synthesize normalString;
- (NSObject *)weakObject {
return nil;
}
- (void)setWeakObject:(NSObject *)weakObject {
}
@dynamic untypedObject;
@end
@implementation EXTRuntimeExtensionsTest
- (void)testPropertyAttributesForBOOL {
objc_property_t property = class_getProperty([RuntimeTestClass class], "normalBool");
NSLog(@"property attributes: %s", property_getAttributes(property));
ext_propertyAttributes *attributes = ext_copyPropertyAttributes(property);
STAssertTrue(attributes != NULL, @"could not get property attributes");
STAssertEquals(attributes->readonly, YES, @"");
STAssertEquals(attributes->nonatomic, YES, @"");
STAssertEquals(attributes->weak, NO, @"");
STAssertEquals(attributes->canBeCollected, NO, @"");
STAssertEquals(attributes->dynamic, NO, @"");
STAssertEquals(attributes->memoryManagementPolicy, ext_propertyMemoryManagementPolicyAssign, @"");
STAssertEquals(attributes->getter, @selector(isNormalBool), @"");
STAssertEquals(attributes->setter, @selector(setNormalBool:), @"");
STAssertTrue(strcmp(attributes->ivar, "_normalBool") == 0, @"expected property ivar name to be '_normalBool'");
STAssertTrue(strlen(attributes->type) > 0, @"property type is missing from attributes");
NSUInteger size = 0;
NSGetSizeAndAlignment(attributes->type, &size, NULL);
STAssertTrue(size > 0, @"invalid property type %s, has no size", attributes->type);
STAssertNil(attributes->objectClass, @"");
free(attributes);
}
- (void)testPropertyAttributesForArray {
objc_property_t property = class_getProperty([RuntimeTestClass class], "array");
NSLog(@"property attributes: %s", property_getAttributes(property));
ext_propertyAttributes *attributes = ext_copyPropertyAttributes(property);
STAssertTrue(attributes != NULL, @"could not get property attributes");
STAssertEquals(attributes->readonly, NO, @"");
STAssertEquals(attributes->nonatomic, YES, @"");
STAssertEquals(attributes->weak, NO, @"");
STAssertEquals(attributes->canBeCollected, NO, @"");
STAssertEquals(attributes->dynamic, NO, @"");
STAssertEquals(attributes->memoryManagementPolicy, ext_propertyMemoryManagementPolicyRetain, @"");
STAssertEquals(attributes->getter, @selector(whoopsWhatArray), @"");
STAssertEquals(attributes->setter, @selector(setThatArray:), @"");
STAssertTrue(strcmp(attributes->ivar, "m_array") == 0, @"expected property ivar name to be 'm_array'");
STAssertTrue(strlen(attributes->type) > 0, @"property type is missing from attributes");
NSUInteger size = 0;
NSGetSizeAndAlignment(attributes->type, &size, NULL);
STAssertTrue(size > 0, @"invalid property type %s, has no size", attributes->type);
STAssertEqualObjects(attributes->objectClass, [NSArray class], @"");
free(attributes);
}
- (void)testPropertyAttributesForNormalString {
objc_property_t property = class_getProperty([RuntimeTestClass class], "normalString");
NSLog(@"property attributes: %s", property_getAttributes(property));
ext_propertyAttributes *attributes = ext_copyPropertyAttributes(property);
STAssertTrue(attributes != NULL, @"could not get property attributes");
STAssertEquals(attributes->readonly, NO, @"");
STAssertEquals(attributes->nonatomic, NO, @"");
STAssertEquals(attributes->weak, NO, @"");
STAssertEquals(attributes->canBeCollected, NO, @"");
STAssertEquals(attributes->dynamic, NO, @"");
STAssertEquals(attributes->memoryManagementPolicy, ext_propertyMemoryManagementPolicyCopy, @"");
STAssertEquals(attributes->getter, @selector(normalString), @"");
STAssertEquals(attributes->setter, @selector(setNormalString:), @"");
STAssertTrue(strcmp(attributes->ivar, "normalString") == 0, @"expected property ivar name to match the name of the property");
STAssertTrue(strlen(attributes->type) > 0, @"property type is missing from attributes");
NSUInteger size = 0;
NSGetSizeAndAlignment(attributes->type, &size, NULL);
STAssertTrue(size > 0, @"invalid property type %s, has no size", attributes->type);
STAssertEqualObjects(attributes->objectClass, [NSString class], @"");
free(attributes);
}
- (void)testPropertyAttributesForUntypedObject {
objc_property_t property = class_getProperty([RuntimeTestClass class], "untypedObject");
NSLog(@"property attributes: %s", property_getAttributes(property));
ext_propertyAttributes *attributes = ext_copyPropertyAttributes(property);
STAssertTrue(attributes != NULL, @"could not get property attributes");
STAssertEquals(attributes->readonly, NO, @"");
STAssertEquals(attributes->nonatomic, NO, @"");
STAssertEquals(attributes->weak, NO, @"");
STAssertEquals(attributes->canBeCollected, NO, @"");
STAssertEquals(attributes->dynamic, YES, @"");
STAssertEquals(attributes->memoryManagementPolicy, ext_propertyMemoryManagementPolicyAssign, @"");
STAssertEquals(attributes->getter, @selector(untypedObject), @"");
STAssertEquals(attributes->setter, @selector(setUntypedObject:), @"");
STAssertTrue(attributes->ivar == NULL, @"untypedObject property should not have a backing ivar");
STAssertTrue(strlen(attributes->type) > 0, @"property type is missing from attributes");
NSUInteger size = 0;
NSGetSizeAndAlignment(attributes->type, &size, NULL);
STAssertTrue(size > 0, @"invalid property type %s, has no size", attributes->type);
// cannot get class for type 'id'
STAssertNil(attributes->objectClass, @"");
free(attributes);
}
- (void)testPropertyAttributesForWeakObject {
objc_property_t property = class_getProperty([RuntimeTestClass class], "weakObject");
NSLog(@"property attributes: %s", property_getAttributes(property));
ext_propertyAttributes *attributes = ext_copyPropertyAttributes(property);
STAssertTrue(attributes != NULL, @"could not get property attributes");
STAssertEquals(attributes->readonly, NO, @"");
STAssertEquals(attributes->nonatomic, YES, @"");
STAssertEquals(attributes->weak, YES, @"");
STAssertEquals(attributes->canBeCollected, NO, @"");
STAssertEquals(attributes->dynamic, NO, @"");
STAssertEquals(attributes->memoryManagementPolicy, ext_propertyMemoryManagementPolicyAssign, @"");
STAssertEquals(attributes->getter, @selector(weakObject), @"");
STAssertEquals(attributes->setter, @selector(setWeakObject:), @"");
STAssertTrue(attributes->ivar == NULL, @"weakObject property should not have a backing ivar");
STAssertTrue(strlen(attributes->type) > 0, @"property type is missing from attributes");
NSUInteger size = 0;
NSGetSizeAndAlignment(attributes->type, &size, NULL);
STAssertTrue(size > 0, @"invalid property type %s, has no size", attributes->type);
STAssertEqualObjects(attributes->objectClass, [NSObject class], @"");
free(attributes);
}
@end