Skip to content

Commit 21404fe

Browse files
authored
Show difference when values are equal if labels differ (#84)
1 parent 805c57f commit 21404fe

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

Sources/CustomDump/Diff.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public func diff<T>(_ lhs: T, _ rhs: T, format: DiffFormat = .default) -> String
3838
isRoot: Bool
3939
) -> String {
4040
let rhsName = rhsName ?? lhsName
41-
guard !isMirrorEqual(lhs, rhs) else {
41+
guard lhsName != rhsName || !isMirrorEqual(lhs, rhs) else {
4242
return _customDump(lhs, name: rhsName, indent: indent, isRoot: isRoot, maxDepth: 0)
4343
.appending(separator)
4444
.indenting(with: format.both + " ")

Tests/CustomDumpTests/DiffTests.swift

+43
Original file line numberDiff line numberDiff line change
@@ -1064,4 +1064,47 @@ final class DiffTests: XCTestCase {
10641064
"""
10651065
)
10661066
}
1067+
1068+
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+
1088+
XCTAssertEqual(
1089+
String(customDumping: Stack(elements: [(.init(rawValue: 0), "Hello")])),
1090+
"""
1091+
[
1092+
#0: "Hello"
1093+
]
1094+
"""
1095+
)
1096+
1097+
XCTAssertEqual(
1098+
diff(
1099+
Stack(elements: [(.init(rawValue: 0), "Hello")]),
1100+
Stack(elements: [(.init(rawValue: 1), "Hello")])
1101+
),
1102+
"""
1103+
[
1104+
- #0: "Hello"
1105+
+ #1: "Hello"
1106+
]
1107+
"""
1108+
)
1109+
}
10671110
}

0 commit comments

Comments
 (0)