Skip to content

Commit 94fd39e

Browse files
committed
Add tests for dispatchError:result:callback:onQueue:
1 parent 302fa39 commit 94fd39e

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

Sources/SPTPersistentCache+Private.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ NS_ASSUME_NONNULL_BEGIN
5858
callback:(SPTPersistentCacheResponseCallback _Nullable)callback
5959
onQueue:(dispatch_queue_t _Nullable)queue;
6060

61+
- (void)dispatchError:(NSError *)error
62+
result:(SPTPersistentCacheResponseCode)result
63+
callback:(SPTPersistentCacheResponseCallback _Nullable)callback
64+
onQueue:(dispatch_queue_t _Nullable)queue;
65+
6166
- (void)dispatchBlock:(dispatch_block_t)block on:(dispatch_queue_t _Nullable)queue;
6267

6368
@end

Tests/SPTPersistentCacheTests.m

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1672,6 +1672,8 @@ - (void)testStoreDataWithCallbackAndNoQueue
16721672
XCTAssertFalse(result);
16731673
}
16741674

1675+
#pragma mark Test Dispatching Empty and Error Responses
1676+
16751677
- (void)testDispatchEmptyResponseWithNilCallbackDoesNothing
16761678
{
16771679
SPTPersistentCacheForUnitTests * const cache = [self createCacheWithTimeCallback:^ NSTimeInterval(){
@@ -1708,6 +1710,50 @@ - (void)testDispatchEmptyResponse
17081710
XCTAssertTrue(cache.test_didDispatchBlock);
17091711
}
17101712

1713+
- (void)testDispatchErrorWithNilCallbackDoesNothing
1714+
{
1715+
SPTPersistentCacheForUnitTests * const cache = [self createCacheWithTimeCallback:^ NSTimeInterval(){
1716+
return kTestEpochTime;
1717+
} expirationTime:SPTPersistentCacheDefaultExpirationTimeSec];
1718+
1719+
[cache dispatchError:[NSError errorWithDomain:SPTPersistentCacheErrorDomain code:0 userInfo:nil]
1720+
result:SPTPersistentCacheResponseCodeOperationError
1721+
callback:nil
1722+
onQueue:dispatch_get_main_queue()];
1723+
1724+
XCTAssertTrue(cache.test_didDispatchBlock == NO);
1725+
}
1726+
1727+
- (void)testDispatchError
1728+
{
1729+
SPTPersistentCacheForUnitTests * const cache = [self createCacheWithTimeCallback:^ NSTimeInterval(){
1730+
return kTestEpochTime;
1731+
} expirationTime:SPTPersistentCacheDefaultExpirationTimeSec];
1732+
1733+
NSError * const error = [NSError errorWithDomain:SPTPersistentCacheErrorDomain
1734+
code:SPTPersistentCacheLoadingErrorNotEnoughDataToGetHeader
1735+
userInfo:nil];
1736+
1737+
__weak XCTestExpectation * const expectation = [self expectationWithDescription:@"callback expectation"];
1738+
SPTPersistentCacheResponseCallback callback = ^(SPTPersistentCacheResponse *response){
1739+
XCTAssertNotNil(response);
1740+
XCTAssertEqualObjects(response.error, error);
1741+
XCTAssertNil(response.record);
1742+
XCTAssertEqual(response.result, SPTPersistentCacheResponseCodeOperationError);
1743+
1744+
[expectation fulfill];
1745+
};
1746+
1747+
[cache dispatchError:error
1748+
result:SPTPersistentCacheResponseCodeOperationError
1749+
callback:callback
1750+
onQueue:dispatch_get_main_queue()];
1751+
1752+
[self waitForExpectationsWithTimeout:0.5 handler:nil];
1753+
XCTAssertTrue(cache.test_didDispatchBlock);
1754+
}
1755+
1756+
17111757
#pragma mark - Internal methods
17121758

17131759
- (void)putFile:(NSString *)file

0 commit comments

Comments
 (0)