-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add XCTest extension for testing async throwing expressions
- Loading branch information
1 parent
d9409af
commit 1a7cb21
Showing
1 changed file
with
77 additions
and
0 deletions.
There are no files selected for viewing
77 changes: 77 additions & 0 deletions
77
Tests/SwiftPolyglotCoreTests/XCTest+AsyncThrowingExpression.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import XCTest | ||
|
||
/// Asserts that an asynchronous expression do not throw an error. | ||
/// (Intended to function as a drop-in asynchronous version of `XCTAssertNoThrow`.) | ||
/// | ||
/// Example usage: | ||
/// | ||
/// await assertNoThrowAsync( | ||
/// try await sut.function() | ||
/// ) { error in | ||
/// XCTAssertEqual(error as? MyError, MyError.specificError) | ||
/// } | ||
/// | ||
/// - Parameters: | ||
/// - expression: An asynchronous expression that can throw an error. | ||
/// - message: An optional description of a failure. | ||
/// - file: The file where the failure occurs. The default is the file path of the test case where this function is being called. | ||
/// - line: The line number where the failure occurs. The default is the line number where this function is being called. | ||
public func XCTAssertNoThrowAsync<T>( | ||
_ expression: @autoclosure () async throws -> T, | ||
_ message: @autoclosure () -> String = "", | ||
file: StaticString = #filePath, | ||
line: UInt = #line | ||
) async { | ||
do { | ||
_ = try await expression() | ||
} catch { | ||
// expected no error to be thrown, but it was | ||
let customMessage = message() | ||
if customMessage.isEmpty { | ||
XCTFail("Asynchronous call did throw an error.", file: file, line: line) | ||
} else { | ||
XCTFail(customMessage, file: file, line: line) | ||
} | ||
} | ||
} | ||
|
||
/// Asserts that an asynchronous expression throws an error. | ||
/// (Intended to function as a drop-in asynchronous version of `XCTAssertThrowsError`.) | ||
/// | ||
/// Example usage: | ||
/// | ||
/// await assertThrowsAsyncError( | ||
/// try await sut.function() | ||
/// ) { error in | ||
/// XCTAssertEqual(error as? MyError, MyError.specificError) | ||
/// } | ||
/// | ||
/// - Parameters: | ||
/// - expression: An asynchronous expression that can throw an error. | ||
/// - message: An optional description of a failure. | ||
/// - file: The file where the failure occurs. The default is the file path of the test case where this function is being called. | ||
/// - line: The line number where the failure occurs. The default is the line number where this function is being called. | ||
/// - errorHandler: An optional handler for errors that `expression` throws. | ||
/// | ||
/// from: https://gitlab.com/-/snippets/2567566 | ||
public func XCTAssertThrowsErrorAsync<T>( | ||
_ expression: @autoclosure () async throws -> T, | ||
_ message: @autoclosure () -> String = "", | ||
file: StaticString = #filePath, | ||
line: UInt = #line, | ||
_ errorHandler: (_ error: any Error) -> Void = { _ in } | ||
) async { | ||
do { | ||
_ = try await expression() | ||
// expected error to be thrown, but it was not | ||
let customMessage = message() | ||
if customMessage.isEmpty { | ||
XCTFail("Asynchronous call did not throw an error.", file: file, line: line) | ||
} else { | ||
XCTFail(customMessage, file: file, line: line) | ||
} | ||
} catch { | ||
errorHandler(error) | ||
} | ||
} | ||
|