Skip to content

Commit 0d3931a

Browse files
committed
attribute: extend scenarios for testing marshalling of Set
The extended test cases cover usages of a Set more completely.
1 parent 53e5996 commit 0d3931a

File tree

1 file changed

+56
-5
lines changed

1 file changed

+56
-5
lines changed

attribute/set_test.go

Lines changed: 56 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -350,11 +350,62 @@ func args(m reflect.Method) []reflect.Value {
350350
}
351351

352352
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
358409
}
359410

360411
func BenchmarkFiltering(b *testing.B) {

0 commit comments

Comments
 (0)