Skip to content

Commit

Permalink
Add ids to second occurrence of a type within a dump (#60)
Browse files Browse the repository at this point in the history
* Add ids to second occurrence of a type within a dump

* Update logic

* Cleanup

* Update tests after rebase

* Use # instead of <>
  • Loading branch information
tahirmt authored Oct 3, 2022
1 parent 8a93a47 commit 8e64f1e
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 32 deletions.
17 changes: 15 additions & 2 deletions Sources/CustomDump/Dump.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ public func customDump<T, TargetStream>(
maxDepth: Int = .max
) -> T where TargetStream: TextOutputStream {

var idPerItem: [ObjectIdentifier: UInt] = [:]
var occurencePerType: [String: UInt] = [:]
var visitedItems: Set<ObjectIdentifier> = []

func customDumpHelp<T, TargetStream>(
Expand Down Expand Up @@ -134,10 +136,21 @@ public func customDump<T, TargetStream>(

case let (value as AnyObject, .class?):
let item = ObjectIdentifier(value)
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))(↩︎)")
out.write("\(typeName(mirror.subjectType))\(id)(↩︎)")
} else {
visitedItems.insert(item)
occurence += 1
var children = Array(mirror.children)

var superclassMirror = mirror.superclassMirror
Expand All @@ -147,7 +160,7 @@ public func customDump<T, TargetStream>(
}
dumpChildren(
of: Mirror(value, children: children),
prefix: "\(typeName(mirror.subjectType))(",
prefix: "\(typeName(mirror.subjectType))\(id)(",
suffix: ")"
)
}
Expand Down
90 changes: 60 additions & 30 deletions Tests/CustomDumpTests/DumpTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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(↩︎)
)
Expand All @@ -853,40 +853,70 @@ final class DumpTests: XCTestCase {
func testRepeatition() {
class Human {
let name = "John"
}

class User: Human {
let email = "[email protected]"
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)

XCTAssertNoDifference(
dump,
"""
[
[0]: DumpTests.Human(
name: "John",
email: "[email protected]",
age: 97
),
[1]: DumpTests.Human(↩︎),
[2]: DumpTests.Human(
name: "John",
email: "[email protected]",
age: 97
),
[3]: DumpTests.Human(↩︎)
]
"""
[
human,
human,
human,
human2,
human2,
human2,
user,
user,
user,
user2,
user2,
user2,
], to: &dump)

XCTAssertNoDifference(
dump,
"""
[
[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(
name: "John",
email: "[email protected]",
age: 97,
human: DumpTests.Human(↩︎)
),
[7]: DumpTests.User(↩︎),
[8]: DumpTests.User(↩︎),
[9]: DumpTests.User#2(
name: "John",
email: "[email protected]",
age: 97,
human: DumpTests.Human#2(↩︎)
),
[10]: DumpTests.User#2(↩︎),
[11]: DumpTests.User#2(↩︎)
]
"""
)
}

Expand Down

0 comments on commit 8e64f1e

Please sign in to comment.