@@ -56,6 +56,146 @@ final class TracedMacroTests: XCTestCase {
56
56
)
57
57
}
58
58
59
+ func test_tracedMacro_sync_throws( ) {
60
+ assertMacroExpansion (
61
+ """
62
+ @Traced
63
+ func syncThrowingExample(param: Int) throws {
64
+ struct ExampleError: Error {
65
+ }
66
+ throw ExampleError()
67
+ }
68
+ """ ,
69
+ expandedSource: """
70
+ func syncThrowingExample(param: Int) throws {
71
+ try withSpan( " syncThrowingExample " ) { span throws in
72
+ struct ExampleError: Error {
73
+ }
74
+ throw ExampleError()
75
+ }
76
+ }
77
+ """ ,
78
+ macros: [ " Traced " : TracedMacro . self]
79
+ )
80
+ }
81
+
82
+ func test_tracedMacro_sync_rethrows( ) {
83
+ assertMacroExpansion (
84
+ """
85
+ @Traced
86
+ func syncRethrowingExample(body: () throws -> Int) rethrows -> Int {
87
+ print( " Starting " )
88
+ let result = try body()
89
+ print( " Ending " )
90
+ return result
91
+ }
92
+ """ ,
93
+ expandedSource: """
94
+ func syncRethrowingExample(body: () throws -> Int) rethrows -> Int {
95
+ try withSpan( " syncRethrowingExample " ) { span throws -> Int in
96
+ print( " Starting " )
97
+ let result = try body()
98
+ print( " Ending " )
99
+ return result
100
+ }
101
+ }
102
+ """ ,
103
+ macros: [ " Traced " : TracedMacro . self]
104
+ )
105
+ }
106
+
107
+ func test_tracedMacro_async_nothrow( ) {
108
+ assertMacroExpansion (
109
+ """
110
+ @Traced
111
+ func asyncNonthrowingExample(param: Int) async {
112
+ print(param)
113
+ }
114
+ """ ,
115
+ expandedSource: """
116
+ func asyncNonthrowingExample(param: Int) async {
117
+ await withSpan( " asyncNonthrowingExample " ) { span async in
118
+ print(param)
119
+ }
120
+ }
121
+ """ ,
122
+ macros: [ " Traced " : TracedMacro . self]
123
+ )
124
+ }
125
+
126
+ func test_tracedMacro_async_throws( ) {
127
+ assertMacroExpansion (
128
+ """
129
+ @Traced
130
+ func asyncThrowingExample(param: Int) async throws {
131
+ try await Task.sleep(for: .seconds(1))
132
+ print( " Hello " )
133
+ }
134
+ """ ,
135
+ expandedSource: """
136
+ func asyncThrowingExample(param: Int) async throws {
137
+ try await withSpan( " asyncThrowingExample " ) { span async throws in
138
+ try await Task.sleep(for: .seconds(1))
139
+ print( " Hello " )
140
+ }
141
+ }
142
+ """ ,
143
+ macros: [ " Traced " : TracedMacro . self]
144
+ )
145
+ }
146
+
147
+ func test_tracedMacro_async_rethrows( ) {
148
+ assertMacroExpansion (
149
+ """
150
+ @Traced
151
+ func asyncRethrowingExample(body: () async throws -> Int) async rethrows -> Int {
152
+ try? await Task.sleep(for: .seconds(1))
153
+ let result = try await body()
154
+ span.attributes[ " result " ] = result
155
+ return result
156
+ }
157
+ """ ,
158
+ expandedSource: """
159
+ func asyncRethrowingExample(body: () async throws -> Int) async rethrows -> Int {
160
+ try await withSpan( " asyncRethrowingExample " ) { span async throws -> Int in
161
+ try? await Task.sleep(for: .seconds(1))
162
+ let result = try await body()
163
+ span.attributes[ " result " ] = result
164
+ return result
165
+ }
166
+ }
167
+ """ ,
168
+ macros: [ " Traced " : TracedMacro . self]
169
+ )
170
+ }
171
+
172
+ // Testing that this expands correctly, but not including this as a
173
+ // compile-test because withSpan doesn't currently support typed throws.
174
+ func test_tracedMacro_async_typed_throws( ) {
175
+ assertMacroExpansion (
176
+ """
177
+ @Traced
178
+ func asyncTypedThrowingExample<Err>(body: () async throws(Err) -> Int) async throws(Err) -> Int {
179
+ try? await Task.sleep(for: .seconds(1))
180
+ let result = try await body()
181
+ span.attributes[ " result " ] = result
182
+ return result
183
+ }
184
+ """ ,
185
+ expandedSource: """
186
+ func asyncTypedThrowingExample<Err>(body: () async throws(Err) -> Int) async throws(Err) -> Int {
187
+ try await withSpan( " asyncTypedThrowingExample " ) { span async throws(Err) -> Int in
188
+ try? await Task.sleep(for: .seconds(1))
189
+ let result = try await body()
190
+ span.attributes[ " result " ] = result
191
+ return result
192
+ }
193
+ }
194
+ """ ,
195
+ macros: [ " Traced " : TracedMacro . self]
196
+ )
197
+ }
198
+
59
199
func test_tracedMacro_accessSpan( ) {
60
200
assertMacroExpansion (
61
201
"""
@@ -83,6 +223,39 @@ func syncNonthrowingExample(param: Int) {
83
223
print ( param)
84
224
}
85
225
226
+ @Traced
227
+ func syncThrowingExample( param: Int ) throws {
228
+ struct ExampleError : Error { }
229
+ throw ExampleError ( )
230
+ }
231
+
232
+ @Traced
233
+ func syncRethrowingExample( body: ( ) throws -> Int ) rethrows -> Int {
234
+ print ( " Starting " )
235
+ let result = try body ( )
236
+ print ( " Ending " )
237
+ return result
238
+ }
239
+
240
+ @Traced
241
+ func asyncNonthrowingExample( param: Int ) async {
242
+ print ( param)
243
+ }
244
+
245
+ @Traced
246
+ func asyncThrowingExample( param: Int ) async throws {
247
+ try await Task . sleep ( for: . seconds( 1 ) )
248
+ print ( " Hello " )
249
+ }
250
+
251
+ @Traced
252
+ func asyncRethrowingExample( body: ( ) async throws -> Int ) async rethrows -> Int {
253
+ try ? await Task . sleep ( for: . seconds( 1 ) )
254
+ let result = try await body ( )
255
+ span. attributes [ " result " ] = result
256
+ return result
257
+ }
258
+
86
259
@Traced
87
260
func example( param: Int ) {
88
261
span. attributes [ " param " ] = param
0 commit comments