Skip to content

Commit 6c9d1ad

Browse files
authored
Re-write in Swift. WORK IN PROGRESS (benski#13)
Re-write in Swift.
1 parent 6f15afd commit 6c9d1ad

18 files changed

+1224
-218
lines changed

BAPromise-Bridging-Header.h

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
//
2+
// Use this file to import your target's public headers that you would like to expose to Swift.
3+
//
4+
5+
6+
#import "BAPromise.h"
7+

BAPromise.podspec

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
Pod::Spec.new do |s|
22
s.name = "BAPromise"
3-
s.version = "2.0.4"
3+
s.version = "3.0.0"
44
s.summary = "Objective C Promise Library"
55
s.description = <<-DESC
6-
Objective C Promise Library. An alternative to NSOperation for asynchronous operations.
6+
Swift Promise Library. An alternative to NSOperation for asynchronous operations.
77
DESC
88
s.homepage = "https://github.com/benski/BAPromise"
99
s.license = "MIT"
1010
s.author = { "Ben Allison" => "[email protected]" }
1111
s.source = { :git => "https://github.com/benski/BAPromise.git", :tag => s.version.to_s }
12-
#s.social_media_url = 'https://twitter.com/NAME'
12+
s.static_framework = true
13+
s.swift_version = '4.1'
1314

14-
s.ios.deployment_target = '8.0'
15-
s.osx.deployment_target = '10.10'
16-
s.tvos.deployment_target = '9.0'
15+
s.ios.deployment_target = '10.0'
16+
s.osx.deployment_target = '10.12'
17+
s.tvos.deployment_target = '10.0'
1718
s.requires_arc = true
1819

1920
s.source_files = 'Classes/*'

BAPromise.xcodeproj/project.pbxproj

+154-155
Large diffs are not rendered by default.

BAPromiseTests/CancelTests.m

+1-1
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ -(void)testCancelThenRaceCondition
215215
[expectation fulfill];
216216
}];
217217

218-
BACancelToken *cancelToken = [[BAPromise fulfilledPromise:nil] then:^id(id obj) {
218+
__block BACancelToken *cancelToken = [[BAPromise fulfilledPromise:nil] then:^id(id obj) {
219219
[cancelToken cancel];
220220
return thenPromise;
221221
}];

BAPromiseTests/CancelTestsSwift.swift

+222
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,222 @@
1+
//
2+
// CancelTestsSwift.swift
3+
// BAPromise
4+
//
5+
// Created by Ben Allison on 8/23/18.
6+
// Copyright © 2018 Ben Allison. All rights reserved.
7+
//
8+
9+
import XCTest
10+
11+
class CancelTestsSwift: XCTestCase {
12+
13+
override func setUp() {
14+
super.setUp()
15+
// Put setup code here. This method is called before the invocation of each test method in the class.
16+
}
17+
18+
override func tearDown() {
19+
// Put teardown code here. This method is called after the invocation of each test method in the class.
20+
super.tearDown()
21+
}
22+
23+
func testCancel() {
24+
let expectation = XCTestExpectation()
25+
let promise = Promise<Void>()
26+
promise.cancelled({ expectation.fulfill() }, on: DispatchQueue.main)
27+
promise.cancel()
28+
self.wait(for: [expectation], timeout: 0.5)
29+
}
30+
31+
func testDoneAfterCancel() {
32+
let expectation = XCTestExpectation()
33+
let promise = Promise<Void>()
34+
promise.cancelled({ expectation.fulfill() }, on: DispatchQueue.main)
35+
36+
let token = promise.then({
37+
XCTFail("Cancelation should prevent calling of done block")
38+
}, observed: {
39+
XCTFail("Cancelation should prevent calling of observed block")
40+
}, rejected: { (error) in
41+
XCTFail("Cancelation should prevent calling of rejected block")
42+
}, always: {
43+
XCTFail("Cancelation should prevent calling of finally block")
44+
}, queue: DispatchQueue.main)
45+
token.cancel()
46+
47+
self.wait(for: [expectation], timeout: 0.5)
48+
RunLoop.current.run(until: Date(timeIntervalSinceNow: 0.1))
49+
}
50+
51+
func testCancelTokenAfterFulfillment() {
52+
let promise = Promise<Void>()
53+
promise.fulfill(with: .success)
54+
promise.cancelled({ XCTFail("Unexpected cancelled callback") }, on: DispatchQueue.main)
55+
let token = promise.then({ }, queue: DispatchQueue.main)
56+
token.cancel()
57+
RunLoop.current.run(until: Date(timeIntervalSinceNow: 0.1))
58+
}
59+
60+
func testCancelPromiseAfterFulfilment() {
61+
let promise = Promise<Void>()
62+
promise.fulfill(with: .success)
63+
promise.cancelled({ XCTFail("Unexpected cancelled callback") }, on: DispatchQueue.main)
64+
promise.cancel()
65+
RunLoop.current.run(until: Date(timeIntervalSinceNow: 0.1))
66+
}
67+
68+
func testCancelPromiseAfterRejection() {
69+
let promise = Promise<Void>()
70+
promise.fulfill(with: .failure(NSError()))
71+
promise.cancelled({ XCTFail("Unexpected cancelled callback") }, on: DispatchQueue.main)
72+
promise.cancel()
73+
RunLoop.current.run(until: Date(timeIntervalSinceNow: 0.1))
74+
}
75+
76+
func testLateCancelCallback() {
77+
let expectation = XCTestExpectation()
78+
let promise = Promise<Void>()
79+
promise.cancel()
80+
promise.cancelled({ expectation.fulfill() }, on: DispatchQueue.main)
81+
82+
self.wait(for: [expectation], timeout: 0.5)
83+
}
84+
85+
func testCancelToken() {
86+
let expectation = XCTestExpectation()
87+
let promise = Promise<Void>()
88+
promise.cancelled({ XCTFail("unepected onCancel") }, on: DispatchQueue.main)
89+
promise.fulfill(with: .success)
90+
91+
let token = promise.then({
92+
XCTFail("Cancelation should prevent calling of done block")
93+
}, observed: {
94+
XCTFail("Cancelation should prevent calling of observed block")
95+
}, rejected: { (error) in
96+
XCTFail("Cancelation should prevent calling of rejected block")
97+
}, always: {
98+
XCTFail("Cancelation should prevent calling of finally block")
99+
}, queue: DispatchQueue.main)
100+
token.cancel()
101+
102+
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.1) {
103+
expectation.fulfill()
104+
}
105+
self.wait(for: [expectation], timeout: 0.5)
106+
107+
}
108+
109+
func testCancelAsyncARC() {
110+
let expectation = XCTestExpectation()
111+
let promise = Promise<Void>()
112+
promise.cancelled({ XCTFail("unepected onCancel") }, on: DispatchQueue.main)
113+
114+
DispatchQueue.main.async {
115+
promise.fulfill(with: .success)
116+
let token = promise.then({
117+
XCTFail("Cancelation should prevent calling of done block")
118+
}, observed: {
119+
XCTFail("Cancelation should prevent calling of observed block")
120+
}, rejected: { (error) in
121+
XCTFail("Cancelation should prevent calling of rejected block")
122+
}, always: {
123+
XCTFail("Cancelation should prevent calling of finally block")
124+
}, queue: DispatchQueue.main)
125+
token.cancel()
126+
}
127+
128+
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.1) {
129+
expectation.fulfill()
130+
}
131+
self.wait(for: [expectation], timeout: 0.5)
132+
133+
}
134+
135+
func testCancelReject() {
136+
let expectation = XCTestExpectation()
137+
let promise = Promise<Void>()
138+
promise.cancelled({ XCTFail("unepected onCancel") }, on: DispatchQueue.main)
139+
promise.fulfill(with: .failure(NSError()))
140+
141+
let token = promise.then({
142+
XCTFail("Cancelation should prevent calling of done block")
143+
}, observed: {
144+
XCTFail("Cancelation should prevent calling of observed block")
145+
}, rejected: { (error) in
146+
XCTFail("Cancelation should prevent calling of rejected block")
147+
}, always: {
148+
XCTFail("Cancelation should prevent calling of finally block")
149+
}, queue: DispatchQueue.main)
150+
token.cancel()
151+
152+
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.1) {
153+
expectation.fulfill()
154+
}
155+
self.wait(for: [expectation], timeout: 0.5)
156+
157+
}
158+
159+
func testCancelAsyncARCReject() {
160+
let expectation = XCTestExpectation()
161+
let promise = Promise<Void>()
162+
promise.cancelled({ XCTFail("unepected onCancel") }, on: DispatchQueue.main)
163+
164+
DispatchQueue.main.async {
165+
promise.fulfill(with: .failure(NSError()))
166+
let token = promise.then({
167+
XCTFail("Cancelation should prevent calling of done block")
168+
}, observed: {
169+
XCTFail("Cancelation should prevent calling of observed block")
170+
}, rejected: { (error) in
171+
XCTFail("Cancelation should prevent calling of rejected block")
172+
}, always: {
173+
XCTFail("Cancelation should prevent calling of finally block")
174+
}, queue: DispatchQueue.main)
175+
token.cancel()
176+
}
177+
178+
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.1) {
179+
expectation.fulfill()
180+
}
181+
self.wait(for: [expectation], timeout: 0.5)
182+
183+
}
184+
185+
func testCancelThen() {
186+
let expectation1 = XCTestExpectation()
187+
let thenPromise = Promise<Void>()
188+
let fulfilledPromise = Promise<Void>()
189+
fulfilledPromise.fulfill(with: .success)
190+
let cancelToken = fulfilledPromise.then({ () -> PromiseResult<Void> in
191+
expectation1.fulfill()
192+
return .promise(thenPromise)
193+
}, queue: DispatchQueue.main)
194+
self.wait(for: [expectation1], timeout: 0.5)
195+
196+
let expectation2 = XCTestExpectation()
197+
thenPromise.cancelled({
198+
expectation2.fulfill()
199+
}, on: DispatchQueue.main)
200+
201+
cancelToken.cancel()
202+
self.wait(for: [expectation2], timeout: 0.5)
203+
}
204+
205+
func testCancelThenRaceCondition() {
206+
let expectation = XCTestExpectation()
207+
let thenPromise = Promise<Void>()
208+
thenPromise.cancelled({
209+
expectation.fulfill()
210+
}, on: DispatchQueue.main)
211+
212+
let fulfilledPromise = Promise<Void>()
213+
fulfilledPromise.fulfill(with: .success)
214+
215+
let cancelToken = fulfilledPromise.then({ () -> PromiseResult<Void> in
216+
return .promise(thenPromise)
217+
}, queue: DispatchQueue.main)
218+
219+
cancelToken.cancel()
220+
self.wait(for: [expectation], timeout: 0.5)
221+
}
222+
}

BAPromiseTests/ChainTests.m

+5-5
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ -(void)testAsyncWhen
7474
}];
7575

7676
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
77-
[promise fulfillWithObject:@7];
77+
[anotherPromise fulfillWithObject:@7];
7878
});
7979

8080
[self waitForExpectationsWithTimeout:0.5 handler:nil];
@@ -106,7 +106,7 @@ -(void)testAsyncWhenFail
106106
}];
107107

108108
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
109-
[promise reject];
109+
[anotherPromise reject];
110110
});
111111

112112
[self waitForExpectationsWithTimeout:0.5 handler:nil];
@@ -130,7 +130,7 @@ -(void)testSimpleWhenFinallyFail
130130
{
131131
XCTestExpectation *expectation = [self expectationWithDescription:@"Promise Resolution"];
132132
BAPromise *promise = [[BAPromise alloc] init];
133-
BAPromise *anotherPromise = [BAPromise rejectedPromise:nil];
133+
BAPromise *anotherPromise = [BAPromise rejectedPromise:[NSError errorWithDomain:@"com.github.benski.promise" code:0 userInfo:nil]];
134134

135135
[promise fulfillWithObject:anotherPromise];
136136
[promise finally:^() {
@@ -152,7 +152,7 @@ -(void)testAsyncWhenFinallyDone
152152
}];
153153

154154
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
155-
[promise fulfillWithObject:nil];
155+
[anotherPromise fulfillWithObject:nil];
156156
});
157157

158158
[self waitForExpectationsWithTimeout:0.5 handler:nil];
@@ -170,7 +170,7 @@ -(void)testAsyncWhenFinallyFail
170170
}];
171171

172172
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
173-
[promise reject];
173+
[anotherPromise reject];
174174
});
175175

176176
[self waitForExpectationsWithTimeout:0.5 handler:nil];

0 commit comments

Comments
 (0)