gojay

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

commit 8460db6b1c6eb3f7bcc6769ed5b3e1c9c69ea155
parent 86e4b630f45da3bb0aa8ff9efa4b73ba416a5da8
Author: francoispqt <francois@parquet.ninja>
Date:   Sat, 28 Apr 2018 22:31:17 +0800

add some tests for decoding

Diffstat:
Mdecode_number_test.go | 34++++++++++++++++++++++++----------
Mdecode_object.go | 3---
Mdecode_stream_test.go | 13+++++++++++++
Mencode_interface_test.go | 7+++++++
4 files changed, 44 insertions(+), 13 deletions(-)

diff --git a/decode_number_test.go b/decode_number_test.go @@ -15,12 +15,19 @@ func TestDecoderIntBasic(t *testing.T) { assert.Equal(t, 124, v, "v must be equal to 124") } func TestDecoderIntNegative(t *testing.T) { - json := []byte(`-124`) + json := []byte(` -124 `) var v int err := Unmarshal(json, &v) assert.Nil(t, err, "Err must be nil") assert.Equal(t, -124, v, "v must be equal to -124") } +func TestDecoderIntNegativeError(t *testing.T) { + json := []byte(` -12x4 `) + var v int + err := Unmarshal(json, &v) + assert.NotNil(t, err, "Err must be nil") + assert.IsType(t, InvalidJSONError(""), err, "err must be of type InvalidJSONError") +} func TestDecoderIntNull(t *testing.T) { json := []byte(`null`) var v int @@ -69,7 +76,7 @@ func TestDecoderIntPoolError(t *testing.T) { assert.True(t, false, "should not be called as decoder should have panicked") } func TestDecoderIntOverfow2(t *testing.T) { - json := []byte(`92233720368547758089`) + json := []byte(`92233720368547758089 `) var v int err := Unmarshal(json, &v) assert.NotNil(t, err, "Err must not be nil as int is overflowing") @@ -92,12 +99,19 @@ func TestDecoderInt32Basic(t *testing.T) { assert.Equal(t, int32(124), v, "v must be equal to 124") } func TestDecoderInt32Negative(t *testing.T) { - json := []byte(`-124`) + json := []byte(`-124 `) var v int32 err := Unmarshal(json, &v) assert.Nil(t, err, "Err must be nil") assert.Equal(t, int32(-124), v, "v must be equal to -124") } +func TestDecoderInt32NegativeError(t *testing.T) { + json := []byte(`-12x4 `) + var v int32 + err := Unmarshal(json, &v) + assert.NotNil(t, err, "Err must be nil") + assert.IsType(t, InvalidJSONError(""), err, "err must be of type InvalidJSONError") +} func TestDecoderInt32Null(t *testing.T) { json := []byte(`null`) var v int32 @@ -127,7 +141,7 @@ func TestDecoderInt32Big(t *testing.T) { assert.Equal(t, int32(2147483647), v, "int32 must be equal to 2147483647") } func TestDecoderInt32Overflow(t *testing.T) { - json := []byte(`2147483648`) + json := []byte(` 2147483648`) var v int32 err := Unmarshal(json, &v) assert.NotNil(t, err, "err must not be nil as int32 overflows") @@ -162,7 +176,7 @@ func TestDecoderInt32tDecoderAPI(t *testing.T) { } func TestDecoderUint32Basic(t *testing.T) { - json := []byte(`124`) + json := []byte(`124 `) var v uint32 err := Unmarshal(json, &v) assert.Nil(t, err, "Err must be nil") @@ -190,7 +204,7 @@ func TestDecoderUint32InvalidJSON(t *testing.T) { assert.IsType(t, InvalidJSONError(""), err, "err must be of type InvalidJSONError") } func TestDecoderUint32Big(t *testing.T) { - json := []byte(`4294967295`) + json := []byte(`4294967295 `) var v uint32 err := Unmarshal(json, &v) assert.Nil(t, err, "err must not be nil as uint32 does not overflow") @@ -233,7 +247,7 @@ func TestDecoderUint32tDecoderAPI(t *testing.T) { } func TestDecoderInt64Basic(t *testing.T) { - json := []byte(`124`) + json := []byte(`124 `) var v int64 err := Unmarshal(json, &v) assert.Nil(t, err, "Err must be nil") @@ -309,7 +323,7 @@ func TestDecoderInt64tDecoderAPI(t *testing.T) { assert.Equal(t, int64(33), v, "v must be equal to 33") } func TestDecoderUint64Basic(t *testing.T) { - json := []byte(`124`) + json := []byte(` 124 `) var v uint64 err := Unmarshal(json, &v) assert.Nil(t, err, "Err must be nil") @@ -378,7 +392,7 @@ func TestDecoderUint64tDecoderAPI(t *testing.T) { assert.Equal(t, uint64(33), v, "v must be equal to 33") } func TestDecoderFloatBasic(t *testing.T) { - json := []byte(`100.11`) + json := []byte(`100.11 `) var v float64 err := Unmarshal(json, &v) assert.Nil(t, err, "Err must be nil") @@ -386,7 +400,7 @@ func TestDecoderFloatBasic(t *testing.T) { } func TestDecoderFloatBig(t *testing.T) { - json := []byte(`89899843.3493493`) + json := []byte(`89899843.3493493 `) var v float64 err := Unmarshal(json, &v) assert.Nil(t, err, "Err must be nil") diff --git a/decode_object.go b/decode_object.go @@ -18,9 +18,6 @@ func (dec *Decoder) DecodeObject(j UnmarshalerObject) error { return err } func (dec *Decoder) decodeObject(j UnmarshalerObject) (int, error) { - if dec.isPooled == 1 { - panic(InvalidUsagePooledDecoderError("Invalid usage of pooled decoder")) - } keys := j.NKeys() for ; dec.cursor < dec.length || dec.read(); dec.cursor++ { switch dec.data[dec.cursor] { diff --git a/decode_stream_test.go b/decode_stream_test.go @@ -369,6 +369,19 @@ func TestStreamDecodingDeadline(t *testing.T) { assert.Equal(t, now.String(), dec.deadline.String(), "dec.now and now should be equal") } +func TestStreamDecodingDeadlineNotSet(t *testing.T) { + dec := Stream.NewDecoder(&StreamReader{}) + _, isSet := dec.Deadline() + assert.Equal(t, false, isSet, "isSet should be false as deadline is not set") +} + +// this test is only relevant for coverage +func TestStreamDecodingValue(t *testing.T) { + dec := Stream.NewDecoder(&StreamReader{}) + v := dec.Value("") + assert.Nil(t, v, "v should be nil") +} + func TestStreamDecodingErrNotSet(t *testing.T) { dec := Stream.NewDecoder(&StreamReader{}) assert.Nil(t, dec.Err(), "dec.Err should be nim") diff --git a/encode_interface_test.go b/encode_interface_test.go @@ -11,6 +11,13 @@ var encoderTestCases = []struct { expectations func(t *testing.T, b []byte, err error) }{ { + v: 100, + expectations: func(t *testing.T, b []byte, err error) { + assert.Nil(t, err, "err should be nil") + assert.Equal(t, "100", string(b), "string(b) should equal 100") + }, + }, + { v: int64(100), expectations: func(t *testing.T, b []byte, err error) { assert.Nil(t, err, "err should be nil")