json: calculate Offset for Indent correctly

Fixes #2171

This is the real change.

R=adg
CC=golang-dev, r, rsc
https://golang.org/cl/4943041
This commit is contained in:
Jeff Hodges 2011-08-22 15:19:27 +10:00 committed by Andrew Gerrand
parent aca4293715
commit 07490c0f05
2 changed files with 26 additions and 0 deletions

View File

@ -59,6 +59,7 @@ func Indent(dst *bytes.Buffer, src []byte, prefix, indent string) os.Error {
needIndent := false
depth := 0
for _, c := range src {
scan.bytes++
v := scan.step(&scan, int(c))
if v == scanSkipSpace {
continue

View File

@ -7,7 +7,9 @@ package json
import (
"bytes"
"math"
"os"
"rand"
"reflect"
"testing"
)
@ -136,6 +138,29 @@ func TestIndentBig(t *testing.T) {
}
}
type indentErrorTest struct {
in string
err os.Error
}
var indentErrorTests = []indentErrorTest{
{`{"X": "foo", "Y"}`, &SyntaxError{"invalid character '}' after object key", 17}},
{`{"X": "foo" "Y": "bar"}`, &SyntaxError{"invalid character '\"' after object key:value pair", 13}},
}
func TestIdentErrors(t *testing.T) {
for i, tt := range indentErrorTests {
slice := make([]uint8, 0)
buf := bytes.NewBuffer(slice)
if err := Indent(buf, []uint8(tt.in), "", ""); err != nil {
if !reflect.DeepEqual(err, tt.err) {
t.Errorf("#%d: Indent: %#v", i, err)
continue
}
}
}
}
func TestNextValueBig(t *testing.T) {
initBig()
var scan scanner