Skip to content

Commit c732fd4

Browse files
authored
Merge pull request #15 from p-x9/feature/macro-test-case
Test cases for `AssociatedObject` macro
2 parents db5b07d + 21ce788 commit c732fd4

File tree

3 files changed

+524
-10
lines changed

3 files changed

+524
-10
lines changed

Package.swift

+4-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,10 @@ let package = Package(
3838
),
3939
.testTarget(
4040
name: "AssociatedObjectTests",
41-
dependencies: ["AssociatedObject"]
41+
dependencies: [
42+
"AssociatedObject",
43+
.product(name: "SwiftSyntaxMacrosTestSupport", package: "swift-syntax")
44+
]
4245
),
4346
]
4447
)

Sources/AssociatedObjectPlugin/AssociatedObjectMacro.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ extension AssociatedObjectMacro: PeerMacro {
3030
bindingSpecifier: .identifier("static var"),
3131
bindings: PatternBindingListSyntax {
3232
PatternBindingSyntax(
33-
pattern: IdentifierPatternSyntax(identifier: .identifier("__associated_\(identifier)Key")),
33+
pattern: IdentifierPatternSyntax(identifier: .identifier("__associated_\(identifier.trimmed)Key")),
3434
typeAnnotation: .init(type: IdentifierTypeSyntax(name: .identifier("UInt8"))),
3535
initializer: InitializerClauseSyntax(value: ExprSyntax(stringLiteral: "0"))
3636
)
@@ -131,7 +131,7 @@ extension AssociatedObjectMacro {
131131
"""
132132
objc_getAssociatedObject(
133133
self,
134-
&Self.__associated_\(identifier)Key
134+
&Self.__associated_\(identifier.trimmed)Key
135135
) as? \(type)
136136
?? \(defaultValue ?? "nil")
137137
"""
@@ -178,7 +178,7 @@ extension AssociatedObjectMacro {
178178
"""
179179
objc_setAssociatedObject(
180180
self,
181-
&Self.__associated_\(identifier)Key,
181+
&Self.__associated_\(identifier.trimmed)Key,
182182
newValue,
183183
\(policy)
184184
)

0 commit comments

Comments
 (0)