Skip to content

Commit

Permalink
test: add unmarshal test with CustomFieldUnsetErr
Browse files Browse the repository at this point in the history
  • Loading branch information
SpectatorNan authored and kevwan committed Aug 5, 2023
1 parent 487de97 commit 70d652a
Showing 1 changed file with 28 additions and 10 deletions.
38 changes: 28 additions & 10 deletions core/mapping/unmarshaler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4791,21 +4791,39 @@ func TestUnmarshalJsonBytesWithAnonymousFieldNotInOptions(t *testing.T) {

func TestUnmarshalJsonBytesWithCustomFieldUnsetErr(t *testing.T) {
type (
Conf struct {
InnerConf struct {
Name string `json:"name"`
}
Conf struct {
Name string `json:"name"`
Inner InnerConf `json:"inner"`
}
)

var (
input = []byte(`{}`)
c Conf
)
t.Run("inner name unset", func(t *testing.T) {
var (
input = []byte(`{"inner": {}, "name": "world"}`)
c Conf
)

e := UnmarshalJsonBytes(input, &c, WithCustomFieldUnsetErr(func(field string) error {
// Here you can customize exceptions and internationalization processing
return fmt.Errorf("the key %s is unset", field)
}))
assert.Error(t, e)
e := UnmarshalJsonBytes(input, &c, WithCustomFieldUnsetErr(func(field string) error {
// Here you can customize exceptions and internationalization processing
return fmt.Errorf("the key %s is unset", field)
}))
assert.Error(t, e)
})

t.Run("name unset", func(t *testing.T) {
var (
input = []byte(`{"inner": {"name":"hello"}`)
c Conf
)
e := UnmarshalJsonBytes(input, &c, WithCustomFieldUnsetErr(func(field string) error {
// Here you can customize exceptions and internationalization processing
return fmt.Errorf("the key %s is unset", field)
}))
assert.Error(t, e)
})
}

func TestUnmarshalNestedPtr(t *testing.T) {
Expand Down

0 comments on commit 70d652a

Please sign in to comment.