Skip to content

Commit

Permalink
Promise.value and Guarantee.value when T == Void (#1096)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
RomanPodymov authored and mxcl committed Sep 25, 2019
1 parent 08fb076 commit 10a10ed
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 1 deletion.
4 changes: 4 additions & 0 deletions Sources/Guarantee.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
7 changes: 6 additions & 1 deletion Sources/Promise.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
}

Expand Down Expand Up @@ -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

Expand Down
13 changes: 13 additions & 0 deletions Tests/CorePromise/GuaranteeTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
13 changes: 13 additions & 0 deletions Tests/CorePromise/PromiseTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
2 changes: 2 additions & 0 deletions Tests/CorePromise/XCTestManifests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ extension GuaranteeTests {
("testFlatMapValues", testFlatMapValues),
("testInit", testInit),
("testMapValues", testMapValues),
("testNoAmbiguityForValue", testNoAmbiguityForValue),
("testSorted", testSorted),
("testSortedBy", testSortedBy),
("testThenFlatMap", testThenFlatMap),
Expand Down Expand Up @@ -137,6 +138,7 @@ extension PromiseTests {
("testIsPending", testIsPending),
("testIsRejected", testIsRejected),
("testIsResolved", testIsResolved),
("testNoAmbiguityForValue", testNoAmbiguityForValue),
("testPipeForResolved", testPipeForResolved),
("testThrowInFirstly", testThrowInFirstly),
("testThrowInInitializer", testThrowInInitializer),
Expand Down

0 comments on commit 10a10ed

Please sign in to comment.