-
-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathXCTestCase+Promises.swift
49 lines (44 loc) · 1.72 KB
/
XCTestCase+Promises.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
//
// XCTestCase+Promises.swift
// CryptomatorCommonCoreTests
//
// Copyright © 2022 Skymatic GmbH. All rights reserved.
//
import Foundation
import XCTest
@testable import Promises
extension XCTestCase {
func XCTAssertRejects<T>(_ expression: Promise<T>, _ message: @autoclosure () -> String = "", timeout seconds: TimeInterval = 1.0, _ errorHandler: @escaping (_ error: Error) -> Void = { _ in }, file: StaticString = #filePath, line: UInt = #line) {
let expectation = XCTestExpectation()
expression.then { _ in
XCTFail("Promise fulfilled", file: file, line: line)
}.catch { error in
errorHandler(error)
}.always {
expectation.fulfill()
}
wait(for: [expectation], timeout: 1.0)
}
func XCTAssertRejects<T>(_ expression: Promise<T>, with expectedError: Error, _ message: @escaping @autoclosure () -> String = "", timeout seconds: TimeInterval = 1.0, file: StaticString = #filePath, line: UInt = #line) {
XCTAssertRejects(expression) { error in
XCTAssertEqual(expectedError as NSError, error as NSError, message(), file: file, line: line)
}
}
func wait<T>(for promise: Promise<T>, timeout seconds: TimeInterval = 1.0, file: StaticString = #filePath, line: UInt = #line) {
let expectation = XCTestExpectation()
promise.then { _ in
expectation.fulfill()
}.catch { error in
XCTFail("Promise rejected with error: \(error)", file: file, line: line)
}
wait(for: [expectation], timeout: 1.0)
}
func XCTAssertGetsNotExecuted<T>(_ promise: Promise<T>, timeout seconds: TimeInterval = 1.0, file: StaticString = #filePath, line: UInt = #line) {
let expectation = XCTestExpectation()
expectation.isInverted = true
promise.always {
expectation.fulfill()
}
wait(for: [expectation], timeout: seconds)
}
}