Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 89 additions & 0 deletions go/jsonutil/json_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package jsonutil

import (
"testing"
)

func TestMarshalNoEscape(t *testing.T) {
cases := []struct {
name string
v interface{}
expected string
}{
{
name: "normal",
v: struct {
Usr string
Pwd string
}{
Usr: "vitess",
Pwd: "vitess",
},
expected: "{\"Usr\":\"vitess\",\"Pwd\":\"vitess\"}",
},
{
name: "not exported",
v: struct {
usr string
pwd string
}{
usr: "vitess",
pwd: "vitess",
},
expected: "{}",
},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
json, _ := MarshalNoEscape(c.v)
sjson := string(json[:len(json)-1])
if sjson != c.expected {
t.Errorf("expected: %v, got: %v", c.expected, sjson)
}
})
}
}

func TestMarshalIndentNoEscape(t *testing.T) {
cases := []struct {
name string
v interface{}
prefix string
ident string
expected string
}{
{
name: "normal",
v: struct {
Usr string
Pwd string
}{
Usr: "vitess",
Pwd: "vitess",
},
prefix: "test",
ident: "\t",
expected: "{\ntest\t\"Usr\": \"vitess\",\ntest\t\"Pwd\": \"vitess\"\ntest}",
},
{
name: "not exported",
v: struct {
usr string
pwd string
}{
usr: "vitess",
pwd: "vitess",
},
expected: "{}",
},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
json, _ := MarshalIndentNoEscape(c.v, c.prefix, c.ident)
sjson := string(json[:len(json)-1])
if sjson != c.expected {
t.Errorf("expected: %v, got: %v", c.expected, sjson)
}
})
}
}