commit 95b09beee18335d7548ad4a2949965cc0a9d6112
parent 38c8a23b2f609fe2ee642f28b4e7543090f8c421
Author: francoispqt <francois@parquet.ninja>
Date: Wed, 29 Aug 2018 12:33:26 +0800
add tests for decode null object and null array
Diffstat:
2 files changed, 170 insertions(+), 10 deletions(-)
diff --git a/decode_object.go b/decode_object.go
@@ -369,7 +369,3 @@ func (f DecodeObjectFunc) UnmarshalJSONObject(dec *Decoder, k string) error {
func (f DecodeObjectFunc) NKeys() int {
return 0
}
-
-func makeUnmarshalerJSONObject(v *interface{}) UnmarshalerJSONObject {
- return nil
-}
diff --git a/decode_object_test.go b/decode_object_test.go
@@ -889,33 +889,197 @@ func TestDecodeObjectBasic0Keys(t *testing.T) {
type ObjectNull struct {
SubObject *ObjectNull
+ SubArray *testSliceBools
}
func (o *ObjectNull) UnmarshalJSONObject(dec *Decoder, k string) error {
switch k {
case "subobject":
return dec.ObjectNull(&o.SubObject)
+ case "subarray":
+ return dec.ArrayNull(&o.SubArray)
}
return nil
}
func (o *ObjectNull) NKeys() int {
- return 1
+ return 2
}
-func TestDecodeObjectNullPtr(t *testing.T) {
+func TestDecodeObjectNull(t *testing.T) {
t.Run("sub obj should not be nil", func(t *testing.T) {
var o = &ObjectNull{}
- var err = UnmarshalJSONObject([]byte(`{"subobject": {}}`), o)
+ var err = UnmarshalJSONObject([]byte(`{"subobject": {},"subarray":[true]}`), o)
assert.Nil(t, err)
assert.NotNil(t, o.SubObject)
+ assert.NotNil(t, o.SubArray)
})
- t.Run("sub obj should be nil", func(t *testing.T) {
+ t.Run("sub obj and sub array should be nil", func(t *testing.T) {
var o = &ObjectNull{}
- var err = UnmarshalJSONObject([]byte(`{"subobject": null`), o)
+ var err = UnmarshalJSONObject([]byte(`{"subobject": null,"subarray": null}`), o)
assert.Nil(t, err)
assert.Nil(t, o.SubObject)
+ assert.Nil(t, o.SubArray)
})
+ t.Run(
+ "sub obj should not be be nil",
+ func(t *testing.T) {
+ var o = &ObjectNull{}
+ var err = UnmarshalJSONObject([]byte(`{"subobject":{"subobject":{}}}`), DecodeObjectFunc(func(dec *Decoder, k string) error {
+ return dec.ObjectNull(&o.SubObject)
+ }))
+ assert.Nil(t, err)
+ assert.NotNil(t, o.SubObject)
+ },
+ )
+ t.Run(
+ "sub obj should be nil",
+ func(t *testing.T) {
+ var o = &ObjectNull{}
+ var err = UnmarshalJSONObject([]byte(`{"subobject":null}`), DecodeObjectFunc(func(dec *Decoder, k string) error {
+ return dec.ObjectNull(&o.SubObject)
+ }))
+ assert.Nil(t, err)
+ assert.Nil(t, o.SubObject)
+ },
+ )
+ t.Run(
+ "should return an error as type is not ptr",
+ func(t *testing.T) {
+ var err = UnmarshalJSONObject([]byte(`{"key":{}}`), DecodeObjectFunc(func(dec *Decoder, k string) error {
+ return dec.ObjectNull("")
+ }))
+ assert.NotNil(t, err)
+ assert.Equal(t, ErrUnmarshalPtrExpected, err)
+ },
+ )
+ t.Run(
+ "should return an error as type is not ptr",
+ func(t *testing.T) {
+ var err = UnmarshalJSONObject([]byte(`{"key":[]}`), DecodeObjectFunc(func(dec *Decoder, k string) error {
+ return dec.ArrayNull("")
+ }))
+ assert.NotNil(t, err)
+ assert.Equal(t, ErrUnmarshalPtrExpected, err)
+ },
+ )
+ t.Run(
+ "should return an error as type is not ptr to UnmarshalerJSONObject",
+ func(t *testing.T) {
+ var err = UnmarshalJSONObject([]byte(`{"key":{}}`), DecodeObjectFunc(func(dec *Decoder, k string) error {
+ var strPtr = new(string)
+ return dec.ObjectNull(&strPtr)
+ }))
+ assert.NotNil(t, err)
+ assert.IsType(t, InvalidUnmarshalError(""), err)
+ },
+ )
+ t.Run(
+ "should return an error as type is not ptr to UnmarshalerJSONObject",
+ func(t *testing.T) {
+ var err = UnmarshalJSONObject([]byte(`{"key":[]}`), DecodeObjectFunc(func(dec *Decoder, k string) error {
+ var strPtr = new(string)
+ return dec.ArrayNull(&strPtr)
+ }))
+ assert.NotNil(t, err)
+ assert.IsType(t, InvalidUnmarshalError(""), err)
+ },
+ )
+ t.Run(
+ "should return an error as type is not ptr to UnmarshalerJSONObject",
+ func(t *testing.T) {
+ var err = UnmarshalJSONObject([]byte(`{"key":{}}`), DecodeObjectFunc(func(dec *Decoder, k string) error {
+ var strPtr = new(string)
+ return dec.ArrayNull(&strPtr)
+ }))
+ assert.NotNil(t, err)
+ assert.IsType(t, InvalidUnmarshalError(""), err)
+ },
+ )
+ t.Run(
+ "should return an error as type is not ptr to UnmarshalerJSONObject",
+ func(t *testing.T) {
+ var err = UnmarshalJSONObject([]byte(`{"key":"`), DecodeObjectFunc(func(dec *Decoder, k string) error {
+ var strPtr = new(string)
+ return dec.ArrayNull(&strPtr)
+ }))
+ assert.NotNil(t, err)
+ assert.IsType(t, InvalidJSONError(""), err)
+ },
+ )
+ t.Run(
+ "skip data",
+ func(t *testing.T) {
+ var err = UnmarshalJSONObject([]byte(`{"key": ""}`), DecodeObjectFunc(func(dec *Decoder, k string) error {
+ var strPtr = new(string)
+ return dec.ObjectNull(&strPtr)
+ }))
+ assert.NotNil(t, err)
+ assert.IsType(t, InvalidUnmarshalError(""), err)
+ },
+ )
+ t.Run(
+ "invalid JSON for object",
+ func(t *testing.T) {
+ var o = &ObjectNull{}
+ var err = UnmarshalJSONObject([]byte(`{"subobject":{"subobject":a}`), DecodeObjectFunc(func(dec *Decoder, k string) error {
+ return dec.ObjectNull(&o.SubObject)
+ }))
+ assert.NotNil(t, err)
+ assert.IsType(t, InvalidJSONError(""), err)
+ },
+ )
+ t.Run(
+ "invalid JSON for object",
+ func(t *testing.T) {
+ var o = &testSliceBools{}
+ var err = UnmarshalJSONObject([]byte(`{"subobject":a`), DecodeObjectFunc(func(dec *Decoder, k string) error {
+ return dec.ArrayNull(&o)
+ }))
+ assert.NotNil(t, err)
+ assert.IsType(t, InvalidJSONError(""), err)
+ },
+ )
+ t.Run(
+ "invalid JSON for object",
+ func(t *testing.T) {
+ var err = UnmarshalJSONObject([]byte(`{"key":a`), DecodeObjectFunc(func(dec *Decoder, k string) error {
+ var strPtr = new(string)
+ return dec.ObjectNull(&strPtr)
+ }))
+ assert.NotNil(t, err)
+ assert.IsType(t, InvalidJSONError(""), err)
+ },
+ )
+ t.Run(
+ "invalid JSON for object",
+ func(t *testing.T) {
+ var err = UnmarshalJSONObject([]byte(`{"subobject": {},"}`), DecodeObjectFunc(func(dec *Decoder, k string) error {
+ var o = &ObjectNull{}
+ return dec.ObjectNull(&o)
+ }))
+ assert.NotNil(t, err)
+ assert.IsType(t, InvalidJSONError(""), err)
+ },
+ )
+ t.Run(
+ "invalid JSON for object",
+ func(t *testing.T) {
+ var o = &ObjectNull{}
+ var err = UnmarshalJSONObject([]byte(`{"subobject": a`), o)
+ assert.NotNil(t, err)
+ assert.IsType(t, InvalidJSONError(""), err)
+ },
+ )
+ t.Run(
+ "invalid JSON for object",
+ func(t *testing.T) {
+ var o = &ObjectNull{}
+ var err = UnmarshalJSONObject([]byte(`{"subobject": na`), o)
+ assert.NotNil(t, err)
+ assert.IsType(t, InvalidJSONError(""), err)
+ },
+ )
}
func TestDecodeObjectComplex(t *testing.T) {
@@ -1069,7 +1233,7 @@ func TestDecoderObject(t *testing.T) {
assertResult(t, v, err)
}
-func TestDecodeObjectNull(t *testing.T) {
+func TestDecodeObjectJSONNull(t *testing.T) {
json := []byte(`null`)
v := &TestObj{}
err := Unmarshal(json, v)