Predicate

public struct Predicate<T>

A Predicate is part of the new matcher API that provides assertions to expectations.

Given a code snippet:

expect(1).to(equal(2)) ^^^^^^^^ Called a “matcher”

A matcher consists of two parts a constructor function and the Predicate. The term Predicate is used as a separate name from Matcher to help transition custom matchers to the new Nimble matcher API.

The Predicate provide the heavy lifting on how to assert against a given value. Internally, predicates are simple wrappers around closures to provide static type information and allow composition and wrapping of existing behaviors.

  • Constructs a predicate that knows how take a given value

    Declaration

    Swift

    public init(_ matcher: @escaping (Expression<T>) throws -> PredicateResult)
  • Uses a predicate on a given value to see if it passes the predicate.

    @param expression The value to run the predicate’s logic against @returns A predicate result indicate passing or failing and an associated error message.

    Declaration

    Swift

    public func satisfies(_ expression: Expression<T>) throws -> PredicateResult
  • Like Predicate() constructor, but automatically guard against nil (actual) values

    Declaration

    Swift

    public static func define(matcher: @escaping (Expression<T>) throws -> PredicateResult) -> Predicate<T>
  • Defines a predicate with a default message that can be returned in the closure Also ensures the predicate’s actual value cannot pass with nil given.

    Declaration

    Swift

    public static func define(_ message: String = "match", matcher: @escaping (Expression<T>, ExpectationMessage) throws -> PredicateResult) -> Predicate<T>
  • Defines a predicate with a default message that can be returned in the closure Unlike define, this allows nil values to succeed if the given closure chooses to.

    Declaration

    Swift

    public static func defineNilable(_ message: String = "match", matcher: @escaping (Expression<T>, ExpectationMessage) throws -> PredicateResult) -> Predicate<T>
  • Provides a simple predicate definition that provides no control over the predefined error message.

    Also ensures the predicate’s actual value cannot pass with nil given.

    Declaration

    Swift

    public static func simple(_ message: String = "match", matcher: @escaping (Expression<T>) throws -> PredicateStatus) -> Predicate<T>
  • Provides a simple predicate definition that provides no control over the predefined error message.

    Unlike simple, this allows nil values to succeed if the given closure chooses to.

    Declaration

    Swift

    public static func simpleNilable(_ message: String = "match", matcher: @escaping (Expression<T>) throws -> PredicateStatus) -> Predicate<T>
  • Returns a new Predicate based on the current one that always fails if nil is given as the actual value.

    Declaration

    Swift

    public var requireNonNil: Predicate<T> { get }