MCPcopy Index your code
hub / github.com/segmentio/encoding / TestCodec

Function TestCodec

json/json_test.go:367–422  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

365}
366
367func TestCodec(t *testing.T) {
368 for _, v1 := range testValues {
369 t.Run(testName(v1), func(t *testing.T) {
370 v2 := newValue(v1)
371
372 a, err := json.MarshalIndent(v1, "", "\t")
373 if err != nil {
374 t.Error(err)
375 return
376 }
377 a = append(a, '\n')
378
379 buf := &bytes.Buffer{}
380 enc := NewEncoder(buf)
381 enc.SetIndent("", "\t")
382
383 if err := enc.Encode(v1); err != nil {
384 t.Error(err)
385 return
386 }
387 b := buf.Bytes()
388
389 if !Valid(b) {
390 t.Error("invalid JSON representation")
391 }
392
393 if !bytes.Equal(a, b) {
394 t.Error("JSON representations mismatch")
395 t.Log("expected:", string(a))
396 t.Log("found: ", string(b))
397 }
398
399 dec := NewDecoder(bytes.NewBuffer(b))
400
401 if err := dec.Decode(v2.Interface()); err != nil {
402 t.Errorf("%T: %v", err, err)
403 return
404 }
405
406 x1 := v1
407 x2 := v2.Elem().Interface()
408
409 if !reflect.DeepEqual(x1, x2) {
410 t.Error("values mismatch")
411 t.Logf("expected: %#v", x1)
412 t.Logf("found: %#v", x2)
413 }
414
415 if b, err := io.ReadAll(dec.Buffered()); err != nil {
416 t.Error(err)
417 } else if len(b) != 0 {
418 t.Errorf("leftover trailing bytes in the decoder: %q", b)
419 }
420 })
421 }
422}
423
424// TestCodecDuration isolates testing of time.Duration. The stdlib un/marshals

Callers

nothing calls this directly

Calls 12

SetIndentMethod · 0.95
EncodeMethod · 0.95
DecodeMethod · 0.95
BufferedMethod · 0.95
testNameFunction · 0.85
newValueFunction · 0.85
BytesMethod · 0.80
NewEncoderFunction · 0.70
ValidFunction · 0.70
NewDecoderFunction · 0.70
ElemMethod · 0.65
ErrorMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…