Skip to content

Commit b411df8

Browse files
committed
Added two unit tests for the each method.
1 parent 72000b3 commit b411df8

File tree

3 files changed

+58
-3
lines changed

3 files changed

+58
-3
lines changed

Ax/Ax.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ final public class Ax {
1313
public typealias ResultClosure = (NSError?) -> Void
1414
public typealias DoneClosure = (NSError?) -> Void
1515
public typealias TaskClosure = (@escaping DoneClosure) -> Void
16-
public typealias IterateeClosure<T> = (T, DoneClosure) -> Void
16+
public typealias IterateeClosure<T> = (T, @escaping DoneClosure) -> Void
1717

1818
public static func each<T>(collection: [T], iteratee: @escaping IterateeClosure<T>, result: @escaping ResultClosure) {
1919
let group = DispatchGroup()

AxTests/AxTests.swift

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -537,14 +537,39 @@ class AxTests: XCTestCase {
537537

538538
//Each tests
539539
func testRunningAnArrayItemsInParallel() {
540-
let ex = expectation(description: "")
540+
let ex = expectation(description: "Running an array of items in parallel without any error.")
541541
let array = [0, 1, 2, 3]
542542

543543
Ax.each(
544544
collection: array,
545545
iteratee: { item, done in
546546
print(item)
547-
done(nil)
547+
self.runAsync(after: 2, closure: {
548+
done(nil)
549+
})
550+
551+
},
552+
result: { error in
553+
XCTAssertNil(error)
554+
ex.fulfill()
555+
}
556+
)
557+
558+
waitForExpectations(timeout: 10) { (error) in
559+
if let error = error {
560+
XCTFail("error: \(error)")
561+
}
562+
}
563+
}
564+
565+
func testRunningAnArrayItemsEmpty() {
566+
let ex = expectation(description: "Running an empty array, it should run without any error.")
567+
568+
Ax.each(
569+
collection: [],
570+
iteratee: { item, done in
571+
let error = NSError(domain: self.errorDomain, code: 666, userInfo: [ NSLocalizedDescriptionKey: "this closure shouldn't have been called."])
572+
done(error)
548573
},
549574
result: { error in
550575
XCTAssertNil(error)
@@ -559,6 +584,36 @@ class AxTests: XCTestCase {
559584
}
560585
}
561586

587+
func testRunningAnArrayWithAnError() {
588+
let ex = expectation(description: "Running an array with an error between the closures, it should throw the error and cancel other calls on closures.")
589+
590+
Ax.each(
591+
collection: [1, 2, 3],
592+
iteratee: { item, done in
593+
self.runAsync(after: 2, closure: {
594+
print(item)
595+
if item == 2 {
596+
let error = NSError(domain: self.errorDomain, code: 666, userInfo: [ NSLocalizedDescriptionKey: "an error happened on item: \(item)"])
597+
done(error)
598+
} else {
599+
done(nil)
600+
}
601+
})
602+
},
603+
result: { error in
604+
print(error)
605+
XCTAssertNotNil(error)
606+
ex.fulfill()
607+
}
608+
)
609+
610+
waitForExpectations(timeout: 10) { (error) in
611+
if let error = error {
612+
XCTFail("error: \(error)")
613+
}
614+
}
615+
}
616+
562617

563618

564619
}

0 commit comments

Comments
 (0)