Skip to content

Commit 65a44c2

Browse files
authored
Merge pull request #1306 from RomanPodymov/v6
More when
2 parents 065a93d + 4ecebcf commit 65a44c2

3 files changed

Lines changed: 66 additions & 7 deletions

File tree

Sources/when.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,11 +391,21 @@ public func when(_ guarantees: Guarantee<Void>...) -> Guarantee<Void> {
391391
return when(guarantees: guarantees)
392392
}
393393

394+
/// Waits on all provided Guarantees.
395+
public func when<T>(_ guarantees: Guarantee<T>...) -> Guarantee<[T]> {
396+
return when(guarantees: guarantees)
397+
}
398+
394399
/// Waits on all provided Guarantees.
395400
public func when(guarantees: [Guarantee<Void>]) -> Guarantee<Void> {
396401
return when(fulfilled: guarantees).recover{ _ in }.asVoid()
397402
}
398403

404+
/// Waits on all provided Guarantees.
405+
public func when<T>(guarantees: [Guarantee<T>]) -> Guarantee<[T]> {
406+
return __when(guarantees).map(on: nil) { guarantees.map { $0.value! } }
407+
}
408+
399409
/// Waits on all provided Guarantees.
400410
public func when<U, V>(guarantees gu: Guarantee<U>, _ gv: Guarantee<V>) -> Guarantee<(U, V)> {
401411
return __when([gu.asVoid(), gv.asVoid()]).map(on: nil) { (gu.value!, gv.value!) }

Tests/CorePromise/WhenTests.swift

Lines changed: 52 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -251,18 +251,64 @@ class WhenTests: XCTestCase {
251251
waitForExpectations(timeout: 1)
252252
}
253253

254-
func testGuaranteeWhen() {
254+
func testGuaranteesWhenVoidVarArgs() {
255255
let ex1 = expectation(description: "")
256-
when(Guarantee(), Guarantee()).done {
256+
var someNumber = 0
257+
let g1 = Guarantee<Void> { resolver in
258+
someNumber += 1
259+
resolver(())
260+
}
261+
let g2 = Guarantee<Void> { resolver in
262+
someNumber += 2
263+
resolver(())
264+
}
265+
when(g1, g2).done {
266+
XCTAssertEqual(someNumber, 3)
267+
ex1.fulfill()
268+
}
269+
wait(for: [ex1], timeout: 10)
270+
}
271+
272+
func testGuaranteesWhenVarArgs() {
273+
let ex1 = expectation(description: "")
274+
let g1 = Guarantee<Int>.value(1)
275+
let g2 = Guarantee<Int>.value(4)
276+
let g3 = Guarantee<Int>.value(2)
277+
let g4 = Guarantee<Int>.value(5)
278+
let g5 = Guarantee<Int>.value(3)
279+
when(g1, g2, g3, g4, g5).done {
280+
XCTAssertEqual($0, [1, 4, 2, 5, 3])
281+
ex1.fulfill()
282+
}
283+
wait(for: [ex1], timeout: 10)
284+
}
285+
286+
func testGuaranteesWhenVoidArray() {
287+
let ex1 = expectation(description: "")
288+
var someNumber = 0
289+
when(guarantees: (0..<100).map { _ in
290+
Guarantee<Void> { resolver in
291+
someNumber += 1
292+
resolver(())
293+
}
294+
}).done {
295+
XCTAssertEqual(someNumber, 100)
257296
ex1.fulfill()
258297
}
259298

260-
let ex2 = expectation(description: "")
261-
when(guarantees: [Guarantee(), Guarantee()]).done {
262-
ex2.fulfill()
299+
wait(for: [ex1], timeout: 10)
300+
}
301+
302+
func testGuaranteesWhenArray() {
303+
let ex1 = expectation(description: "")
304+
when(guarantees: (0..<100).map {
305+
Guarantee<Int>.value($0)
306+
}).done {
307+
XCTAssertEqual($0, Array(0..<100))
308+
ex1.fulfill()
263309
}
264310

265-
wait(for: [ex1, ex2], timeout: 10)
311+
wait(for: [ex1], timeout: 10)
266312
}
267313

268314
func testDoubleTupleGuarantees() {

Tests/CorePromise/XCTestManifests.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,10 @@ extension WhenTests {
256256
("testDoubleTuple", testDoubleTuple),
257257
("testDoubleTupleGuarantees", testDoubleTupleGuarantees),
258258
("testEmpty", testEmpty),
259-
("testGuaranteeWhen", testGuaranteeWhen),
259+
("testGuaranteesWhenArray", testGuaranteesWhenArray),
260+
("testGuaranteesWhenVarArgs", testGuaranteesWhenVarArgs),
261+
("testGuaranteesWhenVoidArray", testGuaranteesWhenVoidArray),
262+
("testGuaranteesWhenVoidVarArgs", testGuaranteesWhenVoidVarArgs),
260263
("testInt", testInt),
261264
("testProgress", testProgress),
262265
("testProgressDoesNotExceed100Percent", testProgressDoesNotExceed100Percent),

0 commit comments

Comments
 (0)