Debian Codegraph @

json/golang_scanner_test.go

raw ◀ ▶
1 Debian patch touch this file
1 // Copyright 2010 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package json
6
7 import (
8 "bytes"
9 "math"
10 "math/rand"
•11 "runtime"
12 "testing"
13 )
14
15 var validTests = []struct {
16 data string
17 ok bool
18 }{
19 {`foo`, false},
20 {`}{`, false},
21 {`{]`, false},
22 {`{}`, true},
23 {`{"foo":"bar"}`, true},
24 {`{"foo":"bar","bar":{"baz":["qux"]}}`, true},
25 }
26
27 func TestValid(t *testing.T) {
28 for _, tt := range validTests {
29 if ok := Valid([]byte(tt.data)); ok != tt.ok {
30 t.Errorf("Valid(%#q) = %v, want %v", tt.data, ok, tt.ok)
31 }
32 }
33 }
34
35 // Tests of simple examples.
36
37 type example struct {
38 compact string
39 indent string
40 }
41
42 var examples = []example{
43 {`1`, `1`},
44 {`{}`, `{}`},
45 {`[]`, `[]`},
46 {`{"":2}`, "{\n\t\"\": 2\n}"},
47 {`[3]`, "[\n\t3\n]"},
48 {`[1,2,3]`, "[\n\t1,\n\t2,\n\t3\n]"},
49 {`{"x":1}`, "{\n\t\"x\": 1\n}"},
50 {ex1, ex1i},
51 {"{\"\":\"<>&\u2028\u2029\"}", "{\n\t\"\": \"<>&\u2028\u2029\"\n}"}, // See golang.org/issue/34070
52 }
53
54 var ex1 = `[true,false,null,"x",1,1.5,0,-5e+2]`
55
56 var ex1i = `[
57 true,
58 false,
59 null,
60 "x",
61 1,
62 1.5,
63 0,
64 -5e+2
65 ]`
66
67 func TestCompact(t *testing.T) {
68 var buf bytes.Buffer
69 for _, tt := range examples {
70 buf.Reset()
71 if err := Compact(&buf, []byte(tt.compact)); err != nil {
72 t.Errorf("Compact(%#q): %v", tt.compact, err)
73 } else if s := buf.String(); s != tt.compact {
74 t.Errorf("Compact(%#q) = %#q, want original", tt.compact, s)
75 }
76
77 buf.Reset()
78 if err := Compact(&buf, []byte(tt.indent)); err != nil {
79 t.Errorf("Compact(%#q): %v", tt.indent, err)
80 continue
81 } else if s := buf.String(); s != tt.compact {
82 t.Errorf("Compact(%#q) = %#q, want %#q", tt.indent, s, tt.compact)
83 }
84 }
85 }
86
87 func TestCompactSeparators(t *testing.T) {
88 // U+2028 and U+2029 should be escaped inside strings.
89 // They should not appear outside strings.
90 tests := []struct {
91 in, compact string
92 }{
93 {"{\"\u2028\": 1}", "{\"\u2028\":1}"},
94 {"{\"\u2029\" :2}", "{\"\u2029\":2}"},
95 }
96 for _, tt := range tests {
97 var buf bytes.Buffer
98 if err := Compact(&buf, []byte(tt.in)); err != nil {
99 t.Errorf("Compact(%q): %v", tt.in, err)
100 } else if s := buf.String(); s != tt.compact {
101 t.Errorf("Compact(%q) = %q, want %q", tt.in, s, tt.compact)
102 }
103 }
104 }
105
106 func TestIndent(t *testing.T) {
107 var buf bytes.Buffer
108 for _, tt := range examples {
109 buf.Reset()
110 if err := Indent(&buf, []byte(tt.indent), "", "\t"); err != nil {
111 t.Errorf("Indent(%#q): %v", tt.indent, err)
112 } else if s := buf.String(); s != tt.indent {
113 t.Errorf("Indent(%#q) = %#q, want original", tt.indent, s)
114 }
115
116 buf.Reset()
117 if err := Indent(&buf, []byte(tt.compact), "", "\t"); err != nil {
118 t.Errorf("Indent(%#q): %v", tt.compact, err)
119 continue
120 } else if s := buf.String(); s != tt.indent {
121 t.Errorf("Indent(%#q) = %#q, want %#q", tt.compact, s, tt.indent)
122 }
123 }
124 }
125
126 // Tests of a large random structure.
127
128 func TestCompactBig(t *testing.T) {
•129 if runtime.GOARCH == "s390x" {
130 t.Skipf("Test does not work correctly on %s.", runtime.GOARCH)
131 }
132 initBig()
133 var buf bytes.Buffer
134 if err := Compact(&buf, jsonBig); err != nil {
135 t.Fatalf("Compact: %v", err)
136 }
137 b := buf.Bytes()
138 if !bytes.Equal(b, jsonBig) {
139 t.Error("Compact(jsonBig) != jsonBig")
140 diff(t, b, jsonBig)
141 return
142 }
143 }
144
145 func TestIndentBig(t *testing.T) {
•146 if runtime.GOARCH == "s390x" {
147 t.Skipf("Test does not work correctly on %s.", runtime.GOARCH)
148 }
149 t.Parallel()
150 initBig()
151 var buf bytes.Buffer
152 if err := Indent(&buf, jsonBig, "", "\t"); err != nil {
153 t.Fatalf("Indent1: %v", err)
154 }
155 b := buf.Bytes()
156 if len(b) == len(jsonBig) {
157 // jsonBig is compact (no unnecessary spaces);
158 // indenting should make it bigger
159 t.Fatalf("Indent(jsonBig) did not get bigger")
160 }
161
162 // should be idempotent
163 var buf1 bytes.Buffer
164 if err := Indent(&buf1, b, "", "\t"); err != nil {
165 t.Fatalf("Indent2: %v", err)
166 }
167 b1 := buf1.Bytes()
168 if !bytes.Equal(b1, b) {
169 t.Error("Indent(Indent(jsonBig)) != Indent(jsonBig)")
170 diff(t, b1, b)
171 return
172 }
173
174 // should get back to original
175 buf1.Reset()
176 if err := Compact(&buf1, b); err != nil {
177 t.Fatalf("Compact: %v", err)
178 }
179 b1 = buf1.Bytes()
180 if !bytes.Equal(b1, jsonBig) {
181 t.Error("Compact(Indent(jsonBig)) != jsonBig")
182 diff(t, b1, jsonBig)
183 return
184 }
185 }
186
187 type indentErrorTest struct {
188 in string
189 err error
190 }
191
192 var indentErrorTests = []indentErrorTest{
193 {`{"X": "foo", "Y"}`, &testSyntaxError{"invalid character '}' after object key", 17}},
194 {`{"X": "foo" "Y": "bar"}`, &testSyntaxError{"invalid character '\"' after object key:value pair", 13}},
195 }
196
197 func TestIndentErrors(t *testing.T) {
198 for i, tt := range indentErrorTests {
199 slice := make([]uint8, 0)
200 buf := bytes.NewBuffer(slice)
201 err := Indent(buf, []uint8(tt.in), "", "")
202 assertErrorPresence(t, tt.err, err, i)
203 }
204 }
205
206 func diff(t *testing.T, a, b []byte) {
207 for i := 0; ; i++ {
208 if i >= len(a) || i >= len(b) || a[i] != b[i] {
209 j := max(0, i-10)
210 t.Errorf("diverge at %d: «%s» vs «%s»", i, trim(a[j:]), trim(b[j:]))
211 return
212 }
213 }
214 }
215
216 func trim(b []byte) []byte {
217 if len(b) > 20 {
218 return b[0:20]
219 }
220 return b
221 }
222
223 // Generate a random JSON object.
224
225 var jsonBig []byte
226
227 func initBig() {
228 n := 10000
229 if testing.Short() {
230 n = 100
231 }
232 b, err := Marshal(genValue(n))
233 if err != nil {
234 panic(err)
235 }
236 jsonBig = b
237 }
238
239 func genValue(n int) any {
240 if n > 1 {
241 switch rand.Intn(2) {
242 case 0:
243 return genArray(n)
244 case 1:
245 return genMap(n)
246 }
247 }
248 switch rand.Intn(3) {
249 case 0:
250 return rand.Intn(2) == 0
251 case 1:
252 return rand.NormFloat64()
253 case 2:
254 return genString(30)
255 }
256 panic("unreachable")
257 }
258
259 func genString(stddev float64) string {
260 n := int(math.Abs(rand.NormFloat64()*stddev + stddev/2))
261 c := make([]rune, n)
262 for i := range c {
263 f := math.Abs(rand.NormFloat64()*64 + 32)
264 if f > 0x10ffff {
265 f = 0x10ffff
266 }
267 c[i] = rune(f)
268 }
269 return string(c)
270 }
271
272 func genArray(n int) []any {
273 f := int(math.Abs(rand.NormFloat64()) * math.Min(10, float64(n/2)))
274 if f > n {
275 f = n
276 }
277 if f < 1 {
278 f = 1
279 }
280 x := make([]any, f)
281 for i := range x {
282 x[i] = genValue(((i+1)*n)/f - (i*n)/f)
283 }
284 return x
285 }
286
287 func genMap(n int) map[string]any {
288 f := int(math.Abs(rand.NormFloat64()) * math.Min(10, float64(n/2)))
289 f = min(f, n)
290 if n > 0 && f == 0 {
291 f = 1
292 }
293 x := make(map[string]any)
294 for i := range f {
295 x[genString(10)] = genValue(((i+1)*n)/f - (i*n)/f)
296 }
297 return x
298 }