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

Function TestSubmessageUnrecognizedFields

proto/proto_test.go:937–983  ·  proto/proto_test.go::TestSubmessageUnrecognizedFields

Check that unrecognized fields of a submessage are preserved.

(t *testing.T)

Source from the content-addressed store, hash-verified

935
936// Check that unrecognized fields of a submessage are preserved.
937func TestSubmessageUnrecognizedFields(t *testing.T) {
938 nm := &pb2.NewMessage{
939 Nested: &pb2.NewMessage_Nested{
940 Name: proto.String("Nigel"),
941 FoodGroup: proto.String("carbs"),
942 },
943 }
944 b, err := proto.Marshal(nm)
945 if err != nil {
946 t.Fatalf("Marshal of NewMessage: %v", err)
947 }
948
949 // Unmarshal into an OldMessage.
950 om := new(pb2.OldMessage)
951 if err := proto.Unmarshal(b, om); err != nil {
952 t.Fatalf("Unmarshal to OldMessage: %v", err)
953 }
954 exp := &pb2.OldMessage{
955 Nested: &pb2.OldMessage_Nested{
956 Name: proto.String("Nigel"),
957 // normal protocol buffer users should not do this
958 XXX_unrecognized: []byte("\x12\x05carbs"),
959 },
960 }
961 if !proto.Equal(om, exp) {
962 t.Errorf("om = %v, want %v", om, exp)
963 }
964
965 // Clone the OldMessage.
966 om = proto.Clone(om).(*pb2.OldMessage)
967 if !proto.Equal(om, exp) {
968 t.Errorf("Clone(om) = %v, want %v", om, exp)
969 }
970
971 // Marshal the OldMessage, then unmarshal it into an empty NewMessage.
972 if b, err = proto.Marshal(om); err != nil {
973 t.Fatalf("Marshal of OldMessage: %v", err)
974 }
975 t.Logf("Marshal(%v) -> %q", om, b)
976 nm2 := new(pb2.NewMessage)
977 if err := proto.Unmarshal(b, nm2); err != nil {
978 t.Fatalf("Unmarshal to NewMessage: %v", err)
979 }
980 if !proto.Equal(nm, nm2) {
981 t.Errorf("NewMessage round-trip: %v => %v", nm, nm2)
982 }
983}
984
985// Check that an int32 field can be upgraded to an int64 field.
986func TestNegativeInt32(t *testing.T) {

Callers

nothing calls this directly

Calls 5

StringFunction · 0.92
MarshalFunction · 0.92
UnmarshalFunction · 0.92
EqualFunction · 0.92
CloneFunction · 0.92

Tested by

no test coverage detected