diff options
author | Andrew Gerrand <adg@golang.org> | 2010-03-17 15:41:16 +1100 |
---|---|---|
committer | Andrew Gerrand <adg@golang.org> | 2010-03-17 15:41:16 +1100 |
commit | 772abc4b04f8a421d983f0571d55bb92a23a3c0c (patch) | |
tree | 53ac09b2d59448188c8ab830472839811983cb7b /src/pkg/json/struct_test.go | |
parent | 9ae1e9707533289470c48e93a2ecf1eb44e29b47 (diff) | |
download | golang-772abc4b04f8a421d983f0571d55bb92a23a3c0c.tar.gz |
json: add MarshalIndent (accepts user-specified indent string)
Fixes issue 661
R=r, rsc, skorobo
CC=golang-dev
http://codereview.appspot.com/576042
Diffstat (limited to 'src/pkg/json/struct_test.go')
-rw-r--r-- | src/pkg/json/struct_test.go | 93 |
1 files changed, 92 insertions, 1 deletions
diff --git a/src/pkg/json/struct_test.go b/src/pkg/json/struct_test.go index 66d6e79c2..d8528f280 100644 --- a/src/pkg/json/struct_test.go +++ b/src/pkg/json/struct_test.go @@ -246,7 +246,98 @@ func TestMarshal(t *testing.T) { s := buf.String() if s != tt.out { - t.Errorf("Marshal(%T) = %q, want %q\n", tt.val, tt.out, s) + t.Errorf("Marshal(%T) = %q, want %q\n", tt.val, s, tt.out) + } + } +} + +type marshalIndentTest struct { + val interface{} + indent string + out string +} + +const marshalIndentTest1 = `[ + 1, + 2, + 3, + 4 +] +` +const marshalIndentTest2 = `[ +[ +1, +2 +], +[ +3, +4 +] +] +` +const marshalIndentTest3 = `[ + [ + 1, + 2 + ], + [ + 3, + 4 + ] +] +` +const marshalIndentTest4 = `[ + [ + 1, + 2 + ], + [ + 3, + 4 + ] +] +` +const marshalIndentTest5 = `{ + "a":1, + "b":"hello" +} +` +const marshalIndentTest6 = `{ + "3":[ + 1, + 2, + 3 + ] +} +` + +var marshalIndentTests = []marshalIndentTest{ + marshalIndentTest{[]int{1, 2, 3, 4}, " ", marshalIndentTest1}, + marshalIndentTest{[][]int{[]int{1, 2}, []int{3, 4}}, "", marshalIndentTest2}, + marshalIndentTest{[][]int{[]int{1, 2}, []int{3, 4}}, " ", marshalIndentTest3}, + marshalIndentTest{[][]int{[]int{1, 2}, []int{3, 4}}, " ", marshalIndentTest4}, + marshalIndentTest{struct { + a int + b string + }{1, "hello"}, + " ", + marshalIndentTest5, + }, + marshalIndentTest{map[string][]int{"3": []int{1, 2, 3}}, " ", marshalIndentTest6}, +} + +func TestMarshalIndent(t *testing.T) { + for _, tt := range marshalIndentTests { + var buf bytes.Buffer + + err := MarshalIndent(&buf, tt.val, tt.indent) + if err != nil { + t.Fatalf("MarshalIndent(%v): %s", tt.val, err) + } + + s := buf.String() + if s != tt.out { + t.Errorf("MarshalIndent(%v) = %q, want %q\n", tt.val, s, tt.out) } } } |