Skip to content

Commit

Permalink
Fix CustomDumpRepresentable max depth (#88)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
stephencelis authored May 4, 2023
1 parent af2eb5e commit 53169c4
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 7 deletions.
8 changes: 4 additions & 4 deletions Sources/CustomDump/Dump.swift
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func _customDump<T, TargetStream>(
maxDepth: maxDepth - 1
)
if childOut.contains("\n") {
if maxDepth == 0 {
if maxDepth <= 0 {
out.write("")
} else {
out.write("\n")
Expand All @@ -128,7 +128,7 @@ func _customDump<T, TargetStream>(
} else {
out.write(childOut)
}
} else if maxDepth == 0 {
} else if maxDepth <= 0 {
out.write("")
} else {
out.write("\n")
Expand Down Expand Up @@ -165,7 +165,7 @@ func _customDump<T, TargetStream>(

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

case let (value as AnyObject, .class?):
Expand Down Expand Up @@ -287,7 +287,7 @@ func _customDump<T, TargetStream>(
default:
if let value = stringFromStringProtocol(value) {
if value.contains(where: \.isNewline) {
if maxDepth == 0 {
if maxDepth <= 0 {
out.write("\"\"")
} else {
let hashes = String(repeating: "#", count: value.hashCount(isMultiline: true))
Expand Down
54 changes: 51 additions & 3 deletions Tests/CustomDumpTests/DiffTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,11 @@ final class DiffTests: XCTestCase {
diff(
User(),
User()
)?.replacingOccurrences(of: "0x[[:xdigit:]]+", with: "", options: .regularExpression),
)?.replacingOccurrences(of: "0x[[:xdigit:]]+", with: "0x", options: .regularExpression),
"""
DiffTests.User(
- _: ObjectIdentifier(…),
+ _: ObjectIdentifier(…),
- _: ObjectIdentifier(0x…),
+ _: ObjectIdentifier(0x…),
id: 42,
name: "Blob"
)
Expand Down Expand Up @@ -446,6 +446,29 @@ final class DiffTests: XCTestCase {
)
}

func testEnumCollapsing() {
enum Offset: Equatable {
case page(Int, perPage: Int = 10)
}
struct State: Equatable {
var offset: Offset
let result: String
}
XCTAssertNoDifference(
diff(
State(offset: .page(1), result: "Hello, world!"),
State(offset: .page(1), result: "Good night, moon!")
),
"""
DiffTests.State(
offset: .page(…),
- result: "Hello, world!"
+ result: "Good night, moon!"
)
"""
)
}

func testOptional() {
XCTAssertEqual(
diff(nil as User?, nil), nil
Expand Down Expand Up @@ -1111,6 +1134,31 @@ final class DiffTests: XCTestCase {
"""
)
}

func testCustomDumpRepresentableCollapsing() {
struct Results: CustomDumpRepresentable, Equatable {
var customDumpValue: Any {
[1, 2]
}
}
struct State: Equatable {
var date: Double
var results: Results
}
XCTAssertNoDifference(
diff(
State(date: 123456789, results: Results()),
State(date: 123456790, results: Results())
),
"""
DiffTests.State(
- date: 123456789.0,
+ date: 123456790.0,
results: […]
)
"""
)
}
}

private struct Stack<State: Equatable>: CustomDumpReflectable, Equatable {
Expand Down

0 comments on commit 53169c4

Please sign in to comment.