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