Skip to content

Commit 87b5d53

Browse files
authored
Combine.Future (#1307)
* Combine.Future * swift test --generate-linuxmain * Fix for iOS < 13.0 * Swift >= 4.1 * Better handler for Swift < 4.1 * Fix tests * Fix for Linux * Additional fix for Linux * More #if * receiveCompletion * Linux again * Delete contents.xcworkspacedata
1 parent 5b90042 commit 87b5d53

File tree

7 files changed

+168
-8
lines changed

7 files changed

+168
-8
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@
66
DerivedData
77
/Extensions/Carthage
88
/Tests/JS-A+/build
9+
.swiftpm/

.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata

Lines changed: 0 additions & 7 deletions
This file was deleted.

PromiseKit.xcodeproj/project.pbxproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@
8383
9E4170F9287D88C900A3B4B5 /* Async.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9E4170F8287D88C800A3B4B5 /* Async.swift */; };
8484
9E4170FC287D8DF900A3B4B5 /* AsyncTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9E4170FA287D8DBD00A3B4B5 /* AsyncTests.swift */; };
8585
9E66231626FE5A8C00FA25CB /* RaceTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 9E66231526FE5A8C00FA25CB /* RaceTests.m */; };
86+
9EC774272991495C00803027 /* Combine.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9EC774262991495C00803027 /* Combine.swift */; };
87+
9EC7742A2991498200803027 /* CombineTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9EC774282991497900803027 /* CombineTests.swift */; };
8688
C013F7382048E3B6006B57B1 /* MockNodeEnvironment.swift in Sources */ = {isa = PBXBuildFile; fileRef = C013F7372048E3B6006B57B1 /* MockNodeEnvironment.swift */; };
8789
C013F73A2049076A006B57B1 /* JSPromise.swift in Sources */ = {isa = PBXBuildFile; fileRef = C013F7392049076A006B57B1 /* JSPromise.swift */; };
8890
C013F73C20494291006B57B1 /* JSAdapter.swift in Sources */ = {isa = PBXBuildFile; fileRef = C013F73B20494291006B57B1 /* JSAdapter.swift */; };
@@ -229,6 +231,8 @@
229231
9E4170F8287D88C800A3B4B5 /* Async.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = Async.swift; path = Sources/Async.swift; sourceTree = "<group>"; };
230232
9E4170FA287D8DBD00A3B4B5 /* AsyncTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AsyncTests.swift; sourceTree = "<group>"; };
231233
9E66231526FE5A8C00FA25CB /* RaceTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RaceTests.m; sourceTree = "<group>"; };
234+
9EC774262991495C00803027 /* Combine.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = Combine.swift; path = Sources/Combine.swift; sourceTree = "<group>"; };
235+
9EC774282991497900803027 /* CombineTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CombineTests.swift; sourceTree = "<group>"; };
232236
C013F7372048E3B6006B57B1 /* MockNodeEnvironment.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = MockNodeEnvironment.swift; path = "Tests/JS-A+/MockNodeEnvironment.swift"; sourceTree = "<group>"; };
233237
C013F7392049076A006B57B1 /* JSPromise.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = JSPromise.swift; path = "Tests/JS-A+/JSPromise.swift"; sourceTree = "<group>"; };
234238
C013F73B20494291006B57B1 /* JSAdapter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = JSAdapter.swift; path = "Tests/JS-A+/JSAdapter.swift"; sourceTree = "<group>"; };
@@ -341,6 +345,7 @@
341345
isa = PBXGroup;
342346
children = (
343347
9E4170FA287D8DBD00A3B4B5 /* AsyncTests.swift */,
348+
9EC774282991497900803027 /* CombineTests.swift */,
344349
63CF6D7F203CD19200EC8927 /* ThenableTests.swift */,
345350
632FBBE41F33B338008F8FBB /* CatchableTests.swift */,
346351
635D64081D59635300BC0AF5 /* PromiseTests.swift */,
@@ -440,6 +445,7 @@
440445
isa = PBXGroup;
441446
children = (
442447
9E4170F8287D88C800A3B4B5 /* Async.swift */,
448+
9EC774262991495C00803027 /* Combine.swift */,
443449
636A29191F1C156B001229C2 /* Promise.swift */,
444450
636A29221F1C17A6001229C2 /* Guarantee.swift */,
445451
636A29201F1C1716001229C2 /* Thenable.swift */,
@@ -686,6 +692,7 @@
686692
630A8056203CEF6800D25F23 /* AnyPromiseTests.m in Sources */,
687693
635D64221D59635300BC0AF5 /* ZalgoTests.swift in Sources */,
688694
635D64271D59635300BC0AF5 /* RaceTests.swift in Sources */,
695+
9EC7742A2991498200803027 /* CombineTests.swift in Sources */,
689696
632FBBE51F33B338008F8FBB /* CatchableTests.swift in Sources */,
690697
63CF6D80203CD19200EC8927 /* ThenableTests.swift in Sources */,
691698
635D642B1D59635300BC0AF5 /* StressTests.swift in Sources */,
@@ -773,6 +780,7 @@
773780
63B0AC8B1D595E6300FA21D9 /* join.m in Sources */,
774781
63B0AC891D595E6300FA21D9 /* hang.m in Sources */,
775782
63B0AC831D595E6300FA21D9 /* AnyPromise.swift in Sources */,
783+
9EC774272991495C00803027 /* Combine.swift in Sources */,
776784
63D9B2F120338D5D0075C00B /* Deprecations.swift in Sources */,
777785
63B0AC871D595E6300FA21D9 /* Error.swift in Sources */,
778786
0CC3AF2B1FCF84F7000E98C9 /* hang.swift in Sources */,

Sources/Combine.swift

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#if swift(>=4.1)
2+
#if canImport(Combine)
3+
import Combine
4+
5+
@available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, *)
6+
public extension Guarantee {
7+
func future() -> Future<T, Never> {
8+
.init { [weak self] promise in
9+
self?.done { value in
10+
promise(.success(value))
11+
}
12+
}
13+
}
14+
}
15+
16+
@available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, *)
17+
public extension Promise {
18+
func future() -> Future<T, Error> {
19+
.init { [weak self] promise in
20+
self?.done { value in
21+
promise(.success(value))
22+
}.catch { error in
23+
promise(.failure(error))
24+
}
25+
}
26+
}
27+
}
28+
#endif
29+
#endif

Tests/CorePromise/AsyncTests.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import PromiseKit
2-
import Dispatch
32
import XCTest
43

54
private enum Error: Swift.Error { case dummy }
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
#if swift(>=4.1)
2+
#if canImport(Combine)
3+
import Combine
4+
#endif
5+
#endif
6+
import PromiseKit
7+
import XCTest
8+
9+
private enum Error: Swift.Error { case dummy }
10+
11+
class CombineTests: XCTestCase {
12+
private var cancellable: Any?
13+
14+
override func tearDown() {
15+
#if swift(>=4.1)
16+
#if canImport(Combine)
17+
if #available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, *) {
18+
(cancellable as? AnyCancellable)?.cancel()
19+
}
20+
#endif
21+
#endif
22+
}
23+
24+
func testCombinePromiseValue() {
25+
let ex = expectation(description: "")
26+
#if swift(>=4.1)
27+
#if canImport(Combine)
28+
if #available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, *) {
29+
let promise = after(.milliseconds(100)).then(on: nil){ Promise.value(1) }
30+
cancellable = promise.future().sink(receiveCompletion: { result in
31+
switch result {
32+
case .failure:
33+
XCTAssert(false)
34+
default:
35+
XCTAssert(true)
36+
}
37+
}, receiveValue: {
38+
XCTAssertEqual($0, 1)
39+
ex.fulfill()
40+
})
41+
} else {
42+
ex.fulfill()
43+
}
44+
#else
45+
ex.fulfill()
46+
#endif
47+
#else
48+
ex.fulfill()
49+
#endif
50+
51+
wait(for: [ex], timeout: 1)
52+
}
53+
54+
func testCombineGuaranteeValue() {
55+
let ex = expectation(description: "")
56+
#if swift(>=4.1)
57+
#if canImport(Combine)
58+
if #available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, *) {
59+
let promise = after(.milliseconds(100)).then(on: nil){ Guarantee.value(1) }
60+
cancellable = promise.future().sink(receiveCompletion: { result in
61+
switch result {
62+
case .failure:
63+
XCTAssert(false)
64+
default:
65+
XCTAssert(true)
66+
}
67+
}, receiveValue: {
68+
XCTAssertEqual($0, 1)
69+
ex.fulfill()
70+
})
71+
} else {
72+
ex.fulfill()
73+
}
74+
#else
75+
ex.fulfill()
76+
#endif
77+
#else
78+
ex.fulfill()
79+
#endif
80+
81+
wait(for: [ex], timeout: 1)
82+
}
83+
84+
func testCombinePromiseThrow() {
85+
let ex = expectation(description: "")
86+
#if swift(>=4.1)
87+
#if canImport(Combine)
88+
if #available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, *) {
89+
let promise = after(.milliseconds(100)).then(on: nil){ Promise(error: Error.dummy) }.then(on: nil){ Promise.value(1) }
90+
cancellable = promise.future().sink(receiveCompletion: { result in
91+
switch result {
92+
case .failure(let error):
93+
switch error as? Error {
94+
case .dummy:
95+
XCTAssert(true)
96+
default:
97+
XCTAssert(false)
98+
}
99+
default:
100+
XCTAssert(false)
101+
}
102+
ex.fulfill()
103+
}, receiveValue: { _ in
104+
XCTAssert(false)
105+
})
106+
} else {
107+
ex.fulfill()
108+
}
109+
#else
110+
ex.fulfill()
111+
#endif
112+
#else
113+
ex.fulfill()
114+
#endif
115+
116+
wait(for: [ex], timeout: 1)
117+
}
118+
}

Tests/CorePromise/XCTestManifests.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,17 @@ extension CatchableTests {
6666
]
6767
}
6868

69+
extension CombineTests {
70+
// DO NOT MODIFY: This is autogenerated, use:
71+
// `swift test --generate-linuxmain`
72+
// to regenerate.
73+
static let __allTests__CombineTests = [
74+
("testCombineGuaranteeValue", testCombineGuaranteeValue),
75+
("testCombinePromiseThrow", testCombinePromiseThrow),
76+
("testCombinePromiseValue", testCombinePromiseValue),
77+
]
78+
}
79+
6980
extension GuaranteeTests {
7081
// DO NOT MODIFY: This is autogenerated, use:
7182
// `swift test --generate-linuxmain`
@@ -313,6 +324,7 @@ public func __allTests() -> [XCTestCaseEntry] {
313324
testCase(AsyncTests.__allTests__AsyncTests),
314325
testCase(CancellationTests.__allTests__CancellationTests),
315326
testCase(CatchableTests.__allTests__CatchableTests),
327+
testCase(CombineTests.__allTests__CombineTests),
316328
testCase(GuaranteeTests.__allTests__GuaranteeTests),
317329
testCase(HangTests.__allTests__HangTests),
318330
testCase(JoinTests.__allTests__JoinTests),

0 commit comments

Comments
 (0)