From 269bcfb70a8e7093b11fd763fbf7513c3b278169 Mon Sep 17 00:00:00 2001 From: spectatorMrZ Date: Wed, 2 Aug 2023 05:21:15 +0800 Subject: [PATCH] test: add unmarshal test with CustomFieldUnsetErr --- core/mapping/unmarshaler_test.go | 38 +++++++++++++++++++++++--------- 1 file changed, 28 insertions(+), 10 deletions(-) diff --git a/core/mapping/unmarshaler_test.go b/core/mapping/unmarshaler_test.go index 16d6941875d5..aaf29979a5c7 100644 --- a/core/mapping/unmarshaler_test.go +++ b/core/mapping/unmarshaler_test.go @@ -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) {