diff --git a/Sources/CustomDump/Conformances/Swift.swift b/Sources/CustomDump/Conformances/Swift.swift index a41477c..46ae214 100644 --- a/Sources/CustomDump/Conformances/Swift.swift +++ b/Sources/CustomDump/Conformances/Swift.swift @@ -7,6 +7,7 @@ extension Character: CustomDumpRepresentable { extension ObjectIdentifier: CustomDumpStringConvertible { public var customDumpDescription: String { self.debugDescription + .replacingOccurrences(of: "0x0*", with: "0x", options: .regularExpression) } } diff --git a/Sources/CustomDump/Diff.swift b/Sources/CustomDump/Diff.swift index 03def97..ee1bc2c 100644 --- a/Sources/CustomDump/Diff.swift +++ b/Sources/CustomDump/Diff.swift @@ -81,8 +81,17 @@ public func diff(_ lhs: T, _ rhs: T, format: DiffFormat = .default) -> String areInIncreasingOrder: ((Mirror.Child, Mirror.Child) -> Bool)? = nil, _ transform: (inout Mirror.Child, Int) -> Void = { _, _ in } ) { - guard !lhsMirror.children.isEmpty || !rhsMirror.children.isEmpty + var lhsChildren = Array(lhsMirror.children) + var rhsChildren = Array(rhsMirror.children) + + guard !isMirrorEqual(lhsChildren, rhsChildren) else { + print( + "// Not equal but no difference detected:" + .indenting(by: indent) + .indenting(with: format.both + " "), + to: &out + ) print( _customDump( lhs, @@ -142,9 +151,6 @@ public func diff(_ lhs: T, _ rhs: T, format: DiffFormat = .default) -> String to: &out ) - var lhsChildren = Array(lhsMirror.children) - var rhsChildren = Array(rhsMirror.children) - if let areInIncreasingOrder = areInIncreasingOrder { lhsChildren.sort(by: areInIncreasingOrder) rhsChildren.sort(by: areInIncreasingOrder) @@ -311,6 +317,14 @@ public func diff(_ lhs: T, _ rhs: T, format: DiffFormat = .default) -> String to: &out ) } else { + let showObjectIdentifiers = lhsItem != rhsItem + && isMirrorEqual(Array(lhsMirror.children), Array(rhsMirror.children)) + let lhsMirror = showObjectIdentifiers + ? Mirror(lhs, children: [("_", lhsItem)] + lhsMirror.children, displayStyle: .class) + : lhsMirror + let rhsMirror = showObjectIdentifiers + ? Mirror(rhs, children: [("_", rhsItem)] + rhsMirror.children, displayStyle: .class) + : rhsMirror visitedItems.insert(lhsItem) diffChildren( lhsMirror, diff --git a/Tests/CustomDumpTests/DiffTests.swift b/Tests/CustomDumpTests/DiffTests.swift index 2d5057f..015a0f0 100644 --- a/Tests/CustomDumpTests/DiffTests.swift +++ b/Tests/CustomDumpTests/DiffTests.swift @@ -76,6 +76,28 @@ final class DiffTests: XCTestCase { ) } + func testClassObjectIdentity() { + class User: NSObject { + let id = 42 + let name = "Blob" + } + + XCTAssertNoDifference( + diff( + User(), + User() + )?.replacingOccurrences(of: "0x[[:xdigit:]]+", with: "…", options: .regularExpression), + """ + DiffTests.User( + - _: ObjectIdentifier(…), + + _: ObjectIdentifier(…), + id: 42, + name: "Blob" + ) + """ + ) + } + func testCollection() { XCTAssertNoDifference( diff( @@ -357,6 +379,7 @@ final class DiffTests: XCTestCase { NeverEqual() ), """ + // Not equal but no difference detected: - NeverEqual() + NeverEqual() """ @@ -416,6 +439,18 @@ final class DiffTests: XCTestCase { ) """ ) + + XCTAssertNoDifference( + diff( + NeverEqualUser(id: 1, name: "Blob"), + NeverEqualUser(id: 1, name: "Blob") + ), + """ + // Not equal but no difference detected: + - NeverEqualUser(…) + + NeverEqualUser(…) + """ + ) } func testTuple() { diff --git a/Tests/CustomDumpTests/Mocks.swift b/Tests/CustomDumpTests/Mocks.swift index f33e92c..b8a36a7 100644 --- a/Tests/CustomDumpTests/Mocks.swift +++ b/Tests/CustomDumpTests/Mocks.swift @@ -82,6 +82,13 @@ struct NestedDate { var date: Date? } struct NeverEqual: Equatable { static func == (lhs: Self, rhs: Self) -> Bool { false } } +struct NeverEqualUser: Equatable { + let id: Int + let name: String + + static func == (lhs: Self, rhs: Self) -> Bool { false } +} + struct Pair { let driver: User, passenger: User } struct Redacted: CustomDumpStringConvertible {