aboutsummaryrefslogtreecommitdiff
path: root/src/encoding/json/stream_test.go
diff options
context:
space:
mode:
authorMichael Fraenkel <michael.fraenkel@gmail.com>2017-10-28 20:50:57 -0400
committerJoe Tsai <joetsai@google.com>2017-10-31 22:44:14 +0000
commitf1ce59d988f02deb47c3fd33cbc40542726ea76c (patch)
tree54c492e4c099cf9bf3b0b5a006e971bd8f5ccf34 /src/encoding/json/stream_test.go
parent6fac1398303c95698880dd16877a8692e86ee15c (diff)
downloadgo-f1ce59d988f02deb47c3fd33cbc40542726ea76c.tar.xz
encoding/json: Include the offset of a SyntaxError
When a SyntaxError occurs, report the current offset within the stream. The code already accounted for the offset within the current buffer being scanned. By including how much data was already scanned, the current offset can be computed. Fixes #22478 Change-Id: I91ecd4cad0b85a5c1556bc597f3ee914e769af01 Reviewed-on: https://go-review.googlesource.com/74251 Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com> Run-TryBot: Joe Tsai <thebrokentoaster@gmail.com> TryBot-Result: Gobot Gobot <gobot@golang.org>
Diffstat (limited to 'src/encoding/json/stream_test.go')
-rw-r--r--src/encoding/json/stream_test.go21
1 files changed, 14 insertions, 7 deletions
diff --git a/src/encoding/json/stream_test.go b/src/encoding/json/stream_test.go
index d0b3ffbce9..83c01d170c 100644
--- a/src/encoding/json/stream_test.go
+++ b/src/encoding/json/stream_test.go
@@ -342,11 +342,18 @@ var tokenStreamCases []tokenStreamCase = []tokenStreamCase{
{json: ` [{"a": 1} {"a": 2}] `, expTokens: []interface{}{
Delim('['),
decodeThis{map[string]interface{}{"a": float64(1)}},
- decodeThis{&SyntaxError{"expected comma after array element", 0}},
+ decodeThis{&SyntaxError{"expected comma after array element", 11}},
}},
- {json: `{ "a" 1 }`, expTokens: []interface{}{
- Delim('{'), "a",
- decodeThis{&SyntaxError{"expected colon after object key", 0}},
+ {json: `{ "` + strings.Repeat("a", 513) + `" 1 }`, expTokens: []interface{}{
+ Delim('{'), strings.Repeat("a", 513),
+ decodeThis{&SyntaxError{"expected colon after object key", 518}},
+ }},
+ {json: `{ "\a" }`, expTokens: []interface{}{
+ Delim('{'),
+ &SyntaxError{"invalid character 'a' in string escape code", 3},
+ }},
+ {json: ` \a`, expTokens: []interface{}{
+ &SyntaxError{"invalid character '\\\\' looking for beginning of value", 1},
}},
}
@@ -367,15 +374,15 @@ func TestDecodeInStream(t *testing.T) {
tk, err = dec.Token()
}
if experr, ok := etk.(error); ok {
- if err == nil || err.Error() != experr.Error() {
- t.Errorf("case %v: Expected error %v in %q, but was %v", ci, experr, tcase.json, err)
+ if err == nil || !reflect.DeepEqual(err, experr) {
+ t.Errorf("case %v: Expected error %#v in %q, but was %#v", ci, experr, tcase.json, err)
}
break
} else if err == io.EOF {
t.Errorf("case %v: Unexpected EOF in %q", ci, tcase.json)
break
} else if err != nil {
- t.Errorf("case %v: Unexpected error '%v' in %q", ci, err, tcase.json)
+ t.Errorf("case %v: Unexpected error '%#v' in %q", ci, err, tcase.json)
break
}
if !reflect.DeepEqual(tk, etk) {