Swiftpack.co - vinceplusplus/TestableAssert as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by vinceplusplus.
vinceplusplus/TestableAssert v1.0.2
Make Swift's assert(), assertionFailure(), precondition(), preconditionFailure(), fatalError() testable.
⭐️ 1
🕓 2 years ago
iOS macOS tvOS
.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: 2 years ago
Advertisement: IndiePitcher.com - Cold Email Software for Startups

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