Skip to content

Commit 84b30e1

Browse files
authored
Only print "no difference detected" when diff output is the same (#85)
* Only print "no difference detected" when diff output is the same * wip * fix for Xcode 13.4.1
1 parent 21404fe commit 84b30e1

File tree

2 files changed

+69
-40
lines changed

2 files changed

+69
-40
lines changed

Sources/CustomDump/Diff.swift

+23-21
Original file line numberDiff line numberDiff line change
@@ -94,32 +94,34 @@ public func diff<T>(_ lhs: T, _ rhs: T, format: DiffFormat = .default) -> String
9494

9595
guard !isMirrorEqual(lhsChildren, rhsChildren)
9696
else {
97-
print(
98-
"// Not equal but no difference detected:"
99-
.indenting(by: indent)
100-
.indenting(with: format.both + " "),
101-
to: &out
97+
let lhsDump = _customDump(
98+
lhs,
99+
name: lhsName,
100+
indent: indent,
101+
isRoot: false,
102+
maxDepth: 0
102103
)
103-
print(
104-
_customDump(
105-
lhs,
106-
name: lhsName,
107-
indent: indent,
108-
isRoot: false,
109-
maxDepth: 0
104+
let rhsDump = _customDump(
105+
rhs,
106+
name: rhsName,
107+
indent: indent,
108+
isRoot: false,
109+
maxDepth: 0
110+
)
111+
if lhsDump == rhsDump {
112+
print(
113+
"// Not equal but no difference detected:"
114+
.indenting(by: indent)
115+
.indenting(with: format.both + " "),
116+
to: &out
110117
)
111-
.indenting(with: format.first + " "),
118+
}
119+
print(
120+
lhsDump.indenting(with: format.first + " "),
112121
to: &out
113122
)
114123
print(
115-
_customDump(
116-
rhs,
117-
name: rhsName,
118-
indent: indent,
119-
isRoot: false,
120-
maxDepth: 0
121-
)
122-
.indenting(with: format.second + " "),
124+
rhsDump.indenting(with: format.second + " "),
123125
terminator: "",
124126
to: &out
125127
)

Tests/CustomDumpTests/DiffTests.swift

+46-19
Original file line numberDiff line numberDiff line change
@@ -1066,25 +1066,6 @@ final class DiffTests: XCTestCase {
10661066
}
10671067

10681068
func testCustomDictionary() {
1069-
struct Stack: CustomDumpReflectable {
1070-
var elements: [(ID, String)]
1071-
1072-
struct ID: CustomDumpStringConvertible, Hashable {
1073-
let rawValue: Int
1074-
var customDumpDescription: String {
1075-
"#\(self.rawValue)"
1076-
}
1077-
}
1078-
1079-
var customDumpMirror: Mirror {
1080-
Mirror(
1081-
self,
1082-
unlabeledChildren: self.elements,
1083-
displayStyle: .dictionary
1084-
)
1085-
}
1086-
}
1087-
10881069
XCTAssertEqual(
10891070
String(customDumping: Stack(elements: [(.init(rawValue: 0), "Hello")])),
10901071
"""
@@ -1106,5 +1087,51 @@ final class DiffTests: XCTestCase {
11061087
]
11071088
"""
11081089
)
1090+
1091+
struct Child {
1092+
struct State: Equatable {}
1093+
}
1094+
struct Parent {
1095+
struct State: Equatable {
1096+
var children: Stack<Child.State>
1097+
}
1098+
}
1099+
XCTAssertNoDifference(
1100+
diff(
1101+
Parent.State(children: Stack(elements: [(.init(rawValue: 0), Child.State())])),
1102+
Parent.State(children: Stack(elements: [(.init(rawValue: 1), Child.State())]))
1103+
),
1104+
"""
1105+
DiffTests.Parent.State(
1106+
children: [
1107+
- #0: DiffTests.Child.State()
1108+
+ #1: DiffTests.Child.State()
1109+
]
1110+
)
1111+
"""
1112+
)
1113+
}
1114+
}
1115+
1116+
fileprivate struct Stack<State: Equatable>: CustomDumpReflectable, Equatable {
1117+
static func == (lhs: Self, rhs: Self) -> Bool {
1118+
zip(lhs.elements, rhs.elements ).allSatisfy(==)
1119+
}
1120+
1121+
var elements: [(ID, State)]
1122+
1123+
struct ID: CustomDumpStringConvertible, Hashable {
1124+
let rawValue: Int
1125+
var customDumpDescription: String {
1126+
"#\(self.rawValue)"
1127+
}
1128+
}
1129+
1130+
var customDumpMirror: Mirror {
1131+
Mirror(
1132+
self,
1133+
unlabeledChildren: self.elements,
1134+
displayStyle: .dictionary
1135+
)
11091136
}
11101137
}

0 commit comments

Comments
 (0)