aboutsummaryrefslogtreecommitdiff
path: root/src/encoding/json/stream_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'src/encoding/json/stream_test.go')
-rw-r--r--src/encoding/json/stream_test.go64
1 files changed, 64 insertions, 0 deletions
diff --git a/src/encoding/json/stream_test.go b/src/encoding/json/stream_test.go
index 9e5d48d39d..0e937cfaa1 100644
--- a/src/encoding/json/stream_test.go
+++ b/src/encoding/json/stream_test.go
@@ -557,3 +557,67 @@ func TestTokenTruncation(t *testing.T) {
}
}
}
+
+func TestDecoderInputOffset(t *testing.T) {
+ const input = ` [
+ [ ] , [ "one" ] , [ "one" , "two" ] ,
+ { } , { "alpha" : "bravo" } , { "alpha" : "bravo" , "fizz" : "buzz" }
+ ] `
+ wantOffsets := []int64{
+ 0, 1, 2, 5, 6, 7, 8, 9, 12, 13, 18, 19, 20, 21, 24, 25, 30, 31,
+ 38, 39, 40, 41, 46, 47, 48, 49, 52, 53, 60, 61, 70, 71, 72, 73,
+ 76, 77, 84, 85, 94, 95, 103, 104, 112, 113, 114, 116, 117, 117,
+ 117, 117,
+ }
+ wantMores := []bool{
+ true, true, false, true, true, false, true, true, true, false,
+ true, false, true, true, true, false, true, true, true, true,
+ true, false, false, false, false,
+ }
+
+ d := NewDecoder(strings.NewReader(input))
+ checkOffset := func() {
+ t.Helper()
+ got := d.InputOffset()
+ if len(wantOffsets) == 0 {
+ t.Fatalf("InputOffset = %d, want nil", got)
+ }
+ want := wantOffsets[0]
+ if got != want {
+ t.Fatalf("InputOffset = %d, want %d", got, want)
+ }
+ wantOffsets = wantOffsets[1:]
+ }
+ checkMore := func() {
+ t.Helper()
+ got := d.More()
+ if len(wantMores) == 0 {
+ t.Fatalf("More = %v, want nil", got)
+ }
+ want := wantMores[0]
+ if got != want {
+ t.Fatalf("More = %v, want %v", got, want)
+ }
+ wantMores = wantMores[1:]
+ }
+ checkOffset()
+ checkMore()
+ checkOffset()
+ for {
+ if _, err := d.Token(); err == io.EOF {
+ break
+ } else if err != nil {
+ t.Fatalf("Token error: %v", err)
+ }
+ checkOffset()
+ checkMore()
+ checkOffset()
+ }
+ checkOffset()
+ checkMore()
+ checkOffset()
+
+ if len(wantOffsets)+len(wantMores) > 0 {
+ t.Fatal("unconsumed testdata")
+ }
+}