go-json/coverage_test.go

418 lines
7.4 KiB
Go
Raw Permalink Normal View History

2020-12-31 13:17:37 +03:00
package json
import (
"bytes"
"strings"
"testing"
)
func intptr(v int) *int {
return &v
}
func int8ptr(v int8) *int8 {
return &v
}
2021-01-01 11:20:39 +03:00
func headIntPtrNilNotRoot() interface{} {
v := struct {
A struct {
A *int `json:"a"`
}
}{}
return v
}
func ptrHeadIntNotRoot() interface{} {
v := struct {
A *struct {
A int `json:"a"`
}
}{A: new(struct {
A int `json:"a"`
})}
v.A.A = 1
return v
}
2020-12-31 13:17:37 +03:00
func TestCoverage(t *testing.T) {
tests := []struct {
name string
expected string
data interface{}
}{
{
2021-01-01 11:20:39 +03:00
name: "HeadIntZero",
expected: `{"a":0}`,
data: struct {
A int `json:"a"`
}{},
},
{
name: "HeadInt",
2020-12-31 13:17:37 +03:00
expected: `{"a":1}`,
data: struct {
A int `json:"a"`
}{A: 1},
},
{
2021-01-01 11:20:39 +03:00
name: "HeadIntPtr",
2020-12-31 13:17:37 +03:00
expected: `{"a":1}`,
data: struct {
A *int `json:"a"`
}{A: intptr(1)},
},
{
2021-01-01 11:20:39 +03:00
name: "HeadIntPtrNil",
expected: `{"a":null}`,
data: struct {
A *int `json:"a"`
}{A: nil},
},
{
name: "PtrHeadIntZero",
expected: `{"a":0}`,
data: &struct {
A int `json:"a"`
}{},
},
{
name: "PtrHeadInt",
2020-12-31 13:17:37 +03:00
expected: `{"a":1}`,
data: &struct {
A int `json:"a"`
}{A: 1},
},
{
2021-01-01 11:20:39 +03:00
name: "PtrHeadIntPtr",
2020-12-31 13:17:37 +03:00
expected: `{"a":1}`,
data: &struct {
A *int `json:"a"`
}{A: intptr(1)},
},
{
2021-01-01 11:20:39 +03:00
name: "PtrHeadIntPtrNil",
expected: `{"a":null}`,
data: &struct {
A *int `json:"a"`
}{A: nil},
},
{
name: "PtrHeadIntNil",
expected: `null`,
data: (*struct {
A *int `json:"a"`
})(nil),
},
{
name: "HeadIntZeroMultiFields",
expected: `{"a":0,"b":0}`,
data: struct {
A int `json:"a"`
B int `json:"b"`
}{},
},
{
name: "HeadIntMultiFields",
2020-12-31 13:17:37 +03:00
expected: `{"a":1,"b":2}`,
data: struct {
A int `json:"a"`
B int `json:"b"`
}{A: 1, B: 2},
},
{
2021-01-01 11:20:39 +03:00
name: "HeadIntPtrMultiFields",
2020-12-31 13:17:37 +03:00
expected: `{"a":1,"b":2}`,
data: struct {
A *int `json:"a"`
B *int `json:"b"`
}{A: intptr(1), B: intptr(2)},
},
2021-01-01 11:20:39 +03:00
{
name: "HeadIntPtrNilMultiFieldsd",
expected: `{"a":null,"b":null}`,
data: struct {
A *int `json:"a"`
B *int `json:"b"`
}{A: nil, B: nil},
},
{
name: "PtrHeadIntZeroMultiFields",
expected: `{"a":0,"b":0}`,
data: &struct {
A int `json:"a"`
B int `json:"b"`
}{},
},
{
name: "PtrHeadIntMultiFields",
expected: `{"a":1,"b":2}`,
data: &struct {
A int `json:"a"`
B int `json:"b"`
}{A: 1, B: 2},
},
{
name: "PtrHeadIntPtrMultiFields",
expected: `{"a":1,"b":2}`,
data: &struct {
A *int `json:"a"`
B *int `json:"b"`
}{A: intptr(1), B: intptr(2)},
},
{
name: "PtrHeadIntPtrNilMultiFields",
expected: `{"a":null,"b":null}`,
data: &struct {
A *int `json:"a"`
B *int `json:"b"`
}{A: nil, B: nil},
},
{
name: "PtrHeadIntNilMultiFields",
expected: `null`,
data: (*struct {
A *int `json:"a"`
B *int `json:"b"`
})(nil),
},
{
name: "HeadIntZeroNotRoot",
expected: `{"A":{"a":0}}`,
data: struct {
A struct {
A int `json:"a"`
}
}{},
},
{
name: "HeadIntNotRoot",
expected: `{"A":{"a":1}}`,
data: struct {
A struct {
A int `json:"a"`
}
}{A: struct {
A int `json:"a"`
}{A: 1}},
},
{
name: "HeadIntPtrNotRoot",
expected: `{"A":{"a":1}}`,
data: struct {
A struct {
A *int `json:"a"`
}
}{A: struct {
A *int `json:"a"`
}{intptr(1)}},
},
/*
{
name: "HeadIntPtrNilNotRoot",
expected: `{"A":{"a":null}}`,
data: struct {
A struct {
A *int `json:"a"`
}
}{},
},
{
name: "PtrHeadIntZeroNotRoot",
expected: `{"A":{"a":0}}`,
data: struct {
A *struct {
A int `json:"a"`
}
}{A: new(struct {
A int `json:"a"`
})},
},
{
name: "PtrHeadIntNotRoot",
expected: `{"A":{"a":1}}`,
data: struct {
A *struct {
A int `json:"a"`
}
}{A: &(struct {
A int `json:"a"`
}{A: 1})},
},
{
name: "PtrHeadIntPtrNotRoot",
expected: `{"A":{"a":1}}`,
data: struct {
A *struct {
A *int `json:"a"`
}
}{A: &(struct {
A *int `json:"a"`
}{A: intptr(1)})},
},
{
name: "PtrHeadIntPtrNilNotRoot",
expected: `{"A":{"a":null}}`,
data: struct {
A *struct {
A *int `json:"a"`
}
}{A: &(struct {
A *int `json:"a"`
}{A: nil})},
},
*/
/*
{
name: "PtrHeadIntNilNotRoot",
expected: `{"A":null}`,
data: struct {
A *struct {
A *int `json:"a"`
}
}{A: nil},
},
*/
{
name: "HeadIntZeroMultiFields",
expected: `{"a":0,"b":0}`,
data: struct {
A int `json:"a"`
B int `json:"b"`
}{},
},
{
name: "HeadIntMultiFields",
expected: `{"a":1,"b":2}`,
data: struct {
A int `json:"a"`
B int `json:"b"`
}{A: 1, B: 2},
},
{
name: "HeadIntPtrMultiFields",
expected: `{"a":1,"b":2}`,
data: struct {
A *int `json:"a"`
B *int `json:"b"`
}{A: intptr(1), B: intptr(2)},
},
{
name: "HeadIntPtrNilMultiFieldsd",
expected: `{"a":null,"b":null}`,
data: struct {
A *int `json:"a"`
B *int `json:"b"`
}{A: nil, B: nil},
},
{
name: "PtrHeadIntZeroMultiFields",
expected: `{"a":0,"b":0}`,
data: &struct {
A int `json:"a"`
B int `json:"b"`
}{},
},
{
name: "PtrHeadIntMultiFields",
expected: `{"a":1,"b":2}`,
data: &struct {
A int `json:"a"`
B int `json:"b"`
}{A: 1, B: 2},
},
{
name: "PtrHeadIntPtrMultiFields",
expected: `{"a":1,"b":2}`,
data: &struct {
A *int `json:"a"`
B *int `json:"b"`
}{A: intptr(1), B: intptr(2)},
},
{
name: "PtrHeadIntPtrNilMultiFields",
expected: `{"a":null,"b":null}`,
data: &struct {
A *int `json:"a"`
B *int `json:"b"`
}{A: nil, B: nil},
},
{
name: "PtrHeadIntNilMultiFields",
expected: `null`,
data: (*struct {
A *int `json:"a"`
B *int `json:"b"`
})(nil),
},
2020-12-31 13:17:37 +03:00
{
name: "Int8Head",
expected: `{"a":1}`,
data: struct {
A int8 `json:"a"`
}{A: 1},
},
{
name: "Int8PtrHead",
expected: `{"a":1}`,
data: struct {
A *int8 `json:"a"`
}{A: int8ptr(1)},
},
/*
{
name: "Int8PtrNilHead",
expected: `{"a":null}`,
data: struct {
A *int8 `json:"a"`
}{A: nil},
},
*/
{
name: "PtrInt8Head",
expected: `{"a":1}`,
data: &struct {
A int8 `json:"a"`
}{A: 1},
},
{
name: "PtrInt8PtrHead",
expected: `{"a":1}`,
data: &struct {
A *int8 `json:"a"`
}{A: int8ptr(1)},
},
{
name: "Int8Field",
expected: `{"a":1,"b":2}`,
data: struct {
A int8 `json:"a"`
B int8 `json:"b"`
}{A: 1, B: 2},
},
{
name: "Int8PtrField",
expected: `{"a":1,"b":2}`,
data: struct {
A *int8 `json:"a"`
B *int8 `json:"b"`
}{A: int8ptr(1), B: int8ptr(2)},
},
}
for _, test := range tests {
for _, htmlEscape := range []bool{true, false} {
var buf bytes.Buffer
enc := NewEncoder(&buf)
enc.SetEscapeHTML(htmlEscape)
if err := enc.Encode(test.data); err != nil {
2021-01-01 11:20:39 +03:00
t.Errorf("%s(htmlEscape:%T): %s: %s", test.name, htmlEscape, test.expected, err)
2020-12-31 13:17:37 +03:00
}
if strings.TrimRight(buf.String(), "\n") != test.expected {
2021-01-01 11:20:39 +03:00
t.Errorf("%s(htmlEscape:%T): expected %q but got %q", test.name, htmlEscape, test.expected, buf.String())
2020-12-31 13:17:37 +03:00
}
}
}
}