Skip to content

Commit

Permalink
Improve custom method for asserting on asynchronous expressions which…
Browse files Browse the repository at this point in the history
… should throw an error
  • Loading branch information
pereBohigas committed May 30, 2024
1 parent 041068d commit 25d35ce
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 24 deletions.
4 changes: 2 additions & 2 deletions Tests/SwiftPolyglotCoreTests/SwiftPolyglotCoreTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ final class SwiftPolyglotCoreTests: XCTestCase {
isRunningInAGitHubAction: false
)

await XCTAssertThrowsErrorAsync(try await swiftPolyglotCore.run())
await XCTAssertThrowsErrorAsync(swiftPolyglotCore.run, SwiftPolyglotError.missingTranslations)
}

func testStringCatalogWithMissingVariations() async throws {
Expand All @@ -87,6 +87,6 @@ final class SwiftPolyglotCoreTests: XCTestCase {
isRunningInAGitHubAction: false
)

await XCTAssertThrowsErrorAsync(try await swiftPolyglotCore.run())
await XCTAssertThrowsErrorAsync(swiftPolyglotCore.run, SwiftPolyglotError.missingTranslations)
}
}
34 changes: 12 additions & 22 deletions Tests/SwiftPolyglotCoreTests/XCTest+AsyncThrowingExpression.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,37 +30,27 @@ public func XCTAssertNoThrowAsync(
///
/// Example usage:
///
/// await assertThrowsAsyncError(
/// try await sut.function()
/// ) { error in
/// XCTAssertEqual(error as? MyError, MyError.specificError)
/// }
/// await assertThrowsAsyncError(sut.function, MyError.specificError)
///
/// - Parameters:
/// - expression: An asynchronous expression that can throw an error.
/// - message: An optional description of a failure.
/// - errorThrown: The error type that should be thrown.
/// - failureMessage: 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(
_ expression: @autoclosure () async throws -> some Any,
_ message: @autoclosure () -> String = "",
/// from: https://arturgruchala.com/testing-async-await-exceptions/
func XCTAssertThrowsErrorAsync<E>(
_ expression: () async throws -> some Any,
_ errorThrown: E,
failureMessage: String = "Asynchronous call did not throw an error.",
file: StaticString = #filePath,
line: UInt = #line,
_ errorHandler: (_ error: any Error) -> Void = { _ in }
) async {
line: UInt = #line
) async where E: Equatable, E: Error {
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)
}
XCTFail(failureMessage, file: file, line: line)
} catch {
errorHandler(error)
XCTAssertEqual(error as? E, errorThrown, "Asynchronous call did not throw the given error \"\(errorThrown)\".")
}
}

0 comments on commit 25d35ce

Please sign in to comment.