-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
json_test.go
72 lines (68 loc) · 1.47 KB
/
json_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package sdkclient_test
import (
"testing"
sdkclient "github.com/fujiwara/awslim"
"github.com/google/go-cmp/cmp"
)
var jsonTestCases = []struct {
Name string
Src any
Expect string
CamelCase bool
}{
{
Name: "no-camel",
Src: struct{ FooBar string }{FooBar: "baz"},
Expect: `{"FooBar":"baz"}`,
CamelCase: false,
},
{
Name: "simple",
Src: struct{ FooBar string }{FooBar: "baz"},
Expect: `{"fooBar":"baz"}`,
CamelCase: true,
},
{
Name: "nested",
Src: struct{ FooBar struct{ BazQux string } }{FooBar: struct{ BazQux string }{BazQux: "quux"}},
Expect: `{"fooBar":{"bazQux":"quux"}}`,
CamelCase: true,
},
{
Name: "array",
Src: []any{struct{ FooBar string }{FooBar: "baz"}, 0, 1, true, nil},
Expect: `[{"fooBar":"baz"},0,1,true,null]`,
CamelCase: true,
},
{
Name: "string",
Src: "FooBar",
Expect: `"FooBar"`,
CamelCase: true,
},
{
Name: "number",
Src: 42,
Expect: `42`,
CamelCase: true,
},
{
Name: "true",
Src: true,
Expect: `true`,
CamelCase: true,
},
}
func TestMarshalJSON(t *testing.T) {
for _, tc := range jsonTestCases {
t.Run(tc.Name, func(t *testing.T) {
b, err := sdkclient.MarshalJSON(tc.Src, tc.CamelCase)
if err != nil {
t.Fatalf("failed to unmarshal: %v", err)
}
if diff := cmp.Diff(tc.Expect, string(b)); diff != "" {
t.Errorf("expect match (-got +want):\n%s", diff)
}
})
}
}