@@ -25,90 +25,130 @@ import SwiftSyntaxMacros
25
25
import SwiftSyntaxMacrosTestSupport
26
26
import XCTest
27
27
28
- fileprivate struct DeclsFromStringsMacro : DeclarationMacro {
29
- static func expansion(
30
- of node: some FreestandingMacroExpansionSyntax ,
31
- in context: some MacroExpansionContext
32
- ) throws -> [ DeclSyntax ] {
33
- var strings : [ String ] = [ ]
34
- for arg in node. arguments {
35
- guard let value = arg. expression. as ( StringLiteralExprSyntax . self) ? . representedLiteralValue else {
36
- continue
37
- }
38
- strings. append ( value)
39
- }
40
-
41
- return strings. map { " \( raw: $0) " }
42
- }
43
- }
44
-
45
28
final class ExtensionMacroTests : XCTestCase {
46
29
private let indentationWidth : Trivia = . spaces( 2 )
47
30
48
- func testExtensionExpansion( ) {
49
- struct SendableExtensionMacro : ExtensionMacro {
50
- static func expansion(
51
- of node: AttributeSyntax ,
52
- attachedTo: some DeclGroupSyntax ,
53
- providingExtensionsOf type: some TypeSyntaxProtocol ,
54
- conformingTo protocols: [ TypeSyntax ] ,
55
- in context: some MacroExpansionContext
56
- ) throws -> [ ExtensionDeclSyntax ] {
57
- let sendableExtension : DeclSyntax =
58
- """
59
- extension \( type. trimmed) : Sendable {}
60
- """
31
+ func testSimpleExpansion( ) {
32
+ assertMacroExpansion (
33
+ """
34
+ @AddSendableExtension
35
+ struct MyType {}
36
+ """ ,
37
+ expandedSource: """
38
+ struct MyType {}
61
39
62
- guard let extensionDecl = sendableExtension. as ( ExtensionDeclSyntax . self) else {
63
- return [ ]
40
+ extension MyType: Sendable {
64
41
}
42
+ """ ,
43
+ macros: [ " AddSendableExtension " : SendableExtensionMacro . self] ,
44
+ indentationWidth: indentationWidth
45
+ )
46
+ }
65
47
66
- return [ extensionDecl]
67
- }
68
- }
69
-
48
+ func testNestedExpansion( ) {
70
49
assertMacroExpansion (
71
50
"""
72
- @AddSendableExtension
73
- struct MyType {
51
+ struct Wrapper {
52
+ @AddSendableExtension
53
+ struct MyType {}
74
54
}
75
55
""" ,
76
56
expandedSource: """
77
-
78
- struct MyType {
57
+ struct Wrapper {
58
+ struct MyType {}
79
59
}
80
60
81
- extension MyType: Sendable {
61
+ extension Wrapper. MyType: Sendable {
82
62
}
83
63
""" ,
84
64
macros: [ " AddSendableExtension " : SendableExtensionMacro . self] ,
85
65
indentationWidth: indentationWidth
86
66
)
67
+ }
87
68
69
+ func testNestedInExtensionExpansion( ) {
88
70
assertMacroExpansion (
89
71
"""
90
72
struct Wrapper {
73
+ struct AnotherWrapper {}
74
+ }
75
+ extension Wrapper.AnotherWrapper {
91
76
@AddSendableExtension
92
- struct MyType {
93
- }
77
+ struct MyType {}
94
78
}
95
79
""" ,
96
80
expandedSource: """
97
81
struct Wrapper {
98
- struct MyType {
82
+ struct AnotherWrapper {}
83
+ }
84
+ extension Wrapper.AnotherWrapper {
85
+ struct MyType {}
86
+ }
87
+
88
+ extension Wrapper.AnotherWrapper.MyType: Sendable {
89
+ }
90
+ """ ,
91
+ macros: [ " AddSendableExtension " : SendableExtensionMacro . self] ,
92
+ indentationWidth: indentationWidth
93
+ )
94
+ }
95
+
96
+ func testComplexNestedExpansion( ) {
97
+ assertMacroExpansion (
98
+ """
99
+ struct Wrapper {}
100
+ extension Wrapper {
101
+ struct AnotherWrapper {
102
+ @AddSendableExtension
103
+ struct MyType {}
104
+ }
105
+ }
106
+ """ ,
107
+ expandedSource: """
108
+ struct Wrapper {}
109
+ extension Wrapper {
110
+ struct AnotherWrapper {
111
+ struct MyType {}
99
112
}
100
113
}
101
114
102
- extension MyType: Sendable {
115
+ extension Wrapper.AnotherWrapper. MyType: Sendable {
103
116
}
104
117
""" ,
105
118
macros: [ " AddSendableExtension " : SendableExtensionMacro . self] ,
106
119
indentationWidth: indentationWidth
107
120
)
108
121
}
109
122
123
+ func testAttachedToInvalid( ) {
124
+ assertMacroExpansion (
125
+ " @AddSendableExtension var foo: Int " ,
126
+ expandedSource: " var foo: Int " ,
127
+ macros: [
128
+ " AddSendableExtension " : SendableExtensionMacro . self
129
+ ]
130
+ )
131
+
132
+ assertMacroExpansion (
133
+ """
134
+ struct Foo {
135
+ @AddSendableExtension var foo: Int
136
+ }
137
+ """ ,
138
+ expandedSource:
139
+ """
140
+ struct Foo {
141
+ var foo: Int
142
+ }
143
+ """ ,
144
+ macros: [
145
+ " AddSendableExtension " : SendableExtensionMacro . self
146
+ ]
147
+ )
148
+ }
149
+
110
150
func testEmpty( ) {
111
- struct TestMacro : ExtensionMacro {
151
+ struct EmptyExtensionMacro : ExtensionMacro {
112
152
static func expansion(
113
153
of node: AttributeSyntax ,
114
154
attachedTo declaration: some DeclGroupSyntax ,
@@ -121,11 +161,32 @@ final class ExtensionMacroTests: XCTestCase {
121
161
}
122
162
123
163
assertMacroExpansion (
124
- " @Test struct Foo {} " ,
164
+ " @Empty struct Foo {} " ,
125
165
expandedSource: " struct Foo {} " ,
126
166
macros: [
127
- " Test " : TestMacro . self
167
+ " Empty " : EmptyExtensionMacro . self
128
168
]
129
169
)
130
170
}
131
171
}
172
+
173
+ fileprivate struct SendableExtensionMacro : ExtensionMacro {
174
+ static func expansion(
175
+ of node: AttributeSyntax ,
176
+ attachedTo: some DeclGroupSyntax ,
177
+ providingExtensionsOf type: some TypeSyntaxProtocol ,
178
+ conformingTo protocols: [ TypeSyntax ] ,
179
+ in context: some MacroExpansionContext
180
+ ) throws -> [ ExtensionDeclSyntax ] {
181
+ let sendableExtension : DeclSyntax =
182
+ """
183
+ extension \( type. trimmed) : Sendable {}
184
+ """
185
+
186
+ guard let extensionDecl = sendableExtension. as ( ExtensionDeclSyntax . self) else {
187
+ return [ ]
188
+ }
189
+
190
+ return [ extensionDecl]
191
+ }
192
+ }
0 commit comments