MCPcopy
hub / github.com/protocolbuffers/protobuf-go / TestEncode

Function TestEncode

proto/encode_test.go:28–59  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

26)
27
28func TestEncode(t *testing.T) {
29 for _, test := range testValidMessages {
30 for _, want := range test.decodeTo {
31 t.Run(fmt.Sprintf("%s (%T)", test.desc, want), func(t *testing.T) {
32 opts := proto.MarshalOptions{
33 AllowPartial: test.partial,
34 }
35 wire, err := opts.Marshal(want)
36 if err != nil {
37 t.Fatalf("Marshal error: %v\nMessage:\n%v", err, prototext.Format(want))
38 }
39
40 size := proto.Size(want)
41 if size != len(wire) {
42 t.Errorf("Size and marshal disagree: Size(m)=%v; len(Marshal(m))=%v\nMessage:\n%v", size, len(wire), prototext.Format(want))
43 }
44
45 got := want.ProtoReflect().New().Interface()
46 uopts := proto.UnmarshalOptions{
47 AllowPartial: test.partial,
48 }
49 if err := uopts.Unmarshal(wire, got); err != nil {
50 t.Errorf("Unmarshal error: %v\nMessage:\n%v", err, prototext.Format(want))
51 return
52 }
53 if !proto.Equal(got, want) && got.ProtoReflect().IsValid() && want.ProtoReflect().IsValid() {
54 t.Errorf("Unmarshal returned unexpected result; got:\n%v\nwant:\n%v", prototext.Format(got), prototext.Format(want))
55 }
56 })
57 }
58 }
59}
60
61func TestEncodeDeterministic(t *testing.T) {
62 for _, test := range testValidMessages {

Callers

nothing calls this directly

Calls 10

MarshalMethod · 0.95
UnmarshalMethod · 0.95
FormatFunction · 0.92
SizeFunction · 0.92
EqualFunction · 0.92
RunMethod · 0.80
InterfaceMethod · 0.65
NewMethod · 0.65
ProtoReflectMethod · 0.65
IsValidMethod · 0.65

Tested by

no test coverage detected