Skip to content

Commit

Permalink
Show difference when values are equal if labels differ (#84)
Browse files Browse the repository at this point in the history
  • Loading branch information
stephencelis authored Apr 12, 2023
1 parent 805c57f commit 21404fe
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Sources/CustomDump/Diff.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public func diff<T>(_ lhs: T, _ rhs: T, format: DiffFormat = .default) -> String
isRoot: Bool
) -> String {
let rhsName = rhsName ?? lhsName
guard !isMirrorEqual(lhs, rhs) else {
guard lhsName != rhsName || !isMirrorEqual(lhs, rhs) else {
return _customDump(lhs, name: rhsName, indent: indent, isRoot: isRoot, maxDepth: 0)
.appending(separator)
.indenting(with: format.both + " ")
Expand Down
43 changes: 43 additions & 0 deletions Tests/CustomDumpTests/DiffTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1064,4 +1064,47 @@ final class DiffTests: XCTestCase {
"""
)
}

func testCustomDictionary() {
struct Stack: CustomDumpReflectable {
var elements: [(ID, String)]

struct ID: CustomDumpStringConvertible, Hashable {
let rawValue: Int
var customDumpDescription: String {
"#\(self.rawValue)"
}
}

var customDumpMirror: Mirror {
Mirror(
self,
unlabeledChildren: self.elements,
displayStyle: .dictionary
)
}
}

XCTAssertEqual(
String(customDumping: Stack(elements: [(.init(rawValue: 0), "Hello")])),
"""
[
#0: "Hello"
]
"""
)

XCTAssertEqual(
diff(
Stack(elements: [(.init(rawValue: 0), "Hello")]),
Stack(elements: [(.init(rawValue: 1), "Hello")])
),
"""
[
- #0: "Hello"
+ #1: "Hello"
]
"""
)
}
}

0 comments on commit 21404fe

Please sign in to comment.