Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More when #1306

Merged
merged 2 commits into from
Feb 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Sources/when.swift
Original file line number Diff line number Diff line change
Expand Up @@ -391,11 +391,21 @@ public func when(_ guarantees: Guarantee<Void>...) -> Guarantee<Void> {
return when(guarantees: guarantees)
}

/// Waits on all provided Guarantees.
public func when<T>(_ guarantees: Guarantee<T>...) -> Guarantee<[T]> {
return when(guarantees: guarantees)
}

/// Waits on all provided Guarantees.
public func when(guarantees: [Guarantee<Void>]) -> Guarantee<Void> {
return when(fulfilled: guarantees).recover{ _ in }.asVoid()
}

/// Waits on all provided Guarantees.
public func when<T>(guarantees: [Guarantee<T>]) -> Guarantee<[T]> {
return __when(guarantees).map(on: nil) { guarantees.map { $0.value! } }
}

/// Waits on all provided Guarantees.
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!) }
Expand Down
58 changes: 52 additions & 6 deletions Tests/CorePromise/WhenTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -251,18 +251,64 @@ class WhenTests: XCTestCase {
waitForExpectations(timeout: 1)
}

func testGuaranteeWhen() {
func testGuaranteesWhenVoidVarArgs() {
let ex1 = expectation(description: "")
when(Guarantee(), Guarantee()).done {
var someNumber = 0
let g1 = Guarantee<Void> { resolver in
someNumber += 1
resolver(())
}
let g2 = Guarantee<Void> { resolver in
someNumber += 2
resolver(())
}
when(g1, g2).done {
XCTAssertEqual(someNumber, 3)
ex1.fulfill()
}
wait(for: [ex1], timeout: 10)
}

func testGuaranteesWhenVarArgs() {
let ex1 = expectation(description: "")
let g1 = Guarantee<Int>.value(1)
let g2 = Guarantee<Int>.value(4)
let g3 = Guarantee<Int>.value(2)
let g4 = Guarantee<Int>.value(5)
let g5 = Guarantee<Int>.value(3)
when(g1, g2, g3, g4, g5).done {
XCTAssertEqual($0, [1, 4, 2, 5, 3])
ex1.fulfill()
}
wait(for: [ex1], timeout: 10)
}

func testGuaranteesWhenVoidArray() {
let ex1 = expectation(description: "")
var someNumber = 0
when(guarantees: (0..<100).map { _ in
Guarantee<Void> { resolver in
someNumber += 1
resolver(())
}
}).done {
XCTAssertEqual(someNumber, 100)
ex1.fulfill()
}

let ex2 = expectation(description: "")
when(guarantees: [Guarantee(), Guarantee()]).done {
ex2.fulfill()
wait(for: [ex1], timeout: 10)
}

func testGuaranteesWhenArray() {
let ex1 = expectation(description: "")
when(guarantees: (0..<100).map {
Guarantee<Int>.value($0)
}).done {
XCTAssertEqual($0, Array(0..<100))
ex1.fulfill()
}

wait(for: [ex1, ex2], timeout: 10)
wait(for: [ex1], timeout: 10)
}

func testDoubleTupleGuarantees() {
Expand Down
5 changes: 4 additions & 1 deletion Tests/CorePromise/XCTestManifests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,10 @@ extension WhenTests {
("testDoubleTuple", testDoubleTuple),
("testDoubleTupleGuarantees", testDoubleTupleGuarantees),
("testEmpty", testEmpty),
("testGuaranteeWhen", testGuaranteeWhen),
("testGuaranteesWhenArray", testGuaranteesWhenArray),
("testGuaranteesWhenVarArgs", testGuaranteesWhenVarArgs),
("testGuaranteesWhenVoidArray", testGuaranteesWhenVoidArray),
("testGuaranteesWhenVoidVarArgs", testGuaranteesWhenVoidVarArgs),
("testInt", testInt),
("testProgress", testProgress),
("testProgressDoesNotExceed100Percent", testProgressDoesNotExceed100Percent),
Expand Down