Skip to content

Commit 2a34993

Browse files
authored
Add String.init(customDumping:) (#79)
* Add `String.init(customDumping:)` It can be useful to get a custom dump representation of a value as a string on a single line. Swift provides `String.init(describing:)` and `String.init(reflecting:)` (though no equivalent for a quick string from `Swift.dump`), so maybe we can provide _similar_ functionality via a string initializer. * oops
1 parent 2bb5551 commit 2a34993

File tree

2 files changed

+21
-36
lines changed

2 files changed

+21
-36
lines changed

Sources/CustomDump/Dump.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,15 @@ public func customDump<T>(
3636
return value
3737
}
3838

39+
extension String {
40+
/// Creates a string dumping the given value.
41+
public init<Subject>(customDumping subject: Subject) {
42+
var dump = ""
43+
customDump(subject, to: &dump)
44+
self = dump
45+
}
46+
}
47+
3948
/// Dumps the given value's contents using its mirror to the specified output stream.
4049
///
4150
/// - Parameters:

Tests/CustomDumpTests/DumpTests.swift

Lines changed: 12 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,8 @@ import XCTest
1616
@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
1717
final class DumpTests: XCTestCase {
1818
func testAnyType() {
19-
var dump = ""
20-
customDump(Foo.Bar.self, to: &dump)
2119
XCTAssertNoDifference(
22-
dump,
20+
String(customDumping: Foo.Bar.self),
2321
"""
2422
Foo.Bar.self
2523
"""
@@ -28,100 +26,78 @@ final class DumpTests: XCTestCase {
2826
struct Feature {
2927
struct State {}
3028
}
31-
dump = ""
32-
customDump(Feature.State.self, to: &dump)
3329
XCTAssertNoDifference(
34-
dump,
30+
String(customDumping: Feature.State.self),
3531
"""
3632
DumpTests.Feature.State.self
3733
"""
3834
)
3935

40-
dump = ""
41-
customDump((x: Double, y: Double).self, to: &dump)
4236
XCTAssertNoDifference(
43-
dump,
37+
String(customDumping: (x: Double, y: Double).self),
4438
"""
4539
(x: Double, y: Double).self
4640
"""
4741
)
4842

49-
dump = ""
50-
customDump(Double?.self, to: &dump)
5143
XCTAssertNoDifference(
52-
dump,
44+
String(customDumping: Double?.self),
5345
"""
5446
Double?.self
5547
"""
5648
)
5749

58-
dump = ""
59-
customDump([Int].self, to: &dump)
6050
XCTAssertNoDifference(
61-
dump,
51+
String(customDumping: [Int].self),
6252
"""
6353
[Int].self
6454
"""
6555
)
6656

67-
dump = ""
68-
customDump([String: Int].self, to: &dump)
6957
XCTAssertNoDifference(
70-
dump,
58+
String(customDumping: [String: Int].self),
7159
"""
7260
[String: Int].self
7361
"""
7462
)
7563

76-
dump = ""
77-
customDump([[Double: Double?]].self, to: &dump)
7864
XCTAssertNoDifference(
79-
dump,
65+
String(customDumping: [[Double: Double?]].self),
8066
"""
8167
[[Double: Double?]].self
8268
"""
8369
)
8470

85-
dump = ""
86-
customDump([[Double: Double]?].self, to: &dump)
8771
XCTAssertNoDifference(
88-
dump,
72+
String(customDumping: [[Double: Double]?].self),
8973
"""
9074
[[Double: Double]?].self
9175
"""
9276
)
9377

94-
dump = ""
95-
customDump([[Double: [Double]]]?.self, to: &dump)
9678
XCTAssertNoDifference(
97-
dump,
79+
String(customDumping: [[Double: [Double]]]?.self),
9880
"""
9981
[[Double: [Double]]]?.self
10082
"""
10183
)
10284

103-
dump = ""
104-
customDump([[[Double: Double]]]?.self, to: &dump)
10585
XCTAssertNoDifference(
106-
dump,
86+
String(customDumping: [[[Double: Double]]]?.self),
10787
"""
10888
[[[Double: Double]]]?.self
10989
"""
11090
)
11191

112-
dump = ""
113-
customDump([Double: [Double?]].self, to: &dump)
11492
XCTAssertNoDifference(
115-
dump,
93+
String(customDumping: [Double: [Double?]].self),
11694
"""
11795
[Double: [Double?]].self
11896
"""
11997
)
12098

121-
dump = ""
122-
customDump([Double: [Double]?].self, to: &dump)
12399
XCTAssertNoDifference(
124-
dump,
100+
String(customDumping: [Double: [Double]?].self),
125101
"""
126102
[Double: [Double]?].self
127103
"""

0 commit comments

Comments
 (0)