go-json/cover_int32_test.go

991 lines
15 KiB
Go
Raw Normal View History

2021-01-16 16:16:26 +03:00
package json_test
import (
"bytes"
"strings"
"testing"
"github.com/goccy/go-json"
)
func TestCoverInt32(t *testing.T) {
type structInt32 struct {
A int32 `json:"a"`
}
type structInt32Ptr struct {
A *int32 `json:"a"`
}
tests := []struct {
name string
expected string
indentExpected string
data interface{}
}{
{
name: "HeadInt32Zero",
expected: `{"a":0}`,
indentExpected: `
{
"a": 0
}
`,
data: struct {
A int32 `json:"a"`
}{},
},
{
name: "HeadInt32",
expected: `{"a":1}`,
indentExpected: `
{
"a": 1
}
`,
data: struct {
A int32 `json:"a"`
}{A: 1},
},
{
name: "HeadInt32Ptr",
expected: `{"a":1}`,
indentExpected: `
{
"a": 1
}
`,
data: struct {
A *int32 `json:"a"`
}{A: int32ptr(1)},
},
{
name: "HeadInt32PtrNil",
expected: `{"a":null}`,
indentExpected: `
{
"a": null
}
`,
data: struct {
A *int32 `json:"a"`
}{A: nil},
},
{
name: "PtrHeadInt32Zero",
expected: `{"a":0}`,
indentExpected: `
{
"a": 0
}
`,
data: &struct {
A int32 `json:"a"`
}{},
},
{
name: "PtrHeadInt32",
expected: `{"a":1}`,
indentExpected: `
{
"a": 1
}
`,
data: &struct {
A int32 `json:"a"`
}{A: 1},
},
{
name: "PtrHeadInt32Ptr",
expected: `{"a":1}`,
indentExpected: `
{
"a": 1
}
`,
data: &struct {
A *int32 `json:"a"`
}{A: int32ptr(1)},
},
{
name: "PtrHeadInt32PtrNil",
expected: `{"a":null}`,
indentExpected: `
{
"a": null
}
`,
data: &struct {
A *int32 `json:"a"`
}{A: nil},
},
{
name: "PtrHeadInt32Nil",
expected: `null`,
indentExpected: `
null
`,
data: (*struct {
A *int32 `json:"a"`
})(nil),
},
{
name: "HeadInt32ZeroMultiFields",
expected: `{"a":0,"b":0}`,
indentExpected: `
{
"a": 0,
"b": 0
}
`,
data: struct {
A int32 `json:"a"`
B int32 `json:"b"`
}{},
},
{
name: "HeadInt32MultiFields",
expected: `{"a":1,"b":2}`,
indentExpected: `
{
"a": 1,
"b": 2
}
`,
data: struct {
A int32 `json:"a"`
B int32 `json:"b"`
}{A: 1, B: 2},
},
{
name: "HeadInt32PtrMultiFields",
expected: `{"a":1,"b":2}`,
indentExpected: `
{
"a": 1,
"b": 2
}
`,
data: struct {
A *int32 `json:"a"`
B *int32 `json:"b"`
}{A: int32ptr(1), B: int32ptr(2)},
},
{
name: "HeadInt32PtrNilMultiFields",
expected: `{"a":null,"b":null}`,
indentExpected: `
{
"a": null,
"b": null
}
`,
data: struct {
A *int32 `json:"a"`
B *int32 `json:"b"`
}{A: nil, B: nil},
},
{
name: "PtrHeadInt32ZeroMultiFields",
expected: `{"a":0,"b":0}`,
indentExpected: `
{
"a": 0,
"b": 0
}
`,
data: &struct {
A int32 `json:"a"`
B int32 `json:"b"`
}{},
},
{
name: "PtrHeadInt32MultiFields",
expected: `{"a":1,"b":2}`,
indentExpected: `
{
"a": 1,
"b": 2
}
`,
data: &struct {
A int32 `json:"a"`
B int32 `json:"b"`
}{A: 1, B: 2},
},
{
name: "PtrHeadInt32PtrMultiFields",
expected: `{"a":1,"b":2}`,
indentExpected: `
{
"a": 1,
"b": 2
}
`,
data: &struct {
A *int32 `json:"a"`
B *int32 `json:"b"`
}{A: int32ptr(1), B: int32ptr(2)},
},
{
name: "PtrHeadInt32PtrNilMultiFields",
expected: `{"a":null,"b":null}`,
indentExpected: `
{
"a": null,
"b": null
}
`,
data: &struct {
A *int32 `json:"a"`
B *int32 `json:"b"`
}{A: nil, B: nil},
},
{
name: "PtrHeadInt32NilMultiFields",
expected: `null`,
indentExpected: `
null
`,
data: (*struct {
A *int32 `json:"a"`
B *int32 `json:"b"`
})(nil),
},
{
name: "HeadInt32ZeroNotRoot",
expected: `{"A":{"a":0}}`,
indentExpected: `
{
"A": {
"a": 0
}
}
`,
data: struct {
A struct {
A int32 `json:"a"`
}
}{},
},
{
name: "HeadInt32NotRoot",
expected: `{"A":{"a":1}}`,
indentExpected: `
{
"A": {
"a": 1
}
}
`,
data: struct {
A struct {
A int32 `json:"a"`
}
}{A: struct {
A int32 `json:"a"`
}{A: 1}},
},
{
name: "HeadInt32PtrNotRoot",
expected: `{"A":{"a":1}}`,
indentExpected: `
{
"A": {
"a": 1
}
}
`,
data: struct {
A struct {
A *int32 `json:"a"`
}
}{A: struct {
A *int32 `json:"a"`
}{int32ptr(1)}},
},
{
name: "HeadInt32PtrNilNotRoot",
expected: `{"A":{"a":null}}`,
indentExpected: `
{
"A": {
"a": null
}
}
`,
data: struct {
A struct {
A *int32 `json:"a"`
}
}{},
},
{
name: "PtrHeadInt32ZeroNotRoot",
expected: `{"A":{"a":0}}`,
indentExpected: `
{
"A": {
"a": 0
}
}
`,
data: struct {
A *struct {
A int32 `json:"a"`
}
}{A: new(struct {
A int32 `json:"a"`
})},
},
{
name: "PtrHeadInt32NotRoot",
expected: `{"A":{"a":1}}`,
indentExpected: `
{
"A": {
"a": 1
}
}
`,
data: struct {
A *struct {
A int32 `json:"a"`
}
}{A: &(struct {
A int32 `json:"a"`
}{A: 1})},
},
{
name: "PtrHeadInt32PtrNotRoot",
expected: `{"A":{"a":1}}`,
indentExpected: `
{
"A": {
"a": 1
}
}
`,
data: struct {
A *struct {
A *int32 `json:"a"`
}
}{A: &(struct {
A *int32 `json:"a"`
}{A: int32ptr(1)})},
},
{
name: "PtrHeadInt32PtrNilNotRoot",
expected: `{"A":{"a":null}}`,
indentExpected: `
{
"A": {
"a": null
}
}
`,
data: struct {
A *struct {
A *int32 `json:"a"`
}
}{A: &(struct {
A *int32 `json:"a"`
}{A: nil})},
},
{
name: "PtrHeadInt32NilNotRoot",
expected: `{"A":null}`,
indentExpected: `
{
"A": null
}
`,
data: struct {
A *struct {
A *int32 `json:"a"`
}
}{A: nil},
},
{
name: "HeadInt32ZeroMultiFieldsNotRoot",
expected: `{"A":{"a":0},"B":{"b":0}}`,
indentExpected: `
{
"A": {
"a": 0
},
"B": {
"b": 0
}
}
`,
data: struct {
A struct {
A int32 `json:"a"`
}
B struct {
B int32 `json:"b"`
}
}{},
},
{
name: "HeadInt32MultiFieldsNotRoot",
expected: `{"A":{"a":1},"B":{"b":2}}`,
indentExpected: `
{
"A": {
"a": 1
},
"B": {
"b": 2
}
}
`,
data: struct {
A struct {
A int32 `json:"a"`
}
B struct {
B int32 `json:"b"`
}
}{A: struct {
A int32 `json:"a"`
}{A: 1}, B: struct {
B int32 `json:"b"`
}{B: 2}},
},
{
name: "HeadInt32PtrMultiFieldsNotRoot",
expected: `{"A":{"a":1},"B":{"b":2}}`,
indentExpected: `
{
"A": {
"a": 1
},
"B": {
"b": 2
}
}
`,
data: struct {
A struct {
A *int32 `json:"a"`
}
B struct {
B *int32 `json:"b"`
}
}{A: struct {
A *int32 `json:"a"`
}{A: int32ptr(1)}, B: struct {
B *int32 `json:"b"`
}{B: int32ptr(2)}},
},
{
name: "HeadInt32PtrNilMultiFieldsNotRoot",
expected: `{"A":{"a":null},"B":{"b":null}}`,
indentExpected: `
{
"A": {
"a": null
},
"B": {
"b": null
}
}
`,
data: struct {
A struct {
A *int32 `json:"a"`
}
B struct {
B *int32 `json:"b"`
}
}{A: struct {
A *int32 `json:"a"`
}{A: nil}, B: struct {
B *int32 `json:"b"`
}{B: nil}},
},
{
name: "PtrHeadInt32ZeroMultiFieldsNotRoot",
expected: `{"A":{"a":0},"B":{"b":0}}`,
indentExpected: `
{
"A": {
"a": 0
},
"B": {
"b": 0
}
}
`,
data: &struct {
A struct {
A int32 `json:"a"`
}
B struct {
B int32 `json:"b"`
}
}{},
},
{
name: "PtrHeadInt32MultiFieldsNotRoot",
expected: `{"A":{"a":1},"B":{"b":2}}`,
indentExpected: `
{
"A": {
"a": 1
},
"B": {
"b": 2
}
}
`,
data: &struct {
A struct {
A int32 `json:"a"`
}
B struct {
B int32 `json:"b"`
}
}{A: struct {
A int32 `json:"a"`
}{A: 1}, B: struct {
B int32 `json:"b"`
}{B: 2}},
},
{
name: "PtrHeadInt32PtrMultiFieldsNotRoot",
expected: `{"A":{"a":1},"B":{"b":2}}`,
indentExpected: `
{
"A": {
"a": 1
},
"B": {
"b": 2
}
}
`,
data: &struct {
A *struct {
A *int32 `json:"a"`
}
B *struct {
B *int32 `json:"b"`
}
}{A: &(struct {
A *int32 `json:"a"`
}{A: int32ptr(1)}), B: &(struct {
B *int32 `json:"b"`
}{B: int32ptr(2)})},
},
{
name: "PtrHeadInt32PtrNilMultiFieldsNotRoot",
expected: `{"A":null,"B":null}`,
indentExpected: `
{
"A": null,
"B": null
}
`,
data: &struct {
A *struct {
A *int32 `json:"a"`
}
B *struct {
B *int32 `json:"b"`
}
}{A: nil, B: nil},
},
{
name: "PtrHeadInt32NilMultiFieldsNotRoot",
expected: `null`,
indentExpected: `
null
`,
data: (*struct {
A *struct {
A *int32 `json:"a"`
}
B *struct {
B *int32 `json:"b"`
}
})(nil),
},
{
name: "PtrHeadInt32DoubleMultiFieldsNotRoot",
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 int32 `json:"a"`
B int32 `json:"b"`
}
B *struct {
A int32 `json:"a"`
B int32 `json:"b"`
}
}{A: &(struct {
A int32 `json:"a"`
B int32 `json:"b"`
}{A: 1, B: 2}), B: &(struct {
A int32 `json:"a"`
B int32 `json:"b"`
}{A: 3, B: 4})},
},
{
name: "PtrHeadInt32NilDoubleMultiFieldsNotRoot",
expected: `{"A":null,"B":null}`,
indentExpected: `
{
"A": null,
"B": null
}
`,
data: &struct {
A *struct {
A int32 `json:"a"`
B int32 `json:"b"`
}
B *struct {
A int32 `json:"a"`
B int32 `json:"b"`
}
}{A: nil, B: nil},
},
{
name: "PtrHeadInt32NilDoubleMultiFieldsNotRoot",
expected: `null`,
indentExpected: `
null
`,
data: (*struct {
A *struct {
A int32 `json:"a"`
B int32 `json:"b"`
}
B *struct {
A int32 `json:"a"`
B int32 `json:"b"`
}
})(nil),
},
{
name: "PtrHeadInt32PtrDoubleMultiFieldsNotRoot",
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 *int32 `json:"a"`
B *int32 `json:"b"`
}
B *struct {
A *int32 `json:"a"`
B *int32 `json:"b"`
}
}{A: &(struct {
A *int32 `json:"a"`
B *int32 `json:"b"`
}{A: int32ptr(1), B: int32ptr(2)}), B: &(struct {
A *int32 `json:"a"`
B *int32 `json:"b"`
}{A: int32ptr(3), B: int32ptr(4)})},
},
{
name: "PtrHeadInt32PtrNilDoubleMultiFieldsNotRoot",
expected: `{"A":null,"B":null}`,
indentExpected: `
{
"A": null,
"B": null
}
`,
data: &struct {
A *struct {
A *int32 `json:"a"`
B *int32 `json:"b"`
}
B *struct {
A *int32 `json:"a"`
B *int32 `json:"b"`
}
}{A: nil, B: nil},
},
{
name: "PtrHeadInt32PtrNilDoubleMultiFieldsNotRoot",
expected: `null`,
indentExpected: `
null
`,
data: (*struct {
A *struct {
A *int32 `json:"a"`
B *int32 `json:"b"`
}
B *struct {
A *int32 `json:"a"`
B *int32 `json:"b"`
}
})(nil),
},
{
name: "AnonymousHeadInt32",
expected: `{"a":1,"b":2}`,
indentExpected: `
{
"a": 1,
"b": 2
}
`,
data: struct {
structInt32
B int32 `json:"b"`
}{
structInt32: structInt32{A: 1},
B: 2,
},
},
{
name: "PtrAnonymousHeadInt32",
expected: `{"a":1,"b":2}`,
indentExpected: `
{
"a": 1,
"b": 2
}
`,
data: struct {
*structInt32
B int32 `json:"b"`
}{
structInt32: &structInt32{A: 1},
B: 2,
},
},
{
name: "NilPtrAnonymousHeadInt32",
expected: `{"b":2}`,
indentExpected: `
{
"b": 2
}
`,
data: struct {
*structInt32
B int32 `json:"b"`
}{
structInt32: nil,
B: 2,
},
},
{
name: "AnonymousHeadInt32Ptr",
expected: `{"a":1,"b":2}`,
indentExpected: `
{
"a": 1,
"b": 2
}
`,
data: struct {
structInt32Ptr
B *int32 `json:"b"`
}{
structInt32Ptr: structInt32Ptr{A: int32ptr(1)},
B: int32ptr(2),
},
},
{
name: "AnonymousHeadInt32PtrNil",
expected: `{"a":null,"b":2}`,
indentExpected: `
{
"a": null,
"b": 2
}
`,
data: struct {
structInt32Ptr
B *int32 `json:"b"`
}{
structInt32Ptr: structInt32Ptr{A: nil},
B: int32ptr(2),
},
},
{
name: "PtrAnonymousHeadInt32Ptr",
expected: `{"a":1,"b":2}`,
indentExpected: `
{
"a": 1,
"b": 2
}
`,
data: struct {
*structInt32Ptr
B *int32 `json:"b"`
}{
structInt32Ptr: &structInt32Ptr{A: int32ptr(1)},
B: int32ptr(2),
},
},
{
name: "NilPtrAnonymousHeadInt32Ptr",
expected: `{"b":2}`,
indentExpected: `
{
"b": 2
}
`,
data: struct {
*structInt32Ptr
B *int32 `json:"b"`
}{
structInt32Ptr: nil,
B: int32ptr(2),
},
},
{
name: "AnonymousHeadInt32Only",
expected: `{"a":1}`,
indentExpected: `
{
"a": 1
}
`,
data: struct {
structInt32
}{
structInt32: structInt32{A: 1},
},
},
{
name: "PtrAnonymousHeadInt32Only",
expected: `{"a":1}`,
indentExpected: `
{
"a": 1
}
`,
data: struct {
*structInt32
}{
structInt32: &structInt32{A: 1},
},
},
{
name: "NilPtrAnonymousHeadInt32Only",
expected: `{}`,
indentExpected: `
{}
`,
data: struct {
*structInt32
}{
structInt32: nil,
},
},
{
name: "AnonymousHeadInt32PtrOnly",
expected: `{"a":1}`,
indentExpected: `
{
"a": 1
}
`,
data: struct {
structInt32Ptr
}{
structInt32Ptr: structInt32Ptr{A: int32ptr(1)},
},
},
{
name: "AnonymousHeadInt32PtrNilOnly",
expected: `{"a":null}`,
indentExpected: `
{
"a": null
}
`,
data: struct {
structInt32Ptr
}{
structInt32Ptr: structInt32Ptr{A: nil},
},
},
{
name: "PtrAnonymousHeadInt32PtrOnly",
expected: `{"a":1}`,
indentExpected: `
{
"a": 1
}
`,
data: struct {
*structInt32Ptr
}{
structInt32Ptr: &structInt32Ptr{A: int32ptr(1)},
},
},
{
name: "NilPtrAnonymousHeadInt32PtrOnly",
expected: `{}`,
indentExpected: `
{}
`,
data: struct {
*structInt32Ptr
}{
structInt32Ptr: 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())
}
}
}
}
}
}