Skip to content

Commit

Permalink
Add XCTest extension for testing async throwing expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
pereBohigas committed May 12, 2024
1 parent d9409af commit 1a7cb21
Showing 1 changed file with 77 additions and 0 deletions.
77 changes: 77 additions & 0 deletions Tests/SwiftPolyglotCoreTests/XCTest+AsyncThrowingExpression.swift
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)
}
}

0 comments on commit 1a7cb21

Please sign in to comment.