Skip to content

Commit 87dd388

Browse files
authored
Abbreviate nested enums (#71)
We can use dot-abbreviation to keep descriptions short but provide enough context.
1 parent ce93271 commit 87dd388

File tree

4 files changed

+45
-13
lines changed

4 files changed

+45
-13
lines changed

Sources/CustomDump/Dump.swift

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,15 @@ public func customDump<T, TargetStream>(
5858
) -> T where TargetStream: TextOutputStream {
5959

6060
var idPerItem: [ObjectIdentifier: UInt] = [:]
61-
var occurencePerType: [String: UInt] = [:]
61+
var occurrencePerType: [String: UInt] = [:]
6262
var visitedItems: Set<ObjectIdentifier> = []
6363

6464
func customDumpHelp<T, TargetStream>(
6565
_ value: T,
6666
to target: inout TargetStream,
6767
name: String?,
6868
indent: Int,
69+
isRoot: Bool,
6970
maxDepth: Int
7071
) where TargetStream: TextOutputStream {
7172
if T.self is AnyObject.Type, withUnsafeBytes(of: value, { $0.allSatisfy { $0 == 0 } }) {
@@ -89,7 +90,12 @@ public func customDump<T, TargetStream>(
8990
var childOut = ""
9091
let child = mirror.children.first!
9192
customDumpHelp(
92-
child.value, to: &childOut, name: child.label, indent: 0, maxDepth: maxDepth - 1
93+
child.value,
94+
to: &childOut,
95+
name: child.label,
96+
indent: 0,
97+
isRoot: false,
98+
maxDepth: maxDepth - 1
9399
)
94100
if childOut.contains("\n") {
95101
if maxDepth == 0 {
@@ -113,7 +119,13 @@ public func customDump<T, TargetStream>(
113119
for (offset, var child) in children.enumerated() {
114120
transform(&child, offset)
115121
customDumpHelp(
116-
child.value, to: &out, name: child.label, indent: 2, maxDepth: maxDepth - 1)
122+
child.value,
123+
to: &out,
124+
name: child.label,
125+
indent: 2,
126+
isRoot: false,
127+
maxDepth: maxDepth - 1
128+
)
117129
if offset != children.count - 1 {
118130
out.write(",")
119131
}
@@ -132,16 +144,18 @@ public func customDump<T, TargetStream>(
132144
out.write(value.customDumpDescription)
133145

134146
case let (value as CustomDumpRepresentable, _):
135-
customDumpHelp(value.customDumpValue, to: &out, name: nil, indent: 0, maxDepth: maxDepth - 1)
147+
customDumpHelp(
148+
value.customDumpValue, to: &out, name: nil, indent: 0, isRoot: false, maxDepth: maxDepth - 1
149+
)
136150

137151
case let (value as AnyObject, .class?):
138152
let item = ObjectIdentifier(value)
139-
var occurence = occurencePerType[typeName(mirror.subjectType), default: 0] {
140-
didSet { occurencePerType[typeName(mirror.subjectType)] = occurence }
153+
var occurrence = occurrencePerType[typeName(mirror.subjectType), default: 0] {
154+
didSet { occurrencePerType[typeName(mirror.subjectType)] = occurrence }
141155
}
142156

143157
var id: String {
144-
let id = idPerItem[item, default: occurence]
158+
let id = idPerItem[item, default: occurrence]
145159
idPerItem[item] = id
146160

147161
return id > 1 ? "#\(id)" : ""
@@ -150,7 +164,7 @@ public func customDump<T, TargetStream>(
150164
out.write("\(typeName(mirror.subjectType))\(id)(↩︎)")
151165
} else {
152166
visitedItems.insert(item)
153-
occurence += 1
167+
occurrence += 1
154168
var children = Array(mirror.children)
155169

156170
var superclassMirror = mirror.superclassMirror
@@ -192,7 +206,7 @@ public func customDump<T, TargetStream>(
192206
}
193207

194208
case (_, .enum?):
195-
out.write("\(typeName(mirror.subjectType)).")
209+
out.write(isRoot ? "\(typeName(mirror.subjectType))." : ".")
196210
if let child = mirror.children.first {
197211
let childMirror = Mirror(customDumpReflecting: child.value)
198212
let associatedValuesMirror =
@@ -215,7 +229,7 @@ public func customDump<T, TargetStream>(
215229

216230
case (_, .optional?):
217231
if let value = mirror.children.first?.value {
218-
customDumpHelp(value, to: &out, name: nil, indent: 0, maxDepth: maxDepth)
232+
customDumpHelp(value, to: &out, name: nil, indent: 0, isRoot: false, maxDepth: maxDepth)
219233
} else {
220234
out.write("nil")
221235
}
@@ -266,7 +280,7 @@ public func customDump<T, TargetStream>(
266280
target.write((name.map { "\($0): " } ?? "").appending(out).indenting(by: indent))
267281
}
268282

269-
customDumpHelp(value, to: &target, name: name, indent: indent, maxDepth: maxDepth)
283+
customDumpHelp(value, to: &target, name: name, indent: indent, isRoot: true, maxDepth: maxDepth)
270284
return value
271285
}
272286

Tests/CustomDumpTests/Conformances/FoundationTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ final class FoundationTests: XCTestCase {
136136
dump,
137137
"""
138138
Calendar(
139-
identifier: Calendar.Identifier.gregorian,
139+
identifier: .gregorian,
140140
locale: Locale(),
141141
timeZone: TimeZone(
142142
identifier: "GMT",

Tests/CustomDumpTests/DumpTests.swift

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,20 @@ final class DumpTests: XCTestCase {
242242
)
243243
"""
244244
)
245+
246+
dump = ""
247+
customDump(Nested.nest(.fizz(0.9, buzz: "2")), to: &dump)
248+
XCTAssertNoDifference(
249+
dump,
250+
"""
251+
Nested.nest(
252+
.fizz(
253+
0.9,
254+
buzz: "2"
255+
)
256+
)
257+
"""
258+
)
245259
}
246260

247261
func testOptional() {
@@ -608,7 +622,7 @@ final class DumpTests: XCTestCase {
608622
dump,
609623
"""
610624
Result.success(
611-
Result.success(42)
625+
.success(42)
612626
)
613627
"""
614628
)

Tests/CustomDumpTests/Mocks.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ enum Enum {
4141
case fu(bar: Int)
4242
}
4343

44+
enum Nested {
45+
case nest(Enum)
46+
}
47+
4448
enum Namespaced {
4549
class Class {
4650
var x: Int

0 commit comments

Comments
 (0)