Skip to content

Commit 97e6158

Browse files
authored
Fixes self references in blocks in tests (parse-community#1267)
1 parent 0be1a78 commit 97e6158

File tree

6 files changed

+38
-27
lines changed

6 files changed

+38
-27
lines changed

Parse/Tests/Other/Cache/TestCache.m

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,27 +31,27 @@ - (instancetype)init {
3131
- (id)objectForKey:(id)key {
3232
__block id results = nil;
3333
dispatch_sync(_queue, ^{
34-
results = _cache[key];
34+
results = self->_cache[key];
3535
});
3636

3737
return results;
3838
}
3939

4040
- (void)setObject:(id)object forKey:(id)aKey {
4141
dispatch_sync(_queue, ^{
42-
_cache[aKey] = object;
42+
self->_cache[aKey] = object;
4343
});
4444
}
4545

4646
- (void)removeObjectForKey:(id)aKey {
4747
dispatch_sync(_queue, ^{
48-
[_cache removeObjectForKey:aKey];
48+
[self->_cache removeObjectForKey:aKey];
4949
});
5050
}
5151

5252
- (void)removeAllObjects {
5353
dispatch_sync(_queue, ^{
54-
[_cache removeAllObjects];
54+
[self->_cache removeAllObjects];
5555
});
5656
}
5757

Parse/Tests/Other/FileManager/TestFileManager.m

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ - (instancetype)init {
4949
- (NSData *)contentsAtPath:(NSString *)path {
5050
__block NSData *results = nil;
5151
dispatch_sync(_queue, ^{
52-
results = _fileContents[path];
52+
results = self->_fileContents[path];
5353
});
5454

5555
return results;
@@ -58,22 +58,22 @@ - (NSData *)contentsAtPath:(NSString *)path {
5858
- (BOOL)createFileAtPath:(NSString *)path contents:(NSData *)data attributes:(NSDictionary *)attributes {
5959
dispatch_sync(_queue, ^{
6060
NSMutableDictionary *newAttributes = [NSMutableDictionary dictionaryWithDictionary:attributes];
61-
NSDate *date = [NSDate dateWithTimeIntervalSince1970:_lastReturnedTime++];
61+
NSDate *date = [NSDate dateWithTimeIntervalSince1970:self->_lastReturnedTime++];
6262

6363
newAttributes[NSFileModificationDate] = newAttributes[NSURLContentModificationDateKey] = date;
6464
newAttributes[NSFileSize] = @([data length]);
6565

66-
_fileContents[path] = data;
67-
[_fileAttributes setObject:newAttributes forKey:path];
66+
self->_fileContents[path] = data;
67+
[self->_fileAttributes setObject:newAttributes forKey:path];
6868

6969
});
7070
return YES;
7171
}
7272

7373
- (BOOL)removeItemAtURL:(NSURL *)URL error:(NSError **)error {
7474
dispatch_sync(_queue, ^{
75-
[_fileContents removeObjectForKey:URL.path];
76-
[_fileAttributes removeObjectForKey:URL.path];
75+
[self->_fileContents removeObjectForKey:URL.path];
76+
[self->_fileAttributes removeObjectForKey:URL.path];
7777
});
7878

7979
return YES;
@@ -94,7 +94,7 @@ - (NSDirectoryEnumerator *)enumeratorAtPath:(NSString *)path {
9494
- (NSDictionary *)attributesOfItemAtPath:(NSString *)path error:(NSError **)error {
9595
__block NSDictionary *results = nil;
9696
dispatch_sync(_queue, ^{
97-
results = [_fileAttributes[path] copy];
97+
results = [self->_fileAttributes[path] copy];
9898
});
9999

100100
return results;
@@ -103,11 +103,11 @@ - (NSDictionary *)attributesOfItemAtPath:(NSString *)path error:(NSError **)erro
103103
- (BOOL)setAttributes:(NSDictionary *)attributes ofItemAtPath:(NSString *)path error:(NSError **)error {
104104
dispatch_sync(_queue, ^{
105105
NSMutableDictionary *newAttributes = [NSMutableDictionary dictionaryWithDictionary:attributes];
106-
NSDate *date = [NSDate dateWithTimeIntervalSince1970:_lastReturnedTime++];
106+
NSDate *date = [NSDate dateWithTimeIntervalSince1970:self->_lastReturnedTime++];
107107

108108
newAttributes[NSFileModificationDate] = newAttributes[NSURLContentModificationDateKey] = date;
109109

110-
[_fileAttributes[path] addEntriesFromDictionary:newAttributes];
110+
[self->_fileAttributes[path] addEntriesFromDictionary:newAttributes];
111111
});
112112

113113
return YES;
@@ -131,7 +131,7 @@ - (instancetype)initWithFileManager:(TestFileManager *)manager path:(NSString *)
131131
_path = [path copy];
132132

133133
dispatch_sync(_manager->_queue, ^{
134-
_enumerator = [manager->_fileContents keyEnumerator];
134+
self->_enumerator = [manager->_fileContents keyEnumerator];
135135
});
136136

137137
return self;
@@ -140,24 +140,24 @@ - (instancetype)initWithFileManager:(TestFileManager *)manager path:(NSString *)
140140
- (NSDictionary *)fileAttributes {
141141
__block NSDictionary *results = nil;
142142
dispatch_sync(_manager->_queue, ^{
143-
results = [_manager->_fileAttributes[_currentPath] copy];
143+
results = [self->_manager->_fileAttributes[self->_currentPath] copy];
144144
});
145145

146146
return results;
147147
}
148148

149149
- (id)nextObject {
150150
dispatch_sync(_manager->_queue, ^{
151-
_currentPath = nil;
151+
self->_currentPath = nil;
152152
while (true) {
153-
if ([_currentPath hasPrefix:_path]) break;
154-
_currentPath = [_enumerator nextObject];
153+
if ([self->_currentPath hasPrefix:self->_path]) break;
154+
self->_currentPath = [self->_enumerator nextObject];
155155

156-
if (!_currentPath) break;
156+
if (!self->_currentPath) break;
157157
}
158158
});
159159

160-
return [_currentPath lastPathComponent];
160+
return [self->_currentPath lastPathComponent];
161161
}
162162

163163
- (void)skipDescendants {

Parse/Tests/Other/StoreKitMocking/PFTestSKPaymentQueue.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ - (void)addPayment:(SKPayment *)payment {
5858
withError:nil
5959
inState:SKPaymentTransactionStatePurchased];
6060
dispatch_async(dispatch_get_main_queue(), ^{
61-
for (NSValue *value in _observers) {
61+
for (NSValue *value in self->_observers) {
6262
id observer = [value nonretainedObjectValue];
6363
if (observer) {
6464
[observer paymentQueue:self updatedTransactions:@[ transaction ]];

Parse/Tests/Other/StoreKitMocking/PFTestSKProductsRequest.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,11 @@ - (void)start {
4646
dispatch_async(dispatch_get_main_queue(), ^{
4747
NSPredicate *filterPredicate = [NSPredicate predicateWithBlock:^BOOL(SKProduct *evaluatedObject,
4848
NSDictionary *bindings) {
49-
return [_productIdentifiers containsObject:evaluatedObject.productIdentifier];
49+
return [self->_productIdentifiers containsObject:evaluatedObject.productIdentifier];
5050
}];
5151
NSSet *validProducts = [_validProducts filteredSetUsingPredicate:filterPredicate];
5252

53-
NSMutableSet *invalidProductIdentifiers = [_productIdentifiers mutableCopy];
53+
NSMutableSet *invalidProductIdentifiers = [self->_productIdentifiers mutableCopy];
5454
[invalidProductIdentifiers minusSet:[_validProducts valueForKey:@"productIdentifier"]];
5555

5656
PFTestSKProductsResponse *response = [[PFTestSKProductsResponse alloc] initWithProducts:[validProducts allObjects]

Parse/Tests/Other/TestCases/TestCase/PFTestCase.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ - (void)setUp {
8989

9090
- (void)tearDown {
9191
dispatch_sync(_mockQueue, ^{
92-
[_mocks makeObjectsPerformSelector:@selector(stopMocking)];
92+
[self->_mocks makeObjectsPerformSelector:@selector(stopMocking)];
9393
});
9494

9595
_mocks = nil;
@@ -177,7 +177,7 @@ - (void)assertDirectory:(NSString *)directoryPath hasContents:(NSDictionary *)ex
177177

178178
- (void)registerMockObject:(id)mockObject {
179179
dispatch_sync(_mockQueue, ^{
180-
[_mocks addObject:mockObject];
180+
[self->_mocks addObject:mockObject];
181181
});
182182
}
183183

Parse/Tests/Unit/SQLiteDatabaseTest.m

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ - (void)tearDown {
4646
BOOL isOpen = [task.result boolValue];
4747

4848
if (isOpen) {
49-
return [database closeAsync];
49+
return [self->database closeAsync];
5050
}
5151
return task;
5252
}] waitUntilFinished];
@@ -63,6 +63,7 @@ - (void)tearDown {
6363

6464
// Should return BFTask to not waste `waitUntilFinished`
6565
- (BFTask *)createDatabaseAsync {
66+
PFSQLiteDatabase *database = self->database;
6667
// Drop existing database first if any.
6768
return [[[[database openAsync] continueWithBlock:^id(BFTask *task) {
6869
return [database executeSQLAsync:@"DROP TABLE test" withArgumentsInArray:nil];
@@ -79,6 +80,7 @@ - (BFTask *)createDatabaseAsync {
7980
///--------------------------------------
8081

8182
- (void)testOpen {
83+
PFSQLiteDatabase *database = self->database;
8284
[[[[[[[[[database openAsync] continueWithBlock:^id(BFTask *task) {
8385
return [database isOpenAsync];
8486
}] continueWithBlock:^id(BFTask *task) {
@@ -104,6 +106,7 @@ - (void)testOpen {
104106
}
105107

106108
- (void)testCRUD {
109+
PFSQLiteDatabase *database = self->database;
107110
[[[[[[[[[[[[database openAsync] continueWithBlock:^id(BFTask *task) {
108111
return [database executeSQLAsync:@"CREATE TABLE test (a text, b text, c integer, d double)"
109112
withArgumentsInArray:nil];
@@ -185,6 +188,7 @@ - (void)testCRUD {
185188

186189
// TODO (hallucinogen): this test consists of three units which can be separated.
187190
- (void)testTransaction {
191+
PFSQLiteDatabase *database = self->database;
188192
[[[[[[[[[[[[[[[[[self createDatabaseAsync] continueWithBlock:^id(BFTask *task) {
189193
return [database beginTransactionAsync];
190194
}] continueWithBlock:^id(BFTask *task) {
@@ -281,7 +285,8 @@ - (void)testTransaction {
281285
return [BFTask taskWithResult:nil];
282286
}] waitUntilFinished];
283287

284-
database = [PFSQLiteDatabase databaseWithPath:[self databasePath]];
288+
self->database = [PFSQLiteDatabase databaseWithPath:[self databasePath]];
289+
database = self->database;
285290
[[[[[[[database openAsync] continueWithBlock:^id(BFTask *task) {
286291
XCTAssertNil(task.error);
287292
return [database executeSQLAsync:@"INSERT INTO test (a, b, c, d) VALUES (?, ?, ?, ?)"
@@ -322,6 +327,7 @@ - (void)testTransaction {
322327
}
323328

324329
- (void)testOperationOnNonExistentTable {
330+
PFSQLiteDatabase *database = self->database;
325331
[[[[[[[self createDatabaseAsync] continueWithBlock:^id(BFTask *task) {
326332
return [database executeSQLAsync:@"INSERT INTO testFake (a, b, c, d) VALUES (?, ?, ?, ?)"
327333
withArgumentsInArray:@[ @"one", @"two", @3, @4.4 ]];
@@ -351,6 +357,7 @@ - (void)testOperationOnNonExistentTable {
351357
}
352358

353359
- (void)testQuery {
360+
PFSQLiteDatabase *database = self->database;
354361
[[[[[[[[[self createDatabaseAsync] continueWithBlock:^id(BFTask *task) {
355362
return [database executeSQLAsync:@"INSERT INTO test (a, b, c, d) VALUES (?, ?, ?, ?)"
356363
withArgumentsInArray:@[ @"one", @"two", @3, @4.4 ]];
@@ -413,6 +420,7 @@ - (void)testQuery {
413420
}
414421

415422
- (void)testCursorAndOperationOnDifferentThread {
423+
PFSQLiteDatabase *database = self->database;
416424
BFTask *taskWithCursor = [[[[[self createDatabaseAsync] continueWithBlock:^id(BFTask *task) {
417425
return [database executeSQLAsync:@"INSERT INTO test (a, b, c, d) VALUES (?, ?, ?, ?)"
418426
withArgumentsInArray:@[ @"one", @"two", @3, @4.4 ]];
@@ -468,6 +476,7 @@ - (void)testCursorAndOperationOnDifferentThread {
468476
}
469477

470478
- (void)testInvalidArgumentCount {
479+
PFSQLiteDatabase *database = self->database;
471480
[[[[self createDatabaseAsync] continueWithBlock:^id(BFTask *task) {
472481
return [database executeSQLAsync:@"INSERT INTO test (a, b, c, d) VALUES (?, ?, ?)"
473482
withArgumentsInArray:@[ @"one", @"two", @3, @4.4 ]];
@@ -479,6 +488,7 @@ - (void)testInvalidArgumentCount {
479488
}
480489

481490
- (void)testInvalidSQL {
491+
PFSQLiteDatabase *database = self->database;
482492
[[[[[self createDatabaseAsync] continueWithBlock:^id(BFTask *task) {
483493
return [database executeSQLAsync:@"INSERT INTO test (a, b, c, d) VALUES (?, ?, ?, ?)"
484494
withArgumentsInArray:@[ @"one", @"two", @3, @4.4 ]];
@@ -492,6 +502,7 @@ - (void)testInvalidSQL {
492502
}
493503

494504
- (void)testColumnTypes {
505+
PFSQLiteDatabase *database = self->database;
495506
[[[[[self createDatabaseAsync] continueWithBlock:^id(BFTask *task) {
496507
return [database executeSQLAsync:@"INSERT INTO test (a, b, c, d) VALUES (?, ?, ?, ?)"
497508
withArgumentsInArray:@[ @1, [NSNull null], @"string", @13.37 ]];

0 commit comments

Comments
 (0)