Skip to content

Commit 53169c4

Browse files
authored
Fix CustomDumpRepresentable max depth (#88)
* Fix `CustomDumpRepresentable` max depth When we reach a custom dump representable value, we should not decrement the max depth when printing it. At times this can cause the max depth to roll from 0 to -1, which currently causes the logic that collapses nodes to leave them expanded. We should update that logic, as well, so I'll do that in another commit. * Treat negative depth as zero * Add/fix some unrelated tests
1 parent af2eb5e commit 53169c4

File tree

2 files changed

+55
-7
lines changed

2 files changed

+55
-7
lines changed

Sources/CustomDump/Dump.swift

+4-4
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ func _customDump<T, TargetStream>(
118118
maxDepth: maxDepth - 1
119119
)
120120
if childOut.contains("\n") {
121-
if maxDepth == 0 {
121+
if maxDepth <= 0 {
122122
out.write("")
123123
} else {
124124
out.write("\n")
@@ -128,7 +128,7 @@ func _customDump<T, TargetStream>(
128128
} else {
129129
out.write(childOut)
130130
}
131-
} else if maxDepth == 0 {
131+
} else if maxDepth <= 0 {
132132
out.write("")
133133
} else {
134134
out.write("\n")
@@ -165,7 +165,7 @@ func _customDump<T, TargetStream>(
165165

166166
case let (value as CustomDumpRepresentable, _):
167167
customDumpHelp(
168-
value.customDumpValue, to: &out, name: nil, indent: 0, isRoot: false, maxDepth: maxDepth - 1
168+
value.customDumpValue, to: &out, name: nil, indent: 0, isRoot: false, maxDepth: maxDepth
169169
)
170170

171171
case let (value as AnyObject, .class?):
@@ -287,7 +287,7 @@ func _customDump<T, TargetStream>(
287287
default:
288288
if let value = stringFromStringProtocol(value) {
289289
if value.contains(where: \.isNewline) {
290-
if maxDepth == 0 {
290+
if maxDepth <= 0 {
291291
out.write("\"\"")
292292
} else {
293293
let hashes = String(repeating: "#", count: value.hashCount(isMultiline: true))

Tests/CustomDumpTests/DiffTests.swift

+51-3
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,11 @@ final class DiffTests: XCTestCase {
103103
diff(
104104
User(),
105105
User()
106-
)?.replacingOccurrences(of: "0x[[:xdigit:]]+", with: "", options: .regularExpression),
106+
)?.replacingOccurrences(of: "0x[[:xdigit:]]+", with: "0x", options: .regularExpression),
107107
"""
108108
DiffTests.User(
109-
- _: ObjectIdentifier(…),
110-
+ _: ObjectIdentifier(…),
109+
- _: ObjectIdentifier(0x…),
110+
+ _: ObjectIdentifier(0x…),
111111
id: 42,
112112
name: "Blob"
113113
)
@@ -446,6 +446,29 @@ final class DiffTests: XCTestCase {
446446
)
447447
}
448448

449+
func testEnumCollapsing() {
450+
enum Offset: Equatable {
451+
case page(Int, perPage: Int = 10)
452+
}
453+
struct State: Equatable {
454+
var offset: Offset
455+
let result: String
456+
}
457+
XCTAssertNoDifference(
458+
diff(
459+
State(offset: .page(1), result: "Hello, world!"),
460+
State(offset: .page(1), result: "Good night, moon!")
461+
),
462+
"""
463+
DiffTests.State(
464+
offset: .page(…),
465+
- result: "Hello, world!"
466+
+ result: "Good night, moon!"
467+
)
468+
"""
469+
)
470+
}
471+
449472
func testOptional() {
450473
XCTAssertEqual(
451474
diff(nil as User?, nil), nil
@@ -1111,6 +1134,31 @@ final class DiffTests: XCTestCase {
11111134
"""
11121135
)
11131136
}
1137+
1138+
func testCustomDumpRepresentableCollapsing() {
1139+
struct Results: CustomDumpRepresentable, Equatable {
1140+
var customDumpValue: Any {
1141+
[1, 2]
1142+
}
1143+
}
1144+
struct State: Equatable {
1145+
var date: Double
1146+
var results: Results
1147+
}
1148+
XCTAssertNoDifference(
1149+
diff(
1150+
State(date: 123456789, results: Results()),
1151+
State(date: 123456790, results: Results())
1152+
),
1153+
"""
1154+
DiffTests.State(
1155+
- date: 123456789.0,
1156+
+ date: 123456790.0,
1157+
results: […]
1158+
)
1159+
"""
1160+
)
1161+
}
11141162
}
11151163

11161164
private struct Stack<State: Equatable>: CustomDumpReflectable, Equatable {

0 commit comments

Comments
 (0)