Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Sources/CustomDump/Conformances/Swift.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ extension Character: CustomDumpRepresentable {
extension ObjectIdentifier: CustomDumpStringConvertible {
public var customDumpDescription: String {
self.debugDescription
.replacingOccurrences(of: "0x0*", with: "0x", options: .regularExpression)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shortens ObjectIdentifier(0x00000000000deadbeef) to ObjectIdentifier(0xdeadbeef).

}
}

Expand Down
22 changes: 18 additions & 4 deletions Sources/CustomDump/Diff.swift
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,17 @@ public func diff<T>(_ 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:"
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'll now print a message when no difference is detected.

.indenting(by: indent)
.indenting(with: format.both + " "),
to: &out
)
print(
_customDump(
lhs,
Expand Down Expand Up @@ -142,9 +151,6 @@ public func diff<T>(_ 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)
Expand Down Expand Up @@ -311,6 +317,14 @@ public func diff<T>(_ lhs: T, _ rhs: T, format: DiffFormat = .default) -> String
to: &out
)
} else {
let showObjectIdentifiers = lhsItem != rhsItem
&& isMirrorEqual(Array(lhsMirror.children), Array(rhsMirror.children))
Comment on lines +320 to +321
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'll only diff/print object identifiers if all the exposed properties are equal. This should avoid noise in classes where are objects are equatable across identity.

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,
Expand Down
35 changes: 35 additions & 0 deletions Tests/CustomDumpTests/DiffTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -357,6 +379,7 @@ final class DiffTests: XCTestCase {
NeverEqual()
),
"""
// Not equal but no difference detected:
- NeverEqual()
+ NeverEqual()
"""
Expand Down Expand Up @@ -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(…)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could expand the properties here to aid a bit more in debugging...

"""
)
}

func testTuple() {
Expand Down
7 changes: 7 additions & 0 deletions Tests/CustomDumpTests/Mocks.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<RawValue>: CustomDumpStringConvertible {
Expand Down