gojay

high performance JSON encoder/decoder with stream API for Golang
git clone git://git.lair.cx/gojay
Log | Files | Refs | README | LICENSE

commit 10ed323cd639a0df5f88905414baeba1c7de1567
parent be7260a29b81275a2a127d049f6402700b999333
Author: francoispqt <francois@parquet.ninja>
Date:   Sun, 24 Mar 2019 01:56:04 +0800

add tests

Diffstat:
Mdecode_number_int_test.go | 24++++++++++++++++++++++++
Mdecode_object_test.go | 9+++++++++
Mdecode_sqlnull_test.go | 43+++++++++++++++++++++++++++++++++++++++++++
Mdecode_string_test.go | 8++++++++
4 files changed, 84 insertions(+), 0 deletions(-)

diff --git a/decode_number_int_test.go b/decode_number_int_test.go @@ -735,6 +735,12 @@ func TestDecoderInt64(t *testing.T) { expectedResult: 0, }, { + name: "before-exp-err-too-big", + json: "10.11231242345325435464364643e1", + expectedResult: 0, + err: true, + }, + { name: "error3", json: "0E40", expectedResult: 0, @@ -1402,6 +1408,12 @@ func TestDecoderInt32(t *testing.T) { expectedResult: -800000, }, { + name: "before-exp-err-too-big", + json: "10.11231242345325435464364643e1", + expectedResult: 0, + err: true, + }, + { name: "exponent-err-", json: "0.1e", expectedResult: 0, @@ -2017,6 +2029,11 @@ func TestDecoderInt16(t *testing.T) { expectedResult: 120, }, { + name: "exponent too big", + json: "1000.202302302422324435342E2", + err: true, + }, + { name: "basic-exponent-positive-positive-exp1", json: "3.5e+001 ", expectedResult: 35, @@ -2751,6 +2768,7 @@ func TestDecoderInt8(t *testing.T) { json: "-3e01", expectedResult: -30, }, + { name: "error3", json: "0E40", @@ -2765,6 +2783,12 @@ func TestDecoderInt8(t *testing.T) { err: true, }, { + name: "before-exp-err-too-big", + json: "10.11231242345325435464364643e1", + expectedResult: 0, + err: true, + }, + { name: "exponent-err-too-big", json: "0.1e10000000000000000000", expectedResult: 0, diff --git a/decode_object_test.go b/decode_object_test.go @@ -1039,6 +1039,15 @@ func TestDecodeObjectNull(t *testing.T) { }, ) t.Run( + "err empty json", + func(t *testing.T) { + var o = &ObjectNull{} + var dec = NewDecoder(strings.NewReader(``)) + var _, err = dec.decodeObjectNull(&o) + assert.NotNil(t, 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 { diff --git a/decode_sqlnull_test.go b/decode_sqlnull_test.go @@ -231,6 +231,7 @@ func TestDecodeSQLNullKeys(t *testing.T) { name string json string expectedResult *SQLDecodeObject + err bool }{ { name: "basic all valid", @@ -358,6 +359,42 @@ func TestDecodeSQLNullKeys(t *testing.T) { }, }, }, + { + name: "err string key", + json: `{ + "s": "`, + err: true, + }, + { + name: "err float key", + json: `{ + "s": null, + "f": 1", + "i": null, + "b": null + }`, + err: true, + }, + { + name: "err int key", + json: `{ + "s": null, + "f": null, + "i": 1", + "b": null + }`, + err: true, + }, + { + name: "err bool key", + json: `{ + "s": null, + "f": null, + "i": null, + "b": tra + }`, + err: true, + }, } for _, testCase := range testCases { @@ -365,6 +402,12 @@ func TestDecodeSQLNullKeys(t *testing.T) { var o = &SQLDecodeObject{} var dec = NewDecoder(strings.NewReader(testCase.json)) var err = dec.Decode(o) + + if testCase.err { + require.NotNil(t, err) + return + } + require.Nil(t, err) require.Equal( t, diff --git a/decode_string_test.go b/decode_string_test.go @@ -702,6 +702,14 @@ func TestDecoderSkipEscapedStringError3(t *testing.T) { assert.IsType(t, InvalidJSONError(""), err, "err must be of type InvalidJSONError") } +func TestDecoderSkipEscapedStringError4(t *testing.T) { + dec := NewDecoder(strings.NewReader(`\u12`)) + defer dec.Release() + err := dec.skipEscapedString() + assert.NotNil(t, err, "Err must be nil") + assert.IsType(t, InvalidJSONError(""), err, "err must be of type InvalidJSONError") +} + func TestDecoderSkipStringError(t *testing.T) { dec := NewDecoder(strings.NewReader(`invalid`)) defer dec.Release()