diff --git a/Sources/CustomDump/Diff.swift b/Sources/CustomDump/Diff.swift index 14ccc2c..95e01a2 100644 --- a/Sources/CustomDump/Diff.swift +++ b/Sources/CustomDump/Diff.swift @@ -360,7 +360,7 @@ public func diff(_ lhs: T, _ rhs: T, format: DiffFormat = .default) -> String elementIndent: 2, elementSeparator: ",", collapseUnchanged: false, - filter: { $0.label.map { !$0.hasPrefix("_$") } ?? true } + filter: macroPropertyFilter(for: lhs) ) } @@ -505,7 +505,7 @@ public func diff(_ lhs: T, _ rhs: T, format: DiffFormat = .default) -> String elementIndent: 2, elementSeparator: ",", collapseUnchanged: false, - filter: { $0.label.map { !$0.hasPrefix("_$") } ?? true } + filter: macroPropertyFilter(for: lhs) ) case (_, .tuple?, _, .tuple?): diff --git a/Sources/CustomDump/Dump.swift b/Sources/CustomDump/Dump.swift index b826bfa..c845619 100644 --- a/Sources/CustomDump/Dump.swift +++ b/Sources/CustomDump/Dump.swift @@ -198,7 +198,7 @@ func _customDump( of: Mirror(value, children: children), prefix: "\(typeName(mirror.subjectType))\(id)(", suffix: ")", - filter: { $0.label.map { !$0.hasPrefix("_$") } ?? true } + filter: macroPropertyFilter(for: value) ) } @@ -279,7 +279,7 @@ func _customDump( of: mirror, prefix: "\(typeName(mirror.subjectType))(", suffix: ")", - filter: { $0.label.map { !$0.hasPrefix("_$") } ?? true } + filter: macroPropertyFilter(for: value) ) case (_, .tuple?): @@ -331,3 +331,9 @@ func _customDump(_ value: Any, name: String?, indent: Int, isRoot: Bool, maxDept _customDump(value, to: &out, name: name, indent: indent, isRoot: isRoot, maxDepth: maxDepth) return out } + +func macroPropertyFilter(for value: Any) -> (Mirror.Child) -> Bool { + value is CustomDumpReflectable + ? { _ in true } + : { $0.label.map { !$0.hasPrefix("_$") } ?? true } +} diff --git a/Tests/CustomDumpTests/DumpTests.swift b/Tests/CustomDumpTests/DumpTests.swift index 4b05472..8cb85c4 100644 --- a/Tests/CustomDumpTests/DumpTests.swift +++ b/Tests/CustomDumpTests/DumpTests.swift @@ -1329,4 +1329,29 @@ final class DumpTests: XCTestCase { """ ) } + + func testExplicitFilteredPropertyPreserved() { + struct ObservationRegistrar {} + class Object: CustomDumpReflectable { + let name = "Blob Sr." + let _$observationRegistrar = ObservationRegistrar() + + var customDumpMirror: Mirror { + Mirror( + self, + children: ["name": self.name, "_$observationRegistrar": self._$observationRegistrar], + displayStyle: .class + ) + } + } + XCTAssertNoDifference( + String(customDumping: Object()), + """ + DumpTests.Object( + name: "Blob Sr.", + _$observationRegistrar: DumpTests.ObservationRegistrar() + ) + """ + ) + } } diff --git a/Tests/CustomDumpTests/Mocks.swift b/Tests/CustomDumpTests/Mocks.swift index ab3d46e..c6a937f 100644 --- a/Tests/CustomDumpTests/Mocks.swift +++ b/Tests/CustomDumpTests/Mocks.swift @@ -94,6 +94,12 @@ struct Wrapper: CustomDumpRepresentable { struct LoginState: CustomDumpReflectable { var email = "", password = "", token: String + init(email: String = "", password: String = "", token: String) { + self.email = email + self.password = password + self.token = token + } + var customDumpMirror: Mirror { .init( self,