summaryrefslogtreecommitdiff
path: root/methods_test.go
diff options
context:
space:
mode:
authorLuke Shumaker <lukeshu@lukeshu.com>2023-01-30 12:31:42 -0700
committerLuke Shumaker <lukeshu@lukeshu.com>2023-01-30 14:41:30 -0700
commitc24b34a47359ffb012b85e329f829b64d9d27215 (patch)
tree0a7acb802d5c9e3cf3ccde658b07f1034f680f52 /methods_test.go
parent8b7c8d2f87f9c4d924d070926fb5ab9860d00c61 (diff)
decode: Fix DecodeTypeError offsets
Diffstat (limited to 'methods_test.go')
-rw-r--r--methods_test.go87
1 files changed, 87 insertions, 0 deletions
diff --git a/methods_test.go b/methods_test.go
index 46e2601..f5d5a9a 100644
--- a/methods_test.go
+++ b/methods_test.go
@@ -232,3 +232,90 @@ func TestMethodsEncode(t *testing.T) {
}
})
}
+
+type tstDecoder struct {
+ n int
+ err string
+}
+
+func (d *tstDecoder) DecodeJSON(r io.RuneScanner) error {
+ for i := 0; i < d.n; i++ {
+ if _, _, err := r.ReadRune(); err != nil {
+ if err == io.EOF {
+ break
+ }
+ return err
+ }
+ }
+ if len(d.err) > 0 {
+ return errors.New(d.err)
+ }
+ return nil
+}
+
+type strUnmarshaler struct {
+ err string
+}
+
+func (u *strUnmarshaler) UnmarshalJSON([]byte) error {
+ if u.err == "" {
+ return nil
+ }
+ return errors.New(u.err)
+}
+
+type textUnmarshaler struct {
+ err string
+}
+
+func (u *textUnmarshaler) UnmarshalText([]byte) error {
+ if u.err == "" {
+ return nil
+ }
+ return errors.New(u.err)
+}
+
+type errTextUnmarshaler struct {
+ S string
+}
+
+func (u *errTextUnmarshaler) UnmarshalText(dat []byte) error {
+ u.S = string(dat)
+ return errors.New("eee")
+}
+
+func TestMethodsDecode(t *testing.T) {
+ t.Parallel()
+ type testcase struct {
+ In string
+ Obj any
+ ExpectedErr string
+ }
+ testcases := map[string]testcase{
+ "decode-basic": {In: `{}`, Obj: &tstDecoder{n: 2}},
+ "decode-basic-eof": {In: `{}`, Obj: &tstDecoder{n: 5}},
+ "decode-syntax-error": {In: `{x}`, Obj: &tstDecoder{n: 5}, ExpectedErr: `json: v: syntax error at input byte 1: object: unexpected character: 'x'`},
+ "unmarshal-syntax-error": {In: `{x}`, Obj: &strUnmarshaler{}, ExpectedErr: `json: v: syntax error at input byte 1: object: unexpected character: 'x'`},
+ "decode-short": {In: `{}`, Obj: &tstDecoder{n: 1}, ExpectedErr: `json: v: cannot decode JSON object at input byte 0 into Go *lowmemjson_test.tstDecoder: did not consume entire object`},
+ "decode-err": {In: `{}`, Obj: &tstDecoder{err: "xxx"}, ExpectedErr: `json: v: cannot decode JSON object at input byte 0 into Go *lowmemjson_test.tstDecoder: xxx`},
+ "decode-err2": {In: `{}`, Obj: &tstDecoder{n: 1, err: "yyy"}, ExpectedErr: `json: v: cannot decode JSON object at input byte 0 into Go *lowmemjson_test.tstDecoder: yyy`},
+ "unmarshal-err": {In: `{}`, Obj: &strUnmarshaler{err: "zzz"}, ExpectedErr: `json: v: cannot decode JSON object at input byte 0 into Go *lowmemjson_test.strUnmarshaler: zzz`},
+ "unmarshaltext": {In: `""`, Obj: &textUnmarshaler{}},
+ "unmarshaltext-nonstr": {In: `{}`, Obj: &textUnmarshaler{}, ExpectedErr: `json: v: cannot decode JSON object at input byte 0 into Go *lowmemjson_test.textUnmarshaler`},
+ "unmarshaltext-err": {In: `""`, Obj: &textUnmarshaler{err: "zzz"}, ExpectedErr: `json: v: cannot decode JSON string at input byte 0 into Go *lowmemjson_test.textUnmarshaler: zzz`},
+ "unmarshaltext-mapkey": {In: `{"a":1}`, Obj: new(map[errTextUnmarshaler]int), ExpectedErr: `json: v: cannot decode JSON string at input byte 1 into Go *lowmemjson_test.errTextUnmarshaler: eee`},
+ }
+ for tcName, tc := range testcases {
+ tc := tc
+ t.Run(tcName, func(t *testing.T) {
+ t.Parallel()
+ obj := tc.Obj
+ err := lowmemjson.NewDecoder(strings.NewReader(tc.In)).Decode(&obj)
+ if tc.ExpectedErr == "" {
+ assert.NoError(t, err)
+ } else {
+ assert.EqualError(t, err, tc.ExpectedErr)
+ }
+ })
+ }
+}