@@ -350,11 +350,62 @@ func args(m reflect.Method) []reflect.Value {
350
350
}
351
351
352
352
func TestJSONMarshaling (t * testing.T ) {
353
- const want = `[{"Key":"A","Value":{"Type":"STRING","Value":"a"}},{"Key":"B","Value":{"Type":"STRING","Value":"b"}}]`
354
- set := attribute .NewSet (attribute .String ("A" , "a" ), attribute .String ("B" , "b" ))
355
- data , err := json .Marshal (set )
356
- require .NoError (t , err )
357
- require .JSONEq (t , want , string (data ))
353
+ valueTests := []struct {
354
+ name string
355
+ set attribute.Set
356
+ want string
357
+ }{
358
+ {
359
+ name : "Empty" ,
360
+ set : attribute .NewSet (),
361
+ want : "[]" ,
362
+ },
363
+ {
364
+ name : "NonEmpty" ,
365
+ set : attribute .NewSet (attribute .Int ("A" , 1 )),
366
+ want : `[{"Key":"A","Value":{"Type":"INT64","Value":1}}]` ,
367
+ },
368
+ }
369
+ for _ , tt := range valueTests {
370
+ t .Run ("Value/" + tt .name , func (t * testing.T ) {
371
+ data , err := json .Marshal (tt .set )
372
+ require .NoError (t , err )
373
+ require .JSONEq (t , tt .want , string (data ))
374
+ })
375
+ }
376
+
377
+ pointerTests := []struct {
378
+ name string
379
+ set * attribute.Set
380
+ want string
381
+ }{
382
+ {
383
+ name : "Nil" ,
384
+ set : nil ,
385
+ want : "null" ,
386
+ },
387
+ {
388
+ name : "Empty" ,
389
+ set : pointerToSet (attribute .NewSet ()),
390
+ want : `[]` ,
391
+ },
392
+ {
393
+ name : "NonEmpty" ,
394
+ set : pointerToSet (attribute .NewSet (attribute .Int ("A" , 1 ))),
395
+ want : `[{"Key":"A","Value":{"Type":"INT64","Value":1}}]` ,
396
+ },
397
+ }
398
+ for _ , tt := range pointerTests {
399
+ t .Run ("Pointer/" + tt .name , func (t * testing.T ) {
400
+ data , err := json .Marshal (tt .set )
401
+ require .NoError (t , err )
402
+ require .JSONEq (t , tt .want , string (data ))
403
+ })
404
+ }
405
+ }
406
+
407
+ func pointerToSet (set attribute.Set ) * attribute.Set {
408
+ return & set
358
409
}
359
410
360
411
func BenchmarkFiltering (b * testing.B ) {
0 commit comments