forked from parse-community/Parse-SDK-iOS-OSX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathObjectSubclassPropertiesTests.m
308 lines (246 loc) · 9.91 KB
/
ObjectSubclassPropertiesTests.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/**
* Copyright (c) 2015-present, Parse, LLC.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
#import "PFObject+Subclass.h"
#import "PFObjectPrivate.h"
#import "PFRelation.h"
#import "PFSubclassing.h"
#import "PFUnitTestCase.h"
#import "Parse_Private.h"
///--------------------------------------
#pragma mark - Helpers
///--------------------------------------
// A test class used to verify that dynamic sythesizers work for properties of the
// object and ivars with copy and retain semantics. Properties that are NSNumbers
// should also support accessors and mutators which automatically unbox the value into
// its corresponding primitive.
@interface PFTestObject : PFObject<PFSubclassing> {
@public
id ivarId;
int ivarInt;
bool ivarCXXBool;
unsigned short ivarShort;
BOOL usedNativeAccessor;
BOOL usedNativeMutator;
}
@property (atomic, retain) id idProperty;
@property (atomic, copy) NSString *stringCopyProperty;
@property (atomic, assign) short shortProperty;
@property (atomic, assign) unsigned short ushortProperty;
@property (atomic, assign) int intProperty;
@property (atomic, assign) uint uintProperty;
@property (atomic, assign) long longProperty;
@property (atomic, assign) unsigned long ulongProperty;
@property (atomic, assign) BOOL boolProperty;
@property (atomic, retain) id ivarId;
@property (atomic, assign) int ivarInt;
@property (atomic, assign) unsigned short ivarShort;
@property (atomic, copy) NSString *stringWithNativeAccessor;
@property (atomic, copy) NSString *stringWithNativeMutator;
@property (atomic, assign) float floatProperty;
@property (atomic, assign) double doubleProperty;
@property (atomic, assign) int x;
@property (atomic, assign) int PascalCaseProperty;
@property (atomic, assign) bool ivarCXXBool;
@property (atomic, assign) bool cxxBool;
@property (atomic, strong) PFRelation *relation;
@end
@implementation PFTestObject
@dynamic idProperty;
@dynamic stringCopyProperty;
@dynamic shortProperty;
@dynamic ushortProperty;
@dynamic intProperty;
@dynamic uintProperty;
@dynamic longProperty;
@dynamic ulongProperty;
@dynamic boolProperty;
@dynamic ivarId;
@dynamic ivarInt;
@dynamic ivarShort;
@dynamic stringWithNativeAccessor;
@dynamic stringWithNativeMutator;
@dynamic floatProperty;
@dynamic doubleProperty;
@dynamic x;
@dynamic PascalCaseProperty;
@dynamic cxxBool;
@dynamic ivarCXXBool;
@dynamic relation;
+ (NSString *)parseClassName {
return @"Test";
}
- (NSString *)stringWithNativeAccessor {
usedNativeAccessor = YES;
return self[@"stringWithNativeAccessor"];
}
- (void)setStringWithNativeMutator:(NSString *)aString {
usedNativeMutator = YES;
self[@"stringWithNativeMutator"] = aString;
}
@end
///--------------------------------------
#pragma mark - ObjectSubclassPropertiesTests
///--------------------------------------
@interface ObjectSubclassPropertiesTests : PFUnitTestCase
@end
@implementation ObjectSubclassPropertiesTests
///--------------------------------------
#pragma mark - Tests
///--------------------------------------
- (void)testDynamicObjectProperties {
PFTestObject *object = [[PFTestObject alloc] initWithClassName:@"Test"];
NSString *idValue = @"foo";
object.idProperty = idValue;
XCTAssertEqualObjects(@"foo", object[@"idProperty"]);
XCTAssertEqualObjects(@"foo", object.idProperty);
XCTAssertEqual(idValue, object.idProperty, @"Should use retain semantics");
}
- (void)testDynamicObjectPropertyWithCopySemantics {
PFTestObject *object = [[PFTestObject alloc] initWithClassName:@"Test"];
// Must use a mutable string so -copy isn't optimized to return self;
NSMutableString *stringValue = [NSMutableString stringWithString:@"stringValue"];
object.stringCopyProperty = stringValue;
XCTAssertEqualObjects(stringValue, object.stringCopyProperty);
XCTAssertNotEqual(stringValue, object.stringCopyProperty,
@"Should use copy semantics");
}
- (void)testBoxedIntegerProperties {
PFTestObject *object = [[PFTestObject alloc] initWithClassName:@"Test"];
// Boxed primitives
object.shortProperty = 1;
XCTAssertEqualObjects(@1, object[@"shortProperty"]);
XCTAssertEqual((short)1, object.shortProperty);
object.ushortProperty = 2;
XCTAssertEqualObjects(@2, object[@"ushortProperty"]);
XCTAssertEqual((unsigned short)2, object.ushortProperty);
object.intProperty = 3;
XCTAssertEqualObjects(@3, object[@"intProperty"]);
XCTAssertEqual((int)3, object.intProperty);
object.uintProperty = 4;
XCTAssertEqualObjects(@4, object[@"uintProperty"]);
XCTAssertEqual((unsigned int)4, object.uintProperty);
object.longProperty = 5;
XCTAssertEqualObjects(@5, object[@"longProperty"]);
XCTAssertEqual((long)5, object.longProperty);
object.ulongProperty = 6;
XCTAssertEqualObjects(@6, object[@"ulongProperty"]);
XCTAssertEqual((unsigned long)6, object.ulongProperty);
}
- (void)testBoxedFloatingPointProperties {
PFTestObject *object = [[PFTestObject alloc] initWithClassName:@"Test"];
object.floatProperty = 1.5;
XCTAssertEqualObjects(@1.5f, object[@"floatProperty"]);
XCTAssertEqual(1.5f, object.floatProperty);
object.doubleProperty = 1.75;
XCTAssertEqualObjects(@1.75, object[@"doubleProperty"]);
XCTAssertEqual(1.75, object.doubleProperty);
}
- (void)testBoxedBooleanProperties {
PFTestObject *object = [[PFTestObject alloc] initWithClassName:@"Test"];
object.boolProperty = YES;
XCTAssertTrue(object.boolProperty);
XCTAssertEqualObjects(@YES, object[@"boolProperty"]);
object.boolProperty = NO;
// HOORAY! Boxing makes if statements work like most users expect they would
XCTAssertFalse(object.boolProperty);
}
- (void)testShortNameProperties {
PFTestObject *object = [[PFTestObject alloc] initWithClassName:@"Test"];
object.x = 1;
XCTAssertEqual(object.x, 1);
XCTAssertEqualObjects(@1, object[@"x"]);
}
- (void)testPascalCaseProperties {
PFTestObject *object = [[PFTestObject alloc] initWithClassName:@"Test"];
object.PascalCaseProperty = 1;
XCTAssertEqual(object.PascalCaseProperty, 1);
XCTAssertEqualObjects(@1, object[@"PascalCaseProperty"]);
}
- (void)testIvarObjectProperties {
PFTestObject *object = [[PFTestObject alloc] initWithClassName:@"Test"];
object->ivarId = @"Hello,";
XCTAssertEqualObjects(@"Hello,", object.ivarId);
object.ivarId = @"World!";
XCTAssertEqualObjects(@"World!", object->ivarId);
XCTAssertNil(object[@"ivarId"]);
}
- (void)testIvarPrimitiveProperties {
PFTestObject *object = [[PFTestObject alloc] initWithClassName:@"Test"];
object->ivarInt = 5;
XCTAssertEqual(5, object.ivarInt);
object.ivarInt = 6;
XCTAssertEqual(6, object->ivarInt);
XCTAssertNil(object[@"ivarInt"]);
// Test something that's not a bus width
object->ivarShort = 42;
XCTAssertEqual((unsigned short)42, object.ivarShort);
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wconversion"
object.ivarShort = 0xCAFEBABE;
XCTAssertEqual((unsigned short)0xBABE, object->ivarShort);
#pragma clang diagnostic pop
XCTAssertNil(object[@"ivarShort"]);
}
- (void)testCXXBoolProperties {
PFTestObject *object = [[PFTestObject alloc] initWithClassName:@"Test"];
object.cxxBool = true;
XCTAssertTrue(object.cxxBool);
XCTAssertEqualObjects(@YES, object[@"cxxBool"]);
object.cxxBool = false;
XCTAssertFalse(object.cxxBool);
}
- (void)testCXXBoolIvarProperties {
PFTestObject *object = [[PFTestObject alloc] initWithClassName:@"Test"];
object->ivarCXXBool = true;
XCTAssertTrue(object.ivarCXXBool);
object->ivarCXXBool = false;
XCTAssertFalse(object->ivarCXXBool);
XCTAssertNil(object[@"ivarCXXBool"]);
}
- (void)testDynamicPropertiesHonorPartialImplementations {
PFTestObject *object = [[PFTestObject alloc] initWithClassName:@"Test"];
object.stringWithNativeAccessor = @"Hello, world!";
XCTAssertEqualObjects(@"Hello, world!", object.stringWithNativeAccessor);
XCTAssertTrue(object->usedNativeAccessor);
object.stringWithNativeMutator = @"Hello, world!";
XCTAssertEqualObjects(@"Hello, world!", object.stringWithNativeMutator);
XCTAssertTrue(object->usedNativeAccessor);
}
- (void)testObjectPropertiesAreRemovedWhenNilled {
PFTestObject *object = [[PFTestObject alloc] initWithClassName:@"Test"];
object.stringCopyProperty = @"Hello, world!";
XCTAssertTrue([object.allKeys containsObject:@"stringCopyProperty"]);
object.stringCopyProperty = nil;
XCTAssertFalse([object.allKeys containsObject:@"stringCopyProperty"]);
}
// I'm not so sure the mutator is a good idea, but it'd be good to ensure that at least the
// accessors don't choke. Still, it's nice not to blow up if people do crazy things with NSNull.
- (void)testObjectPropertiesDontChokeOnNSNull {
PFTestObject *object = [[PFTestObject alloc] initWithClassName:@"Test"];
object.stringCopyProperty = (NSString *)[NSNull null];
XCTAssertTrue([object.allKeys containsObject:@"stringCopyProperty"]);
XCTAssertEqual((NSString *)nil, object.stringCopyProperty);
object.stringCopyProperty = nil;
XCTAssertFalse([object.allKeys containsObject:@"stringCopyProperty"]);
}
- (void)testBoxedPropertiesDontChokeOnNSNull {
PFTestObject *object = [[PFTestObject alloc] initWithClassName:@"Test"];
object[@"intProperty"] = [NSNull null];
XCTAssertEqual(0, object.intProperty);
}
- (void)testRelationPropertiesCreateRelations {
PFTestObject *object = [PFTestObject object];
id relation = object.relation;
XCTAssertTrue([relation isKindOfClass:[PFRelation class]]);
}
- (void)testRelationPropertiesAreReadOnly {
XCTAssertThrows([PFTestObject object].relation = [[PFRelation alloc] init],
@"Relations are read-only and should not be assignable");
}
@end