Swiftpack.co -  vinceplusplus/TestableAssert as Swift Package
Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
vinceplusplus/TestableAssert
Make Swift's assert(), assertionFailure(), precondition(), preconditionFailure(), fatalError() testable.
.package(url: "https://github.com/vinceplusplus/TestableAssert.git", from: "v1.0.2")

TestableAssert

Use TestableAssert to test if assertions in your code can catch programming errors properly

// TestLibrary.swift

func testWithAssert() {
  assert(false, "assert() message")
}

func testWithPreconditionFailure() {
  preconditionFailure("preconditionFailure() message")
}

func testWithFatalError() {
  fatalError("fatalError() message")
}

func testWithRandomFailure() {
  let testFuncs = [
    testWithAssert,
    testWithPreconditionFailure,
    testWithFatalError,
  ]
  guard let testFunc = testFuncs.randomElement() else {
    fatalError()
  }
  
  testFunc()
}

// Tests.swift

expectFailedAssertion({ testWithAssert() })
expectFailedPrecondition({ testWithPreconditionFailure() }, "preconditionFailure() message")
expectFatalError({ testWithFatalError() })
expectAnyFailure({ testWithRandomFailure() })

Installation

Xcode

Follow the official guide to add to your Xcode project

  1. Add the following package to the product target:
    https://github.com/vinceplusplus/TestableAssertSupport
    
  2. Add the following package to the unit test target:
    https://github.com/vinceplusplus/TestableAssert
    
  3. Add a configuration file to the product target to override built-in assertion functions:
    // TestableAssertSupport+Configuration.swift
    
    import TestableAssertSupport
    
    func assert(_ condition: @autoclosure () -> Bool, _ message: @autoclosure () -> String = String(), file: StaticString = #file, line: UInt = #line) {
      AssertFuncBodies.assert(condition(), message(), file, line)
    }
    
    func assertionFailure(_ message: @autoclosure () -> String = String(), file: StaticString = #file, line: UInt = #line) {
      AssertFuncBodies.assertionFailure(message(), file, line)
    }
    
    func precondition(_ condition: @autoclosure () -> Bool, _ message: @autoclosure () -> String = String(), file: StaticString = #file, line: UInt = #line) {
      AssertFuncBodies.precondition(condition(), message(), file, line)
    }
    
    func preconditionFailure(_ message: @autoclosure () -> String = String(), file: StaticString = #file, line: UInt = #line) -> Never {
      AssertFuncBodies.preconditionFailure(message(), file, line)
    }
    
    func fatalError(_ message: @autoclosure () -> String = String(), file: StaticString = #file, line: UInt = #line) -> Never {
      AssertFuncBodies.fatalError(message(), file, line)
    }
    

Swift Package Manager

  1. Add the following package dependencies:
    .package(url: "https://github.com/vinceplusplus/TestableAssertSupport", from: "1.0.0"),
    .package(url: "https://github.com/vinceplusplus/TestableAssert", from: "1.0.0"),
    
  2. Add TestableAssertSupport as a dependency to the product target
  3. Add TestableAssert as a dependency to the test target
  4. Add a configuration file to the product target to override built-in assertion functions:
    // TestableAssertSupport+Configuration.swift
    
    import TestableAssertSupport
    
    func assert(_ condition: @autoclosure () -> Bool, _ message: @autoclosure () -> String = String(), file: StaticString = #file, line: UInt = #line) {
      AssertFuncBodies.assert(condition(), message(), file, line)
    }
    
    func assertionFailure(_ message: @autoclosure () -> String = String(), file: StaticString = #file, line: UInt = #line) {
      AssertFuncBodies.assertionFailure(message(), file, line)
    }
    
    func precondition(_ condition: @autoclosure () -> Bool, _ message: @autoclosure () -> String = String(), file: StaticString = #file, line: UInt = #line) {
      AssertFuncBodies.precondition(condition(), message(), file, line)
    }
    
    func preconditionFailure(_ message: @autoclosure () -> String = String(), file: StaticString = #file, line: UInt = #line) -> Never {
      AssertFuncBodies.preconditionFailure(message(), file, line)
    }
    
    func fatalError(_ message: @autoclosure () -> String = String(), file: StaticString = #file, line: UInt = #line) -> Never {
      AssertFuncBodies.fatalError(message(), file, line)
    }
    

GitHub

link
Stars: 1
Last commit: 3 weeks ago

Ad: Job Offers

iOS Software Engineer @ Perry Street Software
Perry Street Software is Jack’d and SCRUFF. We are two of the world’s largest gay, bi, trans and queer social dating apps on iOS and Android. Our brands reach more than 20 million members worldwide so members can connect, meet and express themselves on a platform that prioritizes privacy and security. We invest heavily into SwiftUI and using Swift Packages to modularize the codebase.

Submit a free job ad (while I'm testing this). The analytics numbers for this website are here.

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