From 6286750a9194aaeefcea0f894cd58bbbc32d1939 Mon Sep 17 00:00:00 2001 From: Christos Koninis Date: Sat, 26 Jun 2021 20:34:02 +0300 Subject: [PATCH 1/2] Overloaded XCTAssertThrowsError function to alllow to rethrow when the error handler throws --- Sources/XCTest/Public/XCTAssert.swift | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/Sources/XCTest/Public/XCTAssert.swift b/Sources/XCTest/Public/XCTAssert.swift index 109f2721a..c523b7577 100644 --- a/Sources/XCTest/Public/XCTAssert.swift +++ b/Sources/XCTest/Public/XCTAssert.swift @@ -377,6 +377,12 @@ public func XCTFail(_ message: String = "", file: StaticString = #file, line: UI } public func XCTAssertThrowsError(_ expression: @autoclosure () throws -> T, _ message: @autoclosure () -> String = "", file: StaticString = #file, line: UInt = #line, _ errorHandler: (_ error: Swift.Error) -> Void = { _ in }) { + let rethrowsOverload: (() throws -> T, () -> String, StaticString, UInt, (Swift.Error) throws -> Void) throws -> Void = XCTAssertThrowsError + + try? rethrowsOverload(expression, message, file, line, errorHandler) +} + +public func XCTAssertThrowsError(_ expression: @autoclosure () throws -> T, _ message: @autoclosure () -> String = "", file: StaticString = #file, line: UInt = #line, _ errorHandler: (_ error: Swift.Error) throws -> Void = { _ in }) rethrows { _XCTEvaluateAssertion(.throwsError, message: message(), file: file, line: line) { var caughtErrorOptional: Swift.Error? do { @@ -386,7 +392,7 @@ public func XCTAssertThrowsError(_ expression: @autoclosure () throws -> T, _ } if let caughtError = caughtErrorOptional { - errorHandler(caughtError) + try errorHandler(caughtError) return .success } else { return .expectedFailure("did not throw error") From eb1ff85603898625ed5d2b3f18e463eabad33f1a Mon Sep 17 00:00:00 2001 From: Christos Koninis Date: Sun, 27 Jun 2021 13:36:37 +0300 Subject: [PATCH 2/2] Added tests for overloaded XCTAssertThrowsError to validate that it rethrows when the error handler throws --- Tests/Functional/ErrorHandling/main.swift | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/Tests/Functional/ErrorHandling/main.swift b/Tests/Functional/ErrorHandling/main.swift index 84636744a..8d0511b1e 100644 --- a/Tests/Functional/ErrorHandling/main.swift +++ b/Tests/Functional/ErrorHandling/main.swift @@ -16,6 +16,8 @@ class ErrorHandling: XCTestCase { static var allTests = { return [ // Tests for XCTAssertThrowsError + ("test_shouldRethrowErrorFromHandler", test_shouldRethrowErrorFromHandler), + ("test_shouldNotRethrowWhenHandlerDoesNotThrow", test_shouldNotRethrowWhenHandlerDoesNotThrow), ("test_shouldButDoesNotThrowErrorInAssertion", test_shouldButDoesNotThrowErrorInAssertion), ("test_shouldThrowErrorInAssertion", test_shouldThrowErrorInAssertion), ("test_throwsErrorInAssertionButFailsWhenCheckingError", test_throwsErrorInAssertionButFailsWhenCheckingError), @@ -59,6 +61,19 @@ class ErrorHandling: XCTestCase { throw SomeError.anError("an error message") } +// CHECK: Test Case 'ErrorHandling.test_shouldRethrowErrorFromHandler' started at \d+-\d+-\d+ \d+:\d+:\d+\.\d+ +// CHECK: .*[/\\]ErrorHandling[/\\]main.swift:[[@LINE+3]]: error: ErrorHandling.test_shouldRethrowErrorFromHandler : XCTAssertThrowsError threw error "anError\("an error message"\)" - +// CHECK: Test Case 'ErrorHandling.test_shouldRethrowErrorFromHandler' failed \(\d+\.\d+ seconds\) + func test_shouldRethrowErrorFromHandler() throws { + try XCTAssertThrowsError(try functionThatDoesThrowError()) {_ in try functionThatDoesThrowError() } + } + +// CHECK: Test Case 'ErrorHandling.test_shouldNotRethrowWhenHandlerDoesNotThrow' started at \d+-\d+-\d+ \d+:\d+:\d+\.\d+ +// CHECK: Test Case 'ErrorHandling.test_shouldNotRethrowWhenHandlerDoesNotThrow' passed \(\d+\.\d+ seconds\) + func test_shouldNotRethrowWhenHandlerDoesNotThrow() throws { + try XCTAssertThrowsError(try functionThatDoesThrowError()) {_ in try functionThatDoesNotThrowError() } + } + // CHECK: Test Case 'ErrorHandling.test_shouldButDoesNotThrowErrorInAssertion' started at \d+-\d+-\d+ \d+:\d+:\d+\.\d+ // CHECK: .*[/\\]ErrorHandling[/\\]main.swift:[[@LINE+3]]: error: ErrorHandling.test_shouldButDoesNotThrowErrorInAssertion : XCTAssertThrowsError failed: did not throw error - // CHECK: Test Case 'ErrorHandling.test_shouldButDoesNotThrowErrorInAssertion' failed \(\d+\.\d+ seconds\) @@ -278,11 +293,11 @@ class ErrorHandling: XCTestCase { } // CHECK: Test Suite 'ErrorHandling' failed at \d+-\d+-\d+ \d+:\d+:\d+\.\d+ -// CHECK: \t Executed \d+ tests, with \d+ failures \(5 unexpected\) in \d+\.\d+ \(\d+\.\d+\) seconds +// CHECK: \t Executed \d+ tests, with \d+ failures \(6 unexpected\) in \d+\.\d+ \(\d+\.\d+\) seconds XCTMain([testCase(ErrorHandling.allTests)]) // CHECK: Test Suite '.*\.xctest' failed at \d+-\d+-\d+ \d+:\d+:\d+\.\d+ -// CHECK: \t Executed \d+ tests, with \d+ failures \(5 unexpected\) in \d+\.\d+ \(\d+\.\d+\) seconds +// CHECK: \t Executed \d+ tests, with \d+ failures \(6 unexpected\) in \d+\.\d+ \(\d+\.\d+\) seconds // CHECK: Test Suite 'All tests' failed at \d+-\d+-\d+ \d+:\d+:\d+\.\d+ -// CHECK: \t Executed \d+ tests, with \d+ failures \(5 unexpected\) in \d+\.\d+ \(\d+\.\d+\) seconds +// CHECK: \t Executed \d+ tests, with \d+ failures \(6 unexpected\) in \d+\.\d+ \(\d+\.\d+\) seconds