go-json/color_test.go

50 lines
842 B
Go
Raw Normal View History

2021-05-31 18:46:42 +03:00
package json_test
import (
"testing"
"github.com/goccy/go-json"
)
func TestColorize(t *testing.T) {
v := struct {
A int
B uint
C float32
D string
E bool
F []byte
G []int
2021-06-01 09:10:28 +03:00
H *struct{}
I map[string]interface{}
2021-05-31 18:46:42 +03:00
}{
A: 123,
B: 456,
C: 3.14,
D: "hello",
E: true,
F: []byte("binary"),
G: []int{1, 2, 3, 4},
2021-06-01 09:10:28 +03:00
H: nil,
I: map[string]interface{}{
"mapA": -10,
"mapB": 10,
"mapC": nil,
},
2021-05-31 18:46:42 +03:00
}
2021-06-01 09:10:28 +03:00
t.Run("marshal with color", func(t *testing.T) {
b, err := json.MarshalWithOption(v, json.Colorize(json.DefaultColorScheme))
if err != nil {
t.Fatal(err)
}
t.Log(string(b))
})
t.Run("marshal indent with color", func(t *testing.T) {
b, err := json.MarshalIndentWithOption(v, "", "\t", json.Colorize(json.DefaultColorScheme))
if err != nil {
t.Fatal(err)
}
t.Log("\n" + string(b))
})
2021-05-31 18:46:42 +03:00
}