Debian Codegraph @

pkg/internal/third_party/go-json-experiment/json/jsontext/encode_test.go

raw
1 // Copyright 2020 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 jsontext
6
7 import (
8 "bytes"
9 "errors"
10 "io"
11 "path"
12 "slices"
13 "testing"
14
15 "k8s.io/kube-openapi/pkg/internal/third_party/go-json-experiment/json/internal/jsonflags"
16 "k8s.io/kube-openapi/pkg/internal/third_party/go-json-experiment/json/internal/jsontest"
17 "k8s.io/kube-openapi/pkg/internal/third_party/go-json-experiment/json/internal/jsonwire"
18 )
19
20 // TestEncoder tests whether we can produce JSON with either tokens or raw values.
21 func TestEncoder(t *testing.T) {
22 for _, td := range coderTestdata {
23 for _, formatName := range []string{"Compact", "Indented"} {
24 for _, typeName := range []string{"Token", "Value", "TokenDelims"} {
25 t.Run(path.Join(td.name.Name, typeName, formatName), func(t *testing.T) {
26 testEncoder(t, td.name.Where, formatName, typeName, td)
27 })
28 }
29 }
30 }
31 }
32 func testEncoder(t *testing.T, where jsontest.CasePos, formatName, typeName string, td coderTestdataEntry) {
33 var want string
34 var opts []Options
35 dst := new(bytes.Buffer)
36 opts = append(opts, jsonflags.OmitTopLevelNewline|1)
37 want = td.outCompacted
38 switch formatName {
39 case "Indented":
40 opts = append(opts, Multiline(true))
41 opts = append(opts, WithIndentPrefix("\t"))
42 opts = append(opts, WithIndent(" "))
43 if td.outIndented != "" {
44 want = td.outIndented
45 }
46 }
47 enc := NewEncoder(dst, opts...)
48
49 switch typeName {
50 case "Token":
51 var pointers []Pointer
52 for _, tok := range td.tokens {
53 if err := enc.WriteToken(tok); err != nil {
54 t.Fatalf("%s: Encoder.WriteToken error: %v", where, err)
55 }
56 if td.pointers != nil {
57 pointers = append(pointers, enc.StackPointer())
58 }
59 }
60 if !slices.Equal(pointers, td.pointers) {
61 t.Fatalf("%s: pointers mismatch:\ngot %q\nwant %q", where, pointers, td.pointers)
62 }
63 case "Value":
64 if err := enc.WriteValue(Value(td.in)); err != nil {
65 t.Fatalf("%s: Encoder.WriteValue error: %v", where, err)
66 }
67 case "TokenDelims":
68 // Use WriteToken for object/array delimiters, WriteValue otherwise.
69 for _, tok := range td.tokens {
70 switch tok.Kind() {
71 case '{', '}', '[', ']':
72 if err := enc.WriteToken(tok); err != nil {
73 t.Fatalf("%s: Encoder.WriteToken error: %v", where, err)
74 }
75 default:
76 val := Value(tok.String())
77 if tok.Kind() == '"' {
78 val, _ = jsonwire.AppendQuote(nil, tok.String(), &jsonflags.Flags{})
79 }
80 if err := enc.WriteValue(val); err != nil {
81 t.Fatalf("%s: Encoder.WriteValue error: %v", where, err)
82 }
83 }
84 }
85 }
86
87 got := dst.String()
88 if got != want {
89 t.Errorf("%s: output mismatch:\ngot %q\nwant %q", where, got, want)
90 }
91 }
92
93 // TestFaultyEncoder tests that temporary I/O errors are not fatal.
94 func TestFaultyEncoder(t *testing.T) {
95 for _, td := range coderTestdata {
96 for _, typeName := range []string{"Token", "Value"} {
97 t.Run(path.Join(td.name.Name, typeName), func(t *testing.T) {
98 testFaultyEncoder(t, td.name.Where, typeName, td)
99 })
100 }
101 }
102 }
103 func testFaultyEncoder(t *testing.T, where jsontest.CasePos, typeName string, td coderTestdataEntry) {
104 b := &FaultyBuffer{
105 MaxBytes: 1,
106 MayError: io.ErrShortWrite,
107 }
108
109 // Write all the tokens.
110 // Even if the underlying io.Writer may be faulty,
111 // writing a valid token or value is guaranteed to at least
112 // be appended to the internal buffer.
113 // In other words, syntactic errors occur before I/O errors.
114 enc := NewEncoder(b)
115 switch typeName {
116 case "Token":
117 for i, tok := range td.tokens {
118 err := enc.WriteToken(tok)
119 if err != nil && !errors.Is(err, io.ErrShortWrite) {
120 t.Fatalf("%s: %d: Encoder.WriteToken error: %v", where, i, err)
121 }
122 }
123 case "Value":
124 err := enc.WriteValue(Value(td.in))
125 if err != nil && !errors.Is(err, io.ErrShortWrite) {
126 t.Fatalf("%s: Encoder.WriteValue error: %v", where, err)
127 }
128 }
129 gotOutput := string(append(b.B, enc.s.unflushedBuffer()...))
130 wantOutput := td.outCompacted + "\n"
131 if gotOutput != wantOutput {
132 t.Fatalf("%s: output mismatch:\ngot %s\nwant %s", where, gotOutput, wantOutput)
133 }
134 }
135
136 type encoderMethodCall struct {
137 in tokOrVal
138 wantErr error
139 wantPointer Pointer
140 }
141
142 var encoderErrorTestdata = []struct {
143 name jsontest.CaseName
144 opts []Options
145 calls []encoderMethodCall
146 wantOut string
147 }{{
148 name: jsontest.Name("InvalidToken"),
149 calls: []encoderMethodCall{
150 {zeroToken, E(errInvalidToken), ""},
151 },
152 }, {
153 name: jsontest.Name("InvalidValue"),
154 calls: []encoderMethodCall{
155 {Value(`#`), newInvalidCharacterError("#", "at start of value"), ""},
156 },
157 }, {
158 name: jsontest.Name("InvalidValue/DoubleZero"),
159 calls: []encoderMethodCall{
160 {Value(`00`), newInvalidCharacterError("0", "after top-level value").withPos(`0`, ""), ""},
161 },
162 }, {
163 name: jsontest.Name("TruncatedValue"),
164 calls: []encoderMethodCall{
165 {zeroValue, E(io.ErrUnexpectedEOF).withPos("", ""), ""},
166 },
167 }, {
168 name: jsontest.Name("TruncatedNull"),
169 calls: []encoderMethodCall{
170 {Value(`nul`), E(io.ErrUnexpectedEOF).withPos("nul", ""), ""},
171 },
172 }, {
173 name: jsontest.Name("InvalidNull"),
174 calls: []encoderMethodCall{
175 {Value(`nulL`), newInvalidCharacterError("L", "in literal null (expecting 'l')").withPos(`nul`, ""), ""},
176 },
177 }, {
178 name: jsontest.Name("TruncatedFalse"),
179 calls: []encoderMethodCall{
180 {Value(`fals`), E(io.ErrUnexpectedEOF).withPos("fals", ""), ""},
181 },
182 }, {
183 name: jsontest.Name("InvalidFalse"),
184 calls: []encoderMethodCall{
185 {Value(`falsE`), newInvalidCharacterError("E", "in literal false (expecting 'e')").withPos(`fals`, ""), ""},
186 },
187 }, {
188 name: jsontest.Name("TruncatedTrue"),
189 calls: []encoderMethodCall{
190 {Value(`tru`), E(io.ErrUnexpectedEOF).withPos(`tru`, ""), ""},
191 },
192 }, {
193 name: jsontest.Name("InvalidTrue"),
194 calls: []encoderMethodCall{
195 {Value(`truE`), newInvalidCharacterError("E", "in literal true (expecting 'e')").withPos(`tru`, ""), ""},
196 },
197 }, {
198 name: jsontest.Name("TruncatedString"),
199 calls: []encoderMethodCall{
200 {Value(`"star`), E(io.ErrUnexpectedEOF).withPos(`"star`, ""), ""},
201 },
202 }, {
203 name: jsontest.Name("InvalidString"),
204 calls: []encoderMethodCall{
205 {Value(`"ok` + "\x00"), newInvalidCharacterError("\x00", `in string (expecting non-control character)`).withPos(`"ok`, ""), ""},
206 },
207 }, {
208 name: jsontest.Name("ValidString/AllowInvalidUTF8/Token"),
209 opts: []Options{AllowInvalidUTF8(true)},
210 calls: []encoderMethodCall{
211 {String("living\xde\xad\xbe\xef"), nil, ""},
212 },
213 wantOut: "\"living\xde\xad\ufffd\ufffd\"\n",
214 }, {
215 name: jsontest.Name("ValidString/AllowInvalidUTF8/Value"),
216 opts: []Options{AllowInvalidUTF8(true)},
217 calls: []encoderMethodCall{
218 {Value("\"living\xde\xad\xbe\xef\""), nil, ""},
219 },
220 wantOut: "\"living\xde\xad\ufffd\ufffd\"\n",
221 }, {
222 name: jsontest.Name("InvalidString/RejectInvalidUTF8"),
223 opts: []Options{AllowInvalidUTF8(false)},
224 calls: []encoderMethodCall{
225 {String("living\xde\xad\xbe\xef"), E(jsonwire.ErrInvalidUTF8), ""},
226 {Value("\"living\xde\xad\xbe\xef\""), E(jsonwire.ErrInvalidUTF8).withPos("\"living\xde\xad", ""), ""},
227 {BeginObject, nil, ""},
228 {String("name"), nil, ""},
229 {BeginArray, nil, ""},
230 {String("living\xde\xad\xbe\xef"), E(jsonwire.ErrInvalidUTF8).withPos(`{"name":[`, "/name/0"), ""},
231 {Value("\"living\xde\xad\xbe\xef\""), E(jsonwire.ErrInvalidUTF8).withPos("{\"name\":[\"living\xde\xad", "/name/0"), ""},
232 },
233 wantOut: `{"name":[`,
234 }, {
235 name: jsontest.Name("TruncatedNumber"),
236 calls: []encoderMethodCall{
237 {Value(`0.`), E(io.ErrUnexpectedEOF).withPos("0", ""), ""},
238 },
239 }, {
240 name: jsontest.Name("InvalidNumber"),
241 calls: []encoderMethodCall{
242 {Value(`0.e`), newInvalidCharacterError("e", "in number (expecting digit)").withPos(`0.`, ""), ""},
243 },
244 }, {
245 name: jsontest.Name("TruncatedObject/AfterStart"),
246 calls: []encoderMethodCall{
247 {Value(`{`), E(io.ErrUnexpectedEOF).withPos("{", ""), ""},
248 },
249 }, {
250 name: jsontest.Name("TruncatedObject/AfterName"),
251 calls: []encoderMethodCall{
252 {Value(`{"X"`), E(io.ErrUnexpectedEOF).withPos(`{"X"`, "/X"), ""},
253 },
254 }, {
255 name: jsontest.Name("TruncatedObject/AfterColon"),
256 calls: []encoderMethodCall{
257 {Value(`{"X":`), E(io.ErrUnexpectedEOF).withPos(`{"X":`, "/X"), ""},
258 },
259 }, {
260 name: jsontest.Name("TruncatedObject/AfterValue"),
261 calls: []encoderMethodCall{
262 {Value(`{"0":0`), E(io.ErrUnexpectedEOF).withPos(`{"0":0`, ""), ""},
263 },
264 }, {
265 name: jsontest.Name("TruncatedObject/AfterComma"),
266 calls: []encoderMethodCall{
267 {Value(`{"0":0,`), E(io.ErrUnexpectedEOF).withPos(`{"0":0,`, ""), ""},
268 },
269 }, {
270 name: jsontest.Name("InvalidObject/MissingColon"),
271 calls: []encoderMethodCall{
272 {Value(` { "fizz" "buzz" } `), newInvalidCharacterError("\"", "after object name (expecting ':')").withPos(` { "fizz" `, "/fizz"), ""},
273 {Value(` { "fizz" , "buzz" } `), newInvalidCharacterError(",", "after object name (expecting ':')").withPos(` { "fizz" `, "/fizz"), ""},
274 },
275 }, {
276 name: jsontest.Name("InvalidObject/MissingComma"),
277 calls: []encoderMethodCall{
278 {Value(` { "fizz" : "buzz" "gazz" } `), newInvalidCharacterError("\"", "after object value (expecting ',' or '}')").withPos(` { "fizz" : "buzz" `, ""), ""},
279 {Value(` { "fizz" : "buzz" : "gazz" } `), newInvalidCharacterError(":", "after object value (expecting ',' or '}')").withPos(` { "fizz" : "buzz" `, ""), ""},
280 },
281 }, {
282 name: jsontest.Name("InvalidObject/ExtraComma"),
283 calls: []encoderMethodCall{
284 {Value(` { , } `), newInvalidCharacterError(",", `at start of string (expecting '"')`).withPos(` { `, ""), ""},
285 {Value(` { "fizz" : "buzz" , } `), newInvalidCharacterError("}", `at start of string (expecting '"')`).withPos(` { "fizz" : "buzz" , `, ""), ""},
286 },
287 }, {
288 name: jsontest.Name("InvalidObject/InvalidName"),
289 calls: []encoderMethodCall{
290 {Value(`{ null }`), newInvalidCharacterError("n", `at start of string (expecting '"')`).withPos(`{ `, ""), ""},
291 {Value(`{ false }`), newInvalidCharacterError("f", `at start of string (expecting '"')`).withPos(`{ `, ""), ""},
292 {Value(`{ true }`), newInvalidCharacterError("t", `at start of string (expecting '"')`).withPos(`{ `, ""), ""},
293 {Value(`{ 0 }`), newInvalidCharacterError("0", `at start of string (expecting '"')`).withPos(`{ `, ""), ""},
294 {Value(`{ {} }`), newInvalidCharacterError("{", `at start of string (expecting '"')`).withPos(`{ `, ""), ""},
295 {Value(`{ [] }`), newInvalidCharacterError("[", `at start of string (expecting '"')`).withPos(`{ `, ""), ""},
296 {BeginObject, nil, ""},
297 {Null, E(ErrNonStringName).withPos(`{`, ""), ""},
298 {Value(`null`), E(ErrNonStringName).withPos(`{`, ""), ""},
299 {False, E(ErrNonStringName).withPos(`{`, ""), ""},
300 {Value(`false`), E(ErrNonStringName).withPos(`{`, ""), ""},
301 {True, E(ErrNonStringName).withPos(`{`, ""), ""},
302 {Value(`true`), E(ErrNonStringName).withPos(`{`, ""), ""},
303 {Uint(0), E(ErrNonStringName).withPos(`{`, ""), ""},
304 {Value(`0`), E(ErrNonStringName).withPos(`{`, ""), ""},
305 {BeginObject, E(ErrNonStringName).withPos(`{`, ""), ""},
306 {Value(`{}`), E(ErrNonStringName).withPos(`{`, ""), ""},
307 {BeginArray, E(ErrNonStringName).withPos(`{`, ""), ""},
308 {Value(`[]`), E(ErrNonStringName).withPos(`{`, ""), ""},
309 {EndObject, nil, ""},
310 },
311 wantOut: "{}\n",
312 }, {
313 name: jsontest.Name("InvalidObject/InvalidValue"),
314 calls: []encoderMethodCall{
315 {Value(`{ "0": x }`), newInvalidCharacterError("x", `at start of value`).withPos(`{ "0": `, "/0"), ""},
316 },
317 }, {
318 name: jsontest.Name("InvalidObject/MismatchingDelim"),
319 calls: []encoderMethodCall{
320 {Value(` { ] `), newInvalidCharacterError("]", `at start of string (expecting '"')`).withPos(` { `, ""), ""},
321 {Value(` { "0":0 ] `), newInvalidCharacterError("]", `after object value (expecting ',' or '}')`).withPos(` { "0":0 `, ""), ""},
322 {BeginObject, nil, ""},
323 {EndArray, E(errMismatchDelim).withPos(`{`, ""), ""},
324 {Value(`]`), newInvalidCharacterError("]", "at start of value").withPos(`{`, ""), ""},
325 {EndObject, nil, ""},
326 },
327 wantOut: "{}\n",
328 }, {
329 name: jsontest.Name("ValidObject/UniqueNames"),
330 calls: []encoderMethodCall{
331 {BeginObject, nil, ""},
332 {String("0"), nil, ""},
333 {Uint(0), nil, ""},
334 {String("1"), nil, ""},
335 {Uint(1), nil, ""},
336 {EndObject, nil, ""},
337 {Value(` { "0" : 0 , "1" : 1 } `), nil, ""},
338 },
339 wantOut: `{"0":0,"1":1}` + "\n" + `{"0":0,"1":1}` + "\n",
340 }, {
341 name: jsontest.Name("ValidObject/DuplicateNames"),
342 opts: []Options{AllowDuplicateNames(true)},
343 calls: []encoderMethodCall{
344 {BeginObject, nil, ""},
345 {String("0"), nil, ""},
346 {Uint(0), nil, ""},
347 {String("0"), nil, ""},
348 {Uint(0), nil, ""},
349 {EndObject, nil, ""},
350 {Value(` { "0" : 0 , "0" : 0 } `), nil, ""},
351 },
352 wantOut: `{"0":0,"0":0}` + "\n" + `{"0":0,"0":0}` + "\n",
353 }, {
354 name: jsontest.Name("InvalidObject/DuplicateNames"),
355 calls: []encoderMethodCall{
356 {BeginObject, nil, ""},
357 {String("X"), nil, ""},
358 {BeginObject, nil, ""},
359 {EndObject, nil, ""},
360 {String("X"), E(ErrDuplicateName).withPos(`{"X":{},`, "/X"), "/X"},
361 {Value(`"X"`), E(ErrDuplicateName).withPos(`{"X":{},`, "/X"), "/X"},
362 {String("Y"), nil, ""},
363 {BeginObject, nil, ""},
364 {EndObject, nil, ""},
365 {String("X"), E(ErrDuplicateName).withPos(`{"X":{},"Y":{},`, "/X"), "/Y"},
366 {Value(`"X"`), E(ErrDuplicateName).withPos(`{"X":{},"Y":{},`, "/X"), "/Y"},
367 {String("Y"), E(ErrDuplicateName).withPos(`{"X":{},"Y":{},`, "/Y"), "/Y"},
368 {Value(`"Y"`), E(ErrDuplicateName).withPos(`{"X":{},"Y":{},`, "/Y"), "/Y"},
369 {EndObject, nil, ""},
370 {Value(` { "X" : 0 , "Y" : 1 , "X" : 0 } `), E(ErrDuplicateName).withPos(`{"X":{},"Y":{}}`+"\n"+` { "X" : 0 , "Y" : 1 , `, "/X"), ""},
371 },
372 wantOut: `{"X":{},"Y":{}}` + "\n",
373 }, {
374 name: jsontest.Name("TruncatedArray/AfterStart"),
375 calls: []encoderMethodCall{
376 {Value(`[`), E(io.ErrUnexpectedEOF).withPos(`[`, ""), ""},
377 },
378 }, {
379 name: jsontest.Name("TruncatedArray/AfterValue"),
380 calls: []encoderMethodCall{
381 {Value(`[0`), E(io.ErrUnexpectedEOF).withPos(`[0`, ""), ""},
382 },
383 }, {
384 name: jsontest.Name("TruncatedArray/AfterComma"),
385 calls: []encoderMethodCall{
386 {Value(`[0,`), E(io.ErrUnexpectedEOF).withPos(`[0,`, ""), ""},
387 },
388 }, {
389 name: jsontest.Name("TruncatedArray/MissingComma"),
390 calls: []encoderMethodCall{
391 {Value(` [ "fizz" "buzz" ] `), newInvalidCharacterError("\"", "after array value (expecting ',' or ']')").withPos(` [ "fizz" `, ""), ""},
392 },
393 }, {
394 name: jsontest.Name("InvalidArray/MismatchingDelim"),
395 calls: []encoderMethodCall{
396 {Value(` [ } `), newInvalidCharacterError("}", `at start of value`).withPos(` [ `, "/0"), ""},
397 {BeginArray, nil, ""},
398 {EndObject, E(errMismatchDelim).withPos(`[`, "/0"), ""},
399 {Value(`}`), newInvalidCharacterError("}", "at start of value").withPos(`[`, "/0"), ""},
400 {EndArray, nil, ""},
401 },
402 wantOut: "[]\n",
403 }, {
404 name: jsontest.Name("Format/Object/SpaceAfterColon"),
405 opts: []Options{SpaceAfterColon(true)},
406 calls: []encoderMethodCall{{Value(`{"fizz":"buzz","wizz":"wuzz"}`), nil, ""}},
407 wantOut: "{\"fizz\": \"buzz\",\"wizz\": \"wuzz\"}\n",
408 }, {
409 name: jsontest.Name("Format/Object/SpaceAfterComma"),
410 opts: []Options{SpaceAfterComma(true)},
411 calls: []encoderMethodCall{{Value(`{"fizz":"buzz","wizz":"wuzz"}`), nil, ""}},
412 wantOut: "{\"fizz\":\"buzz\", \"wizz\":\"wuzz\"}\n",
413 }, {
414 name: jsontest.Name("Format/Object/SpaceAfterColonAndComma"),
415 opts: []Options{SpaceAfterColon(true), SpaceAfterComma(true)},
416 calls: []encoderMethodCall{{Value(`{"fizz":"buzz","wizz":"wuzz"}`), nil, ""}},
417 wantOut: "{\"fizz\": \"buzz\", \"wizz\": \"wuzz\"}\n",
418 }, {
419 name: jsontest.Name("Format/Object/NoSpaceAfterColon+SpaceAfterComma+Multiline"),
420 opts: []Options{SpaceAfterColon(false), SpaceAfterComma(true), Multiline(true)},
421 calls: []encoderMethodCall{{Value(`{"fizz":"buzz","wizz":"wuzz"}`), nil, ""}},
422 wantOut: "{\n\t\"fizz\":\"buzz\", \n\t\"wizz\":\"wuzz\"\n}\n",
423 }, {
424 name: jsontest.Name("Format/Array/SpaceAfterComma"),
425 opts: []Options{SpaceAfterComma(true)},
426 calls: []encoderMethodCall{{Value(`["fizz","buzz"]`), nil, ""}},
427 wantOut: "[\"fizz\", \"buzz\"]\n",
428 }, {
429 name: jsontest.Name("Format/Array/NoSpaceAfterComma+Multiline"),
430 opts: []Options{SpaceAfterComma(false), Multiline(true)},
431 calls: []encoderMethodCall{{Value(`["fizz","buzz"]`), nil, ""}},
432 wantOut: "[\n\t\"fizz\",\n\t\"buzz\"\n]\n",
433 }, {
434 name: jsontest.Name("Format/ReorderWithWhitespace"),
435 opts: []Options{
436 AllowDuplicateNames(true),
437 AllowInvalidUTF8(true),
438 ReorderRawObjects(true),
439 SpaceAfterComma(true),
440 SpaceAfterColon(false),
441 Multiline(true),
442 WithIndentPrefix(" "),
443 WithIndent("\t"),
444 PreserveRawStrings(true),
445 },
446 calls: []encoderMethodCall{
447 {BeginArray, nil, ""},
448 {BeginArray, nil, ""},
449 {Value(` { "fizz" : "buzz" ,
450 "zip" : {
451 "x` + "\xfd" + `x" : 123 , "x` + "\xff" + `x" : 123, "x` + "\xfe" + `x" : 123
452 },
453 "zap" : {
454 "xxx" : 333, "xxx": 1, "xxx": 22
455 },
456 "alpha" : "bravo" } `), nil, ""},
457 {EndArray, nil, ""},
458 {EndArray, nil, ""},
459 },
460 wantOut: "[\n \t[\n \t\t{\n \t\t\t\"alpha\":\"bravo\", \n \t\t\t\"fizz\":\"buzz\", \n \t\t\t\"zap\":{\n \t\t\t\t\"xxx\":1, \n \t\t\t\t\"xxx\":22, \n \t\t\t\t\"xxx\":333\n \t\t\t}, \n \t\t\t\"zip\":{\n \t\t\t\t\"x\xfdx\":123, \n \t\t\t\t\"x\xfex\":123, \n \t\t\t\t\"x\xffx\":123\n \t\t\t}\n \t\t}\n \t]\n ]\n",
461 }, {
462 name: jsontest.Name("Format/CanonicalizeRawInts"),
463 opts: []Options{CanonicalizeRawInts(true), SpaceAfterComma(true)},
464 calls: []encoderMethodCall{
465 {Value(`[0.100,5.0,1E6,-9223372036854775808,-10,-1,-0,0,1,10,9223372036854775807]`), nil, ""},
466 },
467 wantOut: "[0.100, 5.0, 1E6, -9223372036854776000, -10, -1, 0, 0, 1, 10, 9223372036854776000]\n",
468 }, {
469 name: jsontest.Name("Format/CanonicalizeRawFloats"),
470 opts: []Options{CanonicalizeRawFloats(true), SpaceAfterComma(true)},
471 calls: []encoderMethodCall{
472 {Value(`[0.100,5.0,1E6,-9223372036854775808,-10,-1,-0,0,1,10,9223372036854775807]`), nil, ""},
473 },
474 wantOut: "[0.1, 5, 1000000, -9223372036854775808, -10, -1, 0, 0, 1, 10, 9223372036854775807]\n",
475 }, {
476 name: jsontest.Name("ErrorPosition"),
477 calls: []encoderMethodCall{
478 {Value(` "a` + "\xff" + `0" `), E(jsonwire.ErrInvalidUTF8).withPos(` "a`, ""), ""},
479 {String(`a` + "\xff" + `0`), E(jsonwire.ErrInvalidUTF8).withPos(``, ""), ""},
480 },
481 }, {
482 name: jsontest.Name("ErrorPosition/0"),
483 calls: []encoderMethodCall{
484 {Value(` [ "a` + "\xff" + `1" ] `), E(jsonwire.ErrInvalidUTF8).withPos(` [ "a`, "/0"), ""},
485 {BeginArray, nil, ""},
486 {Value(` "a` + "\xff" + `1" `), E(jsonwire.ErrInvalidUTF8).withPos(`[ "a`, "/0"), ""},
487 {String(`a` + "\xff" + `1`), E(jsonwire.ErrInvalidUTF8).withPos(`[`, "/0"), ""},
488 },
489 wantOut: `[`,
490 }, {
491 name: jsontest.Name("ErrorPosition/1"),
492 calls: []encoderMethodCall{
493 {Value(` [ "a1" , "b` + "\xff" + `1" ] `), E(jsonwire.ErrInvalidUTF8).withPos(` [ "a1" , "b`, "/1"), ""},
494 {BeginArray, nil, ""},
495 {String("a1"), nil, ""},
496 {Value(` "b` + "\xff" + `1" `), E(jsonwire.ErrInvalidUTF8).withPos(`["a1", "b`, "/1"), ""},
497 {String(`b` + "\xff" + `1`), E(jsonwire.ErrInvalidUTF8).withPos(`["a1",`, "/1"), ""},
498 },
499 wantOut: `["a1"`,
500 }, {
501 name: jsontest.Name("ErrorPosition/0/0"),
502 calls: []encoderMethodCall{
503 {Value(` [ [ "a` + "\xff" + `2" ] ] `), E(jsonwire.ErrInvalidUTF8).withPos(` [ [ "a`, "/0/0"), ""},
504 {BeginArray, nil, ""},
505 {Value(` [ "a` + "\xff" + `2" ] `), E(jsonwire.ErrInvalidUTF8).withPos(`[ [ "a`, "/0/0"), ""},
506 {BeginArray, nil, "/0"},
507 {Value(` "a` + "\xff" + `2" `), E(jsonwire.ErrInvalidUTF8).withPos(`[[ "a`, "/0/0"), "/0"},
508 {String(`a` + "\xff" + `2`), E(jsonwire.ErrInvalidUTF8).withPos(`[[`, "/0/0"), "/0"},
509 },
510 wantOut: `[[`,
511 }, {
512 name: jsontest.Name("ErrorPosition/1/0"),
513 calls: []encoderMethodCall{
514 {Value(` [ "a1" , [ "a` + "\xff" + `2" ] ] `), E(jsonwire.ErrInvalidUTF8).withPos(` [ "a1" , [ "a`, "/1/0"), ""},
515 {BeginArray, nil, ""},
516 {String("a1"), nil, "/0"},
517 {Value(` [ "a` + "\xff" + `2" ] `), E(jsonwire.ErrInvalidUTF8).withPos(`["a1", [ "a`, "/1/0"), ""},
518 {BeginArray, nil, "/1"},
519 {Value(` "a` + "\xff" + `2" `), E(jsonwire.ErrInvalidUTF8).withPos(`["a1",[ "a`, "/1/0"), "/1"},
520 {String(`a` + "\xff" + `2`), E(jsonwire.ErrInvalidUTF8).withPos(`["a1",[`, "/1/0"), "/1"},
521 },
522 wantOut: `["a1",[`,
523 }, {
524 name: jsontest.Name("ErrorPosition/0/1"),
525 calls: []encoderMethodCall{
526 {Value(` [ [ "a2" , "b` + "\xff" + `2" ] ] `), E(jsonwire.ErrInvalidUTF8).withPos(` [ [ "a2" , "b`, "/0/1"), ""},
527 {BeginArray, nil, ""},
528 {Value(` [ "a2" , "b` + "\xff" + `2" ] `), E(jsonwire.ErrInvalidUTF8).withPos(`[ [ "a2" , "b`, "/0/1"), ""},
529 {BeginArray, nil, "/0"},
530 {String("a2"), nil, "/0/0"},
531 {Value(` "b` + "\xff" + `2" `), E(jsonwire.ErrInvalidUTF8).withPos(`[["a2", "b`, "/0/1"), "/0/0"},
532 {String(`b` + "\xff" + `2`), E(jsonwire.ErrInvalidUTF8).withPos(`[["a2",`, "/0/1"), "/0/0"},
533 },
534 wantOut: `[["a2"`,
535 }, {
536 name: jsontest.Name("ErrorPosition/1/1"),
537 calls: []encoderMethodCall{
538 {Value(` [ "a1" , [ "a2" , "b` + "\xff" + `2" ] ] `), E(jsonwire.ErrInvalidUTF8).withPos(` [ "a1" , [ "a2" , "b`, "/1/1"), ""},
539 {BeginArray, nil, ""},
540 {String("a1"), nil, "/0"},
541 {Value(` [ "a2" , "b` + "\xff" + `2" ] `), E(jsonwire.ErrInvalidUTF8).withPos(`["a1", [ "a2" , "b`, "/1/1"), ""},
542 {BeginArray, nil, "/1"},
543 {String("a2"), nil, "/1/0"},
544 {Value(` "b` + "\xff" + `2" `), E(jsonwire.ErrInvalidUTF8).withPos(`["a1",["a2", "b`, "/1/1"), "/1/0"},
545 {String(`b` + "\xff" + `2`), E(jsonwire.ErrInvalidUTF8).withPos(`["a1",["a2",`, "/1/1"), "/1/0"},
546 },
547 wantOut: `["a1",["a2"`,
548 }, {
549 name: jsontest.Name("ErrorPosition/a1-"),
550 calls: []encoderMethodCall{
551 {Value(` { "a` + "\xff" + `1" : "b1" } `), E(jsonwire.ErrInvalidUTF8).withPos(` { "a`, ""), ""},
552 {BeginObject, nil, ""},
553 {Value(` "a` + "\xff" + `1" `), E(jsonwire.ErrInvalidUTF8).withPos(`{ "a`, ""), ""},
554 {String(`a` + "\xff" + `1`), E(jsonwire.ErrInvalidUTF8).withPos(`{`, ""), ""},
555 },
556 wantOut: `{`,
557 }, {
558 name: jsontest.Name("ErrorPosition/a1"),
559 calls: []encoderMethodCall{
560 {Value(` { "a1" : "b` + "\xff" + `1" } `), E(jsonwire.ErrInvalidUTF8).withPos(` { "a1" : "b`, "/a1"), ""},
561 {BeginObject, nil, ""},
562 {String("a1"), nil, "/a1"},
563 {Value(` "b` + "\xff" + `1" `), E(jsonwire.ErrInvalidUTF8).withPos(`{"a1": "b`, "/a1"), ""},
564 {String(`b` + "\xff" + `1`), E(jsonwire.ErrInvalidUTF8).withPos(`{"a1":`, "/a1"), ""},
565 },
566 wantOut: `{"a1"`,
567 }, {
568 name: jsontest.Name("ErrorPosition/c1-"),
569 calls: []encoderMethodCall{
570 {Value(` { "a1" : "b1" , "c` + "\xff" + `1" : "d1" } `), E(jsonwire.ErrInvalidUTF8).withPos(` { "a1" : "b1" , "c`, ""), ""},
571 {BeginObject, nil, ""},
572 {String("a1"), nil, "/a1"},
573 {String("b1"), nil, "/a1"},
574 {Value(` "c` + "\xff" + `1" `), E(jsonwire.ErrInvalidUTF8).withPos(`{"a1":"b1": "c`, ""), "/a1"},
575 {String(`c` + "\xff" + `1`), E(jsonwire.ErrInvalidUTF8).withPos(`{"a1":"b1":`, ""), "/a1"},
576 },
577 wantOut: `{"a1":"b1"`,
578 }, {
579 name: jsontest.Name("ErrorPosition/c1"),
580 calls: []encoderMethodCall{
581 {Value(` { "a1" : "b1" , "c1" : "d` + "\xff" + `1" } `), E(jsonwire.ErrInvalidUTF8).withPos(` { "a1" : "b1" , "c1" : "d`, "/c1"), ""},
582 {BeginObject, nil, ""},
583 {String("a1"), nil, "/a1"},
584 {String("b1"), nil, "/a1"},
585 {String("c1"), nil, "/c1"},
586 {Value(` "d` + "\xff" + `1" `), E(jsonwire.ErrInvalidUTF8).withPos(`{"a1":"b1":"c1": "d`, "/c1"), "/c1"},
587 {String(`d` + "\xff" + `1`), E(jsonwire.ErrInvalidUTF8).withPos(`{"a1":"b1":"c1":`, "/c1"), "/c1"},
588 },
589 wantOut: `{"a1":"b1","c1"`,
590 }, {
591 name: jsontest.Name("ErrorPosition/a1/a2-"),
592 calls: []encoderMethodCall{
593 {Value(` { "a1" : { "a` + "\xff" + `2" : "b2" } } `), E(jsonwire.ErrInvalidUTF8).withPos(` { "a1" : { "a`, "/a1"), ""},
594 {BeginObject, nil, ""},
595 {String("a1"), nil, "/a1"},
596 {Value(` { "a` + "\xff" + `2" : "b2" } `), E(jsonwire.ErrInvalidUTF8).withPos(`{"a1": { "a`, "/a1"), ""},
597 {BeginObject, nil, "/a1"},
598 {Value(` "a` + "\xff" + `2" `), E(jsonwire.ErrInvalidUTF8).withPos(`{"a1":{ "a`, "/a1"), "/a1"},
599 {String(`a` + "\xff" + `2`), E(jsonwire.ErrInvalidUTF8).withPos(`{"a1":{`, "/a1"), "/a1"},
600 },
601 wantOut: `{"a1":{`,
602 }, {
603 name: jsontest.Name("ErrorPosition/a1/a2"),
604 calls: []encoderMethodCall{
605 {Value(` { "a1" : { "a2" : "b` + "\xff" + `2" } } `), E(jsonwire.ErrInvalidUTF8).withPos(` { "a1" : { "a2" : "b`, "/a1/a2"), ""},
606 {BeginObject, nil, ""},
607 {String("a1"), nil, "/a1"},
608 {Value(` { "a2" : "b` + "\xff" + `2" } `), E(jsonwire.ErrInvalidUTF8).withPos(`{"a1": { "a2" : "b`, "/a1/a2"), ""},
609 {BeginObject, nil, "/a1"},
610 {String("a2"), nil, "/a1/a2"},
611 {Value(` "b` + "\xff" + `2" `), E(jsonwire.ErrInvalidUTF8).withPos(`{"a1":{"a2": "b`, "/a1/a2"), "/a1/a2"},
612 {String(`b` + "\xff" + `2`), E(jsonwire.ErrInvalidUTF8).withPos(`{"a1":{"a2":`, "/a1/a2"), "/a1/a2"},
613 },
614 wantOut: `{"a1":{"a2"`,
615 }, {
616 name: jsontest.Name("ErrorPosition/a1/c2-"),
617 calls: []encoderMethodCall{
618 {Value(` { "a1" : { "a2" : "b2" , "c` + "\xff" + `2" : "d2" } } `), E(jsonwire.ErrInvalidUTF8).withPos(` { "a1" : { "a2" : "b2" , "c`, "/a1"), ""},
619 {BeginObject, nil, ""},
620 {String("a1"), nil, "/a1"},
621 {BeginObject, nil, "/a1"},
622 {String("a2"), nil, "/a1/a2"},
623 {String("b2"), nil, "/a1/a2"},
624 {Value(` "c` + "\xff" + `2" `), E(jsonwire.ErrInvalidUTF8).withPos(`{"a1":{"a2":"b2", "c`, "/a1"), "/a1/a2"},
625 {String(`c` + "\xff" + `2`), E(jsonwire.ErrInvalidUTF8).withPos(`{"a1":{"a2":"b2",`, "/a1"), "/a1/a2"},
626 },
627 wantOut: `{"a1":{"a2":"b2"`,
628 }, {
629 name: jsontest.Name("ErrorPosition/a1/c2"),
630 calls: []encoderMethodCall{
631 {Value(` { "a1" : { "a2" : "b2" , "c2" : "d` + "\xff" + `2" } } `), E(jsonwire.ErrInvalidUTF8).withPos(` { "a1" : { "a2" : "b2" , "c2" : "d`, "/a1/c2"), ""},
632 {BeginObject, nil, ""},
633 {String("a1"), nil, "/a1"},
634 {Value(` { "a2" : "b2" , "c2" : "d` + "\xff" + `2" } `), E(jsonwire.ErrInvalidUTF8).withPos(`{"a1": { "a2" : "b2" , "c2" : "d`, "/a1/c2"), ""},
635 {BeginObject, nil, ""},
636 {String("a2"), nil, "/a1/a2"},
637 {String("b2"), nil, "/a1/a2"},
638 {String("c2"), nil, "/a1/c2"},
639 {Value(` "d` + "\xff" + `2" `), E(jsonwire.ErrInvalidUTF8).withPos(`{"a1":{"a2":"b2","c2": "d`, "/a1/c2"), "/a1/c2"},
640 {String(`d` + "\xff" + `2`), E(jsonwire.ErrInvalidUTF8).withPos(`{"a1":{"a2":"b2","c2":`, "/a1/c2"), "/a1/c2"},
641 },
642 wantOut: `{"a1":{"a2":"b2","c2"`,
643 }, {
644 name: jsontest.Name("ErrorPosition/1/a2"),
645 calls: []encoderMethodCall{
646 {Value(` [ "a1" , { "a2" : "b` + "\xff" + `2" } ] `), E(jsonwire.ErrInvalidUTF8).withPos(` [ "a1" , { "a2" : "b`, "/1/a2"), ""},
647 {BeginArray, nil, ""},
648 {String("a1"), nil, "/0"},
649 {Value(` { "a2" : "b` + "\xff" + `2" } `), E(jsonwire.ErrInvalidUTF8).withPos(`["a1", { "a2" : "b`, "/1/a2"), ""},
650 {BeginObject, nil, "/1"},
651 {String("a2"), nil, "/1/a2"},
652 {Value(` "b` + "\xff" + `2" `), E(jsonwire.ErrInvalidUTF8).withPos(`["a1",{"a2": "b`, "/1/a2"), "/1/a2"},
653 {String(`b` + "\xff" + `2`), E(jsonwire.ErrInvalidUTF8).withPos(`["a1",{"a2":`, "/1/a2"), "/1/a2"},
654 },
655 wantOut: `["a1",{"a2"`,
656 }, {
657 name: jsontest.Name("ErrorPosition/c1/1"),
658 calls: []encoderMethodCall{
659 {Value(` { "a1" : "b1" , "c1" : [ "a2" , "b` + "\xff" + `2" ] } `), E(jsonwire.ErrInvalidUTF8).withPos(` { "a1" : "b1" , "c1" : [ "a2" , "b`, "/c1/1"), ""},
660 {BeginObject, nil, ""},
661 {String("a1"), nil, "/a1"},
662 {String("b1"), nil, "/a1"},
663 {String("c1"), nil, "/c1"},
664 {Value(` [ "a2" , "b` + "\xff" + `2" ] `), E(jsonwire.ErrInvalidUTF8).withPos(`{"a1":"b1","c1": [ "a2" , "b`, "/c1/1"), ""},
665 {BeginArray, nil, "/c1"},
666 {String("a2"), nil, "/c1/0"},
667 {Value(` "b` + "\xff" + `2" `), E(jsonwire.ErrInvalidUTF8).withPos(`{"a1":"b1","c1":["a2", "b`, "/c1/1"), "/c1/0"},
668 {String(`b` + "\xff" + `2`), E(jsonwire.ErrInvalidUTF8).withPos(`{"a1":"b1","c1":["a2",`, "/c1/1"), "/c1/0"},
669 },
670 wantOut: `{"a1":"b1","c1":["a2"`,
671 }, {
672 name: jsontest.Name("ErrorPosition/0/a1/1/c3/1"),
673 calls: []encoderMethodCall{
674 {Value(` [ { "a1" : [ "a2" , { "a3" : "b3" , "c3" : [ "a4" , "b` + "\xff" + `4" ] } ] } ] `), E(jsonwire.ErrInvalidUTF8).withPos(` [ { "a1" : [ "a2" , { "a3" : "b3" , "c3" : [ "a4" , "b`, "/0/a1/1/c3/1"), ""},
675 {BeginArray, nil, ""},
676 {Value(` { "a1" : [ "a2" , { "a3" : "b3" , "c3" : [ "a4" , "b` + "\xff" + `4" ] } ] } `), E(jsonwire.ErrInvalidUTF8).withPos(`[ { "a1" : [ "a2" , { "a3" : "b3" , "c3" : [ "a4" , "b`, "/0/a1/1/c3/1"), ""},
677 {BeginObject, nil, "/0"},
678 {String("a1"), nil, "/0/a1"},
679 {Value(` [ "a2" , { "a3" : "b3" , "c3" : [ "a4" , "b` + "\xff" + `4" ] } ] `), E(jsonwire.ErrInvalidUTF8).withPos(`[{"a1": [ "a2" , { "a3" : "b3" , "c3" : [ "a4" , "b`, "/0/a1/1/c3/1"), ""},
680 {BeginArray, nil, ""},
681 {String("a2"), nil, "/0/a1/0"},
682 {Value(` { "a3" : "b3" , "c3" : [ "a4" , "b` + "\xff" + `4" ] } `), E(jsonwire.ErrInvalidUTF8).withPos(`[{"a1":["a2", { "a3" : "b3" , "c3" : [ "a4" , "b`, "/0/a1/1/c3/1"), ""},
683 {BeginObject, nil, "/0/a1/1"},
684 {String("a3"), nil, "/0/a1/1/a3"},
685 {String("b3"), nil, "/0/a1/1/a3"},
686 {String("c3"), nil, "/0/a1/1/c3"},
687 {Value(` [ "a4" , "b` + "\xff" + `4" ] `), E(jsonwire.ErrInvalidUTF8).withPos(`[{"a1":["a2",{"a3":"b3","c3": [ "a4" , "b`, "/0/a1/1/c3/1"), ""},
688 {BeginArray, nil, "/0/a1/1/c3"},
689 {String("a4"), nil, "/0/a1/1/c3/0"},
690 {Value(` "b` + "\xff" + `4" `), E(jsonwire.ErrInvalidUTF8).withPos(`[{"a1":["a2",{"a3":"b3","c3":["a4", "b`, "/0/a1/1/c3/1"), "/0/a1/1/c3/0"},
691 {String(`b` + "\xff" + `4`), E(jsonwire.ErrInvalidUTF8).withPos(`[{"a1":["a2",{"a3":"b3","c3":["a4",`, "/0/a1/1/c3/1"), "/0/a1/1/c3/0"},
692 },
693 wantOut: `[{"a1":["a2",{"a3":"b3","c3":["a4"`,
694 }}
695
696 // TestEncoderErrors test that Encoder errors occur when we expect and
697 // leaves the Encoder in a consistent state.
698 func TestEncoderErrors(t *testing.T) {
699 for _, td := range encoderErrorTestdata {
700 t.Run(path.Join(td.name.Name), func(t *testing.T) {
701 testEncoderErrors(t, td.name.Where, td.opts, td.calls, td.wantOut)
702 })
703 }
704 }
705 func testEncoderErrors(t *testing.T, where jsontest.CasePos, opts []Options, calls []encoderMethodCall, wantOut string) {
706 dst := new(bytes.Buffer)
707 enc := NewEncoder(dst, opts...)
708 for i, call := range calls {
709 var gotErr error
710 switch tokVal := call.in.(type) {
711 case Token:
712 gotErr = enc.WriteToken(tokVal)
713 case Value:
714 gotErr = enc.WriteValue(tokVal)
715 }
716 if !equalError(gotErr, call.wantErr) {
717 t.Fatalf("%s: %d: error mismatch:\ngot %v\nwant %v", where, i, gotErr, call.wantErr)
718 }
719 if call.wantPointer != "" {
720 gotPointer := enc.StackPointer()
721 if gotPointer != call.wantPointer {
722 t.Fatalf("%s: %d: Encoder.StackPointer = %s, want %s", where, i, gotPointer, call.wantPointer)
723 }
724 }
725 }
726 gotOut := dst.String() + string(enc.s.unflushedBuffer())
727 if gotOut != wantOut {
728 t.Fatalf("%s: output mismatch:\ngot %q\nwant %q", where, gotOut, wantOut)
729 }
730 gotOffset := int(enc.OutputOffset())
731 wantOffset := len(wantOut)
732 if gotOffset != wantOffset {
733 t.Fatalf("%s: Encoder.OutputOffset = %v, want %v", where, gotOffset, wantOffset)
734 }
735 }
736
737 // TestEncoderReset tests that the encoder preserves its internal
738 // buffer between Reset calls to avoid frequent allocations when reusing the encoder.
739 // It ensures that the buffer capacity is maintained while avoiding aliasing
740 // issues with [bytes.Buffer].
741 func TestEncoderReset(t *testing.T) {
742 // Create an encoder with a reasonably large JSON input to ensure buffer growth.
743 largeJSON := `{"key1":"value1","key2":"value2","key3":"value3","key4":"value4","key5":"value5"}` + "\n"
744 bb := new(bytes.Buffer)
745 enc := NewEncoder(struct{ io.Writer }{bb}) // mask out underlying [bytes.Buffer]
746
747 t.Run("Test capacity preservation", func(t *testing.T) {
748 // Write the first JSON value to grow the internal buffer.
749 err := enc.WriteValue(append(enc.AvailableBuffer(), largeJSON...))
750 if err != nil {
751 t.Fatalf("first WriteValue failed: %v", err)
752 }
753 if bb.String() != largeJSON {
754 t.Fatalf("first WriteValue = %q, want %q", bb.String(), largeJSON)
755 }
756
757 // Get the buffer capacity after first use.
758 initialCapacity := cap(enc.s.Buf)
759 initialCacheCapacity := cap(enc.s.availBuffer)
760 if initialCapacity == 0 {
761 t.Fatalf("expected non-zero buffer capacity after first use")
762 }
763 if initialCacheCapacity == 0 {
764 t.Fatalf("expected non-zero cache capacity after first use")
765 }
766
767 // Reset with a new writer - this should preserve the buffer capacity.
768 bb.Reset()
769 enc.Reset(struct{ io.Writer }{bb})
770
771 // Verify the buffer capacity is preserved (or at least not smaller).
772 preservedCapacity := cap(enc.s.Buf)
773 if preservedCapacity < initialCapacity {
774 t.Fatalf("buffer capacity reduced after Reset: got %d, want at least %d", preservedCapacity, initialCapacity)
775 }
776 preservedCacheCapacity := cap(enc.s.availBuffer)
777 if preservedCacheCapacity < initialCacheCapacity {
778 t.Fatalf("cache capacity reduced after Reset: got %d, want at least %d", preservedCapacity, initialCapacity)
779 }
780
781 // Write the second JSON value to ensure the encoder still works correctly.
782 err = enc.WriteValue(append(enc.AvailableBuffer(), largeJSON...))
783 if err != nil {
784 t.Fatalf("second WriteValue failed: %v", err)
785 }
786 if bb.String() != largeJSON {
787 t.Fatalf("second WriteValue = %q, want %q", bb.String(), largeJSON)
788 }
789 })
790
791 t.Run("Test aliasing with bytes.Buffer", func(t *testing.T) {
792 // Test with bytes.Buffer to verify proper aliasing behavior.
793 bb.Reset()
794 enc.Reset(bb)
795
796 // Write the third JSON value to ensure functionality with bytes.Buffer.
797 err := enc.WriteValue([]byte(largeJSON))
798 if err != nil {
799 t.Fatalf("fourth WriteValue failed: %v", err)
800 }
801 if bb.String() != largeJSON {
802 t.Fatalf("fourth WriteValue = %q, want %q", bb.String(), largeJSON)
803 }
804 // The encoder buffer should alias bytes.Buffer's internal buffer.
805 if cap(enc.s.Buf) == 0 || cap(bb.AvailableBuffer()) == 0 || &enc.s.Buf[:1][0] != &bb.AvailableBuffer()[:1][0] {
806 t.Fatalf("encoder buffer does not alias bytes.Buffer")
807 }
808 })
809
810 t.Run("Test aliasing removed after Reset", func(t *testing.T) {
811 // Reset with a new reader and verify the buffer is not aliased.
812 bb.Reset()
813 enc.Reset(struct{ io.Writer }{bb})
814 err := enc.WriteValue([]byte(largeJSON))
815 if err != nil {
816 t.Fatalf("fifth WriteValue failed: %v", err)
817 }
818 if bb.String() != largeJSON {
819 t.Fatalf("fourth WriteValue = %q, want %q", bb.String(), largeJSON)
820 }
821
822 // The encoder buffer should not alias the bytes.Buffer's internal buffer.
823 if cap(enc.s.Buf) == 0 || cap(bb.AvailableBuffer()) == 0 || &enc.s.Buf[:1][0] == &bb.AvailableBuffer()[:1][0] {
824 t.Fatalf("encoder buffer aliases bytes.Buffer")
825 }
826 })
827 }