-
Notifications
You must be signed in to change notification settings - Fork 95
/
Copy pathExpectNoDifference.swift
96 lines (93 loc) · 2.83 KB
/
ExpectNoDifference.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
96
import IssueReporting
/// Asserts that two values have no difference.
///
/// Similar to `XCTAssertEqual`, but that function uses either `TextOutputStreamable`,
/// `CustomStringConvertible` or `CustomDebugStringConvertible` in order to display a failure
/// message:
///
/// ```swift
/// XCTAssertEqual(user1, user2)
/// ```
/// ```text
/// XCTAssertEqual failed: ("User(id: 42, name: "Blob")") is not equal to ("User(id: 42, name: "Blob, Esq.")")
/// ```
///
/// `expectNoDifference` uses the output of ``diff(_:_:format:)`` to display a failure message,
/// which helps highlight the differences between the given values:
///
/// ```swift
/// expectNoDifference(user1, user2)
/// ```
/// ```text
/// expectNoDifference failed: …
///
/// User(
/// id: 42,
/// - name: "Blob"
/// + name: "Blob, Esq."
/// )
///
/// (First: -, Second: +)
/// ```
///
/// - Parameters:
/// - expression1: An expression of type `T`, where `T` is `Equatable`.
/// - expression2: A second expression of type `T`, where `T` is `Equatable`.
/// - message: An optional description of a failure.
/// - fileID: The file where the failure occurs. The default is the file ID of the test case where
/// you call this function.
/// - filePath: The file where the failure occurs. The default is the file path of the test case
/// where you call this function.
/// - line: The line number where the failure occurs. The default is the line number where you
/// call this function.
/// - line: The column where the failure occurs. The default is the column where you call this
/// function.
public func expectNoDifference<T: Equatable>(
_ expression1: @autoclosure () throws -> T,
_ expression2: @autoclosure () throws -> T,
_ message: @autoclosure () -> String? = nil,
fileID: StaticString = #fileID,
filePath: StaticString = #filePath,
line: UInt = #line,
column: UInt = #column
) {
do {
let expression1 = try expression1()
let expression2 = try expression2()
let message = message()
guard expression1 != expression2 else { return }
let format = DiffFormat.proportional
guard let difference = diff(expression1, expression2, format: format)
else {
reportIssue(
"""
("\(expression1)" is not equal to ("\(expression2)"), but no difference was detected.
""",
fileID: fileID,
filePath: filePath,
line: line,
column: column
)
return
}
reportIssue(
"""
\(message?.appending(" - ") ?? "")Difference: …
\(difference.indenting(by: 2))
(First: \(format.first), Second: \(format.second))
""",
fileID: fileID,
filePath: filePath,
line: line,
column: column
)
} catch {
reportIssue(
error,
fileID: fileID,
filePath: filePath,
line: line,
column: column
)
}
}