Skip to content

Commit

Permalink
Merge pull request #119 from BoltsFramework/nlutsenko.coverage
Browse files Browse the repository at this point in the history
Add more tests, remove dead code to improve code coverage.
  • Loading branch information
nlutsenko committed Aug 11, 2015
2 parents d313346 + 93ddbaf commit 5e4f650
Show file tree
Hide file tree
Showing 9 changed files with 218 additions and 92 deletions.
10 changes: 8 additions & 2 deletions Bolts.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@
81D0EE9219AFAA6F0000AE75 /* BFMeasurementEvent.h in Headers */ = {isa = PBXBuildFile; fileRef = B242FAB819A567660097ECAE /* BFMeasurementEvent.h */; settings = {ATTRIBUTES = (Public, ); }; };
81D0EE9319AFAA6F0000AE75 /* BFURL.h in Headers */ = {isa = PBXBuildFile; fileRef = 8103FA6419900A84000BAE3F /* BFURL.h */; settings = {ATTRIBUTES = (Public, ); }; };
81D0EE9419AFAA6F0000AE75 /* BFWebViewAppLinkResolver.h in Headers */ = {isa = PBXBuildFile; fileRef = 8103FA6619900A84000BAE3F /* BFWebViewAppLinkResolver.h */; settings = {ATTRIBUTES = (Public, ); }; };
81DC1A621B7A7F4000F491DC /* ExecutorTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 81DC1A611B7A7F4000F491DC /* ExecutorTests.m */; };
81DC1A631B7A7F4000F491DC /* ExecutorTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 81DC1A611B7A7F4000F491DC /* ExecutorTests.m */; };
85D5138A18E4E45800D19D87 /* AppLinkReturnToRefererViewTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 85D5138918E4E45800D19D87 /* AppLinkReturnToRefererViewTests.m */; };
8E17EC271805D0A40049E862 /* BoltsTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 8EA6BF681805CF5600337041 /* BoltsTests.m */; };
8E8C8EEA17F23D1D00E3F1C7 /* XCTest.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 8E8C8ED217F23C3B00E3F1C7 /* XCTest.framework */; };
Expand Down Expand Up @@ -150,6 +152,7 @@
8122B2881AA0E8220025C5AF /* iOS-Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; name = "iOS-Info.plist"; path = "Resources/iOS-Info.plist"; sourceTree = "<group>"; };
814916E11AD5D46600EE7C63 /* iOS.modulemap */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = "sourcecode.module-map"; name = iOS.modulemap; path = Resources/iOS.modulemap; sourceTree = "<group>"; };
81D0EE7C19AFA8260000AE75 /* UIKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = UIKit.framework; path = Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS7.1.sdk/System/Library/Frameworks/UIKit.framework; sourceTree = DEVELOPER_DIR; };
81DC1A611B7A7F4000F491DC /* ExecutorTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ExecutorTests.m; sourceTree = "<group>"; };
81F0E88D19E5CB5A00812A88 /* Mac-Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; name = "Mac-Info.plist"; path = "Resources/Mac-Info.plist"; sourceTree = "<group>"; };
8550FD2E18EE1B7A00976B4B /* en */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = en; path = en.lproj/Localizable.strings; sourceTree = "<group>"; };
85D5138918E4E45800D19D87 /* AppLinkReturnToRefererViewTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AppLinkReturnToRefererViewTests.m; sourceTree = "<group>"; };
Expand Down Expand Up @@ -298,11 +301,12 @@
8E8C8ED717F23C3B00E3F1C7 /* BoltsTests */ = {
isa = PBXGroup;
children = (
85D5138918E4E45800D19D87 /* AppLinkReturnToRefererViewTests.m */,
1EC3019018CDABCE00D06D07 /* AppLinkTests.m */,
8EA6BF681805CF5600337041 /* BoltsTests.m */,
8E9C3D1C17DE9F6500427E62 /* TaskTests.m */,
7C60AEC51ACF19F900747DD7 /* CancellationTests.m */,
85D5138918E4E45800D19D87 /* AppLinkReturnToRefererViewTests.m */,
81DC1A611B7A7F4000F491DC /* ExecutorTests.m */,
8E9C3D1C17DE9F6500427E62 /* TaskTests.m */,
8E8C8ED817F23C3B00E3F1C7 /* Supporting Files */,
);
path = BoltsTests;
Expand Down Expand Up @@ -589,6 +593,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
81DC1A621B7A7F4000F491DC /* ExecutorTests.m in Sources */,
1EC3019118CDABCE00D06D07 /* AppLinkTests.m in Sources */,
7C60AEC61ACF19F900747DD7 /* CancellationTests.m in Sources */,
85D5138A18E4E45800D19D87 /* AppLinkReturnToRefererViewTests.m in Sources */,
Expand All @@ -601,6 +606,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
81DC1A631B7A7F4000F491DC /* ExecutorTests.m in Sources */,
8E17EC271805D0A40049E862 /* BoltsTests.m in Sources */,
8E8C8F2917F241FF00E3F1C7 /* TaskTests.m in Sources */,
7C60AEC71ACF19FD00747DD7 /* CancellationTests.m in Sources */,
Expand Down
2 changes: 1 addition & 1 deletion Bolts.xcodeproj/xcshareddata/xcschemes/Bolts.xcscheme
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0630"
LastUpgradeVersion = "0640"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
2 changes: 1 addition & 1 deletion Bolts.xcodeproj/xcshareddata/xcschemes/MacBolts.xcscheme
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0630"
LastUpgradeVersion = "0640"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
28 changes: 9 additions & 19 deletions Bolts/Common/BFCancellationTokenSource.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,12 @@

#import "BFCancellationToken.h"

@interface BFCancellationTokenSource ()

@property (nonatomic, strong, readwrite) BFCancellationToken *token;
@property (atomic, assign, readwrite, getter=isCancellationRequested) BOOL cancellationRequested;
@property (atomic, assign) BOOL disposed;
@property (nonatomic, strong) NSObject *lock;

@end

@interface BFCancellationToken (BFCancellationTokenSource)

- (void)cancel;

- (void)cancelAfterDelay:(int)millis;

- (void)dispose;

- (void)throwIfDisposed;

@end
Expand All @@ -37,18 +26,19 @@ @implementation BFCancellationTokenSource

#pragma mark - Initializer

+ (instancetype)cancellationTokenSource {
return [BFCancellationTokenSource new];
}

- (instancetype)init {
if (self = [super init]) {
_token = [BFCancellationToken new];
_lock = [NSObject new];
}
self = [super init];
if (!self) return nil;

_token = [BFCancellationToken new];

return self;
}

+ (instancetype)cancellationTokenSource {
return [BFCancellationTokenSource new];
}

#pragma mark - Custom Setters/Getters

- (BOOL)isCancellationRequested {
Expand Down
10 changes: 0 additions & 10 deletions BoltsTests/AppLinkReturnToRefererViewTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,6 @@ @interface AppLinkReturnToRefererViewTests : XCTestCase

@implementation AppLinkReturnToRefererViewTests

- (void)setUp {
[super setUp];
// Put setup code here; it will be run once, before the first test case.
}

- (void)tearDown {
// Put teardown code here; it will be run once, after the last test case.
[super tearDown];
}

- (void)testInitReturnsValidView {
BFAppLinkReturnToRefererView *view = [[BFAppLinkReturnToRefererView alloc] init];

Expand Down
8 changes: 0 additions & 8 deletions BoltsTests/BoltsTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,6 @@ @interface BoltsTests : XCTestCase

@implementation BoltsTests

- (void)setUp {
[super setUp];
}

- (void)tearDown {
[super tearDown];
}

- (void)testVersion {
XCTAssertEqualObjects(BOLTS_VERSION, [Bolts version]);
}
Expand Down
42 changes: 34 additions & 8 deletions BoltsTests/CancellationTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,6 @@ @interface CancellationTests : XCTestCase

@implementation CancellationTests

- (void)setUp {
[super setUp];
}

- (void)tearDown {
[super tearDown];
}

- (void)testCancellation {
BFCancellationTokenSource *cts = [BFCancellationTokenSource cancellationTokenSource];

Expand Down Expand Up @@ -53,4 +45,38 @@ - (void)testCancellationBlock {
XCTAssertTrue(cancelled, @"Source should be cancelled");
}

- (void)testCancellationAfterDelay {
BFCancellationTokenSource *cts = [BFCancellationTokenSource cancellationTokenSource];

XCTAssertFalse(cts.cancellationRequested, @"Source should not be cancelled");
XCTAssertFalse(cts.token.cancellationRequested, @"Token should not be cancelled");

[cts cancelAfterDelay:200];
XCTAssertFalse(cts.cancellationRequested, @"Source should be cancelled");
XCTAssertFalse(cts.token.cancellationRequested, @"Token should be cancelled");

// Spin the run loop for half a second, since `delay` is in milliseconds, not seconds.
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.5]];

XCTAssertTrue(cts.cancellationRequested, @"Source should be cancelled");
XCTAssertTrue(cts.token.cancellationRequested, @"Token should be cancelled");
}

- (void)testDispose {
BFCancellationTokenSource *cts = [BFCancellationTokenSource cancellationTokenSource];
[cts dispose];
XCTAssertThrowsSpecificNamed([cts cancel], NSException, NSInternalInconsistencyException);
XCTAssertThrowsSpecificNamed(cts.cancellationRequested, NSException, NSInternalInconsistencyException);
XCTAssertThrowsSpecificNamed(cts.token.cancellationRequested, NSException, NSInternalInconsistencyException);

cts = [BFCancellationTokenSource cancellationTokenSource];
[cts cancel];
XCTAssertTrue(cts.cancellationRequested, @"Source should be cancelled");
XCTAssertTrue(cts.token.cancellationRequested, @"Token should be cancelled");

[cts dispose];
XCTAssertThrowsSpecificNamed(cts.cancellationRequested, NSException, NSInternalInconsistencyException);
XCTAssertThrowsSpecificNamed(cts.token.cancellationRequested, NSException, NSInternalInconsistencyException);
}

@end
80 changes: 80 additions & 0 deletions BoltsTests/ExecutorTests.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* Copyright (c) 2014, Facebook, Inc.
* 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 <XCTest/XCTest.h>

#import "Bolts.h"

@interface ExecutorTests : XCTestCase

@end

@implementation ExecutorTests

- (void)testExecuteImmediately {
__block BFTask *task = [BFTask taskWithResult:nil];

XCTestExpectation *expectation = [self expectationWithDescription:@"test immediate executor"];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
task = [task continueWithExecutor:[BFExecutor immediateExecutor] withBlock:^id(BFTask *task) {
return nil;
}];
XCTAssertTrue(task.completed);
[expectation fulfill];
});
[self waitForExpectationsWithTimeout:10.0 handler:nil];
}

- (void)testExecuteOnDispatchQueue {
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0L);
BFExecutor *queueExecutor = [BFExecutor executorWithDispatchQueue:queue];

BFTask *task = [BFTask taskWithResult:nil];
task = [task continueWithExecutor:queueExecutor withBlock:^id(BFTask *task) {
XCTAssertEqual(queue, dispatch_get_current_queue());
return nil;
}];
[task waitUntilFinished];
}

- (void)testExecuteOnOperationQueue {
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
BFExecutor *queueExecutor = [BFExecutor executorWithOperationQueue:queue];

BFTask *task = [BFTask taskWithResult:nil];
task = [task continueWithExecutor:queueExecutor withBlock:^id(BFTask *task) {
XCTAssertEqual(queue, [NSOperationQueue currentQueue]);
return nil;
}];
[task waitUntilFinished];
}

- (void)testMainThreadExecutor {
BFExecutor *executor = [BFExecutor mainThreadExecutor];

XCTestExpectation *immediateExpectation = [self expectationWithDescription:@"test main thread executor on main thread"];
[executor execute:^{
XCTAssertTrue([NSThread isMainThread]);
[immediateExpectation fulfill];
}];

// Behaviour is different when running on main thread (runs immediately) vs running on the background queue.
XCTestExpectation *backgroundExpectation = [self expectationWithDescription:@"test main thread executor on background thread"];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[executor execute:^{
XCTAssertTrue([NSThread isMainThread]);
[backgroundExpectation fulfill];
}];
});

[self waitForExpectationsWithTimeout:10.0 handler:nil];
}

@end
Loading

0 comments on commit 5e4f650

Please sign in to comment.