断言某个表达式会崩溃,通过使用 Mach 异常处理程序或 POSIX 信号处理程序。
在除 tvOS 之外的 Apple 平台上,它使用 Mach 异常处理程序。
在其他平台(如 Linux 或 tvOS)上,它使用 POSIX 信号处理程序。
/// Asserts that an expression crashes.
/// - Parameters:
/// - expression: An `expression` that can crash.
/// - message: An optional description of the failure.
/// - file: The file in which failure occurred.
/// Defaults to the file name of the test case in which this function was called.
/// - line: The line number on which failure occurred.
/// Defaults to the line number on which this function was called.
/// - signalHandler: An optional handler for signal that are produced by `expression`.
/// `SIGILL`, `SIGABRT` or `0` (if `expression` did not crash)
/// - stdoutHandler: An optional handler for stdout that are produced by `expression`.
/// - stderrHandler: An optional handler for stderr that are produced by `expression`.
/// - skipIfBeingDebugged: Skip `expression` if process is being debugged.
/// Use `skipXCTAssertCrashIfIsBeingDebugged` as default.
/// - Returns: A value of type `T?`, the result of evaluating the given `expression`.
/// `nil` if `expression` crashed.
@discardableResult
public func XCTAssertCrash<T>(
_ expression: @escaping @autoclosure () -> T,
_ message: @autoclosure () -> String = "",
file: StaticString = #file,
line: UInt = #line,
signalHandler: (Int32) -> Void = { _ in },
stdoutHandler: (String) -> Void = { _ in },
stderrHandler: (String) -> Void = { _ in },
skipIfBeingDebugged: Bool = skipXCTAssertCrashIfIsBeingDebugged
) -> T?
XCTAssertCrash
无法处理由 abort()
引起的崩溃。XCTAssertCrash
无法处理表达式中设置的断点。当检测到调试器设置的断点处停止时,XCTAssertCrash
会生成 assertionFailure()
。lldb
会在 XCTAssertCrash
检测到信号之前捕获信号并在崩溃时停止。XCTAssertCrash
将跳过表达式的求值。skipXCTAssertCrashIfIsBeingDebugged
设置为 false
。Norio Nomura
本软件包基于 MIT 许可证。有关更多信息,请参见 LICENSE 文件。