@@ -214,6 +214,203 @@ final class TracedMacroTests: XCTestCase {
214
214
macros: [ " Traced " : TracedMacro . self]
215
215
)
216
216
}
217
+
218
+ func test_tracedMacro_specifyOperationName( ) {
219
+ assertMacroExpansion (
220
+ """
221
+ @Traced( " example but with a custom operationName " )
222
+ func example(param: Int) {
223
+ span.attributes[ " param " ] = param
224
+ }
225
+ """ ,
226
+ expandedSource: """
227
+ func example(param: Int) {
228
+ withSpan( " example but with a custom operationName " ) { span in
229
+ span.attributes[ " param " ] = param
230
+ }
231
+ }
232
+ """ ,
233
+ macros: [ " Traced " : TracedMacro . self]
234
+ )
235
+
236
+ assertMacroExpansion (
237
+ """
238
+ let globalName = " example "
239
+
240
+ @Traced(globalName)
241
+ func example(param: Int) {
242
+ span.attributes[ " param " ] = param
243
+ }
244
+ """ ,
245
+ expandedSource: """
246
+ let globalName = " example "
247
+ func example(param: Int) {
248
+ withSpan(globalName) { span in
249
+ span.attributes[ " param " ] = param
250
+ }
251
+ }
252
+ """ ,
253
+ macros: [ " Traced " : TracedMacro . self]
254
+ )
255
+ }
256
+
257
+ func test_tracedMacro_specifyContext( ) {
258
+ assertMacroExpansion (
259
+ """
260
+ @Traced(context: .topLevel)
261
+ func example() {
262
+ print( " Hello " )
263
+ }
264
+ """ ,
265
+ expandedSource: """
266
+ func example() {
267
+ withSpan( " example " , context: .topLevel) { span in
268
+ print( " Hello " )
269
+ }
270
+ }
271
+ """ ,
272
+ macros: [ " Traced " : TracedMacro . self]
273
+ )
274
+ }
275
+
276
+ func test_tracedMacro_specifyKind( ) {
277
+ assertMacroExpansion (
278
+ """
279
+ @Traced(ofKind: .client)
280
+ func example() {
281
+ print( " Hello " )
282
+ }
283
+ """ ,
284
+ expandedSource: """
285
+ func example() {
286
+ withSpan( " example " , ofKind: .client) { span in
287
+ print( " Hello " )
288
+ }
289
+ }
290
+ """ ,
291
+ macros: [ " Traced " : TracedMacro . self]
292
+ )
293
+ }
294
+
295
+ func test_tracedMacro_specifySpanBindingName( ) {
296
+ assertMacroExpansion (
297
+ """
298
+ @Traced(span: " customSpan " )
299
+ func example(span: String) throws {
300
+ customSpan.attributes[ " span " ] = span
301
+ }
302
+ """ ,
303
+ expandedSource: """
304
+ func example(span: String) throws {
305
+ try withSpan( " example " ) { customSpan throws in
306
+ customSpan.attributes[ " span " ] = span
307
+ }
308
+ }
309
+ """ ,
310
+ macros: [ " Traced " : TracedMacro . self]
311
+ )
312
+
313
+ assertMacroExpansion (
314
+ """
315
+ @Traced(span: " _ " )
316
+ func example(span: String) {
317
+ print(span)
318
+ }
319
+ """ ,
320
+ expandedSource: """
321
+ func example(span: String) {
322
+ withSpan( " example " ) { _ in
323
+ print(span)
324
+ }
325
+ }
326
+ """ ,
327
+ macros: [ " Traced " : TracedMacro . self]
328
+ )
329
+ }
330
+
331
+ func test_tracedMacro_specifySpanBindingName_invalid( ) {
332
+ assertMacroExpansion (
333
+ """
334
+ @Traced(span: 1)
335
+ func example(span: String) throws {
336
+ customSpan.attributes[ " span " ] = span
337
+ }
338
+ """ ,
339
+ expandedSource: """
340
+ func example(span: String) throws {
341
+ customSpan.attributes[ " span " ] = span
342
+ }
343
+ """ ,
344
+ diagnostics: [
345
+ . init( message: " span name must be a simple string literal " , line: 1 , column: 1 ) ,
346
+ ] ,
347
+ macros: [ " Traced " : TracedMacro . self]
348
+ )
349
+
350
+ assertMacroExpansion (
351
+ """
352
+ @Traced(span: " invalid name " )
353
+ func example(span: String) throws {
354
+ customSpan.attributes[ " span " ] = span
355
+ }
356
+
357
+ @Traced(span: " 123 " )
358
+ func example2(span: String) throws {
359
+ customSpan.attributes[ " span " ] = span
360
+ }
361
+ """ ,
362
+ expandedSource: """
363
+ func example(span: String) throws {
364
+ customSpan.attributes[ " span " ] = span
365
+ }
366
+ func example2(span: String) throws {
367
+ customSpan.attributes[ " span " ] = span
368
+ }
369
+ """ ,
370
+ diagnostics: [
371
+ . init( message: " 'invalid name' is not a valid parameter name " , line: 1 , column: 1 ) ,
372
+ . init( message: " '123' is not a valid parameter name " , line: 6 , column: 1 ) ,
373
+ ] ,
374
+ macros: [ " Traced " : TracedMacro . self]
375
+ )
376
+
377
+ assertMacroExpansion (
378
+ """
379
+ @Traced(span: " Hello \\ (1) " )
380
+ func example(span: String) throws {
381
+ customSpan.attributes[ " span " ] = span
382
+ }
383
+ """ ,
384
+ expandedSource: """
385
+ func example(span: String) throws {
386
+ customSpan.attributes[ " span " ] = span
387
+ }
388
+ """ ,
389
+ diagnostics: [
390
+ . init( message: " span name must be a simple string literal " , line: 1 , column: 1 ) ,
391
+ ] ,
392
+ macros: [ " Traced " : TracedMacro . self]
393
+ )
394
+ }
395
+
396
+ func test_tracedMacro_multipleMacroParameters( ) {
397
+ assertMacroExpansion (
398
+ """
399
+ @Traced( " custom span name " , context: .topLevel, ofKind: .client, span: " customSpan " )
400
+ func example(span: Int) {
401
+ customSpan.attributes[ " span " ] = span + 1
402
+ }
403
+ """ ,
404
+ expandedSource: """
405
+ func example(span: Int) {
406
+ withSpan( " custom span name " , context: .topLevel, ofKind: .client) { customSpan in
407
+ customSpan.attributes[ " span " ] = span + 1
408
+ }
409
+ }
410
+ """ ,
411
+ macros: [ " Traced " : TracedMacro . self]
412
+ )
413
+ }
217
414
}
218
415
219
416
// MARK: Compile tests
@@ -261,4 +458,9 @@ func example(param: Int) {
261
458
span. attributes [ " param " ] = param
262
459
}
263
460
461
+ @Traced ( " custom span name " , context: . topLevel, ofKind: . client, span: " customSpan " )
462
+ func exampleWithParams( span: Int ) {
463
+ customSpan. attributes [ " span " ] = span + 1
464
+ }
465
+
264
466
#endif
0 commit comments