go-json/cover_uint_test.go

991 lines
15 KiB
Go

package json_test
import (
"bytes"
"strings"
"testing"
"github.com/goccy/go-json"
)
func TestCoverUint(t *testing.T) {
type structUint struct {
A uint `json:"a"`
}
type structUintPtr struct {
A *uint `json:"a"`
}
tests := []struct {
name string
expected string
indentExpected string
data interface{}
}{
{
name: "HeadUintZero",
expected: `{"a":0}`,
indentExpected: `
{
"a": 0
}
`,
data: struct {
A uint `json:"a"`
}{},
},
{
name: "HeadUint",
expected: `{"a":1}`,
indentExpected: `
{
"a": 1
}
`,
data: struct {
A uint `json:"a"`
}{A: 1},
},
{
name: "HeadUintPtr",
expected: `{"a":1}`,
indentExpected: `
{
"a": 1
}
`,
data: struct {
A *uint `json:"a"`
}{A: uptr(1)},
},
{
name: "HeadUintPtrNil",
expected: `{"a":null}`,
indentExpected: `
{
"a": null
}
`,
data: struct {
A *uint `json:"a"`
}{A: nil},
},
{
name: "PtrHeadUintZero",
expected: `{"a":0}`,
indentExpected: `
{
"a": 0
}
`,
data: &struct {
A uint `json:"a"`
}{},
},
{
name: "PtrHeadUint",
expected: `{"a":1}`,
indentExpected: `
{
"a": 1
}
`,
data: &struct {
A uint `json:"a"`
}{A: 1},
},
{
name: "PtrHeadUintPtr",
expected: `{"a":1}`,
indentExpected: `
{
"a": 1
}
`,
data: &struct {
A *uint `json:"a"`
}{A: uptr(1)},
},
{
name: "PtrHeadUintPtrNil",
expected: `{"a":null}`,
indentExpected: `
{
"a": null
}
`,
data: &struct {
A *uint `json:"a"`
}{A: nil},
},
{
name: "PtrHeadUintNil",
expected: `null`,
indentExpected: `
null
`,
data: (*struct {
A *uint `json:"a"`
})(nil),
},
{
name: "HeadUintZeroMultiFields",
expected: `{"a":0,"b":0}`,
indentExpected: `
{
"a": 0,
"b": 0
}
`,
data: struct {
A uint `json:"a"`
B uint `json:"b"`
}{},
},
{
name: "HeadUintMultiFields",
expected: `{"a":1,"b":2}`,
indentExpected: `
{
"a": 1,
"b": 2
}
`,
data: struct {
A uint `json:"a"`
B uint `json:"b"`
}{A: 1, B: 2},
},
{
name: "HeadUintPtrMultiFields",
expected: `{"a":1,"b":2}`,
indentExpected: `
{
"a": 1,
"b": 2
}
`,
data: struct {
A *uint `json:"a"`
B *uint `json:"b"`
}{A: uptr(1), B: uptr(2)},
},
{
name: "HeadUintPtrNilMultiFields",
expected: `{"a":null,"b":null}`,
indentExpected: `
{
"a": null,
"b": null
}
`,
data: struct {
A *uint `json:"a"`
B *uint `json:"b"`
}{A: nil, B: nil},
},
{
name: "PtrHeadUintZeroMultiFields",
expected: `{"a":0,"b":0}`,
indentExpected: `
{
"a": 0,
"b": 0
}
`,
data: &struct {
A uint `json:"a"`
B uint `json:"b"`
}{},
},
{
name: "PtrHeadUintMultiFields",
expected: `{"a":1,"b":2}`,
indentExpected: `
{
"a": 1,
"b": 2
}
`,
data: &struct {
A uint `json:"a"`
B uint `json:"b"`
}{A: 1, B: 2},
},
{
name: "PtrHeadUintPtrMultiFields",
expected: `{"a":1,"b":2}`,
indentExpected: `
{
"a": 1,
"b": 2
}
`,
data: &struct {
A *uint `json:"a"`
B *uint `json:"b"`
}{A: uptr(1), B: uptr(2)},
},
{
name: "PtrHeadUintPtrNilMultiFields",
expected: `{"a":null,"b":null}`,
indentExpected: `
{
"a": null,
"b": null
}
`,
data: &struct {
A *uint `json:"a"`
B *uint `json:"b"`
}{A: nil, B: nil},
},
{
name: "PtrHeadUintNilMultiFields",
expected: `null`,
indentExpected: `
null
`,
data: (*struct {
A *uint `json:"a"`
B *uint `json:"b"`
})(nil),
},
{
name: "HeadUintZeroNotRoot",
expected: `{"A":{"a":0}}`,
indentExpected: `
{
"A": {
"a": 0
}
}
`,
data: struct {
A struct {
A uint `json:"a"`
}
}{},
},
{
name: "HeadUintNotRoot",
expected: `{"A":{"a":1}}`,
indentExpected: `
{
"A": {
"a": 1
}
}
`,
data: struct {
A struct {
A uint `json:"a"`
}
}{A: struct {
A uint `json:"a"`
}{A: 1}},
},
{
name: "HeadUintPtrNotRoot",
expected: `{"A":{"a":1}}`,
indentExpected: `
{
"A": {
"a": 1
}
}
`,
data: struct {
A struct {
A *uint `json:"a"`
}
}{A: struct {
A *uint `json:"a"`
}{uptr(1)}},
},
{
name: "HeadUintPtrNilNotRoot",
expected: `{"A":{"a":null}}`,
indentExpected: `
{
"A": {
"a": null
}
}
`,
data: struct {
A struct {
A *uint `json:"a"`
}
}{},
},
{
name: "PtrHeadUintZeroNotRoot",
expected: `{"A":{"a":0}}`,
indentExpected: `
{
"A": {
"a": 0
}
}
`,
data: struct {
A *struct {
A uint `json:"a"`
}
}{A: new(struct {
A uint `json:"a"`
})},
},
{
name: "PtrHeadUintNotRoot",
expected: `{"A":{"a":1}}`,
indentExpected: `
{
"A": {
"a": 1
}
}
`,
data: struct {
A *struct {
A uint `json:"a"`
}
}{A: &(struct {
A uint `json:"a"`
}{A: 1})},
},
{
name: "PtrHeadUintPtrNotRoot",
expected: `{"A":{"a":1}}`,
indentExpected: `
{
"A": {
"a": 1
}
}
`,
data: struct {
A *struct {
A *uint `json:"a"`
}
}{A: &(struct {
A *uint `json:"a"`
}{A: uptr(1)})},
},
{
name: "PtrHeadUintPtrNilNotRoot",
expected: `{"A":{"a":null}}`,
indentExpected: `
{
"A": {
"a": null
}
}
`,
data: struct {
A *struct {
A *uint `json:"a"`
}
}{A: &(struct {
A *uint `json:"a"`
}{A: nil})},
},
{
name: "PtrHeadUintNilNotRoot",
expected: `{"A":null}`,
indentExpected: `
{
"A": null
}
`,
data: struct {
A *struct {
A *uint `json:"a"`
}
}{A: nil},
},
{
name: "HeadUintZeroMultiFieldsNotRoot",
expected: `{"A":{"a":0},"B":{"b":0}}`,
indentExpected: `
{
"A": {
"a": 0
},
"B": {
"b": 0
}
}
`,
data: struct {
A struct {
A uint `json:"a"`
}
B struct {
B uint `json:"b"`
}
}{},
},
{
name: "HeadUintMultiFieldsNotRoot",
expected: `{"A":{"a":1},"B":{"b":2}}`,
indentExpected: `
{
"A": {
"a": 1
},
"B": {
"b": 2
}
}
`,
data: struct {
A struct {
A uint `json:"a"`
}
B struct {
B uint `json:"b"`
}
}{A: struct {
A uint `json:"a"`
}{A: 1}, B: struct {
B uint `json:"b"`
}{B: 2}},
},
{
name: "HeadUintPtrMultiFieldsNotRoot",
expected: `{"A":{"a":1},"B":{"b":2}}`,
indentExpected: `
{
"A": {
"a": 1
},
"B": {
"b": 2
}
}
`,
data: struct {
A struct {
A *uint `json:"a"`
}
B struct {
B *uint `json:"b"`
}
}{A: struct {
A *uint `json:"a"`
}{A: uptr(1)}, B: struct {
B *uint `json:"b"`
}{B: uptr(2)}},
},
{
name: "HeadUintPtrNilMultiFieldsNotRoot",
expected: `{"A":{"a":null},"B":{"b":null}}`,
indentExpected: `
{
"A": {
"a": null
},
"B": {
"b": null
}
}
`,
data: struct {
A struct {
A *uint `json:"a"`
}
B struct {
B *uint `json:"b"`
}
}{A: struct {
A *uint `json:"a"`
}{A: nil}, B: struct {
B *uint `json:"b"`
}{B: nil}},
},
{
name: "PtrHeadUintZeroMultiFieldsNotRoot",
expected: `{"A":{"a":0},"B":{"b":0}}`,
indentExpected: `
{
"A": {
"a": 0
},
"B": {
"b": 0
}
}
`,
data: &struct {
A struct {
A uint `json:"a"`
}
B struct {
B uint `json:"b"`
}
}{},
},
{
name: "PtrHeadUintMultiFieldsNotRoot",
expected: `{"A":{"a":1},"B":{"b":2}}`,
indentExpected: `
{
"A": {
"a": 1
},
"B": {
"b": 2
}
}
`,
data: &struct {
A struct {
A uint `json:"a"`
}
B struct {
B uint `json:"b"`
}
}{A: struct {
A uint `json:"a"`
}{A: 1}, B: struct {
B uint `json:"b"`
}{B: 2}},
},
{
name: "PtrHeadUintPtrMultiFieldsNotRoot",
expected: `{"A":{"a":1},"B":{"b":2}}`,
indentExpected: `
{
"A": {
"a": 1
},
"B": {
"b": 2
}
}
`,
data: &struct {
A *struct {
A *uint `json:"a"`
}
B *struct {
B *uint `json:"b"`
}
}{A: &(struct {
A *uint `json:"a"`
}{A: uptr(1)}), B: &(struct {
B *uint `json:"b"`
}{B: uptr(2)})},
},
{
name: "PtrHeadUintPtrNilMultiFieldsNotRoot",
expected: `{"A":null,"B":null}`,
indentExpected: `
{
"A": null,
"B": null
}
`,
data: &struct {
A *struct {
A *uint `json:"a"`
}
B *struct {
B *uint `json:"b"`
}
}{A: nil, B: nil},
},
{
name: "PtrHeadUintNilMultiFieldsNotRoot",
expected: `null`,
indentExpected: `
null
`,
data: (*struct {
A *struct {
A *uint `json:"a"`
}
B *struct {
B *uint `json:"b"`
}
})(nil),
},
{
name: "PtrHeadUintDoubleMultiFieldsNotRoot",
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 uint `json:"a"`
B uint `json:"b"`
}
B *struct {
A uint `json:"a"`
B uint `json:"b"`
}
}{A: &(struct {
A uint `json:"a"`
B uint `json:"b"`
}{A: 1, B: 2}), B: &(struct {
A uint `json:"a"`
B uint `json:"b"`
}{A: 3, B: 4})},
},
{
name: "PtrHeadUintNilDoubleMultiFieldsNotRoot",
expected: `{"A":null,"B":null}`,
indentExpected: `
{
"A": null,
"B": null
}
`,
data: &struct {
A *struct {
A uint `json:"a"`
B uint `json:"b"`
}
B *struct {
A uint `json:"a"`
B uint `json:"b"`
}
}{A: nil, B: nil},
},
{
name: "PtrHeadUintNilDoubleMultiFieldsNotRoot",
expected: `null`,
indentExpected: `
null
`,
data: (*struct {
A *struct {
A uint `json:"a"`
B uint `json:"b"`
}
B *struct {
A uint `json:"a"`
B uint `json:"b"`
}
})(nil),
},
{
name: "PtrHeadUintPtrDoubleMultiFieldsNotRoot",
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 *uint `json:"a"`
B *uint `json:"b"`
}
B *struct {
A *uint `json:"a"`
B *uint `json:"b"`
}
}{A: &(struct {
A *uint `json:"a"`
B *uint `json:"b"`
}{A: uptr(1), B: uptr(2)}), B: &(struct {
A *uint `json:"a"`
B *uint `json:"b"`
}{A: uptr(3), B: uptr(4)})},
},
{
name: "PtrHeadUintPtrNilDoubleMultiFieldsNotRoot",
expected: `{"A":null,"B":null}`,
indentExpected: `
{
"A": null,
"B": null
}
`,
data: &struct {
A *struct {
A *uint `json:"a"`
B *uint `json:"b"`
}
B *struct {
A *uint `json:"a"`
B *uint `json:"b"`
}
}{A: nil, B: nil},
},
{
name: "PtrHeadUintPtrNilDoubleMultiFieldsNotRoot",
expected: `null`,
indentExpected: `
null
`,
data: (*struct {
A *struct {
A *uint `json:"a"`
B *uint `json:"b"`
}
B *struct {
A *uint `json:"a"`
B *uint `json:"b"`
}
})(nil),
},
{
name: "AnonymousHeadUint",
expected: `{"a":1,"b":2}`,
indentExpected: `
{
"a": 1,
"b": 2
}
`,
data: struct {
structUint
B uint `json:"b"`
}{
structUint: structUint{A: 1},
B: 2,
},
},
{
name: "PtrAnonymousHeadUint",
expected: `{"a":1,"b":2}`,
indentExpected: `
{
"a": 1,
"b": 2
}
`,
data: struct {
*structUint
B uint `json:"b"`
}{
structUint: &structUint{A: 1},
B: 2,
},
},
{
name: "NilPtrAnonymousHeadUint",
expected: `{"b":2}`,
indentExpected: `
{
"b": 2
}
`,
data: struct {
*structUint
B uint `json:"b"`
}{
structUint: nil,
B: 2,
},
},
{
name: "AnonymousHeadUintPtr",
expected: `{"a":1,"b":2}`,
indentExpected: `
{
"a": 1,
"b": 2
}
`,
data: struct {
structUintPtr
B *uint `json:"b"`
}{
structUintPtr: structUintPtr{A: uptr(1)},
B: uptr(2),
},
},
{
name: "AnonymousHeadUintPtrNil",
expected: `{"a":null,"b":2}`,
indentExpected: `
{
"a": null,
"b": 2
}
`,
data: struct {
structUintPtr
B *uint `json:"b"`
}{
structUintPtr: structUintPtr{A: nil},
B: uptr(2),
},
},
{
name: "PtrAnonymousHeadUintPtr",
expected: `{"a":1,"b":2}`,
indentExpected: `
{
"a": 1,
"b": 2
}
`,
data: struct {
*structUintPtr
B *uint `json:"b"`
}{
structUintPtr: &structUintPtr{A: uptr(1)},
B: uptr(2),
},
},
{
name: "NilPtrAnonymousHeadUintPtr",
expected: `{"b":2}`,
indentExpected: `
{
"b": 2
}
`,
data: struct {
*structUintPtr
B *uint `json:"b"`
}{
structUintPtr: nil,
B: uptr(2),
},
},
{
name: "AnonymousHeadUintOnly",
expected: `{"a":1}`,
indentExpected: `
{
"a": 1
}
`,
data: struct {
structUint
}{
structUint: structUint{A: 1},
},
},
{
name: "PtrAnonymousHeadUintOnly",
expected: `{"a":1}`,
indentExpected: `
{
"a": 1
}
`,
data: struct {
*structUint
}{
structUint: &structUint{A: 1},
},
},
{
name: "NilPtrAnonymousHeadUintOnly",
expected: `{}`,
indentExpected: `
{}
`,
data: struct {
*structUint
}{
structUint: nil,
},
},
{
name: "AnonymousHeadUintPtrOnly",
expected: `{"a":1}`,
indentExpected: `
{
"a": 1
}
`,
data: struct {
structUintPtr
}{
structUintPtr: structUintPtr{A: uptr(1)},
},
},
{
name: "AnonymousHeadUintPtrNilOnly",
expected: `{"a":null}`,
indentExpected: `
{
"a": null
}
`,
data: struct {
structUintPtr
}{
structUintPtr: structUintPtr{A: nil},
},
},
{
name: "PtrAnonymousHeadUintPtrOnly",
expected: `{"a":1}`,
indentExpected: `
{
"a": 1
}
`,
data: struct {
*structUintPtr
}{
structUintPtr: &structUintPtr{A: uptr(1)},
},
},
{
name: "NilPtrAnonymousHeadUintPtrOnly",
expected: `{}`,
indentExpected: `
{}
`,
data: struct {
*structUintPtr
}{
structUintPtr: 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())
}
}
}
}
}
}