Skip to content

Commit 275ed6e

Browse files
authored
Merge pull request #324 from csknns/main-XCTAssertThrowError-improvment
Change XCTAssertThrowsError function signature to rethrow when the error handler throws
2 parents a4a3ef6 + eb1ff85 commit 275ed6e

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

Sources/XCTest/Public/XCTAssert.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,12 @@ public func XCTFail(_ message: String = "", file: StaticString = #file, line: UI
377377
}
378378

379379
public func XCTAssertThrowsError<T>(_ expression: @autoclosure () throws -> T, _ message: @autoclosure () -> String = "", file: StaticString = #file, line: UInt = #line, _ errorHandler: (_ error: Swift.Error) -> Void = { _ in }) {
380+
let rethrowsOverload: (() throws -> T, () -> String, StaticString, UInt, (Swift.Error) throws -> Void) throws -> Void = XCTAssertThrowsError
381+
382+
try? rethrowsOverload(expression, message, file, line, errorHandler)
383+
}
384+
385+
public func XCTAssertThrowsError<T>(_ expression: @autoclosure () throws -> T, _ message: @autoclosure () -> String = "", file: StaticString = #file, line: UInt = #line, _ errorHandler: (_ error: Swift.Error) throws -> Void = { _ in }) rethrows {
380386
_XCTEvaluateAssertion(.throwsError, message: message(), file: file, line: line) {
381387
var caughtErrorOptional: Swift.Error?
382388
do {
@@ -386,7 +392,7 @@ public func XCTAssertThrowsError<T>(_ expression: @autoclosure () throws -> T, _
386392
}
387393

388394
if let caughtError = caughtErrorOptional {
389-
errorHandler(caughtError)
395+
try errorHandler(caughtError)
390396
return .success
391397
} else {
392398
return .expectedFailure("did not throw error")

Tests/Functional/ErrorHandling/main.swift

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ class ErrorHandling: XCTestCase {
1616
static var allTests = {
1717
return [
1818
// Tests for XCTAssertThrowsError
19+
("test_shouldRethrowErrorFromHandler", test_shouldRethrowErrorFromHandler),
20+
("test_shouldNotRethrowWhenHandlerDoesNotThrow", test_shouldNotRethrowWhenHandlerDoesNotThrow),
1921
("test_shouldButDoesNotThrowErrorInAssertion", test_shouldButDoesNotThrowErrorInAssertion),
2022
("test_shouldThrowErrorInAssertion", test_shouldThrowErrorInAssertion),
2123
("test_throwsErrorInAssertionButFailsWhenCheckingError", test_throwsErrorInAssertionButFailsWhenCheckingError),
@@ -59,6 +61,19 @@ class ErrorHandling: XCTestCase {
5961
throw SomeError.anError("an error message")
6062
}
6163

64+
// CHECK: Test Case 'ErrorHandling.test_shouldRethrowErrorFromHandler' started at \d+-\d+-\d+ \d+:\d+:\d+\.\d+
65+
// CHECK: .*[/\\]ErrorHandling[/\\]main.swift:[[@LINE+3]]: error: ErrorHandling.test_shouldRethrowErrorFromHandler : XCTAssertThrowsError threw error "anError\("an error message"\)" -
66+
// CHECK: Test Case 'ErrorHandling.test_shouldRethrowErrorFromHandler' failed \(\d+\.\d+ seconds\)
67+
func test_shouldRethrowErrorFromHandler() throws {
68+
try XCTAssertThrowsError(try functionThatDoesThrowError()) {_ in try functionThatDoesThrowError() }
69+
}
70+
71+
// CHECK: Test Case 'ErrorHandling.test_shouldNotRethrowWhenHandlerDoesNotThrow' started at \d+-\d+-\d+ \d+:\d+:\d+\.\d+
72+
// CHECK: Test Case 'ErrorHandling.test_shouldNotRethrowWhenHandlerDoesNotThrow' passed \(\d+\.\d+ seconds\)
73+
func test_shouldNotRethrowWhenHandlerDoesNotThrow() throws {
74+
try XCTAssertThrowsError(try functionThatDoesThrowError()) {_ in try functionThatDoesNotThrowError() }
75+
}
76+
6277
// CHECK: Test Case 'ErrorHandling.test_shouldButDoesNotThrowErrorInAssertion' started at \d+-\d+-\d+ \d+:\d+:\d+\.\d+
6378
// CHECK: .*[/\\]ErrorHandling[/\\]main.swift:[[@LINE+3]]: error: ErrorHandling.test_shouldButDoesNotThrowErrorInAssertion : XCTAssertThrowsError failed: did not throw error -
6479
// CHECK: Test Case 'ErrorHandling.test_shouldButDoesNotThrowErrorInAssertion' failed \(\d+\.\d+ seconds\)
@@ -278,11 +293,11 @@ class ErrorHandling: XCTestCase {
278293
}
279294

280295
// CHECK: Test Suite 'ErrorHandling' failed at \d+-\d+-\d+ \d+:\d+:\d+\.\d+
281-
// CHECK: \t Executed \d+ tests, with \d+ failures \(5 unexpected\) in \d+\.\d+ \(\d+\.\d+\) seconds
296+
// CHECK: \t Executed \d+ tests, with \d+ failures \(6 unexpected\) in \d+\.\d+ \(\d+\.\d+\) seconds
282297

283298
XCTMain([testCase(ErrorHandling.allTests)])
284299

285300
// CHECK: Test Suite '.*\.xctest' failed at \d+-\d+-\d+ \d+:\d+:\d+\.\d+
286-
// CHECK: \t Executed \d+ tests, with \d+ failures \(5 unexpected\) in \d+\.\d+ \(\d+\.\d+\) seconds
301+
// CHECK: \t Executed \d+ tests, with \d+ failures \(6 unexpected\) in \d+\.\d+ \(\d+\.\d+\) seconds
287302
// CHECK: Test Suite 'All tests' failed at \d+-\d+-\d+ \d+:\d+:\d+\.\d+
288-
// CHECK: \t Executed \d+ tests, with \d+ failures \(5 unexpected\) in \d+\.\d+ \(\d+\.\d+\) seconds
303+
// CHECK: \t Executed \d+ tests, with \d+ failures \(6 unexpected\) in \d+\.\d+ \(\d+\.\d+\) seconds

0 commit comments

Comments
 (0)