commit 4bc096c9a008235e9308f69fdd1a4cffdadbe824
parent 299e79b1f53ac49117aab7bc7790134d7e53a6e0
Author: francoispqt <francois@parquet.ninja>
Date: Sun, 26 Aug 2018 22:07:38 +0800
add tests for decode null methods
Diffstat:
9 files changed, 203 insertions(+), 44 deletions(-)
diff --git a/decode_bool.go b/decode_bool.go
@@ -9,15 +9,6 @@ func (dec *Decoder) DecodeBool(v *bool) error {
}
return dec.decodeBool(v)
}
-
-// DecodeBoolNull is like DecodeBool but it takes a **bool
-// if the JSON is `true` or `false` and the pointer is nil, gojay assigns a new pointer to a bool
-func (dec *Decoder) DecodeBoolNull(v **bool) error {
- if dec.isPooled == 1 {
- panic(InvalidUsagePooledDecoderError("Invalid usage of pooled decoder"))
- }
- return dec.decodeBoolNull(v)
-}
func (dec *Decoder) decodeBool(v *bool) error {
for ; dec.cursor < dec.length || dec.read(); dec.cursor++ {
switch dec.data[dec.cursor] {
diff --git a/decode_bool_test.go b/decode_bool_test.go
@@ -468,6 +468,13 @@ func TestDecoderBoolNull(t *testing.T) {
testCase.expectations(t, v.b, err)
})
}
+ t.Run("decoder-api-invalid-json2", func(t *testing.T) {
+ var v = new(bool)
+ var dec = NewDecoder(strings.NewReader(`folse`))
+ err := dec.BoolNull(&v)
+ assert.NotNil(t, err, "Err must not be nil")
+ assert.IsType(t, InvalidJSONError(""), err, "err should be of type InvalidJSONError")
+ })
}
func TestDecoderBoolDecoderAPI(t *testing.T) {
diff --git a/decode_number_float_test.go b/decode_number_float_test.go
@@ -622,6 +622,26 @@ func TestDecoderFloat64Null(t *testing.T) {
}
})
}
+ t.Run("decoder-api-invalid-json", func(t *testing.T) {
+ var v = new(float64)
+ err := Unmarshal([]byte(``), &v)
+ assert.NotNil(t, err, "Err must not be nil")
+ assert.IsType(t, InvalidJSONError(""), err, "err should be of type InvalidJSONError")
+ })
+ t.Run("decoder-api-invalid-json2", func(t *testing.T) {
+ var v = new(float64)
+ var dec = NewDecoder(strings.NewReader(``))
+ err := dec.FloatNull(&v)
+ assert.NotNil(t, err, "Err must not be nil")
+ assert.IsType(t, InvalidJSONError(""), err, "err should be of type InvalidJSONError")
+ })
+ t.Run("decoder-api-invalid-json2", func(t *testing.T) {
+ var v = new(float64)
+ var dec = NewDecoder(strings.NewReader(``))
+ err := dec.AddFloat64Null(&v)
+ assert.NotNil(t, err, "Err must not be nil")
+ assert.IsType(t, InvalidJSONError(""), err, "err should be of type InvalidJSONError")
+ })
}
func TestDecoderFloat32(t *testing.T) {
@@ -1240,39 +1260,16 @@ func TestDecoderFloat32Null(t *testing.T) {
}
})
}
- t.Run("pool-error", func(t *testing.T) {
- result := float32(1)
- dec := NewDecoder(nil)
- dec.Release()
- defer func() {
- err := recover()
- assert.NotNil(t, err, "err shouldnt be nil")
- assert.IsType(t, InvalidUsagePooledDecoderError(""), err, "err should be of type InvalidUsagePooledDecoderError")
- }()
- _ = dec.DecodeFloat32(&result)
- assert.True(t, false, "should not be called as decoder should have panicked")
- })
- t.Run("decoder-api", func(t *testing.T) {
- var v float32
- dec := NewDecoder(strings.NewReader(`1.25`))
- defer dec.Release()
- err := dec.DecodeFloat32(&v)
- assert.Nil(t, err, "Err must be nil")
- assert.Equal(t, float32(1.25), v, "v must be equal to 1.25")
- })
- t.Run("decoder-api2", func(t *testing.T) {
- var v float32
- dec := NewDecoder(strings.NewReader(`1.25`))
- defer dec.Release()
- err := dec.Decode(&v)
- assert.Nil(t, err, "Err must be nil")
- assert.Equal(t, float32(1.25), v, "v must be equal to 1.25")
+ t.Run("decoder-api-invalid-json", func(t *testing.T) {
+ var v = new(float32)
+ err := Unmarshal([]byte(``), &v)
+ assert.NotNil(t, err, "Err must not be nil")
+ assert.IsType(t, InvalidJSONError(""), err, "err should be of type InvalidJSONError")
})
- t.Run("decoder-api-json-error", func(t *testing.T) {
- var v float32
- dec := NewDecoder(strings.NewReader(``))
- defer dec.Release()
- err := dec.DecodeFloat32(&v)
+ t.Run("decoder-api-invalid-json2", func(t *testing.T) {
+ var v = new(float32)
+ var dec = NewDecoder(strings.NewReader(``))
+ err := dec.Float32Null(&v)
assert.NotNil(t, err, "Err must not be nil")
assert.IsType(t, InvalidJSONError(""), err, "err should be of type InvalidJSONError")
})
diff --git a/decode_number_int_test.go b/decode_number_int_test.go
@@ -578,6 +578,19 @@ func TestDecoderIntNull(t *testing.T) {
}
})
}
+ t.Run("decoder-api-invalid-json", func(t *testing.T) {
+ var v = new(int)
+ err := Unmarshal([]byte(``), &v)
+ assert.NotNil(t, err, "Err must not be nil")
+ assert.IsType(t, InvalidJSONError(""), err, "err should be of type InvalidJSONError")
+ })
+ t.Run("decoder-api-invalid-json2", func(t *testing.T) {
+ var v = new(int)
+ var dec = NewDecoder(strings.NewReader(``))
+ err := dec.IntNull(&v)
+ assert.NotNil(t, err, "Err must not be nil")
+ assert.IsType(t, InvalidJSONError(""), err, "err should be of type InvalidJSONError")
+ })
}
func TestDecoderInt64(t *testing.T) {
@@ -1191,6 +1204,19 @@ func TestDecoderInt64Null(t *testing.T) {
}
})
}
+ t.Run("decoder-api-invalid-json", func(t *testing.T) {
+ var v = new(int64)
+ err := Unmarshal([]byte(``), &v)
+ assert.NotNil(t, err, "Err must not be nil")
+ assert.IsType(t, InvalidJSONError(""), err, "err should be of type InvalidJSONError")
+ })
+ t.Run("decoder-api-invalid-json2", func(t *testing.T) {
+ var v = new(int64)
+ var dec = NewDecoder(strings.NewReader(``))
+ err := dec.Int64Null(&v)
+ assert.NotNil(t, err, "Err must not be nil")
+ assert.IsType(t, InvalidJSONError(""), err, "err should be of type InvalidJSONError")
+ })
}
func TestDecoderInt32(t *testing.T) {
@@ -1864,6 +1890,19 @@ func TestDecoderInt32Null(t *testing.T) {
}
})
}
+ t.Run("decoder-api-invalid-json", func(t *testing.T) {
+ var v = new(int32)
+ err := Unmarshal([]byte(``), &v)
+ assert.NotNil(t, err, "Err must not be nil")
+ assert.IsType(t, InvalidJSONError(""), err, "err should be of type InvalidJSONError")
+ })
+ t.Run("decoder-api-invalid-json2", func(t *testing.T) {
+ var v = new(int32)
+ var dec = NewDecoder(strings.NewReader(``))
+ err := dec.Int32Null(&v)
+ assert.NotNil(t, err, "Err must not be nil")
+ assert.IsType(t, InvalidJSONError(""), err, "err should be of type InvalidJSONError")
+ })
}
func TestDecoderInt16(t *testing.T) {
@@ -2526,6 +2565,19 @@ func TestDecoderInt16Null(t *testing.T) {
}
})
}
+ t.Run("decoder-api-invalid-json", func(t *testing.T) {
+ var v = new(int16)
+ err := Unmarshal([]byte(``), &v)
+ assert.NotNil(t, err, "Err must not be nil")
+ assert.IsType(t, InvalidJSONError(""), err, "err should be of type InvalidJSONError")
+ })
+ t.Run("decoder-api-invalid-json2", func(t *testing.T) {
+ var v = new(int16)
+ var dec = NewDecoder(strings.NewReader(``))
+ err := dec.Int16Null(&v)
+ assert.NotNil(t, err, "Err must not be nil")
+ assert.IsType(t, InvalidJSONError(""), err, "err should be of type InvalidJSONError")
+ })
}
func TestDecoderInt8(t *testing.T) {
@@ -3202,4 +3254,17 @@ func TestDecoderInt8Null(t *testing.T) {
}
})
}
+ t.Run("decoder-api-invalid-json", func(t *testing.T) {
+ var v = new(int8)
+ err := Unmarshal([]byte(``), &v)
+ assert.NotNil(t, err, "Err must not be nil")
+ assert.IsType(t, InvalidJSONError(""), err, "err should be of type InvalidJSONError")
+ })
+ t.Run("decoder-api-invalid-json2", func(t *testing.T) {
+ var v = new(int8)
+ var dec = NewDecoder(strings.NewReader(``))
+ err := dec.Int8Null(&v)
+ assert.NotNil(t, err, "Err must not be nil")
+ assert.IsType(t, InvalidJSONError(""), err, "err should be of type InvalidJSONError")
+ })
}
diff --git a/decode_number_uint_test.go b/decode_number_uint_test.go
@@ -294,6 +294,19 @@ func TestDecoderUint64Null(t *testing.T) {
}
})
}
+ t.Run("decoder-api-invalid-json", func(t *testing.T) {
+ var v = new(uint64)
+ err := Unmarshal([]byte(``), &v)
+ assert.NotNil(t, err, "Err must not be nil")
+ assert.IsType(t, InvalidJSONError(""), err, "err should be of type InvalidJSONError")
+ })
+ t.Run("decoder-api-invalid-json2", func(t *testing.T) {
+ var v = new(uint64)
+ var dec = NewDecoder(strings.NewReader(``))
+ err := dec.Uint64Null(&v)
+ assert.NotNil(t, err, "Err must not be nil")
+ assert.IsType(t, InvalidJSONError(""), err, "err should be of type InvalidJSONError")
+ })
}
func TestDecoderUint32(t *testing.T) {
@@ -597,6 +610,19 @@ func TestDecoderUint32Null(t *testing.T) {
}
})
}
+ t.Run("decoder-api-invalid-json", func(t *testing.T) {
+ var v = new(uint32)
+ err := Unmarshal([]byte(``), &v)
+ assert.NotNil(t, err, "Err must not be nil")
+ assert.IsType(t, InvalidJSONError(""), err, "err should be of type InvalidJSONError")
+ })
+ t.Run("decoder-api-invalid-json2", func(t *testing.T) {
+ var v = new(uint32)
+ var dec = NewDecoder(strings.NewReader(``))
+ err := dec.Uint32Null(&v)
+ assert.NotNil(t, err, "Err must not be nil")
+ assert.IsType(t, InvalidJSONError(""), err, "err should be of type InvalidJSONError")
+ })
}
func TestDecoderUint16(t *testing.T) {
@@ -948,6 +974,19 @@ func TestDecoderUint16Null(t *testing.T) {
}
})
}
+ t.Run("decoder-api-invalid-json", func(t *testing.T) {
+ var v = new(uint16)
+ err := Unmarshal([]byte(``), &v)
+ assert.NotNil(t, err, "Err must not be nil")
+ assert.IsType(t, InvalidJSONError(""), err, "err should be of type InvalidJSONError")
+ })
+ t.Run("decoder-api-invalid-json2", func(t *testing.T) {
+ var v = new(uint16)
+ var dec = NewDecoder(strings.NewReader(``))
+ err := dec.Uint16Null(&v)
+ assert.NotNil(t, err, "Err must not be nil")
+ assert.IsType(t, InvalidJSONError(""), err, "err should be of type InvalidJSONError")
+ })
}
func TestDecoderUint8(t *testing.T) {
@@ -1276,4 +1315,17 @@ func TestDecoderUint8Null(t *testing.T) {
}
})
}
+ t.Run("decoder-api-invalid-json", func(t *testing.T) {
+ var v = new(uint8)
+ err := Unmarshal([]byte(``), &v)
+ assert.NotNil(t, err, "Err must not be nil")
+ assert.IsType(t, InvalidJSONError(""), err, "err should be of type InvalidJSONError")
+ })
+ t.Run("decoder-api-invalid-json2", func(t *testing.T) {
+ var v = new(uint8)
+ var dec = NewDecoder(strings.NewReader(``))
+ err := dec.Uint8Null(&v)
+ assert.NotNil(t, err, "Err must not be nil")
+ assert.IsType(t, InvalidJSONError(""), err, "err should be of type InvalidJSONError")
+ })
}
diff --git a/decode_object_test.go b/decode_object_test.go
@@ -7,7 +7,6 @@ import (
"strings"
"testing"
- "github.com/davecgh/go-spew/spew"
"github.com/stretchr/testify/assert"
)
@@ -497,7 +496,6 @@ func TestDecodeObjectBasic(t *testing.T) {
dec := BorrowDecoder(strings.NewReader(testCase.json))
defer dec.Release()
err := dec.Decode(&s)
- spew.Dump(s)
if testCase.err {
t.Log(err)
assert.NotNil(t, err, "err should not be nil")
diff --git a/decode_string_test.go b/decode_string_test.go
@@ -634,6 +634,13 @@ func TestDecoderStringNull(t *testing.T) {
}
})
}
+ t.Run("decoder-api-invalid-json2", func(t *testing.T) {
+ var v = new(string)
+ var dec = NewDecoder(strings.NewReader(`a`))
+ err := dec.StringNull(&v)
+ assert.NotNil(t, err, "Err must not be nil")
+ assert.IsType(t, InvalidJSONError(""), err, "err should be of type InvalidJSONError")
+ })
}
func TestDecoderStringInvalidType(t *testing.T) {
json := []byte(`1`)
diff --git a/decode_test.go b/decode_test.go
@@ -85,6 +85,16 @@ func allTypesTestCases() []allTypeDecodeTestCase {
},
},
{
+ v: new(*string),
+ d: []byte(`1`),
+ name: "test decode string null",
+ expectations: func(err error, v interface{}, t *testing.T) {
+ vt := v.(**string)
+ assert.NotNil(t, err, "err must be nil")
+ assert.Nil(t, *vt, "v must be nil")
+ },
+ },
+ {
v: new(int),
d: []byte(`1`),
@@ -106,6 +116,14 @@ func allTypesTestCases() []allTypeDecodeTestCase {
},
},
{
+ v: new(*int),
+ d: []byte(`""`),
+ name: "test decode int",
+ expectations: func(err error, v interface{}, t *testing.T) {
+ assert.NotNil(t, err, "err must be nil")
+ },
+ },
+ {
v: new(*int8),
d: []byte(`1`),
name: "test decode int",
@@ -116,6 +134,14 @@ func allTypesTestCases() []allTypeDecodeTestCase {
},
},
{
+ v: new(*int8),
+ d: []byte(`""`),
+ name: "test decode int",
+ expectations: func(err error, v interface{}, t *testing.T) {
+ assert.NotNil(t, err, "err must be nil")
+ },
+ },
+ {
v: new(*int16),
d: []byte(`1`),
name: "test decode int",
@@ -126,6 +152,14 @@ func allTypesTestCases() []allTypeDecodeTestCase {
},
},
{
+ v: new(*int16),
+ d: []byte(`""`),
+ name: "test decode int",
+ expectations: func(err error, v interface{}, t *testing.T) {
+ assert.NotNil(t, err, "err must be nil")
+ },
+ },
+ {
v: new(int64),
d: []byte(`1`),
name: "test decode int64",
@@ -146,6 +180,14 @@ func allTypesTestCases() []allTypeDecodeTestCase {
},
},
{
+ v: new(*int64),
+ d: []byte(`""`),
+ name: "test decode int64",
+ expectations: func(err error, v interface{}, t *testing.T) {
+ assert.NotNil(t, err, "err must be nil")
+ },
+ },
+ {
v: new(uint64),
d: []byte(`1`),
name: "test decode uint64",
diff --git a/gojay_test.go b/gojay_test.go
@@ -100,7 +100,7 @@ func (t *testObject) UnmarshalJSONObject(dec *Decoder, k string) error {
case "testFloat64":
return dec.AddFloat(&t.testFloat64)
case "testFloat64Null":
- return dec.AddFloat64Null(&t.testFloat64Null)
+ return dec.AddFloatNull(&t.testFloat64Null)
case "testFloat32":
return dec.AddFloat32(&t.testFloat32)
case "testFloat32Null":