-
-
Notifications
You must be signed in to change notification settings - Fork 624
/
Copy pathCustomMatchers.swift
95 lines (82 loc) · 3.78 KB
/
CustomMatchers.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
//
// Created by Krzysztof Zabłocki on 22/12/2016.
// Copyright (c) 2016 Pixle. All rights reserved.
//
@testable import Sourcery
@testable import SourceryRuntime
import Nimble
import Quick
/// A Nimble matcher that succeeds when the actual value is equal to the expected value.
/// Values can support equal by supporting the Equatable protocol.
///
/// @see beCloseTo if you want to match imprecise types (eg - floats, doubles).
public func equal<T: Equatable>(_ expectedValue: T?) -> NonNilMatcherFunc<T> where T: Diffable {
return NonNilMatcherFunc { actualExpression, failureMessage in
failureMessage.postfixMessage = "equal <\(stringify(expectedValue))>"
let actualValue = try actualExpression.evaluate()
let matches = actualValue == expectedValue && expectedValue != nil
if expectedValue == nil || actualValue == nil {
if expectedValue == nil {
failureMessage.postfixActual = " (use beNil() to match nils)"
}
return false
}
if !matches, let actual = actualValue, let expected = expectedValue {
let results = DiffableResult()
results.trackDifference(actual: actual, expected: expected)
prepare(failureMessage, results: results, actual: stringify(actualValue))
}
return matches
}
}
/// A Nimble matcher that succeeds when the actual collection is equal to the expected collection.
/// Items must implement the Equatable protocol.
public func equal<T: Equatable>(_ expectedValue: [T]?) -> NonNilMatcherFunc<[T]> where T: Diffable {
return NonNilMatcherFunc { actualExpression, failureMessage in
failureMessage.postfixMessage = "equal <\(stringify(expectedValue))>"
let actualValue = try actualExpression.evaluate()
if expectedValue == nil || actualValue == nil {
if expectedValue == nil {
failureMessage.postfixActual = " (use beNil() to match nils)"
}
return false
}
// swiftlint:disable:next force_unwrapping
let matches = expectedValue! == actualValue!
if !matches, let expected = expectedValue, let actual = actualValue {
let results = DiffableResult()
results.trackDifference(actual: actual, expected: expected)
prepare(failureMessage, results: results, actual: stringify(actual))
}
return matches
}
}
/// A Nimble matcher that succeeds when the actual value is equal to the expected value.
/// Values can support equal by supporting the Equatable protocol.
///
/// @see beCloseTo if you want to match imprecise types (eg - floats, doubles).
public func equal<T: Equatable, C: Equatable>(_ expectedValue: [T: C]?) -> NonNilMatcherFunc<[T: C]> where C: Diffable {
return NonNilMatcherFunc { actualExpression, failureMessage in
failureMessage.postfixMessage = "equal <\(stringify(expectedValue))>"
let actualValue = try actualExpression.evaluate()
if expectedValue == nil || actualValue == nil {
if expectedValue == nil {
failureMessage.postfixActual = " (use beNil() to match nils)"
}
return false
}
// swiftlint:disable:next force_unwrapping
let matches = expectedValue! == actualValue!
if !matches, let expected = expectedValue, let actual = actualValue {
let results = DiffableResult()
results.trackDifference(actual: actual, expected: expected)
prepare(failureMessage, results: results, actual: stringify(actual))
}
return matches
}
}
private func prepare(_ message: FailureMessage, results: DiffableResult, actual: String) {
if !results.isEmpty {
message.stringValue = "\(results)\n Actual: \(actual)\(message.postfixActual)"
}
}