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

Function TestMarshalUnmarshalRepeatedExtension

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

Source from the content-addressed store, hash-verified

440}
441
442func TestMarshalUnmarshalRepeatedExtension(t *testing.T) {
443 // Add a repeated extension to the result.
444 tests := []struct {
445 name string
446 ext []*pb2.ComplexExtension
447 }{
448 {
449 "two fields",
450 []*pb2.ComplexExtension{
451 {First: proto.Int32(7)},
452 {Second: proto.Int32(11)},
453 },
454 },
455 {
456 "repeated field",
457 []*pb2.ComplexExtension{
458 {Third: []int32{1000}},
459 {Third: []int32{2000}},
460 },
461 },
462 {
463 "two fields and repeated field",
464 []*pb2.ComplexExtension{
465 {Third: []int32{1000}},
466 {First: proto.Int32(9)},
467 {Second: proto.Int32(21)},
468 {Third: []int32{2000}},
469 },
470 },
471 }
472 for _, test := range tests {
473 // Marshal message with a repeated extension.
474 msg1 := new(pb2.OtherMessage)
475 err := proto.SetExtension(msg1, pb2.E_RComplex, test.ext)
476 if err != nil {
477 t.Fatalf("[%s] Error setting extension: %v", test.name, err)
478 }
479 b, err := proto.Marshal(msg1)
480 if err != nil {
481 t.Fatalf("[%s] Error marshaling message: %v", test.name, err)
482 }
483
484 // Unmarshal and read the merged proto.
485 msg2 := new(pb2.OtherMessage)
486 err = proto.Unmarshal(b, msg2)
487 if err != nil {
488 t.Fatalf("[%s] Error unmarshaling message: %v", test.name, err)
489 }
490 e, err := proto.GetExtension(msg2, pb2.E_RComplex)
491 if err != nil {
492 t.Fatalf("[%s] Error getting extension: %v", test.name, err)
493 }
494 ext := e.([]*pb2.ComplexExtension)
495 if ext == nil {
496 t.Fatalf("[%s] Invalid extension", test.name)
497 }
498 if len(ext) != len(test.ext) {
499 t.Errorf("[%s] Wrong length of ComplexExtension: got: %v want: %v\n", test.name, len(ext), len(test.ext))

Callers

nothing calls this directly

Calls 6

Int32Function · 0.92
SetExtensionFunction · 0.92
MarshalFunction · 0.92
UnmarshalFunction · 0.92
GetExtensionFunction · 0.92
EqualFunction · 0.92

Tested by

no test coverage detected