Skip to content

Commit 79cb03e

Browse files
authored
Abbreviate optional, array, and dictionary types (#78)
We try our best to pretty-print in a Swift-y fashion, so let's extend that to the shorthand syntaxes for optionals, arrays, and dictionaries.
1 parent de8ba65 commit 79cb03e

File tree

2 files changed

+106
-5
lines changed

2 files changed

+106
-5
lines changed

Sources/CustomDump/Internal/AnyType.swift

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,31 @@ func typeName(
99
with: "",
1010
options: .regularExpression
1111
)
12-
.replacingOccurrences(
13-
of: #"\w+\.([\w.]+)"#,
14-
with: "$1",
15-
options: .regularExpression
16-
)
12+
for _ in 1...10 { // NB: Only handle so much nesting
13+
let abbreviated = name
14+
.replacingOccurrences(
15+
of: #"\bSwift.Optional<([^><]+)>"#,
16+
with: "$1?",
17+
options: .regularExpression
18+
)
19+
.replacingOccurrences(
20+
of: #"\bSwift.Array<([^><]+)>"#,
21+
with: "[$1]",
22+
options: .regularExpression
23+
)
24+
.replacingOccurrences(
25+
of: #"\bSwift.Dictionary<([^,<]+), ([^><]+)>"#,
26+
with: "[$1: $2]",
27+
options: .regularExpression
28+
)
29+
if abbreviated == name { break }
30+
name = abbreviated
31+
}
32+
name = name.replacingOccurrences(
33+
of: #"\w+\.([\w.]+)"#,
34+
with: "$1",
35+
options: .regularExpression
36+
)
1737
if genericsAbbreviated {
1838
name = name.replacingOccurrences(
1939
of: #"<.+>"#,

Tests/CustomDumpTests/DumpTests.swift

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,87 @@ final class DumpTests: XCTestCase {
4545
(x: Double, y: Double).self
4646
"""
4747
)
48+
49+
dump = ""
50+
customDump(Double?.self, to: &dump)
51+
XCTAssertNoDifference(
52+
dump,
53+
"""
54+
Double?.self
55+
"""
56+
)
57+
58+
dump = ""
59+
customDump([Int].self, to: &dump)
60+
XCTAssertNoDifference(
61+
dump,
62+
"""
63+
[Int].self
64+
"""
65+
)
66+
67+
dump = ""
68+
customDump([String: Int].self, to: &dump)
69+
XCTAssertNoDifference(
70+
dump,
71+
"""
72+
[String: Int].self
73+
"""
74+
)
75+
76+
dump = ""
77+
customDump([[Double: Double?]].self, to: &dump)
78+
XCTAssertNoDifference(
79+
dump,
80+
"""
81+
[[Double: Double?]].self
82+
"""
83+
)
84+
85+
dump = ""
86+
customDump([[Double: Double]?].self, to: &dump)
87+
XCTAssertNoDifference(
88+
dump,
89+
"""
90+
[[Double: Double]?].self
91+
"""
92+
)
93+
94+
dump = ""
95+
customDump([[Double: [Double]]]?.self, to: &dump)
96+
XCTAssertNoDifference(
97+
dump,
98+
"""
99+
[[Double: [Double]]]?.self
100+
"""
101+
)
102+
103+
dump = ""
104+
customDump([[[Double: Double]]]?.self, to: &dump)
105+
XCTAssertNoDifference(
106+
dump,
107+
"""
108+
[[[Double: Double]]]?.self
109+
"""
110+
)
111+
112+
dump = ""
113+
customDump([Double: [Double?]].self, to: &dump)
114+
XCTAssertNoDifference(
115+
dump,
116+
"""
117+
[Double: [Double?]].self
118+
"""
119+
)
120+
121+
dump = ""
122+
customDump([Double: [Double]?].self, to: &dump)
123+
XCTAssertNoDifference(
124+
dump,
125+
"""
126+
[Double: [Double]?].self
127+
"""
128+
)
48129
}
49130

50131
func testClass() {

0 commit comments

Comments
 (0)