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

Filter properties prefixed with _$ #100

Merged
merged 1 commit into from
Sep 28, 2023
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
18 changes: 12 additions & 6 deletions Sources/CustomDump/Diff.swift
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,10 @@ public func diff<T>(_ lhs: T, _ rhs: T, format: DiffFormat = .default) -> String
elementIndent: Int,
elementSeparator: String,
collapseUnchanged: Bool,
filter isIncluded: (Mirror.Child) -> Bool = { _ in true },
areEquivalent: (Mirror.Child, Mirror.Child) -> Bool = { $0.label == $1.label },
areInIncreasingOrder: ((Mirror.Child, Mirror.Child) -> Bool)? = nil,
_ transform: (inout Mirror.Child, Int) -> Void = { _, _ in }
map transform: (inout Mirror.Child, Int) -> Void = { _, _ in }
) {
var lhsChildren = Array(lhsMirror.children)
var rhsChildren = Array(rhsMirror.children)
Expand Down Expand Up @@ -156,6 +157,9 @@ public func diff<T>(_ lhs: T, _ rhs: T, format: DiffFormat = .default) -> String
return
}

lhsChildren.removeAll(where: { !isIncluded($0) })
rhsChildren.removeAll(where: { !isIncluded($0) })

let name = rhsName.map { "\($0): " } ?? ""
print(
name
Expand Down Expand Up @@ -355,7 +359,8 @@ public func diff<T>(_ lhs: T, _ rhs: T, format: DiffFormat = .default) -> String
suffix: ")",
elementIndent: 2,
elementSeparator: ",",
collapseUnchanged: false
collapseUnchanged: false,
filter: { $0.label.map { !$0.hasPrefix("_$") } ?? true }
)
}

Expand All @@ -371,7 +376,7 @@ public func diff<T>(_ lhs: T, _ rhs: T, format: DiffFormat = .default) -> String
areEquivalent: {
isIdentityEqual($0.value, $1.value) || isMirrorEqual($0.value, $1.value)
},
{ $0.label = "[\($1)]" }
map: { $0.label = "[\($1)]" }
)

case (_, .dictionary?, _, .dictionary?):
Expand Down Expand Up @@ -443,7 +448,7 @@ public func diff<T>(_ lhs: T, _ rhs: T, format: DiffFormat = .default) -> String
elementIndent: 2,
elementSeparator: ",",
collapseUnchanged: false,
{ child, _ in
map: { child, _ in
if child.label?.first == "." {
child.label = nil
}
Expand Down Expand Up @@ -499,7 +504,8 @@ public func diff<T>(_ lhs: T, _ rhs: T, format: DiffFormat = .default) -> String
suffix: ")",
elementIndent: 2,
elementSeparator: ",",
collapseUnchanged: false
collapseUnchanged: false,
filter: { $0.label.map { !$0.hasPrefix("_$") } ?? true }
)

case (_, .tuple?, _, .tuple?):
Expand All @@ -511,7 +517,7 @@ public func diff<T>(_ lhs: T, _ rhs: T, format: DiffFormat = .default) -> String
elementIndent: 2,
elementSeparator: ",",
collapseUnchanged: false,
{ child, _ in
map: { child, _ in
if child.label?.first == "." {
child.label = nil
}
Expand Down
28 changes: 19 additions & 9 deletions Sources/CustomDump/Dump.swift
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,9 @@ func _customDump<T, TargetStream>(
of mirror: Mirror,
prefix: String,
suffix: String,
filter isIncluded: (Mirror.Child) -> Bool = { _ in true },
by areInIncreasingOrder: ((Mirror.Child, Mirror.Child) -> Bool)? = nil,
_ transform: (inout Mirror.Child, Int) -> Void = { _, _ in }
map transform: (inout Mirror.Child, Int) -> Void = { _, _ in }
) {
out.write(prefix)
if !mirror.children.isEmpty {
Expand Down Expand Up @@ -133,6 +134,7 @@ func _customDump<T, TargetStream>(
} else {
out.write("\n")
var children = Array(mirror.children)
children.removeAll(where: { !isIncluded($0) })
if let areInIncreasingOrder = areInIncreasingOrder {
children.sort(by: areInIncreasingOrder)
}
Expand Down Expand Up @@ -195,12 +197,13 @@ func _customDump<T, TargetStream>(
dumpChildren(
of: Mirror(value, children: children),
prefix: "\(typeName(mirror.subjectType))\(id)(",
suffix: ")"
suffix: ")",
filter: { $0.label.map { !$0.hasPrefix("_$") } ?? true }
)
}

case (_, .collection?):
dumpChildren(of: mirror, prefix: "[", suffix: "]", { $0.label = "[\($1)]" })
dumpChildren(of: mirror, prefix: "[", suffix: "]", map: { $0.label = "[\($1)]" })

case (_, .dictionary?):
if mirror.children.isEmpty {
Expand All @@ -220,13 +223,14 @@ func _customDump<T, TargetStream>(
< _customDump(rhsKey.base, name: nil, indent: 0, isRoot: false, maxDepth: 1)
}
: nil,
{ child, _ in
map: { child, _ in
guard let pair = child.value as? (key: AnyHashable, value: Any) else { return }
let key = _customDump(
pair.key.base, name: nil, indent: 0, isRoot: false, maxDepth: maxDepth - 1
)
child = (key, pair.value)
})
}
)
}

case (_, .enum?):
Expand All @@ -241,7 +245,7 @@ func _customDump<T, TargetStream>(
of: associatedValuesMirror,
prefix: "\(child.label ?? "@unknown")(",
suffix: ")",
{ child, _ in
map: { child, _ in
if child.label?.first == "." {
child.label = nil
}
Expand Down Expand Up @@ -271,18 +275,24 @@ func _customDump<T, TargetStream>(
)

case (_, .struct?):
dumpChildren(of: mirror, prefix: "\(typeName(mirror.subjectType))(", suffix: ")")
dumpChildren(
of: mirror,
prefix: "\(typeName(mirror.subjectType))(",
suffix: ")",
filter: { $0.label.map { !$0.hasPrefix("_$") } ?? true }
)

case (_, .tuple?):
dumpChildren(
of: mirror,
prefix: "(",
suffix: ")",
{ child, _ in
map: { child, _ in
if child.label?.first == "." {
child.label = nil
}
})
}
)

default:
if let value = stringFromStringProtocol(value) {
Expand Down
22 changes: 22 additions & 0 deletions Tests/CustomDumpTests/DiffTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1159,6 +1159,28 @@ final class DiffTests: XCTestCase {
"""
)
}

func testObservationRegistrarFiltered() {
struct ObservationRegistrar: Equatable {}
struct Value: Equatable {
var name: String
let _$observationRegistrar = ObservationRegistrar()
}
let blobSr = Value(name: "Blob Sr.")
let blobJr = Value(name: "Blob Jr.")
XCTAssertNoDifference(
diff(
blobSr,
blobJr
),
"""
DiffTests.Value(
- name: "Blob Sr."
+ name: "Blob Jr."
)
"""
)
}
}

private struct Stack<State: Equatable>: CustomDumpReflectable, Equatable {
Expand Down
28 changes: 28 additions & 0 deletions Tests/CustomDumpTests/DumpTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1301,4 +1301,32 @@ final class DumpTests: XCTestCase {
)
}
#endif

func testObservationRegistrarFiltered() {
struct ObservationRegistrar {}
class Object {
let name = "Blob Sr."
let _$observationRegistrar = ObservationRegistrar()
}
XCTAssertNoDifference(
String(customDumping: Object()),
"""
DumpTests.Object(
name: "Blob Sr."
)
"""
)
struct Value {
let name = "Blob Jr."
let _$observationRegistrar = ObservationRegistrar()
}
XCTAssertNoDifference(
String(customDumping: Value()),
"""
DumpTests.Value(
name: "Blob Jr."
)
"""
)
}
}