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 run successfully without throwing an error
  • Loading branch information
pereBohigas committed May 30, 2024
1 parent a4753a3 commit 041068d
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 17 deletions.
4 changes: 2 additions & 2 deletions Tests/SwiftPolyglotCoreTests/SwiftPolyglotCoreTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ final class SwiftPolyglotCoreTests: XCTestCase {
isRunningInAGitHubAction: false
)

await XCTAssertNoThrowAsync(try await swiftPolyglotCore.run())
await XCTAssertNoThrowAsync(swiftPolyglotCore.run)
}

func testStringCatalogVariationsFullyTranslated() async throws {
Expand All @@ -43,7 +43,7 @@ final class SwiftPolyglotCoreTests: XCTestCase {
isRunningInAGitHubAction: false
)

await XCTAssertNoThrowAsync(try await swiftPolyglotCore.run())
await XCTAssertNoThrowAsync(swiftPolyglotCore.run)
}

func testStringCatalogWithMissingTranslations() async throws {
Expand Down
20 changes: 5 additions & 15 deletions Tests/SwiftPolyglotCoreTests/XCTest+AsyncThrowingExpression.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,23 @@ import XCTest
///
/// Example usage:
///
/// await assertNoThrowAsync(
/// try await sut.function()
/// ) { error in
/// XCTAssertEqual(error as? MyError, MyError.specificError)
/// }
/// await assertNoThrowAsync(sut.function)
///
/// - Parameters:
/// - expression: An asynchronous expression that can throw an error.
/// - message: An optional description of a failure.
/// - 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.
public func XCTAssertNoThrowAsync(
_ expression: @autoclosure () async throws -> some Any,
_ message: @autoclosure () -> String = "",
_ expression: () async throws -> some Any,
failureMessage: String = "Asynchronous call did throw an error.",
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)
}
XCTFail(failureMessage, file: file, line: line)
}
}

Expand Down

0 comments on commit 041068d

Please sign in to comment.