127 lines
3.8 KiB
Swift
127 lines
3.8 KiB
Swift
//
|
|
// PromiseTests.swift
|
|
// SimpleSwiftPromisesTests
|
|
//
|
|
// Created by Shadowfacts on 2/14/20.
|
|
// Copyright © 2020 Shadowfacts. All rights reserved.
|
|
//
|
|
|
|
import XCTest
|
|
@testable import SimpleSwiftPromises
|
|
|
|
class PromiseTests: XCTestCase {
|
|
|
|
override func setUpWithError() throws {
|
|
// Put setup code here. This method is called before the invocation of each test method in the class.
|
|
}
|
|
|
|
override func tearDownWithError() throws {
|
|
// Put teardown code here. This method is called after the invocation of each test method in the class.
|
|
}
|
|
|
|
func assertResultEqual<Result: Equatable>(_ promise: Promise<Result>, _ value: Result, message: String? = nil) {
|
|
let expectation = self.expectation(description: message ?? "promise result assertion")
|
|
promise.then {
|
|
XCTAssertEqual($0, value)
|
|
expectation.fulfill()
|
|
}
|
|
self.waitForExpectations(timeout: 2) { (error) in
|
|
if let error = error {
|
|
XCTFail("didn't resolve promise: \(error)")
|
|
}
|
|
}
|
|
}
|
|
|
|
func testResolveImmediate() {
|
|
assertResultEqual(Promise<String>.resolve("blah"), "blah")
|
|
}
|
|
|
|
func testResolveImmediateMapped() {
|
|
let promise = Promise<String>.resolve("foo").then {
|
|
"test \($0)"
|
|
}.then {
|
|
Promise<String>.resolve("\($0) bar")
|
|
}
|
|
assertResultEqual(promise, "test foo bar")
|
|
}
|
|
|
|
func testContinueAfterReject() {
|
|
let promise = Promise<String>.reject(TestError()).then { (res) in
|
|
XCTFail("then on rejected promise is unreachable")
|
|
}.catch { (error) -> String in
|
|
XCTAssertTrue(error is TestError)
|
|
return "caught"
|
|
}.then {
|
|
"\($0) error"
|
|
}
|
|
assertResultEqual(promise, "caught error")
|
|
}
|
|
|
|
func testResolveDelayed() {
|
|
let promise = Promise<String> { (resolve, reject) in
|
|
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
|
|
resolve("blah")
|
|
}
|
|
}
|
|
assertResultEqual(promise, "blah")
|
|
}
|
|
|
|
func testResolveMappedDelayed() {
|
|
let promise = Promise<String> { (resolve, reject) in
|
|
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
|
|
resolve("foo")
|
|
}
|
|
}.then {
|
|
"\($0) bar"
|
|
}.then { (result) in
|
|
Promise<String> { (resolve, reject) in
|
|
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
|
|
resolve("\(result) baz")
|
|
}
|
|
}
|
|
}
|
|
assertResultEqual(promise, "foo bar baz")
|
|
}
|
|
|
|
func testResolveAll() {
|
|
let promise = Promise<[String]>.all([
|
|
Promise<String>.resolve("a"),
|
|
Promise<String>.resolve("b"),
|
|
Promise<String>.resolve("c"),
|
|
])
|
|
assertResultEqual(promise, ["a", "b", "c"])
|
|
}
|
|
|
|
func testIntermediateReject() {
|
|
let promise = Promise<String>.resolve("foo").then { (_) -> Promise<String> in
|
|
Promise<String>.reject(TestError())
|
|
}.catch { (error) -> String in
|
|
XCTAssertTrue(error is TestError)
|
|
return "caught"
|
|
}.then { (result) -> String in
|
|
"\(result) error"
|
|
}
|
|
assertResultEqual(promise, "caught error")
|
|
}
|
|
|
|
func testResultHelper() {
|
|
let success = Promise<String> { (handler) in
|
|
handler(Result<String, Never>.success("asdf"))
|
|
}
|
|
assertResultEqual(success, "asdf")
|
|
let failure = Promise<String> { (handler) in
|
|
handler(Result<String, TestError>.failure(TestError()))
|
|
}.catch { (error) -> String in
|
|
"blah"
|
|
}
|
|
assertResultEqual(failure, "blah")
|
|
}
|
|
|
|
}
|
|
|
|
struct TestError: Error {
|
|
var localizedDescription: String {
|
|
"test error"
|
|
}
|
|
}
|