test: add tests

test: draft for omitempty on pointer receiver for MarshalJSON

test: move new test onto omitempty marshaling tests
This commit is contained in:
Gabriel Crispino 2023-12-13 22:09:28 -03:00 committed by Gabriel Crispino
parent 92e091e9bb
commit 38a0a5163d
1 changed files with 24 additions and 0 deletions

View File

@ -21,6 +21,18 @@ import (
"github.com/goccy/go-json"
)
type stringAlias string
func (d *stringAlias) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf("\"%s\"", *d)), nil
}
type intAlias int
func (d *intAlias) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf("%d", *d)), nil
}
type recursiveT struct {
A *recursiveT `json:"a,omitempty"`
B *recursiveU `json:"b,omitempty"`
@ -383,6 +395,18 @@ func Test_Marshal(t *testing.T) {
assertErr(t, err)
assertEq(t, "array", `{"b":{"c":1}}`, string(bytes))
})
t.Run("custom type with MarshalJSON implementation with pointer receiver", func(t *testing.T) {
type withOmit struct {
A string `json:"a,omitempty"`
B stringAlias `json:"b,omitempty"`
C intAlias `json:"c,omitempty"`
}
w := withOmit{}
bytes, err := json.Marshal(&w)
assertErr(t, err)
assertEq(t, "custom type with MarshalJSON implementation with pointer receiver", `{}`, string(bytes))
})
})
t.Run("head_omitempty", func(t *testing.T) {
type T struct {