-
Notifications
You must be signed in to change notification settings - Fork 100
Print object identity in diff #29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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:" | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
|
|
@@ -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) | ||
|
|
@@ -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
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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(…) | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shortens
ObjectIdentifier(0x00000000000deadbeef)toObjectIdentifier(0xdeadbeef).