go-json/cover_int64_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 TestCoverInt64(t *testing.T) {
type structInt64 struct {
A int64 `json:"a"`
}
type structInt64Ptr struct {
A *int64 `json:"a"`
}
tests := []struct {
name string
expected string
indentExpected string
data interface{}
}{
{
name: "HeadInt64Zero",
expected: `{"a":0}`,
indentExpected: `
{
"a": 0
}
`,
data: struct {
A int64 `json:"a"`
}{},
},
{
name: "HeadInt64",
expected: `{"a":1}`,
indentExpected: `
{
"a": 1
}
`,
data: struct {
A int64 `json:"a"`
}{A: 1},
},
{
name: "HeadInt64Ptr",
expected: `{"a":1}`,
indentExpected: `
{
"a": 1
}
`,
data: struct {
A *int64 `json:"a"`
}{A: int64ptr(1)},
},
{
name: "HeadInt64PtrNil",
expected: `{"a":null}`,
indentExpected: `
{
"a": null
}
`,
data: struct {
A *int64 `json:"a"`
}{A: nil},
},
{
name: "PtrHeadInt64Zero",
expected: `{"a":0}`,
indentExpected: `
{
"a": 0
}
`,
data: &struct {
A int64 `json:"a"`
}{},
},
{
name: "PtrHeadInt64",
expected: `{"a":1}`,
indentExpected: `
{
"a": 1
}
`,
data: &struct {
A int64 `json:"a"`
}{A: 1},
},
{
name: "PtrHeadInt64Ptr",
expected: `{"a":1}`,
indentExpected: `
{
"a": 1
}
`,
data: &struct {
A *int64 `json:"a"`
}{A: int64ptr(1)},
},
{
name: "PtrHeadInt64PtrNil",
expected: `{"a":null}`,
indentExpected: `
{
"a": null
}
`,
data: &struct {
A *int64 `json:"a"`
}{A: nil},
},
{
name: "PtrHeadInt64Nil",
expected: `null`,
indentExpected: `
null
`,
data: (*struct {
A *int64 `json:"a"`
})(nil),
},
{
name: "HeadInt64ZeroMultiFields",
expected: `{"a":0,"b":0}`,
indentExpected: `
{
"a": 0,
"b": 0
}
`,
data: struct {
A int64 `json:"a"`
B int64 `json:"b"`
}{},
},
{
name: "HeadInt64MultiFields",
expected: `{"a":1,"b":2}`,
indentExpected: `
{
"a": 1,
"b": 2
}
`,
data: struct {
A int64 `json:"a"`
B int64 `json:"b"`
}{A: 1, B: 2},
},
{
name: "HeadInt64PtrMultiFields",
expected: `{"a":1,"b":2}`,
indentExpected: `
{
"a": 1,
"b": 2
}
`,
data: struct {
A *int64 `json:"a"`
B *int64 `json:"b"`
}{A: int64ptr(1), B: int64ptr(2)},
},
{
name: "HeadInt64PtrNilMultiFields",
expected: `{"a":null,"b":null}`,
indentExpected: `
{
"a": null,
"b": null
}
`,
data: struct {
A *int64 `json:"a"`
B *int64 `json:"b"`
}{A: nil, B: nil},
},
{
name: "PtrHeadInt64ZeroMultiFields",
expected: `{"a":0,"b":0}`,
indentExpected: `
{
"a": 0,
"b": 0
}
`,
data: &struct {
A int64 `json:"a"`
B int64 `json:"b"`
}{},
},
{
name: "PtrHeadInt64MultiFields",
expected: `{"a":1,"b":2}`,
indentExpected: `
{
"a": 1,
"b": 2
}
`,
data: &struct {
A int64 `json:"a"`
B int64 `json:"b"`
}{A: 1, B: 2},
},
{
name: "PtrHeadInt64PtrMultiFields",
expected: `{"a":1,"b":2}`,
indentExpected: `
{
"a": 1,
"b": 2
}
`,
data: &struct {
A *int64 `json:"a"`
B *int64 `json:"b"`
}{A: int64ptr(1), B: int64ptr(2)},
},
{
name: "PtrHeadInt64PtrNilMultiFields",
expected: `{"a":null,"b":null}`,
indentExpected: `
{
"a": null,
"b": null
}
`,
data: &struct {
A *int64 `json:"a"`
B *int64 `json:"b"`
}{A: nil, B: nil},
},
{
name: "PtrHeadInt64NilMultiFields",
expected: `null`,
indentExpected: `
null
`,
data: (*struct {
A *int64 `json:"a"`
B *int64 `json:"b"`
})(nil),
},
{
name: "HeadInt64ZeroNotRoot",
expected: `{"A":{"a":0}}`,
indentExpected: `
{
"A": {
"a": 0
}
}
`,
data: struct {
A struct {
A int64 `json:"a"`
}
}{},
},
{
name: "HeadInt64NotRoot",
expected: `{"A":{"a":1}}`,
indentExpected: `
{
"A": {
"a": 1
}
}
`,
data: struct {
A struct {
A int64 `json:"a"`
}
}{A: struct {
A int64 `json:"a"`
}{A: 1}},
},
{
name: "HeadInt64PtrNotRoot",
expected: `{"A":{"a":1}}`,
indentExpected: `
{
"A": {
"a": 1
}
}
`,
data: struct {
A struct {
A *int64 `json:"a"`
}
}{A: struct {
A *int64 `json:"a"`
}{int64ptr(1)}},
},
{
name: "HeadInt64PtrNilNotRoot",
expected: `{"A":{"a":null}}`,
indentExpected: `
{
"A": {
"a": null
}
}
`,
data: struct {
A struct {
A *int64 `json:"a"`
}
}{},
},
{
name: "PtrHeadInt64ZeroNotRoot",
expected: `{"A":{"a":0}}`,
indentExpected: `
{
"A": {
"a": 0
}
}
`,
data: struct {
A *struct {
A int64 `json:"a"`
}
}{A: new(struct {
A int64 `json:"a"`
})},
},
{
name: "PtrHeadInt64NotRoot",
expected: `{"A":{"a":1}}`,
indentExpected: `
{
"A": {
"a": 1
}
}
`,
data: struct {
A *struct {
A int64 `json:"a"`
}
}{A: &(struct {
A int64 `json:"a"`
}{A: 1})},
},
{
name: "PtrHeadInt64PtrNotRoot",
expected: `{"A":{"a":1}}`,
indentExpected: `
{
"A": {
"a": 1
}
}
`,
data: struct {
A *struct {
A *int64 `json:"a"`
}
}{A: &(struct {
A *int64 `json:"a"`
}{A: int64ptr(1)})},
},
{
name: "PtrHeadInt64PtrNilNotRoot",
expected: `{"A":{"a":null}}`,
indentExpected: `
{
"A": {
"a": null
}
}
`,
data: struct {
A *struct {
A *int64 `json:"a"`
}
}{A: &(struct {
A *int64 `json:"a"`
}{A: nil})},
},
{
name: "PtrHeadInt64NilNotRoot",
expected: `{"A":null}`,
indentExpected: `
{
"A": null
}
`,
data: struct {
A *struct {
A *int64 `json:"a"`
}
}{A: nil},
},
{
name: "HeadInt64ZeroMultiFieldsNotRoot",
expected: `{"A":{"a":0},"B":{"b":0}}`,
indentExpected: `
{
"A": {
"a": 0
},
"B": {
"b": 0
}
}
`,
data: struct {
A struct {
A int64 `json:"a"`
}
B struct {
B int64 `json:"b"`
}
}{},
},
{
name: "HeadInt64MultiFieldsNotRoot",
expected: `{"A":{"a":1},"B":{"b":2}}`,
indentExpected: `
{
"A": {
"a": 1
},
"B": {
"b": 2
}
}
`,
data: struct {
A struct {
A int64 `json:"a"`
}
B struct {
B int64 `json:"b"`
}
}{A: struct {
A int64 `json:"a"`
}{A: 1}, B: struct {
B int64 `json:"b"`
}{B: 2}},
},
{
name: "HeadInt64PtrMultiFieldsNotRoot",
expected: `{"A":{"a":1},"B":{"b":2}}`,
indentExpected: `
{
"A": {
"a": 1
},
"B": {
"b": 2
}
}
`,
data: struct {
A struct {
A *int64 `json:"a"`
}
B struct {
B *int64 `json:"b"`
}
}{A: struct {
A *int64 `json:"a"`
}{A: int64ptr(1)}, B: struct {
B *int64 `json:"b"`
}{B: int64ptr(2)}},
},
{
name: "HeadInt64PtrNilMultiFieldsNotRoot",
expected: `{"A":{"a":null},"B":{"b":null}}`,
indentExpected: `
{
"A": {
"a": null
},
"B": {
"b": null
}
}
`,
data: struct {
A struct {
A *int64 `json:"a"`
}
B struct {
B *int64 `json:"b"`
}
}{A: struct {
A *int64 `json:"a"`
}{A: nil}, B: struct {
B *int64 `json:"b"`
}{B: nil}},
},
{
name: "PtrHeadInt64ZeroMultiFieldsNotRoot",
expected: `{"A":{"a":0},"B":{"b":0}}`,
indentExpected: `
{
"A": {
"a": 0
},
"B": {
"b": 0
}
}
`,
data: &struct {
A struct {
A int64 `json:"a"`
}
B struct {
B int64 `json:"b"`
}
}{},
},
{
name: "PtrHeadInt64MultiFieldsNotRoot",
expected: `{"A":{"a":1},"B":{"b":2}}`,
indentExpected: `
{
"A": {
"a": 1
},
"B": {
"b": 2
}
}
`,
data: &struct {
A struct {
A int64 `json:"a"`
}
B struct {
B int64 `json:"b"`
}
}{A: struct {
A int64 `json:"a"`
}{A: 1}, B: struct {
B int64 `json:"b"`
}{B: 2}},
},
{
name: "PtrHeadInt64PtrMultiFieldsNotRoot",
expected: `{"A":{"a":1},"B":{"b":2}}`,
indentExpected: `
{
"A": {
"a": 1
},
"B": {
"b": 2
}
}
`,
data: &struct {
A *struct {
A *int64 `json:"a"`
}
B *struct {
B *int64 `json:"b"`
}
}{A: &(struct {
A *int64 `json:"a"`
}{A: int64ptr(1)}), B: &(struct {
B *int64 `json:"b"`
}{B: int64ptr(2)})},
},
{
name: "PtrHeadInt64PtrNilMultiFieldsNotRoot",
expected: `{"A":null,"B":null}`,
indentExpected: `
{
"A": null,
"B": null
}
`,
data: &struct {
A *struct {
A *int64 `json:"a"`
}
B *struct {
B *int64 `json:"b"`
}
}{A: nil, B: nil},
},
{
name: "PtrHeadInt64NilMultiFieldsNotRoot",
expected: `null`,
indentExpected: `
null
`,
data: (*struct {
A *struct {
A *int64 `json:"a"`
}
B *struct {
B *int64 `json:"b"`
}
})(nil),
},
{
name: "PtrHeadInt64DoubleMultiFieldsNotRoot",
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 int64 `json:"a"`
B int64 `json:"b"`
}
B *struct {
A int64 `json:"a"`
B int64 `json:"b"`
}
}{A: &(struct {
A int64 `json:"a"`
B int64 `json:"b"`
}{A: 1, B: 2}), B: &(struct {
A int64 `json:"a"`
B int64 `json:"b"`
}{A: 3, B: 4})},
},
{
name: "PtrHeadInt64NilDoubleMultiFieldsNotRoot",
expected: `{"A":null,"B":null}`,
indentExpected: `
{
"A": null,
"B": null
}
`,
data: &struct {
A *struct {
A int64 `json:"a"`
B int64 `json:"b"`
}
B *struct {
A int64 `json:"a"`
B int64 `json:"b"`
}
}{A: nil, B: nil},
},
{
name: "PtrHeadInt64NilDoubleMultiFieldsNotRoot",
expected: `null`,
indentExpected: `
null
`,
data: (*struct {
A *struct {
A int64 `json:"a"`
B int64 `json:"b"`
}
B *struct {
A int64 `json:"a"`
B int64 `json:"b"`
}
})(nil),
},
{
name: "PtrHeadInt64PtrDoubleMultiFieldsNotRoot",
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 *int64 `json:"a"`
B *int64 `json:"b"`
}
B *struct {
A *int64 `json:"a"`
B *int64 `json:"b"`
}
}{A: &(struct {
A *int64 `json:"a"`
B *int64 `json:"b"`
}{A: int64ptr(1), B: int64ptr(2)}), B: &(struct {
A *int64 `json:"a"`
B *int64 `json:"b"`
}{A: int64ptr(3), B: int64ptr(4)})},
},
{
name: "PtrHeadInt64PtrNilDoubleMultiFieldsNotRoot",
expected: `{"A":null,"B":null}`,
indentExpected: `
{
"A": null,
"B": null
}
`,
data: &struct {
A *struct {
A *int64 `json:"a"`
B *int64 `json:"b"`
}
B *struct {
A *int64 `json:"a"`
B *int64 `json:"b"`
}
}{A: nil, B: nil},
},
{
name: "PtrHeadInt64PtrNilDoubleMultiFieldsNotRoot",
expected: `null`,
indentExpected: `
null
`,
data: (*struct {
A *struct {
A *int64 `json:"a"`
B *int64 `json:"b"`
}
B *struct {
A *int64 `json:"a"`
B *int64 `json:"b"`
}
})(nil),
},
{
name: "AnonymousHeadInt64",
expected: `{"a":1,"b":2}`,
indentExpected: `
{
"a": 1,
"b": 2
}
`,
data: struct {
structInt64
B int64 `json:"b"`
}{
structInt64: structInt64{A: 1},
B: 2,
},
},
{
name: "PtrAnonymousHeadInt64",
expected: `{"a":1,"b":2}`,
indentExpected: `
{
"a": 1,
"b": 2
}
`,
data: struct {
*structInt64
B int64 `json:"b"`
}{
structInt64: &structInt64{A: 1},
B: 2,
},
},
{
name: "NilPtrAnonymousHeadInt64",
expected: `{"b":2}`,
indentExpected: `
{
"b": 2
}
`,
data: struct {
*structInt64
B int64 `json:"b"`
}{
structInt64: nil,
B: 2,
},
},
{
name: "AnonymousHeadInt64Ptr",
expected: `{"a":1,"b":2}`,
indentExpected: `
{
"a": 1,
"b": 2
}
`,
data: struct {
structInt64Ptr
B *int64 `json:"b"`
}{
structInt64Ptr: structInt64Ptr{A: int64ptr(1)},
B: int64ptr(2),
},
},
{
name: "AnonymousHeadInt64PtrNil",
expected: `{"a":null,"b":2}`,
indentExpected: `
{
"a": null,
"b": 2
}
`,
data: struct {
structInt64Ptr
B *int64 `json:"b"`
}{
structInt64Ptr: structInt64Ptr{A: nil},
B: int64ptr(2),
},
},
{
name: "PtrAnonymousHeadInt64Ptr",
expected: `{"a":1,"b":2}`,
indentExpected: `
{
"a": 1,
"b": 2
}
`,
data: struct {
*structInt64Ptr
B *int64 `json:"b"`
}{
structInt64Ptr: &structInt64Ptr{A: int64ptr(1)},
B: int64ptr(2),
},
},
{
name: "NilPtrAnonymousHeadInt64Ptr",
expected: `{"b":2}`,
indentExpected: `
{
"b": 2
}
`,
data: struct {
*structInt64Ptr
B *int64 `json:"b"`
}{
structInt64Ptr: nil,
B: int64ptr(2),
},
},
{
name: "AnonymousHeadInt64Only",
expected: `{"a":1}`,
indentExpected: `
{
"a": 1
}
`,
data: struct {
structInt64
}{
structInt64: structInt64{A: 1},
},
},
{
name: "PtrAnonymousHeadInt64Only",
expected: `{"a":1}`,
indentExpected: `
{
"a": 1
}
`,
data: struct {
*structInt64
}{
structInt64: &structInt64{A: 1},
},
},
{
name: "NilPtrAnonymousHeadInt64Only",
expected: `{}`,
indentExpected: `
{}
`,
data: struct {
*structInt64
}{
structInt64: nil,
},
},
{
name: "AnonymousHeadInt64PtrOnly",
expected: `{"a":1}`,
indentExpected: `
{
"a": 1
}
`,
data: struct {
structInt64Ptr
}{
structInt64Ptr: structInt64Ptr{A: int64ptr(1)},
},
},
{
name: "AnonymousHeadInt64PtrNilOnly",
expected: `{"a":null}`,
indentExpected: `
{
"a": null
}
`,
data: struct {
structInt64Ptr
}{
structInt64Ptr: structInt64Ptr{A: nil},
},
},
{
name: "PtrAnonymousHeadInt64PtrOnly",
expected: `{"a":1}`,
indentExpected: `
{
"a": 1
}
`,
data: struct {
*structInt64Ptr
}{
structInt64Ptr: &structInt64Ptr{A: int64ptr(1)},
},
},
{
name: "NilPtrAnonymousHeadInt64PtrOnly",
expected: `{}`,
indentExpected: `
{}
`,
data: struct {
*structInt64Ptr
}{
structInt64Ptr: 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())
}
}
}
}
}
}