MCPcopy Create free account
hub / github.com/DoNewsCode/core / TestJSON_Marshal

Function TestJSON_Marshal

codec/json/json_test.go:22–61  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

20}
21
22func TestJSON_Marshal(t *testing.T) {
23 tests := []struct {
24 input interface{}
25 expect string
26 options []Option
27 }{
28 {
29 input: &testMessage{},
30 expect: `{"a":"","b":"","c":""}`,
31 options: nil,
32 },
33 {
34 input: &testMessage{Field1: "a", Field2: "b", Field3: "c"},
35 expect: `{"a":"a","b":"b","c":"c"}`,
36 options: nil,
37 },
38 {
39 input: &testMessage{Field1: "a", Field2: "b", Field3: "c"},
40 expect: `{
41 "a": "a",
42 "b": "b",
43 "c": "c"
44}`,
45 options: []Option{WithIndent(" ")},
46 },
47 }
48 for _, v := range tests {
49 data, err := (NewCodec(v.options...)).Marshal(v.input)
50 if err != nil {
51 t.Errorf("marshal(%#v): %s", v.input, err)
52 }
53 if got, want := string(data), v.expect; got != want {
54 if strings.Contains(want, "\n") {
55 t.Errorf("marshal(%#v):\nHAVE:\n%s\nWANT:\n%s", v.input, got, want)
56 } else {
57 t.Errorf("marshal(%#v):\nhave %#q\nwant %#q", v.input, got, want)
58 }
59 }
60 }
61}
62
63func TestJSON_Unmarshal(t *testing.T) {
64 p := &testMessage{}

Callers

nothing calls this directly

Calls 3

WithIndentFunction · 0.85
NewCodecFunction · 0.85
MarshalMethod · 0.65

Tested by

no test coverage detected