Skip to content
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

Add support to dump superclass nodes #58

Merged
merged 7 commits into from
Oct 3, 2022
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
4 changes: 2 additions & 2 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
"repositoryURL": "https://github.com/pointfreeco/xctest-dynamic-overlay",
"state": {
"branch": null,
"revision": "b9eeb1a7ea3fd6fea54ce57dee2f5794b667c8df",
"version": "0.2.0"
"revision": "30314f1ece684dd60679d598a9b89107557b67d9",
"version": "0.4.1"
}
}
]
Expand Down
13 changes: 12 additions & 1 deletion Sources/CustomDump/Dump.swift
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,18 @@ public func customDump<T, TargetStream>(
out.write("\(typeName(mirror.subjectType))(↩︎)")
} else {
visitedItems.insert(item)
dumpChildren(of: mirror, prefix: "\(typeName(mirror.subjectType))(", suffix: ")")
var children = Array(mirror.children)

var superclassMirror = mirror.superclassMirror
while let mirror = superclassMirror {
children.insert(contentsOf: mirror.children, at: 0)
superclassMirror = mirror.superclassMirror
}
dumpChildren(
of: Mirror(value, children: children),
prefix: "\(typeName(mirror.subjectType))(",
suffix: ")"
)
}

case (_, .collection?):
Expand Down
172 changes: 172 additions & 0 deletions Tests/CustomDumpTests/DumpTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -715,6 +715,178 @@ final class DumpTests: XCTestCase {
)
}

func testSuperclass() {
var dump = ""
class Human {
let name = "John"
let email = "[email protected]"
let age = 97
}

class Doctor: Human {
let field = "Podiatry"
}

customDump(Doctor(), to: &dump)

XCTAssertNoDifference(
dump,
"""
DumpTests.Doctor(
name: "John",
email: "[email protected]",
age: 97,
field: "Podiatry"
)
"""
)
}

func testLayersOfInheritance() {
var dump = ""
class Human {
let name = "John"
let email = "[email protected]"
let age = 97
}

class Doctor: Human {
let field = "Podiatry"
}

class Surgeon: Doctor {
let skillLevel = "Expert"
}

customDump(Surgeon(), to: &dump)

XCTAssertNoDifference(
dump,
"""
DumpTests.Surgeon(
name: "John",
email: "[email protected]",
age: 97,
field: "Podiatry",
skillLevel: "Expert"
)
"""
)
}

func testRecursion() {
class Human {
let name: String

init(name: String) {
self.name = name
}
}

class Child: Human {
weak var parent: Parent?
}

class Parent: Human {
let children: [Human]

init(name: String, children: [Child]) {
self.children = children
super.init(name: name)

children.forEach {
$0.parent = self
}
}
}

let subject = Parent(name: "Arthur", children: [
Child(name: "Virginia"),
Child(name: "Ronald"),
Child(name: "Fred"),
Child(name: "George"),
Child(name: "Percy"),
Child(name: "Charles"),
])

var dump = ""
customDump(subject, to: &dump)

XCTAssertNoDifference(
dump,
"""
DumpTests.Parent(
name: "Arthur",
children: [
[0]: DumpTests.Child(
name: "Virginia",
parent: DumpTests.Parent(↩︎)
),
[1]: DumpTests.Child(
name: "Ronald",
parent: DumpTests.Parent(↩︎)
),
[2]: DumpTests.Child(
name: "Fred",
parent: DumpTests.Parent(↩︎)
),
[3]: DumpTests.Child(
name: "George",
parent: DumpTests.Parent(↩︎)
),
[4]: DumpTests.Child(
name: "Percy",
parent: DumpTests.Parent(↩︎)
),
[5]: DumpTests.Child(
name: "Charles",
parent: DumpTests.Parent(↩︎)
)
]
)
"""
)
}

func testRepeatition() {
class Human {
let name = "John"
let email = "[email protected]"
let age = 97
}

let human = Human()
let human2 = Human()

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(↩︎)
]
"""
)
}

#if canImport(CoreGraphics)
func testCoreGraphics() {
var dump = ""
Expand Down