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

Function TestUnmarshalRepeatingNonRepeatedExtension

proto/extensions_test.go:509–582  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

507}
508
509func TestUnmarshalRepeatingNonRepeatedExtension(t *testing.T) {
510 // We may see multiple instances of the same extension in the wire
511 // format. For example, the proto compiler may encode custom options in
512 // this way. Here, we verify that we merge the extensions together.
513 tests := []struct {
514 name string
515 ext []*pb2.ComplexExtension
516 }{
517 {
518 "two fields",
519 []*pb2.ComplexExtension{
520 {First: proto.Int32(7)},
521 {Second: proto.Int32(11)},
522 },
523 },
524 {
525 "repeated field",
526 []*pb2.ComplexExtension{
527 {Third: []int32{1000}},
528 {Third: []int32{2000}},
529 },
530 },
531 {
532 "two fields and repeated field",
533 []*pb2.ComplexExtension{
534 {Third: []int32{1000}},
535 {First: proto.Int32(9)},
536 {Second: proto.Int32(21)},
537 {Third: []int32{2000}},
538 },
539 },
540 }
541 for _, test := range tests {
542 var buf bytes.Buffer
543 var want pb2.ComplexExtension
544
545 // Generate a serialized representation of a repeated extension
546 // by catenating bytes together.
547 for i, e := range test.ext {
548 // Merge to create the wanted proto.
549 proto.Merge(&want, e)
550
551 // serialize the message
552 msg := new(pb2.OtherMessage)
553 err := proto.SetExtension(msg, pb2.E_Complex, e)
554 if err != nil {
555 t.Fatalf("[%s] Error setting extension %d: %v", test.name, i, err)
556 }
557 b, err := proto.Marshal(msg)
558 if err != nil {
559 t.Fatalf("[%s] Error marshaling message %d: %v", test.name, i, err)
560 }
561 buf.Write(b)
562 }
563
564 // Unmarshal and read the merged proto.
565 msg2 := new(pb2.OtherMessage)
566 err := proto.Unmarshal(buf.Bytes(), msg2)

Callers

nothing calls this directly

Calls 9

BytesMethod · 0.95
Int32Function · 0.92
MergeFunction · 0.92
SetExtensionFunction · 0.92
MarshalFunction · 0.92
UnmarshalFunction · 0.92
GetExtensionFunction · 0.92
EqualFunction · 0.92
WriteMethod · 0.45

Tested by

no test coverage detected