Debian Codegraph @

stream_test.go

raw symbols SCIP
1 // Copyright 2016 Richard Gibson. All rights reserved.
2 // Copyright 2010 The Go Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style
4 // license that can be found in the LICENSE file.
5
6 package canonicaljson
7
8 import (
9 "bytes"
10 "encoding/json"
11 "io/ioutil"
12 "testing"
13 )
14
15 // Test values for the stream test.
16 // One of each JSON kind.
17 var streamTest = []interface{}{
18 0.1,
19 "hello",
20 nil,
21 true,
22 false,
23 []interface{}{"a", "b", "c"},
24 map[string]interface{}{"K": "Kelvin", "ß": "long s"},
25 3.14, // another value to make sure something can follow map
26 }
27
28 var streamEncoded = `1.0E-1
29 "hello"
30 null
31 true
32 false
33 ["a","b","c"]
34 {"ß":"long s","K":"Kelvin"}
35 3.14E0
36 `
37
38 func TestEncoder(t *testing.T) {
39 for i := 0; i <= len(streamTest); i++ {
40 var buf bytes.Buffer
41 enc := NewEncoder(&buf)
42 for j, v := range streamTest[0:i] {
43 if err := enc.Encode(v); err != nil {
44 t.Fatalf("encode #%d: %v", j, err)
45 }
46 }
47 if have, want := buf.String(), nlines(streamEncoded, i); have != want {
48 t.Errorf("encoding %d items: mismatch", i)
49 diff(t, []byte(have), []byte(want))
50 break
51 }
52 }
53 }
54
55 func nlines(s string, n int) string {
56 if n <= 0 {
57 return ""
58 }
59 for i, c := range s {
60 if c == '\n' {
61 if n--; n == 0 {
62 return s[0 : i+1]
63 }
64 }
65 }
66 return s
67 }
68
69 func TestRawMessage(t *testing.T) {
70 // TODO(rsc): Should not need the * in *RawMessage
71 var data struct {
72 X float64
73 Id *RawMessage
74 Y float32
75 }
76 const raw = `["\u0056",null]`
77 const msg = `{"Id":["V",null],"X":1.0E-1,"Y":2.0E-1}`
78 data.X = 0.1
79 data.Y = 0.2
80 id := RawMessage(raw)
81 data.Id = &id
82 b, err := Marshal(&data)
83 if err != nil {
84 t.Fatalf("Marshal: %v", err)
85 }
86 if string(b) != msg {
87 t.Fatalf("Marshal: have %#q want %#q", b, msg)
88 }
89 }
90
91 func TestNullRawMessage(t *testing.T) {
92 // TODO(rsc): Should not need the * in *RawMessage
93 var data struct {
94 X float64
95 Id *RawMessage
96 Y float32
97 }
98 data.Id = new(RawMessage)
99 const msg = `{"Id":null,"X":1.0E-1,"Y":2.0E-1}`
100 err := json.Unmarshal([]byte(msg), &data)
101 if err != nil {
102 t.Fatalf("Unmarshal: %v", err)
103 }
104 if data.Id != nil {
105 t.Fatalf("Raw mismatch: have non-nil, want nil")
106 }
107 b, err := Marshal(&data)
108 if err != nil {
109 t.Fatalf("Marshal: %v", err)
110 }
111 if string(b) != msg {
112 t.Fatalf("Marshal: have %#q want %#q", b, msg)
113 }
114 }
115
116 var blockingTests = []string{
117 `{"x": 1}`,
118 `[1, 2, 3]`,
119 }
120
121 func BenchmarkEncoderEncode(b *testing.B) {
122 b.ReportAllocs()
123 type T struct {
124 X, Y string
125 }
126 v := &T{"foo", "bar"}
127 for i := 0; i < b.N; i++ {
128 if err := NewEncoder(ioutil.Discard).Encode(v); err != nil {
129 b.Fatal(err)
130 }
131 }
132 }
133
134 type tokenStreamCase struct {
135 json string
136 expTokens []interface{}
137 }
138
139 type decodeThis struct {
140 v interface{}
141 }
142
143 var tokenStreamCases []tokenStreamCase = []tokenStreamCase{
144 // streaming token cases
145 {json: `10`, expTokens: []interface{}{float64(10)}},
146 {json: ` [10] `, expTokens: []interface{}{
147 Delim('['), float64(10), Delim(']')}},
148 {json: ` [false,10,"b"] `, expTokens: []interface{}{
149 Delim('['), false, float64(10), "b", Delim(']')}},
150 {json: `{ "a": 1 }`, expTokens: []interface{}{
151 Delim('{'), "a", float64(1), Delim('}')}},
152 {json: `{"a": 1, "b":"3"}`, expTokens: []interface{}{
153 Delim('{'), "a", float64(1), "b", "3", Delim('}')}},
154 {json: ` [{"a": 1},{"a": 2}] `, expTokens: []interface{}{
155 Delim('['),
156 Delim('{'), "a", float64(1), Delim('}'),
157 Delim('{'), "a", float64(2), Delim('}'),
158 Delim(']')}},
159 {json: `{"obj": {"a": 1}}`, expTokens: []interface{}{
160 Delim('{'), "obj", Delim('{'), "a", float64(1), Delim('}'),
161 Delim('}')}},
162 {json: `{"obj": [{"a": 1}]}`, expTokens: []interface{}{
163 Delim('{'), "obj", Delim('['),
164 Delim('{'), "a", float64(1), Delim('}'),
165 Delim(']'), Delim('}')}},
166
167 // streaming tokens with intermittent Decode()
168 {json: `{ "a": 1 }`, expTokens: []interface{}{
169 Delim('{'), "a",
170 decodeThis{float64(1)},
171 Delim('}')}},
172 {json: ` [ { "a" : 1 } ] `, expTokens: []interface{}{
173 Delim('['),
174 decodeThis{map[string]interface{}{"a": float64(1)}},
175 Delim(']')}},
176 {json: ` [{"a": 1},{"a": 2}] `, expTokens: []interface{}{
177 Delim('['),
178 decodeThis{map[string]interface{}{"a": float64(1)}},
179 decodeThis{map[string]interface{}{"a": float64(2)}},
180 Delim(']')}},
181 {json: `{ "obj" : [ { "a" : 1 } ] }`, expTokens: []interface{}{
182 Delim('{'), "obj", Delim('['),
183 decodeThis{map[string]interface{}{"a": float64(1)}},
184 Delim(']'), Delim('}')}},
185
186 {json: `{"obj": {"a": 1}}`, expTokens: []interface{}{
187 Delim('{'), "obj",
188 decodeThis{map[string]interface{}{"a": float64(1)}},
189 Delim('}')}},
190 {json: `{"obj": [{"a": 1}]}`, expTokens: []interface{}{
191 Delim('{'), "obj",
192 decodeThis{[]interface{}{
193 map[string]interface{}{"a": float64(1)},
194 }},
195 Delim('}')}},
196 {json: ` [{"a": 1} {"a": 2}] `, expTokens: []interface{}{
197 Delim('['),
198 decodeThis{map[string]interface{}{"a": float64(1)}},
199 decodeThis{&SyntaxError{"expected comma after array element", 0}},
200 }},
201 {json: `{ "a" 1 }`, expTokens: []interface{}{
202 Delim('{'), "a",
203 decodeThis{&SyntaxError{"expected colon after object key", 0}},
204 }},
205 }
206
207 func diff(t *testing.T, a, b []byte) {
208 for i := 0; ; i++ {
209 if i >= len(a) || i >= len(b) || a[i] != b[i] {
210 j := i - 10
211 if j < 0 {
212 j = 0
213 }
214 t.Errorf("diverge at %d: «%s» vs «%s»", i, trim(a[j:]), trim(b[j:]))
215 return
216 }
217 }
218 }
219
220 func trim(b []byte) []byte {
221 if len(b) > 20 {
222 return b[0:20]
223 }
224 return b
225 }