Skip to content

Commit d7f18c3

Browse files
authored
Add support to dump superclass nodes (#58)
* Add support to dump subset of children and superclass nodes * Dump superclass by default * Clean up code based on @iampatbrown's feature branch * More cleanup * revert unrelated indentation * Remove new protocols * Revert some syntax changes
1 parent c9b6b94 commit d7f18c3

File tree

3 files changed

+186
-3
lines changed

3 files changed

+186
-3
lines changed

Package.resolved

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Sources/CustomDump/Dump.swift

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,18 @@ public func customDump<T, TargetStream>(
138138
out.write("\(typeName(mirror.subjectType))(↩︎)")
139139
} else {
140140
visitedItems.insert(item)
141-
dumpChildren(of: mirror, prefix: "\(typeName(mirror.subjectType))(", suffix: ")")
141+
var children = Array(mirror.children)
142+
143+
var superclassMirror = mirror.superclassMirror
144+
while let mirror = superclassMirror {
145+
children.insert(contentsOf: mirror.children, at: 0)
146+
superclassMirror = mirror.superclassMirror
147+
}
148+
dumpChildren(
149+
of: Mirror(value, children: children),
150+
prefix: "\(typeName(mirror.subjectType))(",
151+
suffix: ")"
152+
)
142153
}
143154

144155
case (_, .collection?):

Tests/CustomDumpTests/DumpTests.swift

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -715,6 +715,178 @@ final class DumpTests: XCTestCase {
715715
)
716716
}
717717

718+
func testSuperclass() {
719+
var dump = ""
720+
class Human {
721+
let name = "John"
722+
let email = "[email protected]"
723+
let age = 97
724+
}
725+
726+
class Doctor: Human {
727+
let field = "Podiatry"
728+
}
729+
730+
customDump(Doctor(), to: &dump)
731+
732+
XCTAssertNoDifference(
733+
dump,
734+
"""
735+
DumpTests.Doctor(
736+
name: "John",
737+
738+
age: 97,
739+
field: "Podiatry"
740+
)
741+
"""
742+
)
743+
}
744+
745+
func testLayersOfInheritance() {
746+
var dump = ""
747+
class Human {
748+
let name = "John"
749+
let email = "[email protected]"
750+
let age = 97
751+
}
752+
753+
class Doctor: Human {
754+
let field = "Podiatry"
755+
}
756+
757+
class Surgeon: Doctor {
758+
let skillLevel = "Expert"
759+
}
760+
761+
customDump(Surgeon(), to: &dump)
762+
763+
XCTAssertNoDifference(
764+
dump,
765+
"""
766+
DumpTests.Surgeon(
767+
name: "John",
768+
769+
age: 97,
770+
field: "Podiatry",
771+
skillLevel: "Expert"
772+
)
773+
"""
774+
)
775+
}
776+
777+
func testRecursion() {
778+
class Human {
779+
let name: String
780+
781+
init(name: String) {
782+
self.name = name
783+
}
784+
}
785+
786+
class Child: Human {
787+
weak var parent: Parent?
788+
}
789+
790+
class Parent: Human {
791+
let children: [Human]
792+
793+
init(name: String, children: [Child]) {
794+
self.children = children
795+
super.init(name: name)
796+
797+
children.forEach {
798+
$0.parent = self
799+
}
800+
}
801+
}
802+
803+
let subject = Parent(name: "Arthur", children: [
804+
Child(name: "Virginia"),
805+
Child(name: "Ronald"),
806+
Child(name: "Fred"),
807+
Child(name: "George"),
808+
Child(name: "Percy"),
809+
Child(name: "Charles"),
810+
])
811+
812+
var dump = ""
813+
customDump(subject, to: &dump)
814+
815+
XCTAssertNoDifference(
816+
dump,
817+
"""
818+
DumpTests.Parent(
819+
name: "Arthur",
820+
children: [
821+
[0]: DumpTests.Child(
822+
name: "Virginia",
823+
parent: DumpTests.Parent(↩︎)
824+
),
825+
[1]: DumpTests.Child(
826+
name: "Ronald",
827+
parent: DumpTests.Parent(↩︎)
828+
),
829+
[2]: DumpTests.Child(
830+
name: "Fred",
831+
parent: DumpTests.Parent(↩︎)
832+
),
833+
[3]: DumpTests.Child(
834+
name: "George",
835+
parent: DumpTests.Parent(↩︎)
836+
),
837+
[4]: DumpTests.Child(
838+
name: "Percy",
839+
parent: DumpTests.Parent(↩︎)
840+
),
841+
[5]: DumpTests.Child(
842+
name: "Charles",
843+
parent: DumpTests.Parent(↩︎)
844+
)
845+
]
846+
)
847+
"""
848+
)
849+
}
850+
851+
func testRepeatition() {
852+
class Human {
853+
let name = "John"
854+
let email = "[email protected]"
855+
let age = 97
856+
}
857+
858+
let human = Human()
859+
let human2 = Human()
860+
861+
var dump = ""
862+
customDump([
863+
human,
864+
human,
865+
human2,
866+
human2,
867+
], to: &dump)
868+
869+
XCTAssertNoDifference(
870+
dump,
871+
"""
872+
[
873+
[0]: DumpTests.Human(
874+
name: "John",
875+
876+
age: 97
877+
),
878+
[1]: DumpTests.Human(↩︎),
879+
[2]: DumpTests.Human(
880+
name: "John",
881+
882+
age: 97
883+
),
884+
[3]: DumpTests.Human(↩︎)
885+
]
886+
"""
887+
)
888+
}
889+
718890
#if canImport(CoreGraphics)
719891
func testCoreGraphics() {
720892
var dump = ""

0 commit comments

Comments
 (0)