MCPcopy Create free account
hub / github.com/segmentio/encoding / TestStringBytes

Function TestStringBytes

json/golang_encode_test.go:591–637  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

589}
590
591func TestStringBytes(t *testing.T) {
592 t.Parallel()
593 // Test that encodeState.stringBytes and encodeState.string use the same encoding.
594 var r []rune
595 for i := '\u0000'; i <= unicode.MaxRune; i++ {
596 if testing.Short() && i > 1000 {
597 i = unicode.MaxRune
598 }
599 r = append(r, i)
600 }
601 s := string(r) + "\xff\xff\xffhello" // some invalid UTF-8 too
602
603 for _, escapeHTML := range []bool{true, false} {
604 es := &encodeState{}
605 es.string(s, escapeHTML)
606
607 esBytes := &encodeState{}
608 esBytes.stringBytes([]byte(s), escapeHTML)
609
610 enc := es.Buffer.String()
611 encBytes := esBytes.Buffer.String()
612 if enc != encBytes {
613 i := 0
614 for i < len(enc) && i < len(encBytes) && enc[i] == encBytes[i] {
615 i++
616 }
617 enc = enc[i:]
618 encBytes = encBytes[i:]
619 i = 0
620 for i < len(enc) && i < len(encBytes) && enc[len(enc)-i-1] == encBytes[len(encBytes)-i-1] {
621 i++
622 }
623 enc = enc[:len(enc)-i]
624 encBytes = encBytes[:len(encBytes)-i]
625
626 if len(enc) > 20 {
627 enc = enc[:20] + "..."
628 }
629 if len(encBytes) > 20 {
630 encBytes = encBytes[:20] + "..."
631 }
632
633 t.Errorf("with escapeHTML=%t, encodings differ at %#q vs %#q",
634 escapeHTML, enc, encBytes)
635 }
636 }
637}
638
639func TestIssue10281(t *testing.T) {
640 type Foo struct {

Callers

nothing calls this directly

Calls 3

stringMethod · 0.95
stringBytesMethod · 0.95
StringMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…