Skip to content

Commit

Permalink
More when(guarantees:)
Browse files Browse the repository at this point in the history
  • Loading branch information
RomanPodymov committed Jan 20, 2023
1 parent cea8fb6 commit a75f34f
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 2 deletions.
15 changes: 15 additions & 0 deletions Sources/when.swift
Original file line number Diff line number Diff line change
Expand Up @@ -400,3 +400,18 @@ public func when(guarantees: [Guarantee<Void>]) -> Guarantee<Void> {
public func when<U, V>(guarantees gu: Guarantee<U>, _ gv: Guarantee<V>) -> Guarantee<(U, V)> {
return __when([gu.asVoid(), gv.asVoid()]).map(on: nil) { (gu.value!, gv.value!) }
}

/// Waits on all provided Guarantees.
public func when<U, V, W>(guarantees gu: Guarantee<U>, _ gv: Guarantee<V>, _ gw: Guarantee<W>) -> Guarantee<(U, V, W)> {
return __when([gu.asVoid(), gv.asVoid(), gw.asVoid()]).map(on: nil) { (gu.value!, gv.value!, gw.value!) }
}

/// Waits on all provided Guarantees.
public func when<U, V, W, X>(guarantees gu: Guarantee<U>, _ gv: Guarantee<V>, _ gw: Guarantee<W>, _ gx: Guarantee<X>) -> Guarantee<(U, V, W, X)> {
return __when([gu.asVoid(), gv.asVoid(), gw.asVoid(), gx.asVoid()]).map(on: nil) { (gu.value!, gv.value!, gw.value!, gx.value!) }
}

/// Waits on all provided Guarantees.
public func when<U, V, W, X, Y>(guarantees gu: Guarantee<U>, _ gv: Guarantee<V>, _ gw: Guarantee<W>, _ gx: Guarantee<X>, _ gy: Guarantee<Y>) -> Guarantee<(U, V, W, X, Y)> {
return __when([gu.asVoid(), gv.asVoid(), gw.asVoid(), gx.asVoid(), gy.asVoid()]).map(on: nil) { (gu.value!, gv.value!, gw.value!, gx.value!, gy.value!) }
}
52 changes: 50 additions & 2 deletions Tests/CorePromise/WhenTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,10 @@ class WhenTests: XCTestCase {
let progress = Progress(totalUnitCount: 1)
progress.becomeCurrent(withPendingUnitCount: 1)

when(fulfilled: p1, p2, p3, p4).done { _ in
when(guarantees: p1, p2, p3, p4).done { _ in
XCTAssertEqual(progress.completedUnitCount, 1)
ex.fulfill()
}.silenceWarning()
}

progress.resignCurrent()

Expand Down Expand Up @@ -276,4 +276,52 @@ class WhenTests: XCTestCase {
}
waitForExpectations(timeout: 1, handler: nil)
}

func testTripleTupleGuarantees() {
let e1 = expectation(description: "")
let g1 = Guarantee.value(1)
let g2 = Guarantee.value("abc")
let g3 = Guarantee.value( 1.0)
when(guarantees: g1, g2, g3).done { u, v, w in
XCTAssertEqual(1, u)
XCTAssertEqual("abc", v)
XCTAssertEqual(1.0, w)
e1.fulfill()
}
waitForExpectations(timeout: 1, handler: nil)
}

func testQuadrupleTupleGuarantees() {
let e1 = expectation(description: "")
let g1 = Guarantee.value(1)
let g2 = Guarantee.value("abc")
let g3 = Guarantee.value(1.0)
let g4 = Guarantee.value(true)
when(guarantees: g1, g2, g3, g4).done { u, v, w, x in
XCTAssertEqual(1, u)
XCTAssertEqual("abc", v)
XCTAssertEqual(1.0, w)
XCTAssertEqual(true, x)
e1.fulfill()
}
waitForExpectations(timeout: 1, handler: nil)
}

func testQuintupleTupleGuarantees() {
let e1 = expectation(description: "")
let g1 = Guarantee.value(1)
let g2 = Guarantee.value("abc")
let g3 = Guarantee.value(1.0)
let g4 = Guarantee.value(true)
let g5 = Guarantee.value("a" as Character)
when(guarantees: g1, g2, g3, g4, g5).done { u, v, w, x, y in
XCTAssertEqual(1, u)
XCTAssertEqual("abc", v)
XCTAssertEqual(1.0, w)
XCTAssertEqual(true, x)
XCTAssertEqual("a" as Character, y)
e1.fulfill()
}
waitForExpectations(timeout: 1, handler: nil)
}
}

0 comments on commit a75f34f

Please sign in to comment.