Function any(_:where:)
public func any<T>(_ type: T.Type = T.self, where predicate: @escaping (_ value: T) -> Bool) -> T
Matches any argument values where the predicate returns true
.
Argument matching allows you to stub or verify specific invocations of parameterized methods.
Use the argument matcher any(where:)
to match objects with custom equality logic. This is
particularly useful for parameter types that do not conform to Equatable
.
// Value type not explicitly conforming to `Equatable`
struct Fruit {
let size: Int
}
protocol Bird {
func eat(_ fruit: Fruit)
}
given(bird.eat(any(where: { $0.size < 100 })))
.will { print($0.size) }
let apple = Fruit(size: 42)
bird.eat(apple) // Prints "42"
let pear = Fruit(size: 9001)
bird.eat(pear) // Error: Missing stubbed implementation
Methods overloaded by parameter type can be disambiguated by explicitly specifying the type.
protocol Bird {
func eat<T>(_ object: T) // Overloaded generically
func eat(_ fruit: Fruit) // Overloaded explicitly
func eat(_ fruits: [Fruit])
}
given(bird.eat(any(Fruit.self, where: { $0.size < 100 })))
.will { print($0) }
let apple = Fruit(size: 42)
bird.eat(apple) // Prints "42"
bird.eat("Apple") // Error: Missing stubbed implementation
Parameters
Name | Type | Description |
---|---|---|
type | T.Type |
The parameter type used to disambiguate overloaded methods. |
predicate | @escaping (_ value: T) -> Bool |
A closure that takes a value and returns |