...
1 package json
2
3 import (
4 "bytes"
5 "testing"
6 )
7
8 func TestObject(t *testing.T) {
9 buffer := bytes.NewBuffer(nil)
10 scratch := make([]byte, 64)
11
12 object := newObject(buffer, &scratch)
13 object.Key("foo").String("bar")
14 object.Key("faz").String("baz")
15 object.Close()
16
17 e := []byte(`{"foo":"bar","faz":"baz"}`)
18 if a := buffer.Bytes(); bytes.Compare(e, a) != 0 {
19 t.Errorf("expected %+q, but got %+q", e, a)
20 }
21 }
22
23 func TestObjectKey_escaped(t *testing.T) {
24 jsonEncoder := NewEncoder()
25 object := jsonEncoder.Object()
26 object.Key("foo\"").String("bar")
27 object.Key("faz").String("baz")
28 object.Close()
29
30 e := []byte(`{"foo\"":"bar","faz":"baz"}`)
31 if a := object.w.Bytes(); bytes.Compare(e, a) != 0 {
32 t.Errorf("expected %+q, but got %+q", e, a)
33 }
34 }
35
View as plain text