diff --git a/pkg/flatten/flatten_test.go b/pkg/flatten/flatten_test.go new file mode 100644 index 000000000..5a647f2e9 --- /dev/null +++ b/pkg/flatten/flatten_test.go @@ -0,0 +1,33 @@ +package flatten + +import ( + "github.com/icinga/icingadb/pkg/types" + "github.com/stretchr/testify/assert" + "testing" +) + +func TestFlatten(t *testing.T) { + for _, st := range []struct { + name string + prefix string + value any + output map[string]types.String + }{ + {"nil", "a", nil, map[string]types.String{"a": types.NewString("null")}}, + {"bool", "b", true, map[string]types.String{"b": types.NewString("true")}}, + {"int", "c", 42, map[string]types.String{"c": types.NewString("42")}}, + {"float", "d", 77.7, map[string]types.String{"d": types.NewString("77.7")}}, + {"large_float", "e", 1e23, map[string]types.String{"e": types.NewString("100000000000000000000000")}}, + {"string", "f", "\x00", map[string]types.String{"f": types.NewString("\x00")}}, + {"nil_slice", "g", []any(nil), map[string]types.String{"g": {}}}, + {"empty_slice", "h", []any{}, map[string]types.String{"h": {}}}, + {"slice", "i", []any{nil}, map[string]types.String{"i[0]": types.NewString("null")}}, + {"nil_map", "j", map[string]any(nil), map[string]types.String{"j": {}}}, + {"empty_map", "k", map[string]any{}, map[string]types.String{"k": {}}}, + {"map", "l", map[string]any{" ": nil}, map[string]types.String{"l. ": types.NewString("null")}}, + } { + t.Run(st.name, func(t *testing.T) { + assert.Equal(t, st.output, Flatten(st.value, st.prefix)) + }) + } +}