-
-
Notifications
You must be signed in to change notification settings - Fork 872
/
BaseStateTests.m
311 lines (212 loc) · 9.34 KB
/
BaseStateTests.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
309
310
311
/**
* 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 Darwin.libkern.OSAtomic;
#import "PFBaseState.h"
#import "PFTestCase.h"
@interface PFTestBaseStateSubclass : PFBaseState <PFBaseStateSubclass> {
@public
__unsafe_unretained id _assignValue;
}
@property (nonatomic, readwrite, strong) id defaultValue;
@property (nonatomic, readwrite, unsafe_unretained) id assignValue;
@property (nonatomic, readwrite, weak) id weakValue;
@property (nonatomic, readwrite, strong) id strongValue;
@property (nonatomic, readwrite, copy) id copyValue NS_RETURNS_NOT_RETAINED;
@property (nonatomic, readwrite, copy) id mutableCopyValue NS_RETURNS_NOT_RETAINED;
@end
@implementation PFTestBaseStateSubclass
- (id)init {
self = [super init];
if (!self) return nil;
_defaultValue = @"Hello, World!";
return self;
}
+ (NSDictionary *)propertyAttributes {
return @{
@"defaultValue" : [PFPropertyAttributes attributesWithAssociationType:PFPropertyInfoAssociationTypeCopy],
@"assignValue" : [PFPropertyAttributes attributesWithAssociationType:PFPropertyInfoAssociationTypeAssign],
@"weakValue" : [PFPropertyAttributes attributesWithAssociationType:PFPropertyInfoAssociationTypeWeak],
@"strongValue" : [PFPropertyAttributes attributesWithAssociationType:PFPropertyInfoAssociationTypeStrong],
@"copyValue" : [PFPropertyAttributes attributesWithAssociationType:PFPropertyInfoAssociationTypeCopy],
@"mutableCopyValue" : [PFPropertyAttributes attributesWithAssociationType:PFPropertyInfoAssociationTypeMutableCopy],
};
}
- (id)nilValueForProperty:(NSString *)propertyName {
return nil;
}
@end
@interface BaseStateTests : PFTestCase
@end
@implementation BaseStateTests
///--------------------------------------
#pragma mark - Association Types
///--------------------------------------
- (void)testDefaultValues {
PFTestBaseStateSubclass *state = [[PFTestBaseStateSubclass alloc] init];
XCTAssertEqualObjects(state.defaultValue, @"Hello, World!");
state.defaultValue = @"Different String";
PFTestBaseStateSubclass *newState = [PFTestBaseStateSubclass stateWithState:state];
XCTAssertNotEqualObjects(newState.defaultValue, @"Hello, World!");
}
- (void)testAssignValue {
// Some random value. Don't treat this as an actual object!
__unsafe_unretained id theValue = (__bridge id)(void *)0xDEADBEEF;
PFTestBaseStateSubclass *state = [[PFTestBaseStateSubclass alloc] init];
state.assignValue = theValue;
PFTestBaseStateSubclass *newState = [[PFTestBaseStateSubclass alloc] initWithState:state];
// Cannot use dot-syntax here. ARC is dumb and tries to retain it anyway.
__unsafe_unretained id valueA = state->_assignValue;
__unsafe_unretained id valueB = newState->_assignValue;
XCTAssertEqual(valueA, valueB);
XCTAssertEqual(valueB, theValue);
}
- (void)testWeakValue {
PFTestBaseStateSubclass *state, *newState;
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-repeated-use-of-weak"
@autoreleasepool {
id theValue = [NSObject new];
state = [[PFTestBaseStateSubclass alloc] init];
state.weakValue = theValue;
newState = [[PFTestBaseStateSubclass alloc] initWithState:state];
XCTAssertEqual(state.weakValue, newState.weakValue);
XCTAssertEqual(state.weakValue, theValue);
theValue = nil;
}
OSMemoryBarrier();
XCTAssertEqual(state.weakValue, newState.weakValue);
XCTAssertEqual(state.weakValue, nil);
#pragma clang diagnostic pop
}
- (void)testStrongValue {
__weak id weakValue;
PFTestBaseStateSubclass *state, *newState;
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-repeated-use-of-weak"
@autoreleasepool {
id theValue = [NSObject new];
weakValue = theValue;
state = [[PFTestBaseStateSubclass alloc] init];
state.strongValue = theValue;
newState = [[PFTestBaseStateSubclass alloc] initWithState:state];
XCTAssertEqual(state.strongValue, newState.strongValue);
XCTAssertEqual(state.strongValue, theValue);
}
OSMemoryBarrier();
XCTAssertNotNil(weakValue);
XCTAssertEqual(state.strongValue, newState.strongValue);
XCTAssertEqual(state.strongValue, weakValue);
#pragma clang diagnostic pop
}
- (void)testCopyValue {
NSMutableString *originalValue = [NSMutableString stringWithFormat:@"Foo"];
PFTestBaseStateSubclass *state, *newState;
state = [[PFTestBaseStateSubclass alloc] init];
state.copyValue = originalValue;
newState = [[PFTestBaseStateSubclass alloc] initWithState:state];
XCTAssertNotEqual(state.copyValue, originalValue);
// Copying a NSMutableString gives a NSString, which when copied again returns itself.
XCTAssertEqual(state.copyValue, newState.copyValue);
XCTAssertEqualObjects(state.copyValue, originalValue);
XCTAssertEqualObjects(state.copyValue, newState.copyValue);
// Same reason as above.
XCTAssertEqual([state.copyValue copy], [state.copyValue copy]);
XCTAssertEqual([newState.copyValue copy], [newState.copyValue copy]);
}
- (void)testMutableCopyValue {
NSString *originalValue = @"Bar";
PFTestBaseStateSubclass *state, *newState;
state = [[PFTestBaseStateSubclass alloc] init];
state.mutableCopyValue = originalValue;
newState = [[PFTestBaseStateSubclass alloc] initWithState:state];
XCTAssertNotEqual(state.mutableCopyValue, newState.mutableCopyValue);
// Default copy setters won't invoke -mutableCopy.
XCTAssertEqual(originalValue, state.mutableCopyValue);
XCTAssertEqualObjects(state.mutableCopyValue, newState.mutableCopyValue);
XCTAssertEqualObjects(state.mutableCopyValue, originalValue);
XCTAssertThrows([state.mutableCopyValue appendString:@"Foo"]);
XCTAssertNoThrow([newState.mutableCopyValue appendString:@"Foo"]);
}
///--------------------------------------
#pragma mark - Description
///--------------------------------------
- (void)testDescription {
PFTestBaseStateSubclass *state = [[PFTestBaseStateSubclass alloc] init];
state.strongValue = @15;
NSString *oldDescription = [state description];
state.strongValue = @25;
NSString *newDescritption = [state description];
XCTAssertNotEqualObjects(oldDescription, newDescritption);
}
- (void)testDebugDescription {
PFTestBaseStateSubclass *state = [[PFTestBaseStateSubclass alloc] init];
state.strongValue = @[ @1, @2, @3 ];
XCTAssertNotEqualObjects([state description], [state debugDescription]);
}
///--------------------------------------
#pragma mark - Dictionary Representation
///--------------------------------------
- (void)testDictionaryRepresentation {
PFTestBaseStateSubclass *state = [[PFTestBaseStateSubclass alloc] init];
NSMutableDictionary *expected = [@{
@"defaultValue" : @"Hello, World!",
} mutableCopy];
XCTAssertEqualObjects(expected, [state dictionaryRepresentation]);
state.strongValue = @25;
expected[@"strongValue"] = @25;
XCTAssertEqualObjects(expected, [state dictionaryRepresentation]);
}
// As this is a method only used when debugging, this simple of a test case should suffice.
- (void)testDebugQuickLookObject {
PFTestBaseStateSubclass *state = [[PFTestBaseStateSubclass alloc] init];
XCTAssertEqualObjects([[state dictionaryRepresentation] description], [state debugQuickLookObject]);
}
///--------------------------------------
#pragma mark - Equality
///--------------------------------------
- (void)testEquality {
PFTestBaseStateSubclass *state1, *state2;
state1 = [[PFTestBaseStateSubclass alloc] init];
state2 = [[PFTestBaseStateSubclass alloc] init];
// Reasoning: XCTAssertEqualObjects checks for pointers for us already, so we dont' get 100% coverage without this.
XCTAssertTrue([state1 isEqual:state1]);
XCTAssertEqualObjects(state1, state2);
XCTAssertNotEqualObjects(state1, nil);
XCTAssertNotEqualObjects(state1, @"Hello, World!");
state1.strongValue = @25;
XCTAssertNotEqualObjects(state1, state2);
state2.strongValue = @25;
XCTAssertEqualObjects(state1, state2);
}
///--------------------------------------
#pragma mark - Comparison
///--------------------------------------
- (void)testCompare {
PFTestBaseStateSubclass *state1, *state2;
state1 = [[PFTestBaseStateSubclass alloc] init];
state2 = [[PFTestBaseStateSubclass alloc] init];
XCTAssertEqual([state1 compare:state2], NSOrderedSame);
state1.strongValue = @25;
state2.strongValue = @20;
XCTAssertEqual([state1 compare:state2], NSOrderedDescending);
XCTAssertEqual([state2 compare:state1], NSOrderedAscending);
state2.strongValue = @30;
XCTAssertEqual([state1 compare:state2], NSOrderedAscending);
XCTAssertEqual([state2 compare:state1], NSOrderedDescending);
}
///--------------------------------------
#pragma mark - Hashing
///--------------------------------------
- (void)testHash {
PFTestBaseStateSubclass *state = [[PFTestBaseStateSubclass alloc] init];
NSUInteger oldHash = [state hash];
state.strongValue = @25;
XCTAssertNotEqual(oldHash, [state hash]);
}
@end