Giter Site home page Giter Site logo

ikesyo / cwlpreconditiontesting Goto Github PK

View Code? Open in Web Editor NEW

This project forked from mattgallagher/cwlpreconditiontesting

0.0 2.0 0.0 215 KB

A Mach exception handler that allows Swift precondition failures to be caught and tested.

License: ISC License

Objective-C 24.07% Swift 44.15% C 31.77%

cwlpreconditiontesting's Introduction

CwlPreconditionTesting

A Mach exception handler, written in Swift and Objective-C, that allows EXC_BAD_INSTRUCTION (as raised by Swift's assertionFailure/preconditionFailure/fatalError) to be caught and tested.

NOTE: the iOS code runs in the simulator only. It is for logic testing and cannot be deployed to the device due to the Mach exception API being private on iOS.

For an extended discussion of this code, please see the Cocoa with Love article:

Partial functions in Swift, Part 2: Catching precondition failures

Requirements

From version 2.0.0-beta.1, building CwlPreconditionTesting requires Swift 5 or newer and the Swift Package Manager.

For use with older versions of Swift or other package managers, use version 1.2.0 or older.

Adding to your project

Add the following to the dependencies array in your "Package.swift" file:

 .package(url: "https://github.com/mattgallagher/CwlPreconditionTesting.git", from: Version("2.0.0-beta.1"))

Or by adding https://github.com/mattgallagher/CwlPreconditionTesting.git, version 2.0.0-beta.1 or later, to the list of Swift packages for any project in Xcode.

Usage

On macOS and iOS you can use the regular version:

import CwlPreconditionTesting

let e = catchBadInstruction {
	precondition(false, "THIS PRECONDITION FAILURE IS EXPECTED")
}

on tvOS, Linux and other platforms, you can use the POSIX version:

import CwlPosixPreconditionTesting

let e = catchBadInstruction {
	precondition(false, "THIS PRECONDITION FAILURE IS EXPECTED")
}

Warning: this POSIX version can't be used when lldb is attached since lldb's Mach exception handler blocks the SIGILL from ever occurring. You should disable the "Debug Executable" setting for the tests in Xcode. The POSIX version of the signal handler is also whole process (rather than correctly scoped to the thread where the "catch" occurs).

cwlpreconditiontesting's People

Contributors

abbeycode avatar ikesyo avatar jeffh avatar mattgallagher avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.