mirror of
https://github.com/json-iterator/go.git
synced 2025-04-20 11:28:49 +02:00
Merge pull request #255 from bboreham/error-test
Add a test for input errors, and fix one bug that it finds
This commit is contained in:
commit
fb4d53e4cc
@ -494,13 +494,16 @@ func (decoder *generalStructDecoder) Decode(ptr unsafe.Pointer, iter *Iterator)
|
|||||||
if !iter.readObjectStart() {
|
if !iter.readObjectStart() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
decoder.decodeOneField(ptr, iter)
|
var c byte
|
||||||
for iter.nextToken() == ',' {
|
for c = ','; c == ','; c = iter.nextToken() {
|
||||||
decoder.decodeOneField(ptr, iter)
|
decoder.decodeOneField(ptr, iter)
|
||||||
}
|
}
|
||||||
if iter.Error != nil && iter.Error != io.EOF {
|
if iter.Error != nil && iter.Error != io.EOF {
|
||||||
iter.Error = fmt.Errorf("%v.%s", decoder.typ, iter.Error.Error())
|
iter.Error = fmt.Errorf("%v.%s", decoder.typ, iter.Error.Error())
|
||||||
}
|
}
|
||||||
|
if c != '}' {
|
||||||
|
iter.ReportError("struct Decode", `expect }, but found `+string([]byte{c}))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (decoder *generalStructDecoder) decodeOneField(ptr unsafe.Pointer, iter *Iterator) {
|
func (decoder *generalStructDecoder) decodeOneField(ptr unsafe.Pointer, iter *Iterator) {
|
||||||
|
36
value_tests/error_test.go
Normal file
36
value_tests/error_test.go
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
package test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/json-iterator/go"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
"reflect"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Test_errorInput(t *testing.T) {
|
||||||
|
for _, testCase := range unmarshalCases {
|
||||||
|
if testCase.obj != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
valType := reflect.TypeOf(testCase.ptr).Elem()
|
||||||
|
t.Run(valType.String(), func(t *testing.T) {
|
||||||
|
for _, data := range []string{
|
||||||
|
`x`,
|
||||||
|
`n`,
|
||||||
|
`nul`,
|
||||||
|
`{x}`,
|
||||||
|
`{"x"}`,
|
||||||
|
`{"x": "y"x}`,
|
||||||
|
`{"x": "y"`,
|
||||||
|
`{"x": "y", "a"}`,
|
||||||
|
`[`,
|
||||||
|
`[{"x": "y"}`,
|
||||||
|
} {
|
||||||
|
ptrVal := reflect.New(valType)
|
||||||
|
ptr := ptrVal.Interface()
|
||||||
|
err := jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal([]byte(data), ptr)
|
||||||
|
require.Error(t, err, "on input %q", data)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user