Skip to content

Commit

Permalink
test: improve json parser test cases for json-iterator/go
Browse files Browse the repository at this point in the history
  • Loading branch information
JacksonTian committed Mar 26, 2024
1 parent ea6c47e commit 8bea888
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 22 deletions.
2 changes: 1 addition & 1 deletion go.mod
Expand Up @@ -4,7 +4,7 @@ require (
github.com/HdrHistogram/hdrhistogram-go v1.1.2 // indirect
github.com/goji/httpauth v0.0.0-20160601135302-2da839ab0f4d
github.com/jmespath/go-jmespath v0.4.0
github.com/json-iterator/go v1.1.5
github.com/json-iterator/go v1.1.12
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2
github.com/opentracing/opentracing-go v1.2.1-0.20220228012449-10b1cf09e00b
Expand Down
6 changes: 4 additions & 2 deletions go.sum
Expand Up @@ -14,17 +14,19 @@ github.com/goji/httpauth v0.0.0-20160601135302-2da839ab0f4d/go.mod h1:nnjvkQ9ptG
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0/go.mod h1:E/TSTwGwJL78qG/PmXZO1EjYhfJinVAhrmmHX6Z8B9k=
github.com/google/go-cmp v0.5.4 h1:L8R9j+yAqZuZjsqh/z+F1NCffTKKLShY6zXTItVIZ8M=
github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg=
github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo=
github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGwWFoC7ycTf1rcQZHOlsJ6N8=
github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U=
github.com/json-iterator/go v1.1.5 h1:gL2yXlmiIo4+t+y32d4WGwOjKGYcGOuyrg46vadswDE=
github.com/json-iterator/go v1.1.5/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/jung-kurt/gofpdf v1.0.3-0.20190309125859-24315acbbda5/go.mod h1:7Id9E/uU8ce6rXgefFLlgrJj/GYY22cpxn+r32jIOes=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
Expand Down
38 changes: 19 additions & 19 deletions sdk/responses/json_parser_test.go
Expand Up @@ -96,13 +96,13 @@ func TestUnmarshal_int(t *testing.T) {

err = jsonParser.Unmarshal(from, to)
assert.NotNil(t, err)
assert.Equal(t, "struct { INT int }.INT: fuzzy decode int: exceed range, error found in #10 byte of ...|00000000.1|..., bigger context ...|100000000000000000000000000.1|...", err.Error())
assert.Contains(t, err.Error(), "INT: fuzzy decode int: exceed range, error found in")

from = []byte(`{"INT":{}}`)

err = jsonParser.Unmarshal(from, to)
assert.NotNil(t, err)
assert.Equal(t, "struct { INT int }.INT: readUint64: unexpected character: \xff, error found in #0 byte of ...||..., bigger context ...||...", err.Error())
assert.Contains(t, err.Error(), "INT: readUint64: unexpected character: \xff")
}

func TestUnmarshal_uint(t *testing.T) {
Expand Down Expand Up @@ -132,7 +132,7 @@ func TestUnmarshal_uint(t *testing.T) {

err = jsonParser.Unmarshal(from, to)
assert.NotNil(t, err)
assert.Equal(t, "struct { UINT uint }.UINT: fuzzy decode uint: exceed range, error found in #10 byte of ...|00000000.1|..., bigger context ...|100000000000000000000000000.1|...", err.Error())
assert.Contains(t, err.Error(), "UINT: fuzzy decode uint: exceed range, error found in #10")
}

func TestUnmarshal_int8(t *testing.T) {
Expand Down Expand Up @@ -162,7 +162,7 @@ func TestUnmarshal_int8(t *testing.T) {

err = jsonParser.Unmarshal(from, to)
assert.NotNil(t, err)
assert.Equal(t, "struct { INT8 int8 }.INT8: fuzzy decode int8: exceed range, error found in #10 byte of ...|00000000.1|..., bigger context ...|100000000000000000000000000.1|...", err.Error())
assert.Contains(t, err.Error(), "INT8: fuzzy decode int8: exceed range")
}

func TestUnmarshal_uint8(t *testing.T) {
Expand Down Expand Up @@ -192,7 +192,7 @@ func TestUnmarshal_uint8(t *testing.T) {

err = jsonParser.Unmarshal(from, to)
assert.NotNil(t, err)
assert.Equal(t, "struct { UINT8 uint8 }.UINT8: fuzzy decode uint8: exceed range, error found in #10 byte of ...|00000000.1|..., bigger context ...|100000000000000000000000000.1|...", err.Error())
assert.Contains(t, err.Error(), "UINT8: fuzzy decode uint8: exceed range")
}

func TestUnmarshal_int16(t *testing.T) {
Expand Down Expand Up @@ -222,7 +222,7 @@ func TestUnmarshal_int16(t *testing.T) {

err = jsonParser.Unmarshal(from, to)
assert.NotNil(t, err)
assert.Equal(t, "struct { INT16 int16 }.INT16: fuzzy decode int16: exceed range, error found in #10 byte of ...|00000000.1|..., bigger context ...|100000000000000000000000000.1|...", err.Error())
assert.Contains(t, err.Error(), "INT16: fuzzy decode int16: exceed range")
}

func TestUnmarshal_uint16(t *testing.T) {
Expand Down Expand Up @@ -252,7 +252,7 @@ func TestUnmarshal_uint16(t *testing.T) {

err = jsonParser.Unmarshal(from, to)
assert.NotNil(t, err)
assert.Equal(t, "struct { UINT16 uint16 }.UINT16: fuzzy decode uint16: exceed range, error found in #10 byte of ...|00000000.1|..., bigger context ...|100000000000000000000000000.1|...", err.Error())
assert.Contains(t, err.Error(), "UINT16: fuzzy decode uint16: exceed range", err.Error())
}

func TestUnmarshal_int32(t *testing.T) {
Expand Down Expand Up @@ -282,7 +282,7 @@ func TestUnmarshal_int32(t *testing.T) {

err = jsonParser.Unmarshal(from, to)
assert.NotNil(t, err)
assert.Equal(t, "struct { INT32 int32 }.INT32: fuzzy decode int32: exceed range, error found in #10 byte of ...|00000000.1|..., bigger context ...|100000000000000000000000000.1|...", err.Error())
assert.Contains(t, err.Error(), "INT32: fuzzy decode int32: exceed range")
}

func TestUnmarshal_uint32(t *testing.T) {
Expand Down Expand Up @@ -312,7 +312,7 @@ func TestUnmarshal_uint32(t *testing.T) {

err = jsonParser.Unmarshal(from, to)
assert.NotNil(t, err)
assert.Equal(t, "struct { UINT32 uint32 }.UINT32: fuzzy decode uint32: exceed range, error found in #10 byte of ...|00000000.1|..., bigger context ...|100000000000000000000000000.1|...", err.Error())
assert.Contains(t, err.Error(), "UINT32: fuzzy decode uint32: exceed range")
}

func TestUnmarshal_int64(t *testing.T) {
Expand Down Expand Up @@ -342,7 +342,7 @@ func TestUnmarshal_int64(t *testing.T) {

err = jsonParser.Unmarshal(from, to)
assert.NotNil(t, err)
assert.Equal(t, "struct { INT64 int64 }.INT64: fuzzy decode int64: exceed range, error found in #10 byte of ...|00000000.1|..., bigger context ...|100000000000000000000000000.1|...", err.Error())
assert.Contains(t, err.Error(), "INT64: fuzzy decode int64: exceed range")
}

func TestUnmarshal_uint64(t *testing.T) {
Expand Down Expand Up @@ -372,7 +372,7 @@ func TestUnmarshal_uint64(t *testing.T) {

err = jsonParser.Unmarshal(from, to)
assert.NotNil(t, err)
assert.Equal(t, "struct { UINT64 uint64 }.UINT64: fuzzy decode uint64: exceed range, error found in #10 byte of ...|00000000.1|..., bigger context ...|100000000000000000000000000.1|...", err.Error())
assert.Contains(t, err.Error(), "UINT64: fuzzy decode uint64: exceed range")
}

func TestUnmarshal_string(t *testing.T) {
Expand Down Expand Up @@ -424,7 +424,7 @@ func TestUnmarshal_string(t *testing.T) {

err = jsonParser.Unmarshal(from, to)
assert.NotNil(t, err)
assert.Equal(t, "struct { STRING string }.STRING: fuzzyStringDecoder: not number or string or bool, error found in #10 byte of ...|{\"STRING\":{}}|..., bigger context ...|{\"STRING\":{}}|...", err.Error())
assert.Contains(t, err.Error(), "STRING: fuzzyStringDecoder: not number or string or bool")
}

func TestUnmarshal_bool(t *testing.T) {
Expand Down Expand Up @@ -465,7 +465,7 @@ func TestUnmarshal_bool(t *testing.T) {

err = jsonParser.Unmarshal(from, to)
assert.NotNil(t, err)
assert.Equal(t, "struct { BOOL bool }.BOOL: fuzzyBoolDecoder: get value from json.number failed, error found in #10 byte of ...|0000000000}|..., bigger context ...|{\"BOOL\": 1000000000000000000000000000000000000000}|...", err.Error())
assert.Contains(t, err.Error(), "BOOL: fuzzyBoolDecoder: get value from json.number failed")

// bool to string
from = []byte(`{"BOOL":"true"}`)
Expand All @@ -490,7 +490,7 @@ func TestUnmarshal_bool(t *testing.T) {

err = jsonParser.Unmarshal(from, to)
assert.NotNil(t, err)
assert.Equal(t, "struct { BOOL bool }.BOOL: fuzzyBoolDecoder: unsupported bool value: other, error found in #10 byte of ...|L\":\"other\"}|..., bigger context ...|{\"BOOL\":\"other\"}|...", err.Error())
assert.Contains(t, err.Error(), "BOOL: fuzzyBoolDecoder: unsupported bool value: other")

// nil to bool
from = []byte(`{"BOOL":null}`)
Expand All @@ -507,7 +507,7 @@ func TestUnmarshal_bool(t *testing.T) {

err = jsonParser.Unmarshal(from, to)
assert.NotNil(t, err)
assert.Equal(t, "struct { BOOL bool }.BOOL: fuzzyBoolDecoder: not number or string or nil, error found in #8 byte of ...|{\"BOOL\":{}}|..., bigger context ...|{\"BOOL\":{}}|...", err.Error())
assert.Contains(t, err.Error(), "BOOL: fuzzyBoolDecoder: not number or string or nil")
}

func TestUnmarshal_array(t *testing.T) {
Expand Down Expand Up @@ -571,7 +571,7 @@ func TestUnmarshal_float32(t *testing.T) {

err = jsonParser.Unmarshal(from, to)
assert.NotNil(t, err)
assert.Equal(t, "struct { FLOAT32 float32 }.FLOAT32: readFloat32: leading dot is invalid, error found in #0 byte of ...|.|..., bigger context ...|.|...", err.Error())
assert.Contains(t, err.Error(), "FLOAT32: readFloat32: leading dot is invalid")

// bool to float32
from = []byte(`{"FLOAT32":true}`)
Expand Down Expand Up @@ -607,7 +607,7 @@ func TestUnmarshal_float32(t *testing.T) {

err = jsonParser.Unmarshal(from, to)
assert.NotNil(t, err)
assert.Equal(t, "struct { FLOAT32 float32 }.FLOAT32: nullableFuzzyFloat32Decoder: not number or string, error found in #10 byte of ...|\"FLOAT32\":{}}|..., bigger context ...|{\"FLOAT32\":{}}|...", err.Error())
assert.Contains(t, err.Error(), "FLOAT32: nullableFuzzyFloat32Decoder: not number or string")
}

func TestUnmarshal_float64(t *testing.T) {
Expand Down Expand Up @@ -656,7 +656,7 @@ func TestUnmarshal_float64(t *testing.T) {

err = jsonParser.Unmarshal(from, to)
assert.NotNil(t, err)
assert.Equal(t, "struct { FLOAT64 float64 }.FLOAT64: readFloat64: leading dot is invalid, error found in #0 byte of ...|.|..., bigger context ...|.|...", err.Error())
assert.Contains(t, err.Error(), "FLOAT64: readFloat64: leading dot is invalid")

// bool to float64
from = []byte(`{"FLOAT64":true}`)
Expand Down Expand Up @@ -692,7 +692,7 @@ func TestUnmarshal_float64(t *testing.T) {

err = jsonParser.Unmarshal(from, to)
assert.NotNil(t, err)
assert.Equal(t, "struct { FLOAT64 float64 }.FLOAT64: nullableFuzzyFloat64Decoder: not number or string, error found in #10 byte of ...|\"FLOAT64\":{}}|..., bigger context ...|{\"FLOAT64\":{}}|...", err.Error())
assert.Contains(t, err.Error(), "FLOAT64: nullableFuzzyFloat64Decoder: not number or string")
}

func TestUnmarshalWithArray(t *testing.T) {
Expand Down

0 comments on commit 8bea888

Please sign in to comment.