Skip to content

Commit 0ff5e25

Browse files
authored
AnyHashable dump conformance (#64)
* AnyHashable dump conformance Without it the Mirror for AnyHashable has a style of nil so it just uses string interpolation and doesn't dump properly * Add tests * Move to Swift.swift file
1 parent 819d9d3 commit 0ff5e25

File tree

3 files changed

+74
-0
lines changed

3 files changed

+74
-0
lines changed

Sources/CustomDump/Conformances/Swift.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,9 @@ extension UnicodeScalar: CustomDumpRepresentable {
2222
String(self)
2323
}
2424
}
25+
26+
extension AnyHashable: CustomDumpRepresentable {
27+
public var customDumpValue: Any {
28+
base
29+
}
30+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import CustomDump
2+
import XCTest
3+
4+
final class SwiftTests: XCTestCase {
5+
func testCharacter() {
6+
let character: Character = "a"
7+
var dump = ""
8+
customDump(
9+
character,
10+
to: &dump
11+
)
12+
XCTAssertNoDifference(
13+
dump,
14+
"""
15+
"a"
16+
"""
17+
)
18+
}
19+
20+
func testStaticString() {
21+
let string: StaticString = "hello world!"
22+
var dump = ""
23+
customDump(
24+
string,
25+
to: &dump
26+
)
27+
XCTAssertNoDifference(
28+
dump,
29+
"""
30+
"hello world!"
31+
"""
32+
)
33+
}
34+
35+
func testUnicodeScalar() throws {
36+
let scalar = try XCTUnwrap("a".unicodeScalars.first)
37+
var dump = ""
38+
customDump(
39+
scalar,
40+
to: &dump
41+
)
42+
XCTAssertNoDifference(
43+
dump,
44+
"""
45+
"a"
46+
"""
47+
)
48+
}
49+
50+
func testAnyHashable() {
51+
let user: AnyHashable = HashableUser(id: 1, name: "James")
52+
var dump = ""
53+
customDump(
54+
user,
55+
to: &dump
56+
)
57+
XCTAssertNoDifference(
58+
dump,
59+
"""
60+
HashableUser(
61+
id: 1,
62+
name: "James"
63+
)
64+
"""
65+
)
66+
}
67+
}

Tests/CustomDumpTests/Mocks.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,3 +122,4 @@ struct Redacted<RawValue>: CustomDumpStringConvertible {
122122
}
123123

124124
struct User: Equatable, Identifiable { var id: Int, name: String }
125+
struct HashableUser: Equatable, Identifiable, Hashable { var id: Int, name: String }

0 commit comments

Comments
 (0)