From 5dd8a03cdac38542332a4123140fe401bbc8e533 Mon Sep 17 00:00:00 2001 From: Wyatt Mufson Date: Tue, 1 Oct 2019 15:26:14 -0500 Subject: [PATCH] Custom error type testing --- Tests/Tests.swift | 114 ++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 110 insertions(+), 4 deletions(-) diff --git a/Tests/Tests.swift b/Tests/Tests.swift index 9697266..b35bc3f 100644 --- a/Tests/Tests.swift +++ b/Tests/Tests.swift @@ -12,6 +12,13 @@ class Tests: XCTestCase { super.tearDown() } + struct CustomError: Error { + var msg: String + var code: Int + } + + typealias NPromise = BasePromise + func testAll() { let expectation = XCTestExpectation(description: "Test all") @@ -493,14 +500,113 @@ class Tests: XCTestCase { self.wait(for: [expectation], timeout: 10) } - func testCustomErrorType() { - typealias NPromise = BasePromise - let p = NPromise(15) + func testCustomErrorTypeAll() { + let expectation = XCTestExpectation(description: "Test custom error promise all") + + let p1 = NPromise(15) + let p2 = NPromise { (resolve, _) in + DispatchQueue.global().async { + resolve(3) + } + } + + let p3 = NPromise { (resolve, _) in + DispatchQueue.global().asyncAfter(deadline: .now() + 0.2) { + resolve(3) + } + } + + let p4 = NPromise { (resolve, _) in + DispatchQueue.global().asyncAfter(deadline: .now() + 0.1) { + resolve(4) + } + } + + let p5 = NPromise { (resolve, _) in + DispatchQueue.global().async { + resolve(13) + } + } + + let promises = [p1, p2, p3, p4, p5] + all(promises).then { (result) in + var sum = 0 + for i in result { + sum += i + } + XCTAssertEqual(sum, 38) + expectation.fulfill() + }.catch { _ in + XCTFail() + expectation.fulfill() + } + + self.wait(for: [expectation], timeout: 10) + } + + func testCustomErrorTypeAwait() { + let expectation = XCTestExpectation(description: "Test custom error promise await") + + let p = NPromise { (resolve, _) in + DispatchQueue.global().asyncAfter(deadline: .now() + 0.1) { + resolve(15) + } + } + + do { + let result = try await(p) + XCTAssertEqual(result, 15) + expectation.fulfill() + } catch { + XCTFail() + expectation.fulfill() + } + + self.wait(for: [expectation], timeout: 10) + } + + func testCustomErrorTypeReject() { + let expectation = XCTestExpectation(description: "Test custom error promise reject") + + let errorMsg = "Failed!" + let errorCode = 500 + + let p = NPromise { (_, reject) in + DispatchQueue.global().asyncAfter(deadline: .now() + 0.1) { + reject(CustomError(msg: errorMsg, code: errorCode)) + } + } + + p.then { (i) in + XCTFail() + expectation.fulfill() + }.catch { (e) in + XCTAssertEqual(e.msg, errorMsg) + XCTAssertEqual(e.code, errorCode) + expectation.fulfill() + } + + self.wait(for: [expectation], timeout: 10) + } + + func testCustomErrorTypeResolve() { + let expectation = XCTestExpectation(description: "Test custom error promise resolve") + + let p = NPromise { (resolve, _) in + DispatchQueue.global().asyncAfter(deadline: .now() + 0.1) { + resolve(15) + } + } + p.then { (i) in XCTAssertEqual(i, 15) + expectation.fulfill() }.catch { (e) in - XCTFail(e.domain) + XCTFail() + expectation.fulfill() } + + self.wait(for: [expectation], timeout: 10) } func testMultiReject() {