XCTAssertCrash

断言某个表达式会崩溃,通过使用 Mach 异常处理程序或 POSIX 信号处理程序。

在除 tvOS 之外的 Apple 平台上,它使用 Mach 异常处理程序。
在其他平台(如 Linux 或 tvOS)上,它使用 POSIX 信号处理程序。

SwiftPM xcodebuild Nightly

用法

/// 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?

局限性

Mach 异常处理程序

POSIX 信号处理程序

作者

Norio Nomura

许可证

本软件包基于 MIT 许可证。有关更多信息,请参见 LICENSE 文件。

参考