From 0662b84a3551af6986300d7e33e6aa5889cfe209 Mon Sep 17 00:00:00 2001 From: Pieter Noordhuis Date: Tue, 29 Oct 2024 12:44:13 +0100 Subject: [PATCH] Add test for complex value --- libs/dyn/jsonsaver/saver_test.go | 33 ++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/libs/dyn/jsonsaver/saver_test.go b/libs/dyn/jsonsaver/saver_test.go index 7355dce8cb..06c4542b13 100644 --- a/libs/dyn/jsonsaver/saver_test.go +++ b/libs/dyn/jsonsaver/saver_test.go @@ -56,3 +56,36 @@ func TestMarshalSequence(t *testing.T) { require.NoError(t, err) require.JSONEq(t, `["value1","value2"]`, string(b)) } + +func TestMarshalComplex(t *testing.T) { + map1 := dyn.NewMapping() + map1.Set(dyn.V("str1"), dyn.V("value1")) + map1.Set(dyn.V("str2"), dyn.V("value2")) + + seq1 := []dyn.Value{} + seq1 = append(seq1, dyn.V("value1")) + seq1 = append(seq1, dyn.V("value2")) + + root := dyn.NewMapping() + root.Set(dyn.V("map1"), dyn.V(map1)) + root.Set(dyn.V("seq1"), dyn.V(seq1)) + + // Marshal without indent. + b, err := Marshal(dyn.V(root)) + require.NoError(t, err) + require.Equal(t, `{"map1":{"str1":"value1","str2":"value2"},"seq1":["value1","value2"]}`+"\n", string(b)) + + // Marshal with indent. + b, err = MarshalIndent(dyn.V(root), "", " ") + require.NoError(t, err) + require.Equal(t, `{ + "map1": { + "str1": "value1", + "str2": "value2" + }, + "seq1": [ + "value1", + "value2" + ] +}`+"\n", string(b)) +}