Skip to content

Commit

Permalink
Combine.Future (#1307)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
RomanPodymov authored Feb 11, 2023
1 parent 5b90042 commit 87b5d53
Show file tree
Hide file tree
Showing 7 changed files with 168 additions and 8 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
DerivedData
/Extensions/Carthage
/Tests/JS-A+/build
.swiftpm/
7 changes: 0 additions & 7 deletions .swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata

This file was deleted.

8 changes: 8 additions & 0 deletions PromiseKit.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@
9E4170F9287D88C900A3B4B5 /* Async.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9E4170F8287D88C800A3B4B5 /* Async.swift */; };
9E4170FC287D8DF900A3B4B5 /* AsyncTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9E4170FA287D8DBD00A3B4B5 /* AsyncTests.swift */; };
9E66231626FE5A8C00FA25CB /* RaceTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 9E66231526FE5A8C00FA25CB /* RaceTests.m */; };
9EC774272991495C00803027 /* Combine.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9EC774262991495C00803027 /* Combine.swift */; };
9EC7742A2991498200803027 /* CombineTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9EC774282991497900803027 /* CombineTests.swift */; };
C013F7382048E3B6006B57B1 /* MockNodeEnvironment.swift in Sources */ = {isa = PBXBuildFile; fileRef = C013F7372048E3B6006B57B1 /* MockNodeEnvironment.swift */; };
C013F73A2049076A006B57B1 /* JSPromise.swift in Sources */ = {isa = PBXBuildFile; fileRef = C013F7392049076A006B57B1 /* JSPromise.swift */; };
C013F73C20494291006B57B1 /* JSAdapter.swift in Sources */ = {isa = PBXBuildFile; fileRef = C013F73B20494291006B57B1 /* JSAdapter.swift */; };
Expand Down Expand Up @@ -229,6 +231,8 @@
9E4170F8287D88C800A3B4B5 /* Async.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = Async.swift; path = Sources/Async.swift; sourceTree = "<group>"; };
9E4170FA287D8DBD00A3B4B5 /* AsyncTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AsyncTests.swift; sourceTree = "<group>"; };
9E66231526FE5A8C00FA25CB /* RaceTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RaceTests.m; sourceTree = "<group>"; };
9EC774262991495C00803027 /* Combine.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = Combine.swift; path = Sources/Combine.swift; sourceTree = "<group>"; };
9EC774282991497900803027 /* CombineTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CombineTests.swift; sourceTree = "<group>"; };
C013F7372048E3B6006B57B1 /* MockNodeEnvironment.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = MockNodeEnvironment.swift; path = "Tests/JS-A+/MockNodeEnvironment.swift"; sourceTree = "<group>"; };
C013F7392049076A006B57B1 /* JSPromise.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = JSPromise.swift; path = "Tests/JS-A+/JSPromise.swift"; sourceTree = "<group>"; };
C013F73B20494291006B57B1 /* JSAdapter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = JSAdapter.swift; path = "Tests/JS-A+/JSAdapter.swift"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -341,6 +345,7 @@
isa = PBXGroup;
children = (
9E4170FA287D8DBD00A3B4B5 /* AsyncTests.swift */,
9EC774282991497900803027 /* CombineTests.swift */,
63CF6D7F203CD19200EC8927 /* ThenableTests.swift */,
632FBBE41F33B338008F8FBB /* CatchableTests.swift */,
635D64081D59635300BC0AF5 /* PromiseTests.swift */,
Expand Down Expand Up @@ -440,6 +445,7 @@
isa = PBXGroup;
children = (
9E4170F8287D88C800A3B4B5 /* Async.swift */,
9EC774262991495C00803027 /* Combine.swift */,
636A29191F1C156B001229C2 /* Promise.swift */,
636A29221F1C17A6001229C2 /* Guarantee.swift */,
636A29201F1C1716001229C2 /* Thenable.swift */,
Expand Down Expand Up @@ -686,6 +692,7 @@
630A8056203CEF6800D25F23 /* AnyPromiseTests.m in Sources */,
635D64221D59635300BC0AF5 /* ZalgoTests.swift in Sources */,
635D64271D59635300BC0AF5 /* RaceTests.swift in Sources */,
9EC7742A2991498200803027 /* CombineTests.swift in Sources */,
632FBBE51F33B338008F8FBB /* CatchableTests.swift in Sources */,
63CF6D80203CD19200EC8927 /* ThenableTests.swift in Sources */,
635D642B1D59635300BC0AF5 /* StressTests.swift in Sources */,
Expand Down Expand Up @@ -773,6 +780,7 @@
63B0AC8B1D595E6300FA21D9 /* join.m in Sources */,
63B0AC891D595E6300FA21D9 /* hang.m in Sources */,
63B0AC831D595E6300FA21D9 /* AnyPromise.swift in Sources */,
9EC774272991495C00803027 /* Combine.swift in Sources */,
63D9B2F120338D5D0075C00B /* Deprecations.swift in Sources */,
63B0AC871D595E6300FA21D9 /* Error.swift in Sources */,
0CC3AF2B1FCF84F7000E98C9 /* hang.swift in Sources */,
Expand Down
29 changes: 29 additions & 0 deletions Sources/Combine.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#if swift(>=4.1)
#if canImport(Combine)
import Combine

@available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, *)
public extension Guarantee {
func future() -> Future<T, Never> {
.init { [weak self] promise in
self?.done { value in
promise(.success(value))
}
}
}
}

@available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, *)
public extension Promise {
func future() -> Future<T, Error> {
.init { [weak self] promise in
self?.done { value in
promise(.success(value))
}.catch { error in
promise(.failure(error))
}
}
}
}
#endif
#endif
1 change: 0 additions & 1 deletion Tests/CorePromise/AsyncTests.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import PromiseKit
import Dispatch
import XCTest

private enum Error: Swift.Error { case dummy }
Expand Down
118 changes: 118 additions & 0 deletions Tests/CorePromise/CombineTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
#if swift(>=4.1)
#if canImport(Combine)
import Combine
#endif
#endif
import PromiseKit
import XCTest

private enum Error: Swift.Error { case dummy }

class CombineTests: XCTestCase {
private var cancellable: Any?

override func tearDown() {
#if swift(>=4.1)
#if canImport(Combine)
if #available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, *) {
(cancellable as? AnyCancellable)?.cancel()
}
#endif
#endif
}

func testCombinePromiseValue() {
let ex = expectation(description: "")
#if swift(>=4.1)
#if canImport(Combine)
if #available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, *) {
let promise = after(.milliseconds(100)).then(on: nil){ Promise.value(1) }
cancellable = promise.future().sink(receiveCompletion: { result in
switch result {
case .failure:
XCTAssert(false)
default:
XCTAssert(true)
}
}, receiveValue: {
XCTAssertEqual($0, 1)
ex.fulfill()
})
} else {
ex.fulfill()
}
#else
ex.fulfill()
#endif
#else
ex.fulfill()
#endif

wait(for: [ex], timeout: 1)
}

func testCombineGuaranteeValue() {
let ex = expectation(description: "")
#if swift(>=4.1)
#if canImport(Combine)
if #available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, *) {
let promise = after(.milliseconds(100)).then(on: nil){ Guarantee.value(1) }
cancellable = promise.future().sink(receiveCompletion: { result in
switch result {
case .failure:
XCTAssert(false)
default:
XCTAssert(true)
}
}, receiveValue: {
XCTAssertEqual($0, 1)
ex.fulfill()
})
} else {
ex.fulfill()
}
#else
ex.fulfill()
#endif
#else
ex.fulfill()
#endif

wait(for: [ex], timeout: 1)
}

func testCombinePromiseThrow() {
let ex = expectation(description: "")
#if swift(>=4.1)
#if canImport(Combine)
if #available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, *) {
let promise = after(.milliseconds(100)).then(on: nil){ Promise(error: Error.dummy) }.then(on: nil){ Promise.value(1) }
cancellable = promise.future().sink(receiveCompletion: { result in
switch result {
case .failure(let error):
switch error as? Error {
case .dummy:
XCTAssert(true)
default:
XCTAssert(false)
}
default:
XCTAssert(false)
}
ex.fulfill()
}, receiveValue: { _ in
XCTAssert(false)
})
} else {
ex.fulfill()
}
#else
ex.fulfill()
#endif
#else
ex.fulfill()
#endif

wait(for: [ex], timeout: 1)
}
}
12 changes: 12 additions & 0 deletions Tests/CorePromise/XCTestManifests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,17 @@ extension CatchableTests {
]
}

extension CombineTests {
// DO NOT MODIFY: This is autogenerated, use:
// `swift test --generate-linuxmain`
// to regenerate.
static let __allTests__CombineTests = [
("testCombineGuaranteeValue", testCombineGuaranteeValue),
("testCombinePromiseThrow", testCombinePromiseThrow),
("testCombinePromiseValue", testCombinePromiseValue),
]
}

extension GuaranteeTests {
// DO NOT MODIFY: This is autogenerated, use:
// `swift test --generate-linuxmain`
Expand Down Expand Up @@ -313,6 +324,7 @@ public func __allTests() -> [XCTestCaseEntry] {
testCase(AsyncTests.__allTests__AsyncTests),
testCase(CancellationTests.__allTests__CancellationTests),
testCase(CatchableTests.__allTests__CatchableTests),
testCase(CombineTests.__allTests__CombineTests),
testCase(GuaranteeTests.__allTests__GuaranteeTests),
testCase(HangTests.__allTests__HangTests),
testCase(JoinTests.__allTests__JoinTests),
Expand Down

0 comments on commit 87b5d53

Please sign in to comment.