From d7c7c47991526074260f79ecec4191e8ff8d1e6c Mon Sep 17 00:00:00 2001 From: Mahmood Tahir Date: Fri, 23 Sep 2022 22:52:09 -0400 Subject: [PATCH 1/5] Add ids to second occurrence of a type within a dump --- Sources/CustomDump/Dump.swift | 13 +++++++++-- Tests/CustomDumpTests/DumpTests.swift | 32 +++++++++++++-------------- 2 files changed, 27 insertions(+), 18 deletions(-) diff --git a/Sources/CustomDump/Dump.swift b/Sources/CustomDump/Dump.swift index b98ae73..9405a7e 100644 --- a/Sources/CustomDump/Dump.swift +++ b/Sources/CustomDump/Dump.swift @@ -36,6 +36,11 @@ public func customDump( return value } +private struct UniqueOccurance: Hashable { + let typeName: String + let objectId: ObjectIdentifier +} + /// Dumps the given value's contents using its mirror to the specified output stream. /// /// - Parameters: @@ -57,6 +62,7 @@ public func customDump( maxDepth: Int = .max ) -> T where TargetStream: TextOutputStream { + var idPerOccurance: [UniqueOccurance: Int] = [:] var visitedItems: Set = [] func customDumpHelp( @@ -134,8 +140,11 @@ public func customDump( case let (value as AnyObject, .class?): let item = ObjectIdentifier(value) + let id = idPerOccurance[.init(typeName: typeName(mirror.subjectType), objectId: item), default: idPerOccurance.count] + idPerOccurance[.init(typeName: typeName(mirror.subjectType), objectId: item)] = id + let idString = id > 0 ? "<\(id)>" : "" if visitedItems.contains(item) { - out.write("\(typeName(mirror.subjectType))(↩︎)") + out.write("\(typeName(mirror.subjectType))\(idString)(↩︎)") } else { visitedItems.insert(item) var children = Array(mirror.children) @@ -147,7 +156,7 @@ public func customDump( } dumpChildren( of: Mirror(value, children: children), - prefix: "\(typeName(mirror.subjectType))(", + prefix: "\(typeName(mirror.subjectType))\(idString)(", suffix: ")" ) } diff --git a/Tests/CustomDumpTests/DumpTests.swift b/Tests/CustomDumpTests/DumpTests.swift index 0649c49..4e31e8a 100644 --- a/Tests/CustomDumpTests/DumpTests.swift +++ b/Tests/CustomDumpTests/DumpTests.swift @@ -871,22 +871,22 @@ final class DumpTests: XCTestCase { XCTAssertNoDifference( dump, - """ - [ - [0]: DumpTests.Human( - name: "John", - email: "john@me.com", - age: 97 - ), - [1]: DumpTests.Human(↩︎), - [2]: DumpTests.Human( - name: "John", - email: "john@me.com", - age: 97 - ), - [3]: DumpTests.Human(↩︎) - ] - """ + """ + [ + [0]: DumpTests.Human( + name: "John", + email: "john@me.com", + age: 97 + ), + [1]: DumpTests.Human(↩︎), + [2]: DumpTests.Human<1>( + name: "John", + email: "john@me.com", + age: 97 + ), + [3]: DumpTests.Human<1>(↩︎) + ] + """ ) } From ab4943851fd50a7546c686cc4b50be055817a42b Mon Sep 17 00:00:00 2001 From: Mahmood Tahir Date: Sat, 24 Sep 2022 23:57:41 -0400 Subject: [PATCH 2/5] Update logic --- Sources/CustomDump/Dump.swift | 19 ++++++--- Tests/CustomDumpTests/DumpTests.swift | 56 ++++++++++++++++++++------- 2 files changed, 56 insertions(+), 19 deletions(-) diff --git a/Sources/CustomDump/Dump.swift b/Sources/CustomDump/Dump.swift index 9405a7e..f5697cb 100644 --- a/Sources/CustomDump/Dump.swift +++ b/Sources/CustomDump/Dump.swift @@ -62,7 +62,8 @@ public func customDump( maxDepth: Int = .max ) -> T where TargetStream: TextOutputStream { - var idPerOccurance: [UniqueOccurance: Int] = [:] + var idPerItem: [ObjectIdentifier: UInt] = [:] + var occurencePerType: [String: UInt] = [:] var visitedItems: Set = [] func customDumpHelp( @@ -140,13 +141,21 @@ public func customDump( case let (value as AnyObject, .class?): let item = ObjectIdentifier(value) - let id = idPerOccurance[.init(typeName: typeName(mirror.subjectType), objectId: item), default: idPerOccurance.count] - idPerOccurance[.init(typeName: typeName(mirror.subjectType), objectId: item)] = id - let idString = id > 0 ? "<\(id)>" : "" + var occurence = occurencePerType[typeName(mirror.subjectType), default: 0] { + didSet { occurencePerType[typeName(mirror.subjectType)] = occurence } + } + + var id: String { + let id = idPerItem[item, default: occurence] + idPerItem[item] = id + + return id > 1 ? "<\(id)>" : "" + } if visitedItems.contains(item) { - out.write("\(typeName(mirror.subjectType))\(idString)(↩︎)") + out.write("\(typeName(mirror.subjectType))\(id)(↩︎)") } else { visitedItems.insert(item) + occurence += 1 var children = Array(mirror.children) var superclassMirror = mirror.superclassMirror diff --git a/Tests/CustomDumpTests/DumpTests.swift b/Tests/CustomDumpTests/DumpTests.swift index 4e31e8a..a34e3da 100644 --- a/Tests/CustomDumpTests/DumpTests.swift +++ b/Tests/CustomDumpTests/DumpTests.swift @@ -853,38 +853,66 @@ final class DumpTests: XCTestCase { func testRepeatition() { class Human { let name = "John" + } + + class User: Human { let email = "john@me.com" let age = 97 + + let human: Human + + init(human: Human) { + self.human = human + } } let human = Human() let human2 = Human() + let user = User(human: human) + let user2 = User(human: human2) + var dump = "" customDump( - [ - human, - human, - human2, - human2, - ], to: &dump) + [ + human, + human, + human, + human2, + human2, + human2, + user, + user, + user, + user2, + user2, + user2, + ], to: &dump) XCTAssertNoDifference( dump, """ [ - [0]: DumpTests.Human( - name: "John", + [0]: DumpTests.Human(name: "John"), + [1]: DumpTests.Human(↩︎), + [2]: DumpTests.Human(↩︎), + [3]: DumpTests.Human<2>(name: "John"), + [4]: DumpTests.Human<2>(↩︎), + [5]: DumpTests.Human<2>(↩︎), + [6]: DumpTests.User( email: "john@me.com", - age: 97 + age: 97, + human: DumpTests.Human(↩︎) ), - [1]: DumpTests.Human(↩︎), - [2]: DumpTests.Human<1>( - name: "John", + [7]: DumpTests.User(↩︎), + [8]: DumpTests.User(↩︎), + [9]: DumpTests.User<2>( email: "john@me.com", - age: 97 + age: 97, + human: DumpTests.Human<2>(↩︎) ), - [3]: DumpTests.Human<1>(↩︎) + [10]: DumpTests.User<2>(↩︎), + [11]: DumpTests.User<2>(↩︎) ] """ ) From f0ebd3a3aaf0eeb5f06fe9a2a4c123f5ec36da9e Mon Sep 17 00:00:00 2001 From: Mahmood Tahir Date: Sat, 24 Sep 2022 23:58:20 -0400 Subject: [PATCH 3/5] Cleanup --- Sources/CustomDump/Dump.swift | 5 ----- 1 file changed, 5 deletions(-) diff --git a/Sources/CustomDump/Dump.swift b/Sources/CustomDump/Dump.swift index f5697cb..0de71f6 100644 --- a/Sources/CustomDump/Dump.swift +++ b/Sources/CustomDump/Dump.swift @@ -36,11 +36,6 @@ public func customDump( return value } -private struct UniqueOccurance: Hashable { - let typeName: String - let objectId: ObjectIdentifier -} - /// Dumps the given value's contents using its mirror to the specified output stream. /// /// - Parameters: From 96f3a9eb716c5c43243c5d99d34cdc9051d1c217 Mon Sep 17 00:00:00 2001 From: Mahmood Tahir Date: Mon, 3 Oct 2022 13:01:04 -0400 Subject: [PATCH 4/5] Update tests after rebase --- Sources/CustomDump/Dump.swift | 2 +- Tests/CustomDumpTests/DumpTests.swift | 12 +++++++----- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/Sources/CustomDump/Dump.swift b/Sources/CustomDump/Dump.swift index 0de71f6..bf28477 100644 --- a/Sources/CustomDump/Dump.swift +++ b/Sources/CustomDump/Dump.swift @@ -160,7 +160,7 @@ public func customDump( } dumpChildren( of: Mirror(value, children: children), - prefix: "\(typeName(mirror.subjectType))\(idString)(", + prefix: "\(typeName(mirror.subjectType))\(id)(", suffix: ")" ) } diff --git a/Tests/CustomDumpTests/DumpTests.swift b/Tests/CustomDumpTests/DumpTests.swift index a34e3da..067b176 100644 --- a/Tests/CustomDumpTests/DumpTests.swift +++ b/Tests/CustomDumpTests/DumpTests.swift @@ -824,23 +824,23 @@ final class DumpTests: XCTestCase { name: "Virginia", parent: DumpTests.Parent(↩︎) ), - [1]: DumpTests.Child( + [1]: DumpTests.Child<2>( name: "Ronald", parent: DumpTests.Parent(↩︎) ), - [2]: DumpTests.Child( + [2]: DumpTests.Child<3>( name: "Fred", parent: DumpTests.Parent(↩︎) ), - [3]: DumpTests.Child( + [3]: DumpTests.Child<4>( name: "George", parent: DumpTests.Parent(↩︎) ), - [4]: DumpTests.Child( + [4]: DumpTests.Child<5>( name: "Percy", parent: DumpTests.Parent(↩︎) ), - [5]: DumpTests.Child( + [5]: DumpTests.Child<6>( name: "Charles", parent: DumpTests.Parent(↩︎) ) @@ -900,6 +900,7 @@ final class DumpTests: XCTestCase { [4]: DumpTests.Human<2>(↩︎), [5]: DumpTests.Human<2>(↩︎), [6]: DumpTests.User( + name: "John", email: "john@me.com", age: 97, human: DumpTests.Human(↩︎) @@ -907,6 +908,7 @@ final class DumpTests: XCTestCase { [7]: DumpTests.User(↩︎), [8]: DumpTests.User(↩︎), [9]: DumpTests.User<2>( + name: "John", email: "john@me.com", age: 97, human: DumpTests.Human<2>(↩︎) From fb714f58a3f11404972a88281b2f1ebcc0739fe0 Mon Sep 17 00:00:00 2001 From: Mahmood Tahir Date: Mon, 3 Oct 2022 15:40:24 -0400 Subject: [PATCH 5/5] Use # instead of <> --- Sources/CustomDump/Dump.swift | 2 +- Tests/CustomDumpTests/DumpTests.swift | 24 ++++++++++++------------ 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/Sources/CustomDump/Dump.swift b/Sources/CustomDump/Dump.swift index bf28477..d413431 100644 --- a/Sources/CustomDump/Dump.swift +++ b/Sources/CustomDump/Dump.swift @@ -144,7 +144,7 @@ public func customDump( let id = idPerItem[item, default: occurence] idPerItem[item] = id - return id > 1 ? "<\(id)>" : "" + return id > 1 ? "#\(id)" : "" } if visitedItems.contains(item) { out.write("\(typeName(mirror.subjectType))\(id)(↩︎)") diff --git a/Tests/CustomDumpTests/DumpTests.swift b/Tests/CustomDumpTests/DumpTests.swift index 067b176..a3a5444 100644 --- a/Tests/CustomDumpTests/DumpTests.swift +++ b/Tests/CustomDumpTests/DumpTests.swift @@ -824,23 +824,23 @@ final class DumpTests: XCTestCase { name: "Virginia", parent: DumpTests.Parent(↩︎) ), - [1]: DumpTests.Child<2>( + [1]: DumpTests.Child#2( name: "Ronald", parent: DumpTests.Parent(↩︎) ), - [2]: DumpTests.Child<3>( + [2]: DumpTests.Child#3( name: "Fred", parent: DumpTests.Parent(↩︎) ), - [3]: DumpTests.Child<4>( + [3]: DumpTests.Child#4( name: "George", parent: DumpTests.Parent(↩︎) ), - [4]: DumpTests.Child<5>( + [4]: DumpTests.Child#5( name: "Percy", parent: DumpTests.Parent(↩︎) ), - [5]: DumpTests.Child<6>( + [5]: DumpTests.Child#6( name: "Charles", parent: DumpTests.Parent(↩︎) ) @@ -896,9 +896,9 @@ final class DumpTests: XCTestCase { [0]: DumpTests.Human(name: "John"), [1]: DumpTests.Human(↩︎), [2]: DumpTests.Human(↩︎), - [3]: DumpTests.Human<2>(name: "John"), - [4]: DumpTests.Human<2>(↩︎), - [5]: DumpTests.Human<2>(↩︎), + [3]: DumpTests.Human#2(name: "John"), + [4]: DumpTests.Human#2(↩︎), + [5]: DumpTests.Human#2(↩︎), [6]: DumpTests.User( name: "John", email: "john@me.com", @@ -907,14 +907,14 @@ final class DumpTests: XCTestCase { ), [7]: DumpTests.User(↩︎), [8]: DumpTests.User(↩︎), - [9]: DumpTests.User<2>( + [9]: DumpTests.User#2( name: "John", email: "john@me.com", age: 97, - human: DumpTests.Human<2>(↩︎) + human: DumpTests.Human#2(↩︎) ), - [10]: DumpTests.User<2>(↩︎), - [11]: DumpTests.User<2>(↩︎) + [10]: DumpTests.User#2(↩︎), + [11]: DumpTests.User#2(↩︎) ] """ )