gojay

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

decode_unsafe_test.go (12029B)


      1 package gojay
      2 
      3 import (
      4 	"fmt"
      5 	"testing"
      6 
      7 	"github.com/stretchr/testify/assert"
      8 )
      9 
     10 func TestUnmarshalUnsafeAllTypes(t *testing.T) {
     11 	testCases := []struct {
     12 		name         string
     13 		v            interface{}
     14 		d            []byte
     15 		expectations func(err error, v interface{}, t *testing.T)
     16 	}{
     17 		{
     18 			v:    new(string),
     19 			d:    []byte(`"test string"`),
     20 			name: "test decode string",
     21 			expectations: func(err error, v interface{}, t *testing.T) {
     22 				vt := v.(*string)
     23 				assert.Nil(t, err, "err must be nil")
     24 				assert.Equal(t, "test string", *vt, "v must be equal to 1")
     25 			},
     26 		},
     27 		{
     28 			v:    new(string),
     29 			d:    []byte(`null`),
     30 			name: "test decode string null",
     31 			expectations: func(err error, v interface{}, t *testing.T) {
     32 				vt := v.(*string)
     33 				assert.Nil(t, err, "err must be nil")
     34 				assert.Equal(t, "", *vt, "v must be equal to 1")
     35 			},
     36 		},
     37 		{
     38 			v:    new(int),
     39 			d:    []byte(`1`),
     40 			name: "test decode int",
     41 			expectations: func(err error, v interface{}, t *testing.T) {
     42 				vt := v.(*int)
     43 				assert.Nil(t, err, "err must be nil")
     44 				assert.Equal(t, 1, *vt, "v must be equal to 1")
     45 			},
     46 		},
     47 		{
     48 			v:    new(int8),
     49 			d:    []byte(`1`),
     50 			name: "test decode int8",
     51 			expectations: func(err error, v interface{}, t *testing.T) {
     52 				vt := v.(*int8)
     53 				assert.Nil(t, err, "err must be nil")
     54 				assert.Equal(t, int8(1), *vt, "v must be equal to 1")
     55 			},
     56 		},
     57 		{
     58 			v:    new(int16),
     59 			d:    []byte(`1`),
     60 			name: "test decode int16",
     61 			expectations: func(err error, v interface{}, t *testing.T) {
     62 				vt := v.(*int16)
     63 				assert.Nil(t, err, "err must be nil")
     64 				assert.Equal(t, int16(1), *vt, "v must be equal to 1")
     65 			},
     66 		},
     67 		{
     68 			v:    new(int32),
     69 			d:    []byte(`1`),
     70 			name: "test decode int32",
     71 			expectations: func(err error, v interface{}, t *testing.T) {
     72 				vt := v.(*int32)
     73 				assert.Nil(t, err, "err must be nil")
     74 				assert.Equal(t, int32(1), *vt, "v must be equal to 1")
     75 			},
     76 		},
     77 		{
     78 			v:    new(int64),
     79 			d:    []byte(`1`),
     80 			name: "test decode int64",
     81 			expectations: func(err error, v interface{}, t *testing.T) {
     82 				vt := v.(*int64)
     83 				assert.Nil(t, err, "err must be nil")
     84 				assert.Equal(t, int64(1), *vt, "v must be equal to 1")
     85 			},
     86 		},
     87 		{
     88 			v:    new(uint64),
     89 			d:    []byte(`1`),
     90 			name: "test decode uint64",
     91 			expectations: func(err error, v interface{}, t *testing.T) {
     92 				vt := v.(*uint64)
     93 				assert.Nil(t, err, "err must be nil")
     94 				assert.Equal(t, uint64(1), *vt, "v must be equal to 1")
     95 			},
     96 		},
     97 		{
     98 			v:    new(uint64),
     99 			d:    []byte(`-1`),
    100 			name: "test decode uint64 negative",
    101 			expectations: func(err error, v interface{}, t *testing.T) {
    102 				vt := v.(*uint64)
    103 				assert.NotNil(t, err, "err must not be nil")
    104 				assert.Equal(t, uint64(0), *vt, "v must be equal to 1")
    105 			},
    106 		},
    107 		{
    108 			v:    new(int32),
    109 			d:    []byte(`1`),
    110 			name: "test decode int32",
    111 			expectations: func(err error, v interface{}, t *testing.T) {
    112 				vt := v.(*int32)
    113 				assert.Nil(t, err, "err must be nil")
    114 				assert.Equal(t, int32(1), *vt, "v must be equal to 1")
    115 			},
    116 		},
    117 		{
    118 			v:    new(uint32),
    119 			d:    []byte(`1`),
    120 			name: "test decode uint32",
    121 			expectations: func(err error, v interface{}, t *testing.T) {
    122 				vt := v.(*uint32)
    123 				assert.Nil(t, err, "err must be nil")
    124 				assert.Equal(t, uint32(1), *vt, "v must be equal to 1")
    125 			},
    126 		},
    127 		{
    128 			v:    new(uint32),
    129 			d:    []byte(`-1`),
    130 			name: "test decode uint32 negative",
    131 			expectations: func(err error, v interface{}, t *testing.T) {
    132 				vt := v.(*uint32)
    133 				assert.NotNil(t, err, "err must not be nil")
    134 				assert.Equal(t, uint32(0), *vt, "v must be equal to 1")
    135 			},
    136 		},
    137 		{
    138 			v:    new(uint8),
    139 			d:    []byte(`1`),
    140 			name: "test decode int8",
    141 			expectations: func(err error, v interface{}, t *testing.T) {
    142 				vt := v.(*uint8)
    143 				assert.Nil(t, err, "err must be nil")
    144 				assert.Equal(t, uint8(1), *vt, "v must be equal to 1")
    145 			},
    146 		},
    147 		{
    148 			v:    new(uint16),
    149 			d:    []byte(`1`),
    150 			name: "test decode uint16",
    151 			expectations: func(err error, v interface{}, t *testing.T) {
    152 				vt := v.(*uint16)
    153 				assert.Nil(t, err, "err must be nil")
    154 				assert.Equal(t, uint16(1), *vt, "v must be equal to 1")
    155 			},
    156 		},
    157 		{
    158 			v:    new(float64),
    159 			d:    []byte(`1.15`),
    160 			name: "test decode float64",
    161 			expectations: func(err error, v interface{}, t *testing.T) {
    162 				vt := v.(*float64)
    163 				assert.Nil(t, err, "err must be nil")
    164 				assert.Equal(t, float64(1.15), *vt, "v must be equal to 1")
    165 			},
    166 		},
    167 		{
    168 			v:    new(float64),
    169 			d:    []byte(`null`),
    170 			name: "test decode float64 null",
    171 			expectations: func(err error, v interface{}, t *testing.T) {
    172 				vt := v.(*float64)
    173 				assert.Nil(t, err, "err must be nil")
    174 				assert.Equal(t, float64(0), *vt, "v must be equal to 1")
    175 			},
    176 		},
    177 		{
    178 			v:    new(float32),
    179 			d:    []byte(`1.15`),
    180 			name: "test decode float64",
    181 			expectations: func(err error, v interface{}, t *testing.T) {
    182 				vt := v.(*float32)
    183 				assert.Nil(t, err, "err must be nil")
    184 				assert.Equal(t, float32(1.15), *vt, "v must be equal to 1")
    185 			},
    186 		},
    187 		{
    188 			v:    new(bool),
    189 			d:    []byte(`true`),
    190 			name: "test decode bool true",
    191 			expectations: func(err error, v interface{}, t *testing.T) {
    192 				vt := v.(*bool)
    193 				assert.Nil(t, err, "err must be nil")
    194 				assert.Equal(t, true, *vt, "v must be equal to 1")
    195 			},
    196 		},
    197 		{
    198 			v:    new(bool),
    199 			d:    []byte(`false`),
    200 			name: "test decode bool false",
    201 			expectations: func(err error, v interface{}, t *testing.T) {
    202 				vt := v.(*bool)
    203 				assert.Nil(t, err, "err must be nil")
    204 				assert.Equal(t, false, *vt, "v must be equal to 1")
    205 			},
    206 		},
    207 		{
    208 			v:    new(bool),
    209 			d:    []byte(`null`),
    210 			name: "test decode bool null",
    211 			expectations: func(err error, v interface{}, t *testing.T) {
    212 				vt := v.(*bool)
    213 				assert.Nil(t, err, "err must be nil")
    214 				assert.Equal(t, false, *vt, "v must be equal to 1")
    215 			},
    216 		},
    217 		{
    218 			v:    new(testDecodeObj),
    219 			d:    []byte(`{"test":"test"}`),
    220 			name: "test decode object",
    221 			expectations: func(err error, v interface{}, t *testing.T) {
    222 				vt := v.(*testDecodeObj)
    223 				assert.Nil(t, err, "err must be nil")
    224 				assert.Equal(t, "test", vt.test, "v.test must be equal to 'test'")
    225 			},
    226 		},
    227 		{
    228 			v:    new(testDecodeObj),
    229 			d:    []byte(`{"test":null}`),
    230 			name: "test decode object null key",
    231 			expectations: func(err error, v interface{}, t *testing.T) {
    232 				vt := v.(*testDecodeObj)
    233 				assert.Nil(t, err, "err must be nil")
    234 				assert.Equal(t, "", vt.test, "v.test must be equal to 'test'")
    235 			},
    236 		},
    237 		{
    238 			v:    new(testDecodeObj),
    239 			d:    []byte(`null`),
    240 			name: "test decode object null",
    241 			expectations: func(err error, v interface{}, t *testing.T) {
    242 				vt := v.(*testDecodeObj)
    243 				assert.Nil(t, err, "err must be nil")
    244 				assert.Equal(t, "", vt.test, "v.test must be equal to 'test'")
    245 			},
    246 		},
    247 		{
    248 			v:    new(testDecodeSlice),
    249 			d:    []byte(`[{"test":"test"}]`),
    250 			name: "test decode slice",
    251 			expectations: func(err error, v interface{}, t *testing.T) {
    252 				vtPtr := v.(*testDecodeSlice)
    253 				vt := *vtPtr
    254 				assert.Nil(t, err, "err must be nil")
    255 				assert.Len(t, vt, 1, "len of vt must be 1")
    256 				assert.Equal(t, "test", vt[0].test, "vt[0].test must be equal to 'test'")
    257 			},
    258 		},
    259 		{
    260 			v:    new(testDecodeSlice),
    261 			d:    []byte(`[{"test":"test"},{"test":"test2"}]`),
    262 			name: "test decode slice",
    263 			expectations: func(err error, v interface{}, t *testing.T) {
    264 				vtPtr := v.(*testDecodeSlice)
    265 				vt := *vtPtr
    266 				assert.Nil(t, err, "err must be nil")
    267 				assert.Len(t, vt, 2, "len of vt must be 2")
    268 				assert.Equal(t, "test", vt[0].test, "vt[0].test must be equal to 'test'")
    269 				assert.Equal(t, "test2", vt[1].test, "vt[1].test must be equal to 'test2'")
    270 			},
    271 		},
    272 		{
    273 			v:    new(struct{}),
    274 			d:    []byte(`{"test":"test"}`),
    275 			name: "test decode invalid type",
    276 			expectations: func(err error, v interface{}, t *testing.T) {
    277 				assert.NotNil(t, err, "err must not be nil")
    278 				assert.IsType(t, InvalidUnmarshalError(""), err, "err must be of type InvalidUnmarshalError")
    279 				assert.Equal(t, fmt.Sprintf(invalidUnmarshalErrorMsg, v), err.Error(), "err message should be equal to invalidUnmarshalErrorMsg")
    280 			},
    281 		},
    282 		{
    283 			v:    new(int),
    284 			d:    []byte(`1a2`),
    285 			name: "test decode invalid json",
    286 			expectations: func(err error, v interface{}, t *testing.T) {
    287 				assert.NotNil(t, err, "err must not be nil")
    288 				assert.IsType(t, InvalidJSONError(""), err, "err must be of type InvalidJSONError")
    289 			},
    290 		},
    291 	}
    292 	for _, testCase := range testCases {
    293 		testCase := testCase
    294 		t.Run(testCase.name, func(*testing.T) {
    295 			err := Unsafe.Unmarshal(testCase.d, testCase.v)
    296 			testCase.expectations(err, testCase.v, t)
    297 		})
    298 	}
    299 }
    300 
    301 func TestUnmarshalUnsafeObjects(t *testing.T) {
    302 	testCases := []struct {
    303 		name         string
    304 		v            UnmarshalerJSONObject
    305 		d            []byte
    306 		expectations func(err error, v interface{}, t *testing.T)
    307 	}{
    308 		{
    309 			v:    new(testDecodeObj),
    310 			d:    []byte(`{"test":"test"}`),
    311 			name: "test decode object",
    312 			expectations: func(err error, v interface{}, t *testing.T) {
    313 				vt := v.(*testDecodeObj)
    314 				assert.Nil(t, err, "err must be nil")
    315 				assert.Equal(t, "test", vt.test, "v.test must be equal to 'test'")
    316 			},
    317 		},
    318 		{
    319 			v:    new(testDecodeObj),
    320 			d:    []byte(`{"test":null}`),
    321 			name: "test decode object null key",
    322 			expectations: func(err error, v interface{}, t *testing.T) {
    323 				vt := v.(*testDecodeObj)
    324 				assert.Nil(t, err, "err must be nil")
    325 				assert.Equal(t, "", vt.test, "v.test must be equal to 'test'")
    326 			},
    327 		},
    328 		{
    329 			v:    new(testDecodeObj),
    330 			d:    []byte(`null`),
    331 			name: "test decode object null",
    332 			expectations: func(err error, v interface{}, t *testing.T) {
    333 				vt := v.(*testDecodeObj)
    334 				assert.Nil(t, err, "err must be nil")
    335 				assert.Equal(t, "", vt.test, "v.test must be equal to 'test'")
    336 			},
    337 		},
    338 		{
    339 			v:    new(testDecodeObj),
    340 			d:    []byte(`invalid json`),
    341 			name: "test decode object null",
    342 			expectations: func(err error, v interface{}, t *testing.T) {
    343 				assert.NotNil(t, err, "err must not be nil")
    344 				assert.IsType(t, InvalidJSONError(""), err, "err must be of type InvalidJSONError")
    345 			},
    346 		},
    347 	}
    348 	for _, testCase := range testCases {
    349 		testCase := testCase
    350 		t.Run(testCase.name, func(*testing.T) {
    351 			err := Unsafe.UnmarshalJSONObject(testCase.d, testCase.v)
    352 			testCase.expectations(err, testCase.v, t)
    353 		})
    354 	}
    355 }
    356 
    357 func TestUnmarshalUnsafeArrays(t *testing.T) {
    358 	testCases := []struct {
    359 		name         string
    360 		v            UnmarshalerJSONArray
    361 		d            []byte
    362 		expectations func(err error, v interface{}, t *testing.T)
    363 	}{
    364 		{
    365 			v:    new(testDecodeSlice),
    366 			d:    []byte(`[{"test":"test"}]`),
    367 			name: "test decode slice",
    368 			expectations: func(err error, v interface{}, t *testing.T) {
    369 				vtPtr := v.(*testDecodeSlice)
    370 				vt := *vtPtr
    371 				assert.Nil(t, err, "err must be nil")
    372 				assert.Len(t, vt, 1, "len of vt must be 1")
    373 				assert.Equal(t, "test", vt[0].test, "vt[0].test must be equal to 'test'")
    374 			},
    375 		},
    376 		{
    377 			v:    new(testDecodeSlice),
    378 			d:    []byte(`[{"test":"test"},{"test":"test2"}]`),
    379 			name: "test decode slice",
    380 			expectations: func(err error, v interface{}, t *testing.T) {
    381 				vtPtr := v.(*testDecodeSlice)
    382 				vt := *vtPtr
    383 				assert.Nil(t, err, "err must be nil")
    384 				assert.Len(t, vt, 2, "len of vt must be 2")
    385 				assert.Equal(t, "test", vt[0].test, "vt[0].test must be equal to 'test'")
    386 				assert.Equal(t, "test2", vt[1].test, "vt[1].test must be equal to 'test2'")
    387 			},
    388 		},
    389 		{
    390 			v:    new(testDecodeSlice),
    391 			d:    []byte(`invalid json`),
    392 			name: "test decode object null",
    393 			expectations: func(err error, v interface{}, t *testing.T) {
    394 				assert.NotNil(t, err, "err must not be nil")
    395 				assert.IsType(t, InvalidJSONError(""), err, "err must be of type InvalidJSONError")
    396 			},
    397 		},
    398 	}
    399 	for _, testCase := range testCases {
    400 		testCase := testCase
    401 		t.Run(testCase.name, func(*testing.T) {
    402 			err := Unsafe.UnmarshalJSONArray(testCase.d, testCase.v)
    403 			testCase.expectations(err, testCase.v, t)
    404 		})
    405 	}
    406 }