Skip to content

Commit e5e8eb7

Browse files
committed
First pass at implementing XCTAssertNoThrow
1 parent 8fff45d commit e5e8eb7

File tree

1 file changed

+33
-6
lines changed

1 file changed

+33
-6
lines changed

stdlib/public/SDK/XCTest/XCTest.swift

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1030,6 +1030,39 @@ public func XCTAssertThrowsError<T>(_ expression: @autoclosure () throws -> T, _
10301030
}
10311031
}
10321032

1033+
public func XCTAssertNoThrow<T>(_ expression: @autoclosure () throws -> T, _ message: @autoclosure () -> String = "", file: StaticString = #file, line: UInt = #line, _ errorHandler: (_ error: Error) -> Void = { _ in }) {
1034+
let assertionType = _XCTAssertionType.assertion_NoThrow
1035+
1036+
// evaluate expression exactly once
1037+
var caughtErrorOptional: Error?
1038+
1039+
let result = _XCTRunThrowableBlock {
1040+
do {
1041+
_ = try expression
1042+
} catch {
1043+
caughtErrorOptional = error
1044+
}
1045+
}
1046+
1047+
switch result {
1048+
case .success:
1049+
guard let caughtError = caughtErrorOptional else {
1050+
return
1051+
}
1052+
1053+
_XCTRegisterFailure(true, "XCTAssertNoThrow failed: threw error \"\(error)\"", message, file, line)
1054+
1055+
case .failedWithError(let error):
1056+
_XCTRegisterFailure(false, "XCTAssertNoThrow failed: threw error \"\(error)\"", message, file, line)
1057+
1058+
case .failedWithException(_, _, let reason):
1059+
_XCTRegisterFailure(true, "XCTAssertNoThrow failed: throwing \(reason)", message, file, line)
1060+
1061+
case .failedWithUnknownException:
1062+
_XCTRegisterFailure(true, "XCTAssertNoThrow failed: throwing an unknown exception", message, file, line)
1063+
}
1064+
}
1065+
10331066
#if XCTEST_ENABLE_EXCEPTION_ASSERTIONS
10341067
// --- Currently-Unsupported Assertions ---
10351068

@@ -1051,12 +1084,6 @@ public func XCTAssertThrowsSpecificNamed(_ expression: @autoclosure () -> Any?,
10511084
// FIXME: Unsupported
10521085
}
10531086

1054-
public func XCTAssertNoThrow(_ expression: @autoclosure () -> Any?, _ message: @autoclosure () -> String = "", file: StaticString = #file, line: UInt = #line) {
1055-
let assertionType = _XCTAssertionType.assertion_NoThrow
1056-
1057-
// FIXME: Unsupported
1058-
}
1059-
10601087
public func XCTAssertNoThrowSpecific(_ expression: @autoclosure () -> Any?, _ exception: Any, _ message: @autoclosure () -> String = "", file: StaticString = #file, line: UInt = #line) {
10611088
let assertionType = _XCTAssertionType.assertion_NoThrowSpecific
10621089

0 commit comments

Comments
 (0)