gojay

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

commit 56bfd9b9802e2c729d36ce885448cbfda23bf682
parent ecb8d5814bccf5611b694b65ff05bd86b7b8ee1c
Author: francoispqt <francois@parquet.ninja>
Date:   Tue,  1 May 2018 23:03:29 +0800

add tests

Diffstat:
Mdecode_array.go | 2+-
Mdecode_array_test.go | 51+++++++++++++++++++++++++++++++++++++++++++++++++++
Mdecode_test.go | 63++++++++++++++-------------------------------------------------
3 files changed, 66 insertions(+), 50 deletions(-)

diff --git a/decode_array.go b/decode_array.go @@ -109,5 +109,5 @@ func (dec *Decoder) skipArray() (int, error) { continue } } - return 0, nil + return 0, InvalidJSONError("Invalid JSON") } diff --git a/decode_array_test.go b/decode_array_test.go @@ -190,6 +190,57 @@ func TestDecoderSliceDecoderAPIError(t *testing.T) { assert.IsType(t, InvalidJSONError(""), err, "err message must be 'Invalid JSON'") } +func TestUnmarshalArrays(t *testing.T) { + testCases := []struct { + name string + v UnmarshalerArray + d []byte + expectations func(err error, v interface{}, t *testing.T) + }{ + { + v: new(testDecodeSlice), + d: []byte(`[{"test":"test"}]`), + name: "test decode slice", + expectations: func(err error, v interface{}, t *testing.T) { + vtPtr := v.(*testDecodeSlice) + vt := *vtPtr + assert.Nil(t, err, "err must be nil") + assert.Len(t, vt, 1, "len of vt must be 1") + assert.Equal(t, "test", vt[0].test, "vt[0].test must be equal to 'test'") + }, + }, + { + v: new(testDecodeSlice), + d: []byte(`[{"test":"test"},{"test":"test2"}]`), + name: "test decode slice", + expectations: func(err error, v interface{}, t *testing.T) { + vtPtr := v.(*testDecodeSlice) + vt := *vtPtr + assert.Nil(t, err, "err must be nil") + assert.Len(t, vt, 2, "len of vt must be 2") + assert.Equal(t, "test", vt[0].test, "vt[0].test must be equal to 'test'") + assert.Equal(t, "test2", vt[1].test, "vt[1].test must be equal to 'test2'") + }, + }, + { + v: new(testDecodeSlice), + d: []byte(`invalid json`), + name: "test decode object null", + expectations: func(err error, v interface{}, t *testing.T) { + assert.NotNil(t, err, "err must not be nil") + assert.IsType(t, InvalidJSONError(""), err, "err must be of type InvalidJSONError") + }, + }, + } + for _, testCase := range testCases { + testCase := testCase + t.Run(testCase.name, func(*testing.T) { + err := UnmarshalArray(testCase.d, testCase.v) + testCase.expectations(err, testCase.v, t) + }) + } +} + func TestSkipArray(t *testing.T) { testCases := []struct { json string diff --git a/decode_test.go b/decode_test.go @@ -4,6 +4,7 @@ import ( "fmt" "io" "reflect" + "strings" "testing" "github.com/stretchr/testify/assert" @@ -553,53 +554,17 @@ func TestUnmarshalObjects(t *testing.T) { } } -func TestUnmarshalArrays(t *testing.T) { - testCases := []struct { - name string - v UnmarshalerArray - d []byte - expectations func(err error, v interface{}, t *testing.T) - }{ - { - v: new(testDecodeSlice), - d: []byte(`[{"test":"test"}]`), - name: "test decode slice", - expectations: func(err error, v interface{}, t *testing.T) { - vtPtr := v.(*testDecodeSlice) - vt := *vtPtr - assert.Nil(t, err, "err must be nil") - assert.Len(t, vt, 1, "len of vt must be 1") - assert.Equal(t, "test", vt[0].test, "vt[0].test must be equal to 'test'") - }, - }, - { - v: new(testDecodeSlice), - d: []byte(`[{"test":"test"},{"test":"test2"}]`), - name: "test decode slice", - expectations: func(err error, v interface{}, t *testing.T) { - vtPtr := v.(*testDecodeSlice) - vt := *vtPtr - assert.Nil(t, err, "err must be nil") - assert.Len(t, vt, 2, "len of vt must be 2") - assert.Equal(t, "test", vt[0].test, "vt[0].test must be equal to 'test'") - assert.Equal(t, "test2", vt[1].test, "vt[1].test must be equal to 'test2'") - }, - }, - { - v: new(testDecodeSlice), - d: []byte(`invalid json`), - name: "test decode object null", - expectations: func(err error, v interface{}, t *testing.T) { - assert.NotNil(t, err, "err must not be nil") - assert.IsType(t, InvalidJSONError(""), err, "err must be of type InvalidJSONError") - }, - }, - } - for _, testCase := range testCases { - testCase := testCase - t.Run(testCase.name, func(*testing.T) { - err := UnmarshalArray(testCase.d, testCase.v) - testCase.expectations(err, testCase.v, t) - }) - } +func TestSkipData(t *testing.T) { + t.Run("error-invalid-json", func(t *testing.T) { + dec := NewDecoder(strings.NewReader("")) + err := dec.skipData() + assert.NotNil(t, err, "err should not be nil as data is empty") + assert.IsType(t, InvalidJSONError(""), err, "err should of type InvalidJSONError") + }) + t.Run("skip-array-error-invalid-json", func(t *testing.T) { + dec := NewDecoder(strings.NewReader("")) + _, err := dec.skipArray() + assert.NotNil(t, err, "err should not be nil as data is empty") + assert.IsType(t, InvalidJSONError(""), err, "err should of type InvalidJSONError") + }) }