commit 6caa011f1327f51a9515a27a8dd8ef13c96b63b8
parent c59a4b578d512909a38fada3747ff978bc79be28
Author: francoispqt <francois@parquet.ninja>
Date: Fri, 27 Apr 2018 00:46:55 +0800
add tests for unsafe package
Diffstat:
A | decode_unsafe_test.go | | | 338 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1 file changed, 338 insertions(+), 0 deletions(-)
diff --git a/decode_unsafe_test.go b/decode_unsafe_test.go
@@ -0,0 +1,338 @@
+package gojay
+
+import (
+ "fmt"
+ "reflect"
+ "testing"
+
+ "github.com/stretchr/testify/assert"
+)
+
+func TestUnmarshalUnsafeAllTypes(t *testing.T) {
+ testCases := []struct {
+ name string
+ v interface{}
+ d []byte
+ expectations func(err error, v interface{}, t *testing.T)
+ }{
+ {
+ v: new(string),
+ d: []byte(`"test string"`),
+ name: "test decode string",
+ expectations: func(err error, v interface{}, t *testing.T) {
+ vt := v.(*string)
+ assert.Nil(t, err, "err must be nil")
+ assert.Equal(t, "test string", *vt, "v must be equal to 1")
+ },
+ },
+ {
+ v: new(string),
+ d: []byte(`null`),
+ name: "test decode string null",
+ expectations: func(err error, v interface{}, t *testing.T) {
+ vt := v.(*string)
+ assert.Nil(t, err, "err must be nil")
+ assert.Equal(t, "", *vt, "v must be equal to 1")
+ },
+ },
+ {
+ v: new(int),
+ d: []byte(`1`),
+ name: "test decode int",
+ expectations: func(err error, v interface{}, t *testing.T) {
+ vt := v.(*int)
+ assert.Nil(t, err, "err must be nil")
+ assert.Equal(t, 1, *vt, "v must be equal to 1")
+ },
+ },
+ {
+ v: new(int64),
+ d: []byte(`1`),
+ name: "test decode int64",
+ expectations: func(err error, v interface{}, t *testing.T) {
+ vt := v.(*int64)
+ assert.Nil(t, err, "err must be nil")
+ assert.Equal(t, int64(1), *vt, "v must be equal to 1")
+ },
+ },
+ {
+ v: new(uint64),
+ d: []byte(`1`),
+ name: "test decode uint64",
+ expectations: func(err error, v interface{}, t *testing.T) {
+ vt := v.(*uint64)
+ assert.Nil(t, err, "err must be nil")
+ assert.Equal(t, uint64(1), *vt, "v must be equal to 1")
+ },
+ },
+ {
+ v: new(uint64),
+ d: []byte(`-1`),
+ name: "test decode uint64 negative",
+ expectations: func(err error, v interface{}, t *testing.T) {
+ vt := v.(*uint64)
+ assert.Nil(t, err, "err must be nil")
+ assert.Equal(t, uint64(1), *vt, "v must be equal to 1")
+ },
+ },
+ {
+ v: new(int32),
+ d: []byte(`1`),
+ name: "test decode int32",
+ expectations: func(err error, v interface{}, t *testing.T) {
+ vt := v.(*int32)
+ assert.Nil(t, err, "err must be nil")
+ assert.Equal(t, int32(1), *vt, "v must be equal to 1")
+ },
+ },
+ {
+ v: new(uint32),
+ d: []byte(`1`),
+ name: "test decode uint32",
+ expectations: func(err error, v interface{}, t *testing.T) {
+ vt := v.(*uint32)
+ assert.Nil(t, err, "err must be nil")
+ assert.Equal(t, uint32(1), *vt, "v must be equal to 1")
+ },
+ },
+ {
+ v: new(uint32),
+ d: []byte(`-1`),
+ name: "test decode uint32 negative",
+ expectations: func(err error, v interface{}, t *testing.T) {
+ vt := v.(*uint32)
+ assert.Nil(t, err, "err must be nil")
+ assert.Equal(t, uint32(1), *vt, "v must be equal to 1")
+ },
+ },
+ {
+ v: new(float64),
+ d: []byte(`1.15`),
+ name: "test decode float64",
+ expectations: func(err error, v interface{}, t *testing.T) {
+ vt := v.(*float64)
+ assert.Nil(t, err, "err must be nil")
+ assert.Equal(t, float64(1.15), *vt, "v must be equal to 1")
+ },
+ },
+ {
+ v: new(float64),
+ d: []byte(`null`),
+ name: "test decode float64 null",
+ expectations: func(err error, v interface{}, t *testing.T) {
+ vt := v.(*float64)
+ assert.Nil(t, err, "err must be nil")
+ assert.Equal(t, float64(0), *vt, "v must be equal to 1")
+ },
+ },
+ {
+ v: new(bool),
+ d: []byte(`true`),
+ name: "test decode bool true",
+ expectations: func(err error, v interface{}, t *testing.T) {
+ vt := v.(*bool)
+ assert.Nil(t, err, "err must be nil")
+ assert.Equal(t, true, *vt, "v must be equal to 1")
+ },
+ },
+ {
+ v: new(bool),
+ d: []byte(`false`),
+ name: "test decode bool false",
+ expectations: func(err error, v interface{}, t *testing.T) {
+ vt := v.(*bool)
+ assert.Nil(t, err, "err must be nil")
+ assert.Equal(t, false, *vt, "v must be equal to 1")
+ },
+ },
+ {
+ v: new(bool),
+ d: []byte(`null`),
+ name: "test decode bool null",
+ expectations: func(err error, v interface{}, t *testing.T) {
+ vt := v.(*bool)
+ assert.Nil(t, err, "err must be nil")
+ assert.Equal(t, false, *vt, "v must be equal to 1")
+ },
+ },
+ {
+ v: new(testDecodeObj),
+ d: []byte(`{"test":"test"}`),
+ name: "test decode object",
+ expectations: func(err error, v interface{}, t *testing.T) {
+ vt := v.(*testDecodeObj)
+ assert.Nil(t, err, "err must be nil")
+ assert.Equal(t, "test", vt.test, "v.test must be equal to 'test'")
+ },
+ },
+ {
+ v: new(testDecodeObj),
+ d: []byte(`{"test":null}`),
+ name: "test decode object null key",
+ expectations: func(err error, v interface{}, t *testing.T) {
+ vt := v.(*testDecodeObj)
+ assert.Nil(t, err, "err must be nil")
+ assert.Equal(t, "", vt.test, "v.test must be equal to 'test'")
+ },
+ },
+ {
+ v: new(testDecodeObj),
+ d: []byte(`null`),
+ name: "test decode object null",
+ expectations: func(err error, v interface{}, t *testing.T) {
+ vt := v.(*testDecodeObj)
+ assert.Nil(t, err, "err must be nil")
+ assert.Equal(t, "", vt.test, "v.test must be equal to 'test'")
+ },
+ },
+ {
+ 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(struct{}),
+ d: []byte(`{"test":"test"}`),
+ name: "test decode invalid type",
+ expectations: func(err error, v interface{}, t *testing.T) {
+ assert.NotNil(t, err, "err must not be nil")
+ assert.IsType(t, InvalidUnmarshalError(""), err, "err must be of type InvalidUnmarshalError")
+ assert.Equal(t, fmt.Sprintf(invalidUnmarshalErrorMsg, reflect.TypeOf(v).String()), err.Error(), "err message should be equal to invalidUnmarshalErrorMsg")
+ },
+ },
+ }
+ for _, testCase := range testCases {
+ testCase := testCase
+ t.Run(testCase.name, func(*testing.T) {
+ err := Unsafe.Unmarshal(testCase.d, testCase.v)
+ testCase.expectations(err, testCase.v, t)
+ })
+ }
+}
+
+func TestUnmarshalUnsafeObjects(t *testing.T) {
+ testCases := []struct {
+ name string
+ v UnmarshalerObject
+ d []byte
+ expectations func(err error, v interface{}, t *testing.T)
+ }{
+ {
+ v: new(testDecodeObj),
+ d: []byte(`{"test":"test"}`),
+ name: "test decode object",
+ expectations: func(err error, v interface{}, t *testing.T) {
+ vt := v.(*testDecodeObj)
+ assert.Nil(t, err, "err must be nil")
+ assert.Equal(t, "test", vt.test, "v.test must be equal to 'test'")
+ },
+ },
+ {
+ v: new(testDecodeObj),
+ d: []byte(`{"test":null}`),
+ name: "test decode object null key",
+ expectations: func(err error, v interface{}, t *testing.T) {
+ vt := v.(*testDecodeObj)
+ assert.Nil(t, err, "err must be nil")
+ assert.Equal(t, "", vt.test, "v.test must be equal to 'test'")
+ },
+ },
+ {
+ v: new(testDecodeObj),
+ d: []byte(`null`),
+ name: "test decode object null",
+ expectations: func(err error, v interface{}, t *testing.T) {
+ vt := v.(*testDecodeObj)
+ assert.Nil(t, err, "err must be nil")
+ assert.Equal(t, "", vt.test, "v.test must be equal to 'test'")
+ },
+ },
+ {
+ v: new(testDecodeObj),
+ 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 := Unsafe.UnmarshalObject(testCase.d, testCase.v)
+ testCase.expectations(err, testCase.v, t)
+ })
+ }
+}
+
+func TestUnmarshalUnsafeArrays(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 := Unsafe.UnmarshalArray(testCase.d, testCase.v)
+ testCase.expectations(err, testCase.v, t)
+ })
+ }
+}