MCPcopy
hub / github.com/golang/protobuf / TestStringEscaping

Function TestStringEscaping

proto/text_test.go:520–564  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

518}
519
520func TestStringEscaping(t *testing.T) {
521 testCases := []struct {
522 in *pb2.Strings
523 out string
524 }{
525 {
526 // Test data from C++ test (TextFormatTest.StringEscape).
527 // Single divergence: we don't escape apostrophes.
528 &pb2.Strings{StringField: proto.String("\"A string with ' characters \n and \r newlines and \t tabs and \001 slashes \\ and multiple spaces")},
529 "string_field: \"\\\"A string with ' characters \\n and \\r newlines and \\t tabs and \\001 slashes \\\\ and multiple spaces\"\n",
530 },
531 {
532 // Test data from the same C++ test.
533 &pb2.Strings{StringField: proto.String("\350\260\267\346\255\214")},
534 "string_field: \"\\350\\260\\267\\346\\255\\214\"\n",
535 },
536 {
537 // Some UTF-8.
538 &pb2.Strings{StringField: proto.String("\x00\x01\xff\x81")},
539 `string_field: "\000\001\377\201"` + "\n",
540 },
541 }
542
543 for _, tc := range testCases {
544 t.Run("", func(t *testing.T) {
545 var buf bytes.Buffer
546 if err := proto.MarshalText(&buf, tc.in); err != nil {
547 t.Fatalf("proto.MarsalText error: %v", err)
548 }
549 got := buf.String()
550 if got != tc.out {
551 t.Fatalf("want:\n%s\n\nwant:\n%s", got, tc.out)
552 }
553
554 // Check round-trip.
555 pb := new(pb2.Strings)
556 if err := proto.UnmarshalText(got, pb); err != nil {
557 t.Fatalf("proto.UnmarshalText error: %v", err)
558 }
559 if !proto.Equal(pb, tc.in) {
560 t.Fatalf("proto.Equal mismatch:\ngot:\n%v\n\nwant:\n%v", pb, tc.in)
561 }
562 })
563 }
564}
565
566// A limitedWriter accepts some output before it fails.
567// This is a proxy for something like a nearly-full or imminently-failing disk,

Callers

nothing calls this directly

Calls 5

StringFunction · 0.92
MarshalTextFunction · 0.92
UnmarshalTextFunction · 0.92
EqualFunction · 0.92
StringMethod · 0.45

Tested by

no test coverage detected