Skip to content

Commit c3c9102

Browse files
authored
Add support for string descriptions (#3)
This enables an identifier to be described as a string, by proxying its raw identifier’s description.
1 parent 01af4b5 commit c3c9102

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

Sources/Identity/Identity.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,14 @@ extension Identifier: ExpressibleByStringInterpolation
6969
where Value.RawIdentifier: ExpressibleByStringInterpolation,
7070
Value.RawIdentifier.StringLiteralType == String {}
7171

72+
// MARK: - String conversion support
73+
74+
extension Identifier: CustomStringConvertible {
75+
public var description: String {
76+
return "\(rawValue)"
77+
}
78+
}
79+
7280
// MARK: - Compiler-generated protocol support
7381

7482
extension Identifier: Equatable where Value.RawIdentifier: Equatable {}

Tests/IdentityTests/IdentityTests.swift

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,23 @@ final class IdentityTests: XCTestCase {
5959
XCTAssertEqual(model.id, "Hello, \("world!")")
6060
}
6161

62+
func testIdentifierDescription() {
63+
struct StringModel: Identifiable {
64+
let id: ID
65+
}
66+
67+
struct IntModel: Identifiable {
68+
typealias RawIdentifier = Int
69+
let id: ID
70+
}
71+
72+
let stringID: StringModel.ID = "An ID"
73+
let intID: IntModel.ID = 7
74+
75+
XCTAssertEqual(stringID.description, "An ID")
76+
XCTAssertEqual(intID.description, "7")
77+
}
78+
6279
func testAllTestsRunOnLinux() {
6380
verifyAllTestsRunOnLinux()
6481
}
@@ -70,6 +87,7 @@ extension IdentityTests: LinuxTestable {
7087
("testIntBasedIdentifier", testIntBasedIdentifier),
7188
("testCodableIdentifier", testCodableIdentifier),
7289
("testIdentifierEncodedAsSingleValue", testIdentifierEncodedAsSingleValue),
73-
("testExpressingIdentifierUsingStringInterpolation", testExpressingIdentifierUsingStringInterpolation)
90+
("testExpressingIdentifierUsingStringInterpolation", testExpressingIdentifierUsingStringInterpolation),
91+
("testIdentifierDescription", testIdentifierDescription)
7492
]
7593
}

0 commit comments

Comments
 (0)