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