(t *testing.T)
| 507 | } |
| 508 | |
| 509 | func 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) |
nothing calls this directly
no test coverage detected