Swiftpack.co - norio-nomura/XCTAssertCrash as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by norio-nomura.
norio-nomura/XCTAssertCrash 0.2.0
Asserts that an expression crashes by using Mach Exception Handler or POSIX Signal Handler.
⭐️ 51
🕓 4 years ago
iOS macOS tvOS
.package(url: "https://github.com/norio-nomura/XCTAssertCrash.git", from: "0.2.0")

XCTAssertCrash

Asserts that an expression crashes by using Mach Exception Handler or POSIX Signal Handler.

On Apple Platforms except tvOS, it uses Mach Exception Handler.
On other Platforms like Linux or tvOS, it uses POSIX Signal Handler.

SwiftPM xcodebuild Nightly

Usage

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

Limitations

Mach Exception Handler

  • XCTAssertCrash can not handle crashes caused by abort().
  • XCTAssertCrash can not handle a breakpoint set in expression. On detecting a stop at a breakpoint set by the debugger, XCTAssertCrash generates assertionFailure().

POSIX Signal Handler

  • lldb will catch the signal and stop on crash before XCTAssertCrash detects it.
  • So, if the process is being debugged, XCTAssertCrash will skip the evaluation of the expression by default.
    To avoid this behavior,
    • Set skipXCTAssertCrashIfIsBeingDebugged to false.
      or
    • Disable debug.
      e.g. In Xcode, go to 'Edit Scheme > Test > Info' and uncheck 'Debug Executable'.

Author

Norio Nomura

License

This package is available under the MIT license. See the LICENSE file for more info.

References

GitHub

link
Stars: 51
Last commit: 4 years ago
Advertisement: IndiePitcher.com - Cold Email Software for Startups

Release Notes

0.2.0
4 years ago
  • Support lower os versions

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics