Promise.value and Guarantee.value when T == Void (#1096)

* Promise.value when T == Void

* Guarantee.value where T == Void

* Replace class with static because Promise is final

* Optimised Guarantee.value where Value == Void

* Add comment

* Implementation with Void()

* Tests for Promise<Void>.value

* Tests for Guarantee<Void>.value

* Added tests for Promise<Void> and Guarantee<Void>
This commit is contained in:
Roman Podymov 2019-09-25 19:24:39 +02:00 committed by Max Howell
parent 08fb076071
commit 10a10ed8bd
5 changed files with 38 additions and 1 deletions

View File

@ -280,6 +280,10 @@ public extension Guarantee where T == Void {
convenience init() {
self.init(box: SealedBox(value: Void()))
}
static var value: Guarantee<Void> {
return .value(Void())
}
}
#endif

View File

@ -38,7 +38,7 @@ public final class Promise<T>: Thenable, CatchMixin {
return .value(bar)
}
*/
public class func value(_ value: T) -> Promise<T> {
public static func value(_ value: T) -> Promise<T> {
return Promise(box: SealedBox(value: .fulfilled(value)))
}
@ -136,6 +136,11 @@ extension Promise where T == Void {
public convenience init() {
self.init(box: SealedBox(value: .fulfilled(Void())))
}
/// Returns a new promise fulfilled with `Void`
public static var value: Promise<Void> {
return .value(Void())
}
}
#endif

View File

@ -125,4 +125,17 @@ class GuaranteeTests: XCTestCase {
wait(for: [ex], timeout: 10)
}
#if swift(>=3.1)
func testNoAmbiguityForValue() {
let ex = expectation(description: "")
let a = Guarantee<Void>.value
let b = Guarantee<Void>.value(Void())
let c = Guarantee<Void>.value(())
when(fulfilled: a, b, c).done {
ex.fulfill()
}.cauterize()
wait(for: [ex], timeout: 10)
}
#endif
}

View File

@ -136,4 +136,17 @@ class PromiseTests: XCTestCase {
}.silenceWarning()
wait(for: [ex], timeout: 10)
}
#if swift(>=3.1)
func testNoAmbiguityForValue() {
let ex = expectation(description: "")
let a = Promise<Void>.value
let b = Promise<Void>.value(Void())
let c = Promise<Void>.value(())
when(fulfilled: a, b, c).done {
ex.fulfill()
}.cauterize()
wait(for: [ex], timeout: 10)
}
#endif
}

View File

@ -60,6 +60,7 @@ extension GuaranteeTests {
("testFlatMapValues", testFlatMapValues),
("testInit", testInit),
("testMapValues", testMapValues),
("testNoAmbiguityForValue", testNoAmbiguityForValue),
("testSorted", testSorted),
("testSortedBy", testSortedBy),
("testThenFlatMap", testThenFlatMap),
@ -137,6 +138,7 @@ extension PromiseTests {
("testIsPending", testIsPending),
("testIsRejected", testIsRejected),
("testIsResolved", testIsResolved),
("testNoAmbiguityForValue", testNoAmbiguityForValue),
("testPipeForResolved", testPipeForResolved),
("testThrowInFirstly", testThrowInFirstly),
("testThrowInInitializer", testThrowInInitializer),