package json_test import ( "bytes" "strings" "testing" "github.com/goccy/go-json" ) func TestCoverInt8(t *testing.T) { type structInt8 struct { A int8 `json:"a"` } type structInt8Ptr struct { A *int8 `json:"a"` } tests := []struct { name string expected string indentExpected string data interface{} }{ { name: "HeadInt8Zero", expected: `{"a":0}`, indentExpected: ` { "a": 0 } `, data: struct { A int8 `json:"a"` }{}, }, { name: "HeadInt8", expected: `{"a":1}`, indentExpected: ` { "a": 1 } `, data: struct { A int8 `json:"a"` }{A: 1}, }, { name: "HeadInt8Ptr", expected: `{"a":1}`, indentExpected: ` { "a": 1 } `, data: struct { A *int8 `json:"a"` }{A: int8ptr(1)}, }, { name: "HeadInt8PtrNil", expected: `{"a":null}`, indentExpected: ` { "a": null } `, data: struct { A *int8 `json:"a"` }{A: nil}, }, { name: "PtrHeadInt8Zero", expected: `{"a":0}`, indentExpected: ` { "a": 0 } `, data: &struct { A int8 `json:"a"` }{}, }, { name: "PtrHeadInt8", expected: `{"a":1}`, indentExpected: ` { "a": 1 } `, data: &struct { A int8 `json:"a"` }{A: 1}, }, { name: "PtrHeadInt8Ptr", expected: `{"a":1}`, indentExpected: ` { "a": 1 } `, data: &struct { A *int8 `json:"a"` }{A: int8ptr(1)}, }, { name: "PtrHeadInt8PtrNil", expected: `{"a":null}`, indentExpected: ` { "a": null } `, data: &struct { A *int8 `json:"a"` }{A: nil}, }, { name: "PtrHeadInt8Nil", expected: `null`, indentExpected: ` null `, data: (*struct { A *int8 `json:"a"` })(nil), }, { name: "HeadInt8ZeroMultiFields", expected: `{"a":0,"b":0}`, indentExpected: ` { "a": 0, "b": 0 } `, data: struct { A int8 `json:"a"` B int8 `json:"b"` }{}, }, { name: "HeadInt8MultiFields", expected: `{"a":1,"b":2}`, indentExpected: ` { "a": 1, "b": 2 } `, data: struct { A int8 `json:"a"` B int8 `json:"b"` }{A: 1, B: 2}, }, { name: "HeadInt8PtrMultiFields", expected: `{"a":1,"b":2}`, indentExpected: ` { "a": 1, "b": 2 } `, data: struct { A *int8 `json:"a"` B *int8 `json:"b"` }{A: int8ptr(1), B: int8ptr(2)}, }, { name: "HeadInt8PtrNilMultiFields", expected: `{"a":null,"b":null}`, indentExpected: ` { "a": null, "b": null } `, data: struct { A *int8 `json:"a"` B *int8 `json:"b"` }{A: nil, B: nil}, }, { name: "PtrHeadInt8ZeroMultiFields", expected: `{"a":0,"b":0}`, indentExpected: ` { "a": 0, "b": 0 } `, data: &struct { A int8 `json:"a"` B int8 `json:"b"` }{}, }, { name: "PtrHeadInt8MultiFields", expected: `{"a":1,"b":2}`, indentExpected: ` { "a": 1, "b": 2 } `, data: &struct { A int8 `json:"a"` B int8 `json:"b"` }{A: 1, B: 2}, }, { name: "PtrHeadInt8PtrMultiFields", expected: `{"a":1,"b":2}`, indentExpected: ` { "a": 1, "b": 2 } `, data: &struct { A *int8 `json:"a"` B *int8 `json:"b"` }{A: int8ptr(1), B: int8ptr(2)}, }, { name: "PtrHeadInt8PtrNilMultiFields", expected: `{"a":null,"b":null}`, indentExpected: ` { "a": null, "b": null } `, data: &struct { A *int8 `json:"a"` B *int8 `json:"b"` }{A: nil, B: nil}, }, { name: "PtrHeadInt8NilMultiFields", expected: `null`, indentExpected: ` null `, data: (*struct { A *int8 `json:"a"` B *int8 `json:"b"` })(nil), }, { name: "HeadInt8ZeroNotRoot", expected: `{"A":{"a":0}}`, indentExpected: ` { "A": { "a": 0 } } `, data: struct { A struct { A int8 `json:"a"` } }{}, }, { name: "HeadInt8NotRoot", expected: `{"A":{"a":1}}`, indentExpected: ` { "A": { "a": 1 } } `, data: struct { A struct { A int8 `json:"a"` } }{A: struct { A int8 `json:"a"` }{A: 1}}, }, { name: "HeadInt8PtrNotRoot", expected: `{"A":{"a":1}}`, indentExpected: ` { "A": { "a": 1 } } `, data: struct { A struct { A *int8 `json:"a"` } }{A: struct { A *int8 `json:"a"` }{int8ptr(1)}}, }, { name: "HeadInt8PtrNilNotRoot", expected: `{"A":{"a":null}}`, indentExpected: ` { "A": { "a": null } } `, data: struct { A struct { A *int8 `json:"a"` } }{}, }, { name: "PtrHeadInt8ZeroNotRoot", expected: `{"A":{"a":0}}`, indentExpected: ` { "A": { "a": 0 } } `, data: struct { A *struct { A int8 `json:"a"` } }{A: new(struct { A int8 `json:"a"` })}, }, { name: "PtrHeadInt8NotRoot", expected: `{"A":{"a":1}}`, indentExpected: ` { "A": { "a": 1 } } `, data: struct { A *struct { A int8 `json:"a"` } }{A: &(struct { A int8 `json:"a"` }{A: 1})}, }, { name: "PtrHeadInt8PtrNotRoot", expected: `{"A":{"a":1}}`, indentExpected: ` { "A": { "a": 1 } } `, data: struct { A *struct { A *int8 `json:"a"` } }{A: &(struct { A *int8 `json:"a"` }{A: int8ptr(1)})}, }, { name: "PtrHeadInt8PtrNilNotRoot", expected: `{"A":{"a":null}}`, indentExpected: ` { "A": { "a": null } } `, data: struct { A *struct { A *int8 `json:"a"` } }{A: &(struct { A *int8 `json:"a"` }{A: nil})}, }, { name: "PtrHeadInt8NilNotRoot", expected: `{"A":null}`, indentExpected: ` { "A": null } `, data: struct { A *struct { A *int8 `json:"a"` } }{A: nil}, }, { name: "HeadInt8ZeroMultiFieldsNotRoot", expected: `{"A":{"a":0},"B":{"b":0}}`, indentExpected: ` { "A": { "a": 0 }, "B": { "b": 0 } } `, data: struct { A struct { A int8 `json:"a"` } B struct { B int8 `json:"b"` } }{}, }, { name: "HeadInt8MultiFieldsNotRoot", expected: `{"A":{"a":1},"B":{"b":2}}`, indentExpected: ` { "A": { "a": 1 }, "B": { "b": 2 } } `, data: struct { A struct { A int8 `json:"a"` } B struct { B int8 `json:"b"` } }{A: struct { A int8 `json:"a"` }{A: 1}, B: struct { B int8 `json:"b"` }{B: 2}}, }, { name: "HeadInt8PtrMultiFieldsNotRoot", expected: `{"A":{"a":1},"B":{"b":2}}`, indentExpected: ` { "A": { "a": 1 }, "B": { "b": 2 } } `, data: struct { A struct { A *int8 `json:"a"` } B struct { B *int8 `json:"b"` } }{A: struct { A *int8 `json:"a"` }{A: int8ptr(1)}, B: struct { B *int8 `json:"b"` }{B: int8ptr(2)}}, }, { name: "HeadInt8PtrNilMultiFieldsNotRoot", expected: `{"A":{"a":null},"B":{"b":null}}`, indentExpected: ` { "A": { "a": null }, "B": { "b": null } } `, data: struct { A struct { A *int8 `json:"a"` } B struct { B *int8 `json:"b"` } }{A: struct { A *int8 `json:"a"` }{A: nil}, B: struct { B *int8 `json:"b"` }{B: nil}}, }, { name: "PtrHeadInt8ZeroMultiFieldsNotRoot", expected: `{"A":{"a":0},"B":{"b":0}}`, indentExpected: ` { "A": { "a": 0 }, "B": { "b": 0 } } `, data: &struct { A struct { A int8 `json:"a"` } B struct { B int8 `json:"b"` } }{}, }, { name: "PtrHeadInt8MultiFieldsNotRoot", expected: `{"A":{"a":1},"B":{"b":2}}`, indentExpected: ` { "A": { "a": 1 }, "B": { "b": 2 } } `, data: &struct { A struct { A int8 `json:"a"` } B struct { B int8 `json:"b"` } }{A: struct { A int8 `json:"a"` }{A: 1}, B: struct { B int8 `json:"b"` }{B: 2}}, }, { name: "PtrHeadInt8PtrMultiFieldsNotRoot", expected: `{"A":{"a":1},"B":{"b":2}}`, indentExpected: ` { "A": { "a": 1 }, "B": { "b": 2 } } `, data: &struct { A *struct { A *int8 `json:"a"` } B *struct { B *int8 `json:"b"` } }{A: &(struct { A *int8 `json:"a"` }{A: int8ptr(1)}), B: &(struct { B *int8 `json:"b"` }{B: int8ptr(2)})}, }, { name: "PtrHeadInt8PtrNilMultiFieldsNotRoot", expected: `{"A":null,"B":null}`, indentExpected: ` { "A": null, "B": null } `, data: &struct { A *struct { A *int8 `json:"a"` } B *struct { B *int8 `json:"b"` } }{A: nil, B: nil}, }, { name: "PtrHeadInt8NilMultiFieldsNotRoot", expected: `null`, indentExpected: ` null `, data: (*struct { A *struct { A *int8 `json:"a"` } B *struct { B *int8 `json:"b"` } })(nil), }, { name: "PtrHeadInt8DoubleMultiFieldsNotRoot", expected: `{"A":{"a":1,"b":2},"B":{"a":3,"b":4}}`, indentExpected: ` { "A": { "a": 1, "b": 2 }, "B": { "a": 3, "b": 4 } } `, data: &struct { A *struct { A int8 `json:"a"` B int8 `json:"b"` } B *struct { A int8 `json:"a"` B int8 `json:"b"` } }{A: &(struct { A int8 `json:"a"` B int8 `json:"b"` }{A: 1, B: 2}), B: &(struct { A int8 `json:"a"` B int8 `json:"b"` }{A: 3, B: 4})}, }, { name: "PtrHeadInt8NilDoubleMultiFieldsNotRoot", expected: `{"A":null,"B":null}`, indentExpected: ` { "A": null, "B": null } `, data: &struct { A *struct { A int8 `json:"a"` B int8 `json:"b"` } B *struct { A int8 `json:"a"` B int8 `json:"b"` } }{A: nil, B: nil}, }, { name: "PtrHeadInt8NilDoubleMultiFieldsNotRoot", expected: `null`, indentExpected: ` null `, data: (*struct { A *struct { A int8 `json:"a"` B int8 `json:"b"` } B *struct { A int8 `json:"a"` B int8 `json:"b"` } })(nil), }, { name: "PtrHeadInt8PtrDoubleMultiFieldsNotRoot", expected: `{"A":{"a":1,"b":2},"B":{"a":3,"b":4}}`, indentExpected: ` { "A": { "a": 1, "b": 2 }, "B": { "a": 3, "b": 4 } } `, data: &struct { A *struct { A *int8 `json:"a"` B *int8 `json:"b"` } B *struct { A *int8 `json:"a"` B *int8 `json:"b"` } }{A: &(struct { A *int8 `json:"a"` B *int8 `json:"b"` }{A: int8ptr(1), B: int8ptr(2)}), B: &(struct { A *int8 `json:"a"` B *int8 `json:"b"` }{A: int8ptr(3), B: int8ptr(4)})}, }, { name: "PtrHeadInt8PtrNilDoubleMultiFieldsNotRoot", expected: `{"A":null,"B":null}`, indentExpected: ` { "A": null, "B": null } `, data: &struct { A *struct { A *int8 `json:"a"` B *int8 `json:"b"` } B *struct { A *int8 `json:"a"` B *int8 `json:"b"` } }{A: nil, B: nil}, }, { name: "PtrHeadInt8PtrNilDoubleMultiFieldsNotRoot", expected: `null`, indentExpected: ` null `, data: (*struct { A *struct { A *int8 `json:"a"` B *int8 `json:"b"` } B *struct { A *int8 `json:"a"` B *int8 `json:"b"` } })(nil), }, { name: "AnonymousHeadInt8", expected: `{"a":1,"b":2}`, indentExpected: ` { "a": 1, "b": 2 } `, data: struct { structInt8 B int8 `json:"b"` }{ structInt8: structInt8{A: 1}, B: 2, }, }, { name: "PtrAnonymousHeadInt8", expected: `{"a":1,"b":2}`, indentExpected: ` { "a": 1, "b": 2 } `, data: struct { *structInt8 B int8 `json:"b"` }{ structInt8: &structInt8{A: 1}, B: 2, }, }, { name: "NilPtrAnonymousHeadInt8", expected: `{"b":2}`, indentExpected: ` { "b": 2 } `, data: struct { *structInt8 B int8 `json:"b"` }{ structInt8: nil, B: 2, }, }, { name: "AnonymousHeadInt8Ptr", expected: `{"a":1,"b":2}`, indentExpected: ` { "a": 1, "b": 2 } `, data: struct { structInt8Ptr B *int8 `json:"b"` }{ structInt8Ptr: structInt8Ptr{A: int8ptr(1)}, B: int8ptr(2), }, }, { name: "AnonymousHeadInt8PtrNil", expected: `{"a":null,"b":2}`, indentExpected: ` { "a": null, "b": 2 } `, data: struct { structInt8Ptr B *int8 `json:"b"` }{ structInt8Ptr: structInt8Ptr{A: nil}, B: int8ptr(2), }, }, { name: "PtrAnonymousHeadInt8Ptr", expected: `{"a":1,"b":2}`, indentExpected: ` { "a": 1, "b": 2 } `, data: struct { *structInt8Ptr B *int8 `json:"b"` }{ structInt8Ptr: &structInt8Ptr{A: int8ptr(1)}, B: int8ptr(2), }, }, { name: "NilPtrAnonymousHeadInt8Ptr", expected: `{"b":2}`, indentExpected: ` { "b": 2 } `, data: struct { *structInt8Ptr B *int8 `json:"b"` }{ structInt8Ptr: nil, B: int8ptr(2), }, }, { name: "AnonymousHeadInt8Only", expected: `{"a":1}`, indentExpected: ` { "a": 1 } `, data: struct { structInt8 }{ structInt8: structInt8{A: 1}, }, }, { name: "PtrAnonymousHeadInt8Only", expected: `{"a":1}`, indentExpected: ` { "a": 1 } `, data: struct { *structInt8 }{ structInt8: &structInt8{A: 1}, }, }, { name: "NilPtrAnonymousHeadInt8Only", expected: `{}`, indentExpected: ` {} `, data: struct { *structInt8 }{ structInt8: nil, }, }, { name: "AnonymousHeadInt8PtrOnly", expected: `{"a":1}`, indentExpected: ` { "a": 1 } `, data: struct { structInt8Ptr }{ structInt8Ptr: structInt8Ptr{A: int8ptr(1)}, }, }, { name: "AnonymousHeadInt8PtrNilOnly", expected: `{"a":null}`, indentExpected: ` { "a": null } `, data: struct { structInt8Ptr }{ structInt8Ptr: structInt8Ptr{A: nil}, }, }, { name: "PtrAnonymousHeadInt8PtrOnly", expected: `{"a":1}`, indentExpected: ` { "a": 1 } `, data: struct { *structInt8Ptr }{ structInt8Ptr: &structInt8Ptr{A: int8ptr(1)}, }, }, { name: "NilPtrAnonymousHeadInt8PtrOnly", expected: `{}`, indentExpected: ` {} `, data: struct { *structInt8Ptr }{ structInt8Ptr: nil, }, }, } for _, test := range tests { for _, indent := range []bool{true, false} { for _, htmlEscape := range []bool{true, false} { var buf bytes.Buffer enc := json.NewEncoder(&buf) enc.SetEscapeHTML(htmlEscape) if indent { enc.SetIndent("", " ") } if err := enc.Encode(test.data); err != nil { t.Fatalf("%s(htmlEscape:%T): %s: %s", test.name, htmlEscape, test.expected, err) } if indent { got := "\n" + buf.String() if got != test.indentExpected { t.Fatalf("%s(htmlEscape:%T): expected %q but got %q", test.name, htmlEscape, test.indentExpected, got) } } else { if strings.TrimRight(buf.String(), "\n") != test.expected { t.Fatalf("%s(htmlEscape:%T): expected %q but got %q", test.name, htmlEscape, test.expected, buf.String()) } } } } } }