github.com/bingoohuang/gg@v0.0.0-20240325092523-45da7dee9335/pkg/jsoni/api_tests/marshal_indent_test.go (about)

     1  package test
     2  
     3  import (
     4  	"context"
     5  	"encoding/json"
     6  	"testing"
     7  
     8  	"github.com/bingoohuang/gg/pkg/jsoni"
     9  	"github.com/stretchr/testify/require"
    10  )
    11  
    12  func Test_marshal_indent(t *testing.T) {
    13  	should := require.New(t)
    14  	obj := struct {
    15  		F1 int
    16  		F2 []int
    17  	}{1, []int{2, 3, 4}}
    18  	output, err := json.MarshalIndent(obj, "", "  ")
    19  	should.Nil(err)
    20  	should.Equal("{\n  \"F1\": 1,\n  \"F2\": [\n    2,\n    3,\n    4\n  ]\n}", string(output))
    21  	output, err = jsoni.MarshalIndent(obj, "", "  ")
    22  	should.Nil(err)
    23  	should.Equal("{\n  \"F1\": 1,\n  \"F2\": [\n    2,\n    3,\n    4\n  ]\n}", string(output))
    24  }
    25  
    26  func Test_marshal_indent_map(t *testing.T) {
    27  	should := require.New(t)
    28  	obj := map[int]int{1: 2}
    29  	output, err := json.MarshalIndent(obj, "", "  ")
    30  	should.Nil(err)
    31  	should.Equal("{\n  \"1\": 2\n}", string(output))
    32  	output, err = jsoni.MarshalIndent(obj, "", "  ")
    33  	should.Nil(err)
    34  	should.Equal("{\n  \"1\": 2\n}", string(output))
    35  	output, err = jsoni.ConfigCompatibleWithStandardLibrary.MarshalIndent(context.Background(), obj, "", "  ")
    36  	should.Nil(err)
    37  	should.Equal("{\n  \"1\": 2\n}", string(output))
    38  }