error_test.go 730 B

123456789101112131415161718192021222324252627282930313233343536
  1. package test
  2. import (
  3. "github.com/json-iterator/go"
  4. "github.com/stretchr/testify/require"
  5. "reflect"
  6. "testing"
  7. )
  8. func Test_errorInput(t *testing.T) {
  9. for _, testCase := range unmarshalCases {
  10. if testCase.obj != nil {
  11. continue
  12. }
  13. valType := reflect.TypeOf(testCase.ptr).Elem()
  14. t.Run(valType.String(), func(t *testing.T) {
  15. for _, data := range []string{
  16. `x`,
  17. `n`,
  18. `nul`,
  19. `{x}`,
  20. `{"x"}`,
  21. `{"x": "y"x}`,
  22. `{"x": "y"`,
  23. `{"x": "y", "a"}`,
  24. `[`,
  25. `[{"x": "y"}`,
  26. } {
  27. ptrVal := reflect.New(valType)
  28. ptr := ptrVal.Interface()
  29. err := jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal([]byte(data), ptr)
  30. require.Error(t, err, "on input %q", data)
  31. }
  32. })
  33. }
  34. }