Add swiftdoc for convenience matchers as well as some info to README.md.
This commit is contained in:
parent
c5f6d0cd25
commit
d48f2ab5cc
|
@ -277,6 +277,8 @@ These basic values are extended to conform to `Matchable`:
|
|||
- `UInt32`
|
||||
- `UInt64`
|
||||
|
||||
Matchers for `Array`, `Dictionary`, and `Set` are automatically synthesised as long as the type of the element conforms to `Matchable`.
|
||||
|
||||
#### B) Custom matchers
|
||||
If Cuckoo doesn't know the type you are trying to compare, you have to write your own method `equal(to:)` using a `ParameterMatcher`. Add this method to your test file:
|
||||
|
||||
|
@ -329,6 +331,8 @@ anyThrowingClosure()
|
|||
notNil()
|
||||
```
|
||||
|
||||
Cuckoo also provides plenty of convenience matchers for sequences and dictionaries, allowing you to check if a sequence is a superset of a certain sequence, contains at least one of its elements, or is completely disjunct from it.
|
||||
|
||||
`Matchable` can be chained with methods `or` and `and` like so:
|
||||
|
||||
```Swift
|
||||
|
|
|
@ -18,9 +18,21 @@ extension Array: Matchable where Element: Matchable, Element == Element.MatchedT
|
|||
|
||||
|
||||
// MARK: Contains ANY of the elements.
|
||||
public func containsAnyOf<T>(values elements: T..., where equality: @escaping (T, T) -> Bool) -> ParameterMatcher<[T]> {
|
||||
/**
|
||||
* Matcher for sequences of elements that checks if at least one of the passed elements matches the passed sequence.
|
||||
* - parameter values: Variadic elements that are used for matching.
|
||||
* - parameter where: Closure for determining equality of elements that don't conform to `Equatable`.
|
||||
* - returns: ParameterMatcher object.
|
||||
*/
|
||||
public func containsAnyOf<T, S: Sequence>(values elements: T..., where equality: @escaping (T, T) -> Bool) -> ParameterMatcher<S> where T == S.Element {
|
||||
return containsAnyOf(elements, where: equality)
|
||||
}
|
||||
/**
|
||||
* Matcher for sequences of elements that checks if at least one of the passed elements matches the passed sequence.
|
||||
* - parameter elements: Elements that are used for matching.
|
||||
* - parameter where: Closure for determining equality of elements that don't conform to `Equatable`.
|
||||
* - returns: ParameterMatcher object.
|
||||
*/
|
||||
public func containsAnyOf<IN: Sequence, OUT: Sequence>(_ elements: IN, where equality: @escaping (IN.Element, IN.Element) -> Bool) -> ParameterMatcher<OUT> where IN.Element == OUT.Element {
|
||||
return ParameterMatcher { sequence in
|
||||
elements.contains { element in
|
||||
|
@ -31,16 +43,36 @@ public func containsAnyOf<IN: Sequence, OUT: Sequence>(_ elements: IN, where equ
|
|||
}
|
||||
}
|
||||
|
||||
public func containsAnyOf<T: Equatable>(values elements: T...) -> ParameterMatcher<[T]> {
|
||||
/**
|
||||
* Matcher for sequences of `Equatable` elements that checks if at least one of the passed elements matches the passed sequence.
|
||||
* - parameter values: Variadic elements that are used for matching.
|
||||
* - returns: ParameterMatcher object.
|
||||
*/
|
||||
public func containsAnyOf<T: Equatable, S: Sequence>(values elements: T...) -> ParameterMatcher<S> where T == S.Element {
|
||||
return containsAnyOf(elements)
|
||||
}
|
||||
/**
|
||||
* Matcher for sequences of `Equatable` elements that checks if at least one of the passed elements matches the passed sequence.
|
||||
* - parameter elements: Elements that are used for matching.
|
||||
* - returns: ParameterMatcher object.
|
||||
*/
|
||||
public func containsAnyOf<IN: Sequence, OUT: Sequence>(_ elements: IN) -> ParameterMatcher<OUT> where IN.Element: Equatable, IN.Element == OUT.Element {
|
||||
return containsAnyOf(elements, where: ==)
|
||||
}
|
||||
|
||||
public func containsAnyOf<T: Hashable>(values elements: T...) -> ParameterMatcher<[T]> {
|
||||
/**
|
||||
* Matcher for sequences of `Hashable` elements that checks if at least one of the passed elements matches the passed sequence.
|
||||
* - parameter values: Variadic elements that are used for matching.
|
||||
* - returns: ParameterMatcher object.
|
||||
*/
|
||||
public func containsAnyOf<T: Hashable, S: Sequence>(values elements: T...) -> ParameterMatcher<S> where T == S.Element {
|
||||
return containsAnyOf(elements)
|
||||
}
|
||||
/**
|
||||
* Matcher for sequences of `Hashable` elements that checks if at least one of the passed elements matches the passed sequence.
|
||||
* - parameter elements: Elements that are used for matching.
|
||||
* - returns: ParameterMatcher object.
|
||||
*/
|
||||
public func containsAnyOf<IN: Sequence, OUT: Sequence>(_ elements: IN) -> ParameterMatcher<OUT> where IN.Element: Hashable, IN.Element == OUT.Element {
|
||||
return ParameterMatcher { sequence in
|
||||
let set = Set(sequence)
|
||||
|
@ -51,30 +83,91 @@ public func containsAnyOf<IN: Sequence, OUT: Sequence>(_ elements: IN) -> Parame
|
|||
|
||||
|
||||
// MARK: Contains ALL of the elements.
|
||||
public func containsAllOf<T>(values elements: T..., where equality: @escaping (T, T) -> Bool) -> ParameterMatcher<[T]> {
|
||||
/**
|
||||
* Matcher for sequences of elements that checks if all of the passed elements match the passed sequence.
|
||||
* - parameter values: Variadic elements that are used for matching.
|
||||
* - parameter where: Closure for determining equality of elements that don't conform to `Equatable`.
|
||||
* - returns: ParameterMatcher object.
|
||||
* - NOTE: When matching an Array, this matcher expects items as many times as they are present in the passed elements.
|
||||
*/
|
||||
public func containsAllOf<T, S: Sequence>(values elements: T..., where equality: @escaping (T, T) -> Bool) -> ParameterMatcher<S> where T == S.Element {
|
||||
return containsAllOf(elements, where: equality)
|
||||
}
|
||||
/**
|
||||
* Matcher for sequences of elements that checks if all of the passed elements match the passed sequence.
|
||||
* - parameter elements: Elements that are used for matching.
|
||||
* - parameter where: Closure for determining equality of elements that don't conform to `Equatable`.
|
||||
* - returns: ParameterMatcher object.
|
||||
* - NOTE: When passing an Array, this matcher expects items as many times as they are present in the passed elements.
|
||||
*/
|
||||
public func containsAllOf<IN: Sequence, OUT: Sequence>(_ elements: IN, where equality: @escaping (IN.Element, IN.Element) -> Bool) -> ParameterMatcher<OUT> where IN.Element == OUT.Element {
|
||||
return ParameterMatcher { sequence in
|
||||
elements.allSatisfy { element in
|
||||
sequence.contains {
|
||||
equality($0, element)
|
||||
var matchedSequence = sequence.map { (element: $0, matched: false) }
|
||||
for element in elements {
|
||||
if let matchedIndex = matchedSequence.firstIndex(where: { !$0.matched && equality($0.element, element) }) {
|
||||
matchedSequence[matchedIndex].matched = true
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
public func containsAllOf<T: Equatable>(values elements: T...) -> ParameterMatcher<[T]> {
|
||||
/**
|
||||
* Matcher for sequences of `Equatable` elements that checks if all of the passed elements match the passed sequence.
|
||||
* - parameter values: Variadic elements that are used for matching.
|
||||
* - returns: ParameterMatcher object.
|
||||
* - NOTE: When passing an Array, this matcher expects items as many times as they are present in the passed elements.
|
||||
*/
|
||||
public func containsAllOf<T: Equatable, S: Sequence>(values elements: T...) -> ParameterMatcher<S> where T == S.Element {
|
||||
return containsAllOf(elements)
|
||||
}
|
||||
/**
|
||||
* Matcher for sequences of `Equatable` elements that checks if all of the passed elements match the passed sequence.
|
||||
* - parameter elements: Elements that are used for matching.
|
||||
* - returns: ParameterMatcher object.
|
||||
* - NOTE: When passing an Array, this matcher expects items as many times as they are present in the passed elements.
|
||||
*/
|
||||
public func containsAllOf<IN: Sequence, OUT: Sequence>(_ elements: IN) -> ParameterMatcher<OUT> where IN.Element: Equatable, IN.Element == OUT.Element {
|
||||
return containsAllOf(elements, where: ==)
|
||||
}
|
||||
|
||||
/**
|
||||
* Matcher for sequences of `Hashable` elements that checks if all of the passed elements match the passed sequence.
|
||||
* - parameter values: Variadic elements that are used for matching.
|
||||
* - returns: ParameterMatcher object.
|
||||
* - NOTE: When passing an Array, this matcher expects items as many times as they are present in the passed elements.
|
||||
*/
|
||||
public func containsAllOf<T: Hashable>(values elements: T...) -> ParameterMatcher<[T]> {
|
||||
return containsAllOf(elements)
|
||||
}
|
||||
/**
|
||||
* Matcher for sequences of `Hashable` elements that checks if all of the passed elements match the passed sequence.
|
||||
* - parameter elements: Elements that are used for matching.
|
||||
* - returns: ParameterMatcher object.
|
||||
* - NOTE: When passing an Array, this matcher expects items as many times as they are present in the passed elements.
|
||||
*/
|
||||
public func containsAllOf<IN: Sequence, OUT: Sequence>(_ elements: IN) -> ParameterMatcher<OUT> where IN.Element: Hashable, IN.Element == OUT.Element {
|
||||
return ParameterMatcher { sequence in
|
||||
let sequenceDictionary: [IN.Element: Int] = sequence.reduce(into: [:]) { dictionary, element in
|
||||
dictionary[element, default: 0] += 1
|
||||
}
|
||||
let elementsDictionary: [IN.Element: Int] = elements.reduce(into: [:]) { dictionary, element in
|
||||
dictionary[element, default: 0] += 1
|
||||
}
|
||||
|
||||
return elementsDictionary.allSatisfy { key, element in
|
||||
sequenceDictionary[key].map { $0 == element } ?? false
|
||||
}
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Matcher for `Set`s of `Hashable` elements that checks if all of the passed elements match the passed `Set`.
|
||||
* - parameter elements: Elements that are used for matching.
|
||||
* - returns: ParameterMatcher object.
|
||||
*/
|
||||
public func containsAllOf<IN: Sequence, OUT: Sequence>(_ elements: IN) -> ParameterMatcher<OUT> where IN.Element: Hashable, IN.Element == OUT.Element, IN: SetAlgebra {
|
||||
return ParameterMatcher { sequence in
|
||||
let set = Set(sequence)
|
||||
let elementsSet = Set(elements)
|
||||
|
@ -84,44 +177,107 @@ public func containsAllOf<IN: Sequence, OUT: Sequence>(_ elements: IN) -> Parame
|
|||
|
||||
|
||||
// MARK: Contains NONE of the elements.
|
||||
public func containsNoneOf<T>(values elements: T..., where equality: @escaping (T, T) -> Bool) -> ParameterMatcher<[T]> {
|
||||
/**
|
||||
* Matcher for sequences of elements that checks if none of the passed elements match the passed sequence.
|
||||
* - parameter values: Variadic elements that are used for matching.
|
||||
* - parameter where: Closure for determining equality of elements that don't conform to `Equatable`.
|
||||
* - returns: ParameterMatcher object.
|
||||
*/
|
||||
public func containsNoneOf<T, S: Sequence>(values elements: T..., where equality: @escaping (T, T) -> Bool) -> ParameterMatcher<S> where T == S.Element {
|
||||
return containsNoneOf(elements, where: equality)
|
||||
}
|
||||
/**
|
||||
* Matcher for sequences of elements that checks if none of the passed elements match the passed sequence.
|
||||
* - parameter elements: Elements that are used for matching.
|
||||
* - parameter where: Closure for determining equality of elements that don't conform to `Equatable`.
|
||||
* - returns: ParameterMatcher object.
|
||||
*/
|
||||
public func containsNoneOf<IN: Sequence, OUT: Sequence>(_ elements: IN, where equality: @escaping (IN.Element, IN.Element) -> Bool) -> ParameterMatcher<OUT> where IN.Element == OUT.Element {
|
||||
return not(containsAnyOf(elements, where: equality))
|
||||
}
|
||||
|
||||
public func containsNoneOf<T: Equatable>(values elements: T...) -> ParameterMatcher<[T]> {
|
||||
/**
|
||||
* Matcher for sequences of `Equatable` elements that checks if none of the passed elements match the passed sequence.
|
||||
* - parameter values: Variadic elements that are used for matching.
|
||||
* - parameter where: Closure for determining equality of elements that don't conform to `Equatable`.
|
||||
* - returns: ParameterMatcher object.
|
||||
*/
|
||||
public func containsNoneOf<T: Equatable, S: Sequence>(values elements: T...) -> ParameterMatcher<S> where T == S.Element {
|
||||
return containsNoneOf(elements)
|
||||
}
|
||||
/**
|
||||
* Matcher for sequences of `Equatable` elements that checks if none of the passed elements match the passed sequence.
|
||||
* - parameter elements: Elements that are used for matching.
|
||||
* - parameter where: Closure for determining equality of elements that don't conform to `Equatable`.
|
||||
* - returns: ParameterMatcher object.
|
||||
*/
|
||||
public func containsNoneOf<IN: Sequence, OUT: Sequence>(_ elements: IN) -> ParameterMatcher<OUT> where IN.Element: Equatable, IN.Element == OUT.Element {
|
||||
return containsNoneOf(elements, where: ==)
|
||||
}
|
||||
|
||||
public func containsNoneOf<T: Hashable>(values elements: T...) -> ParameterMatcher<[T]> {
|
||||
/**
|
||||
* Matcher for sequences of `Hashable` elements that checks if none of the passed elements match the passed sequence.
|
||||
* - parameter values: Variadic elements that are used for matching.
|
||||
* - parameter where: Closure for determining equality of elements that don't conform to `Equatable`.
|
||||
* - returns: ParameterMatcher object.
|
||||
*/
|
||||
public func containsNoneOf<T: Hashable, S: Sequence>(values elements: T...) -> ParameterMatcher<S> where T == S.Element {
|
||||
return containsNoneOf(elements)
|
||||
}
|
||||
/**
|
||||
* Matcher for sequences of `Hashable` elements that checks if none of the passed elements match the passed sequence.
|
||||
* - parameter elements: Elements that are used for matching.
|
||||
* - parameter where: Closure for determining equality of elements that don't conform to `Equatable`.
|
||||
* - returns: ParameterMatcher object.
|
||||
*/
|
||||
public func containsNoneOf<IN: Sequence, OUT: Sequence>(_ elements: IN) -> ParameterMatcher<OUT> where IN.Element: Hashable, IN.Element == OUT.Element {
|
||||
return not(containsAnyOf(elements))
|
||||
}
|
||||
|
||||
|
||||
|
||||
// MARK: Has length N (exact, at least, at most).
|
||||
/**
|
||||
* Matcher for collections of elements that checks if the collection is exactly N elements long.
|
||||
* - parameter exactly: Required length of the matching collection.
|
||||
* - returns: ParameterMatcher object.
|
||||
*/
|
||||
public func hasLength<C: Collection>(exactly requiredExactLength: Int) -> ParameterMatcher<C> {
|
||||
return ParameterMatcher { collection in
|
||||
collection.count == requiredExactLength
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Matcher for collections of elements that checks if the collection is at least N elements long with the option to include the number itself (default).
|
||||
* - parameter atLeast: Required minimum length of the matching collection.
|
||||
* - parameter inclusive: Whether the minimum length itself should be included.
|
||||
* - returns: ParameterMatcher object.
|
||||
*/
|
||||
public func hasLength<C: Collection>(atLeast requiredMinimumLength: Int, inclusive: Bool = true) -> ParameterMatcher<C> {
|
||||
return ParameterMatcher { collection in
|
||||
collection.count > requiredMinimumLength || (inclusive && collection.count == requiredMinimumLength)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Matcher for collections of elements that checks if the collection is at most N elements long with the option to include the number itself (default).
|
||||
* - parameter atMost: Required maximum length of the matching collection.
|
||||
* - parameter inclusive: Whether the maximum length itself should be included.
|
||||
* - returns: ParameterMatcher object.
|
||||
*/
|
||||
public func hasLength<C: Collection>(atMost requiredMaximumLength: Int, inclusive: Bool = true) -> ParameterMatcher<C> {
|
||||
return ParameterMatcher { collection in
|
||||
collection.count < requiredMaximumLength || (inclusive && collection.count == requiredMaximumLength)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Matcher for collections of elements that checks if the collection is at between N and M elements long with the option to include the bounds as well (default).
|
||||
* - parameter inRange: Required length range of the matching collection.
|
||||
* - parameter inclusive: Whether the length range bounds should be included.
|
||||
* - returns: ParameterMatcher object.
|
||||
*/
|
||||
public func hasLength<C: Collection>(inRange requiredLengthRange: CountableRange<Int>, inclusive: Bool = true) -> ParameterMatcher<C> {
|
||||
return hasLength(atLeast: requiredLengthRange.lowerBound, inclusive: inclusive)
|
||||
.and(hasLength(atMost: requiredLengthRange.upperBound, inclusive: inclusive))
|
||||
}
|
||||
|
|
|
@ -22,9 +22,20 @@ extension Dictionary: Matchable where Value: Matchable, Value == Value.MatchedTy
|
|||
|
||||
// MARK: PAIR MATCHING
|
||||
// MARK: Contains ANY of the elements as key-value pairs.
|
||||
/**
|
||||
* Matcher for dictionaries that checks if the matching dictionary contains any of the key-value pairs from the passed dictionary.
|
||||
* - parameter inputDictionary: Dictionary that is used for matching.
|
||||
* - returns: ParameterMatcher object.
|
||||
*/
|
||||
public func containsAnyOf<K, V: Equatable>(_ inputDictionary: [K: V]) -> ParameterMatcher<[K: V]> {
|
||||
return containsAnyOf(inputDictionary, where: ==)
|
||||
}
|
||||
/**
|
||||
* Matcher for dictionaries that checks if the matching dictionary contains any of the key-value pairs from the passed dictionary.
|
||||
* - parameter inputDictionary: Dictionary that is used for matching.
|
||||
* - parameter where: Closure for determining equality of elements that don't conform to `Equatable`.
|
||||
* - returns: ParameterMatcher object.
|
||||
*/
|
||||
public func containsAnyOf<K, V>(_ inputDictionary: [K: V], where equality: @escaping (V, V) -> Bool) -> ParameterMatcher<[K: V]> {
|
||||
return ParameterMatcher { dictionary in
|
||||
inputDictionary.contains { key, value in
|
||||
|
@ -34,9 +45,20 @@ public func containsAnyOf<K, V>(_ inputDictionary: [K: V], where equality: @esca
|
|||
}
|
||||
|
||||
// MARK: Contains ALL of the elements as key-value pairs.
|
||||
/**
|
||||
* Matcher for dictionaries that checks if the matching dictionary contains all of the key-value pairs from the passed dictionary.
|
||||
* - parameter inputDictionary: Dictionary that is used for matching.
|
||||
* - returns: ParameterMatcher object.
|
||||
*/
|
||||
public func containsAllOf<K, V: Equatable>(_ inputDictionary: [K: V]) -> ParameterMatcher<[K: V]> {
|
||||
return containsAllOf(inputDictionary, where: ==)
|
||||
}
|
||||
/**
|
||||
* Matcher for dictionaries that checks if the matching dictionary contains all of the key-value pairs from the passed dictionary.
|
||||
* - parameter inputDictionary: Dictionary that is used for matching.
|
||||
* - parameter where: Closure for determining equality of elements that don't conform to `Equatable`.
|
||||
* - returns: ParameterMatcher object.
|
||||
*/
|
||||
public func containsAllOf<K, V>(_ inputDictionary: [K: V], where equality: @escaping (V, V) -> Bool) -> ParameterMatcher<[K: V]> {
|
||||
return ParameterMatcher { dictionary in
|
||||
inputDictionary.allSatisfy { key, value in
|
||||
|
@ -46,9 +68,20 @@ public func containsAllOf<K, V>(_ inputDictionary: [K: V], where equality: @esca
|
|||
}
|
||||
|
||||
// MARK: Contains NONE of the elements as key-value pairs.
|
||||
/**
|
||||
* Matcher for dictionaries that checks if the matching dictionary contains none of the key-value pairs from the passed dictionary.
|
||||
* - parameter inputDictionary: Dictionary that is used for matching.
|
||||
* - returns: ParameterMatcher object.
|
||||
*/
|
||||
public func containsNoneOf<K, V: Equatable>(_ inputDictionary: [K: V]) -> ParameterMatcher<[K: V]> {
|
||||
return containsNoneOf(inputDictionary, where: ==)
|
||||
}
|
||||
/**
|
||||
* Matcher for dictionaries that checks if the matching dictionary contains none of the key-value pairs from the passed dictionary.
|
||||
* - parameter inputDictionary: Dictionary that is used for matching.
|
||||
* - parameter where: Closure for determining equality of elements that don't conform to `Equatable`.
|
||||
* - returns: ParameterMatcher object.
|
||||
*/
|
||||
public func containsNoneOf<K, V>(_ inputDictionary: [K: V], where equality: @escaping (V, V) -> Bool) -> ParameterMatcher<[K: V]> {
|
||||
return not(containsAnyOf(inputDictionary, where: equality))
|
||||
}
|
||||
|
@ -56,9 +89,19 @@ public func containsNoneOf<K, V>(_ inputDictionary: [K: V], where equality: @esc
|
|||
|
||||
// MARK: KEYS MATCHING
|
||||
// MARK: Contains ANY of the elements as keys.
|
||||
/**
|
||||
* Matcher for dictionaries that checks if the matching dictionary contains any of the passed keys.
|
||||
* - parameter values: Variadic keys that are used for matching.
|
||||
* - returns: ParameterMatcher object.
|
||||
*/
|
||||
public func containsAnyKeysOf<K, V>(values elements: K...) -> ParameterMatcher<[K: V]> {
|
||||
return containsAnyKeysOf(elements)
|
||||
}
|
||||
/**
|
||||
* Matcher for dictionaries that checks if the matching dictionary contains any of the passed keys.
|
||||
* - parameter elements: Keys that are used for matching.
|
||||
* - returns: ParameterMatcher object.
|
||||
*/
|
||||
public func containsAnyKeysOf<S: Sequence, V>(_ elements: S) -> ParameterMatcher<[S.Element: V]> {
|
||||
return ParameterMatcher { dictionary in
|
||||
containsAnyOf(elements).matches(dictionary.keys)
|
||||
|
@ -66,9 +109,19 @@ public func containsAnyKeysOf<S: Sequence, V>(_ elements: S) -> ParameterMatcher
|
|||
}
|
||||
|
||||
// MARK: Contains ALL of the elements as keys.
|
||||
/**
|
||||
* Matcher for dictionaries that checks if the matching dictionary contains all of the passed keys.
|
||||
* - parameter values: Variadic keys that are used for matching
|
||||
* - returns: ParameterMatcher object.
|
||||
*/
|
||||
public func containsAllKeysOf<K, V>(values elements: K...) -> ParameterMatcher<[K: V]> {
|
||||
return containsAllKeysOf(elements)
|
||||
}
|
||||
/**
|
||||
* Matcher for dictionaries that checks if the matching dictionary contains all of the passed keys.
|
||||
* - parameter elements: Keys that are used for matching.
|
||||
* - returns: ParameterMatcher object.
|
||||
*/
|
||||
public func containsAllKeysOf<S: Sequence, V>(_ elements: S) -> ParameterMatcher<[S.Element: V]> {
|
||||
return ParameterMatcher { dictionary in
|
||||
containsAllOf(elements).matches(dictionary.keys)
|
||||
|
@ -76,9 +129,19 @@ public func containsAllKeysOf<S: Sequence, V>(_ elements: S) -> ParameterMatcher
|
|||
}
|
||||
|
||||
// MARK: Contains NONE of the elements as keys.
|
||||
/**
|
||||
* Matcher for dictionaries that checks if the matching dictionary contains none of the passed keys.
|
||||
* - parameter values: Variadic keys that are used for matching
|
||||
* - returns: ParameterMatcher object.
|
||||
*/
|
||||
public func containsNoKeysOf<K, V>(values elements: K...) -> ParameterMatcher<[K: V]> {
|
||||
return containsNoKeysOf(elements)
|
||||
}
|
||||
/**
|
||||
* Matcher for dictionaries that checks if the matching dictionary contains none of the passed keys.
|
||||
* - parameter elements: Keys that are used for matching.
|
||||
* - returns: ParameterMatcher object.
|
||||
*/
|
||||
public func containsNoKeysOf<S: Sequence, V>(_ elements: S) -> ParameterMatcher<[S.Element: V]> {
|
||||
return not(containsAnyKeysOf(elements))
|
||||
}
|
||||
|
@ -86,25 +149,58 @@ public func containsNoKeysOf<S: Sequence, V>(_ elements: S) -> ParameterMatcher<
|
|||
|
||||
// MARK: VALUES MATCHING
|
||||
// MARK: Contains ANY of the elements as values.
|
||||
/**
|
||||
* Matcher for dictionaries that checks if the matching dictionary contains any of the passed values.
|
||||
* - parameter values: Variadic values that are used for matching.
|
||||
* - parameter where: Closure for determining equality of elements that don't conform to `Equatable`.
|
||||
* - returns: ParameterMatcher object.
|
||||
*/
|
||||
public func containsAnyValuesOf<K, V>(values elements: V..., where equality: @escaping (V, V) -> Bool) -> ParameterMatcher<[K: V]> {
|
||||
return containsAnyValuesOf(elements, where: equality)
|
||||
}
|
||||
/**
|
||||
* Matcher for dictionaries that checks if the matching dictionary contains any of the passed values.
|
||||
* - parameter elements: Values that are used for matching.
|
||||
* - parameter where: Closure for determining equality of elements that don't conform to `Equatable`.
|
||||
* - returns: ParameterMatcher object.
|
||||
*/
|
||||
public func containsAnyValuesOf<K, S: Sequence>(_ elements: S, where equality: @escaping (S.Element, S.Element) -> Bool) -> ParameterMatcher<[K: S.Element]> {
|
||||
return ParameterMatcher { dictionary in
|
||||
containsAnyOf(elements, where: equality).matches(dictionary.values)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Matcher for dictionaries that checks if the matching dictionary contains any of the passed values.
|
||||
* - parameter values: Variadic keys that are used for matching
|
||||
* - returns: ParameterMatcher object.
|
||||
*/
|
||||
public func containsAnyValuesOf<K, V: Equatable>(values elements: V...) -> ParameterMatcher<[K: V]> {
|
||||
return containsAnyValuesOf(elements)
|
||||
}
|
||||
/**
|
||||
* Matcher for dictionaries that checks if the matching dictionary contains any of the passed values.
|
||||
* - parameter elements: Keys that are used for matching.
|
||||
* - returns: ParameterMatcher object.
|
||||
*/
|
||||
public func containsAnyValuesOf<K, S: Sequence>(_ elements: S) -> ParameterMatcher<[K: S.Element]> where S.Element: Equatable {
|
||||
return containsAnyValuesOf(elements, where: ==)
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Matcher for dictionaries that checks if the matching dictionary contains any of the passed values.
|
||||
* - parameter values: Variadic values that are used for matching
|
||||
* - returns: ParameterMatcher object.
|
||||
*/
|
||||
public func containsAnyValuesOf<K, V: Hashable>(values elements: V...) -> ParameterMatcher<[K: V]> {
|
||||
return containsAnyValuesOf(elements)
|
||||
}
|
||||
/**
|
||||
* Matcher for dictionaries that checks if the matching dictionary contains none of the passed values.
|
||||
* - parameter elements: Values that are used for matching.
|
||||
* - returns: ParameterMatcher object.
|
||||
*/
|
||||
public func containsAnyValuesOf<K, S: Sequence>(_ elements: S) -> ParameterMatcher<[K: S.Element]> where S.Element: Hashable {
|
||||
return ParameterMatcher { dictionary in
|
||||
containsAnyOf(elements).matches(dictionary.values)
|
||||
|
@ -112,25 +208,57 @@ public func containsAnyValuesOf<K, S: Sequence>(_ elements: S) -> ParameterMatch
|
|||
}
|
||||
|
||||
// MARK: Contains ALL of the elements as values.
|
||||
/**
|
||||
* Matcher for dictionaries that checks if the matching dictionary contains all of the passed values.
|
||||
* - parameter values: Variadic values that are used for matching.
|
||||
* - parameter where: Closure for determining equality of elements that don't conform to `Equatable`.
|
||||
* - returns: ParameterMatcher object.
|
||||
*/
|
||||
public func containsAllValuesOf<K, V>(values elements: V..., where equality: @escaping (V, V) -> Bool) -> ParameterMatcher<[K: V]> {
|
||||
return containsAllValuesOf(elements, where: equality)
|
||||
}
|
||||
/**
|
||||
* Matcher for dictionaries that checks if the matching dictionary contains all of the passed values.
|
||||
* - parameter elements: Values that are used for matching.
|
||||
* - parameter where: Closure for determining equality of elements that don't conform to `Equatable`.
|
||||
* - returns: ParameterMatcher object.
|
||||
*/
|
||||
public func containsAllValuesOf<K, S: Sequence>(_ elements: S, where equality: @escaping (S.Element, S.Element) -> Bool) -> ParameterMatcher<[K: S.Element]> {
|
||||
return ParameterMatcher { dictionary in
|
||||
containsAllOf(elements, where: equality).matches(dictionary.values)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Matcher for dictionaries that checks if the matching dictionary contains all of the passed values.
|
||||
* - parameter values: Variadic values that are used for matching.
|
||||
* - returns: ParameterMatcher object.
|
||||
*/
|
||||
public func containsAllValuesOf<K, V: Equatable>(values elements: V...) -> ParameterMatcher<[K: V]> {
|
||||
return containsAllValuesOf(elements)
|
||||
}
|
||||
/**
|
||||
* Matcher for dictionaries that checks if the matching dictionary contains all of the passed values.
|
||||
* - parameter elements: Values that are used for matching.
|
||||
* - returns: ParameterMatcher object.
|
||||
*/
|
||||
public func containsAllValuesOf<K, S: Sequence>(_ elements: S) -> ParameterMatcher<[K: S.Element]> where S.Element: Equatable {
|
||||
return containsAllValuesOf(elements, where: ==)
|
||||
}
|
||||
|
||||
/**
|
||||
* Matcher for dictionaries that checks if the matching dictionary contains all of the passed values.
|
||||
* - parameter values: Variadic values that are used for matching.
|
||||
* - returns: ParameterMatcher object.
|
||||
*/
|
||||
public func containsAllValuesOf<K, V: Hashable>(values elements: V...) -> ParameterMatcher<[K: V]> {
|
||||
return containsAllValuesOf(elements)
|
||||
}
|
||||
/**
|
||||
* Matcher for dictionaries that checks if the matching dictionary contains all of the passed values.
|
||||
* - parameter elements: Values that are used for matching.
|
||||
* - returns: ParameterMatcher object.
|
||||
*/
|
||||
public func containsAllValuesOf<K, S: Sequence>(_ elements: S) -> ParameterMatcher<[K: S.Element]> where S.Element: Hashable {
|
||||
return ParameterMatcher { dictionary in
|
||||
containsAllOf(elements).matches(dictionary.values)
|
||||
|
@ -138,23 +266,59 @@ public func containsAllValuesOf<K, S: Sequence>(_ elements: S) -> ParameterMatch
|
|||
}
|
||||
|
||||
// MARK: Contains NONE of the elements as values.
|
||||
/**
|
||||
* Matcher for dictionaries that checks if the matching dictionary contains none of the passed values.
|
||||
* - parameter values: Variadic values that are used for matching.
|
||||
* - parameter where: Closure for determining equality of elements that don't conform to `Equatable`.
|
||||
* - returns: ParameterMatcher object.
|
||||
*/
|
||||
public func containsNoValuesOf<K, V>(values elements: V..., where equality: @escaping (V, V) -> Bool) -> ParameterMatcher<[K: V]> {
|
||||
return containsNoValuesOf(elements, where: equality)
|
||||
}
|
||||
/**
|
||||
* Matcher for dictionaries that checks if the matching dictionary contains none of the passed values.
|
||||
* - parameter elements: Values that are used for matching.
|
||||
* - parameter where: Closure for determining equality of elements that don't conform to `Equatable`.
|
||||
* - returns: ParameterMatcher object.
|
||||
*/
|
||||
public func containsNoValuesOf<K, S: Sequence>(_ elements: S, where equality: @escaping (S.Element, S.Element) -> Bool) -> ParameterMatcher<[K: S.Element]> {
|
||||
return not(containsAnyValuesOf(elements, where: equality))
|
||||
}
|
||||
|
||||
/**
|
||||
* Matcher for dictionaries that checks if the matching dictionary contains none of the passed values.
|
||||
* - parameter values: Variadic values that are used for matching.
|
||||
* - parameter where: Closure for determining equality of elements that don't conform to `Equatable`.
|
||||
* - returns: ParameterMatcher object.
|
||||
*/
|
||||
public func containsNoValuesOf<K, V: Equatable>(values elements: V...) -> ParameterMatcher<[K: V]> {
|
||||
return containsNoValuesOf(elements)
|
||||
}
|
||||
/**
|
||||
* Matcher for dictionaries that checks if the matching dictionary contains none of the passed values.
|
||||
* - parameter elements: Values that are used for matching.
|
||||
* - parameter where: Closure for determining equality of elements that don't conform to `Equatable`.
|
||||
* - returns: ParameterMatcher object.
|
||||
*/
|
||||
public func containsNoValuesOf<K, S: Sequence>(_ elements: S) -> ParameterMatcher<[K: S.Element]> where S.Element: Equatable {
|
||||
return containsNoValuesOf(elements, where: ==)
|
||||
}
|
||||
|
||||
/**
|
||||
* Matcher for dictionaries that checks if the matching dictionary contains none of the passed values.
|
||||
* - parameter values: Variadic values that are used for matching.
|
||||
* - parameter where: Closure for determining equality of elements that don't conform to `Equatable`.
|
||||
* - returns: ParameterMatcher object.
|
||||
*/
|
||||
public func containsNoValuesOf<K, V: Hashable>(values elements: V...) -> ParameterMatcher<[K: V]> {
|
||||
return containsNoValuesOf(elements)
|
||||
}
|
||||
/**
|
||||
* Matcher for dictionaries that checks if the matching dictionary contains none of the passed values.
|
||||
* - parameter elements: Values that are used for matching.
|
||||
* - parameter where: Closure for determining equality of elements that don't conform to `Equatable`.
|
||||
* - returns: ParameterMatcher object.
|
||||
*/
|
||||
public func containsNoValuesOf<K, S: Sequence>(_ elements: S) -> ParameterMatcher<[K: S.Element]> where S.Element: Hashable {
|
||||
return not(containsAnyValuesOf(elements))
|
||||
}
|
||||
|
|
|
@ -108,6 +108,18 @@ class ArrayMatcherTest: XCTestCase {
|
|||
XCTAssertFalse(containsAllOf((1...3).map(TestStructs.H.init)).matches([3].map(TestStructs.H.init)))
|
||||
}
|
||||
|
||||
func testContainsAllOfMultiple() {
|
||||
XCTAssertFalse(containsAllOf(values: 1, 3, 2, 2, 1).matches([2]))
|
||||
XCTAssertFalse(containsAllOf(values: 1, 3, 2, 2, 1).matches([1, 2, 3]))
|
||||
XCTAssertFalse(containsAllOf(values: 1, 3, 2, 2, 1).matches([1, 3, 2, 2]))
|
||||
XCTAssertTrue(containsAllOf(values: 1, 3, 2, 2, 1).matches([1, 3, 2, 2, 1]))
|
||||
|
||||
XCTAssertFalse(containsAllOf([1, 3, 2, 2, 1]).matches([2]))
|
||||
XCTAssertFalse(containsAllOf([1, 3, 2, 2, 1]).matches([1, 2, 3]))
|
||||
XCTAssertFalse(containsAllOf([1, 3, 2, 2, 1]).matches([1, 3, 2, 2]))
|
||||
XCTAssertTrue(containsAllOf([1, 3, 2, 2, 1]).matches([1, 3, 2, 2, 1]))
|
||||
}
|
||||
|
||||
// MARK: Contains NONE of the elements.
|
||||
func testContainsNoneOf() {
|
||||
// Variadic parameters.
|
||||
|
|
Loading…
Reference in New Issue