mirror of
https://github.com/json-iterator/go.git
synced 2025-04-20 11:28:49 +02:00
Add a test for input errors
Send various malformed JSON strings into the decoder for each type, and check we get an error each time.
This commit is contained in:
parent
4930b053b8
commit
8f27a81d90
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