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

Function TestRequiredFieldEnforcement

proto/proto_test.go:1232–1251  ·  view source on GitHub ↗

Verify that absent required fields cause Marshal/Unmarshal to return errors.

(t *testing.T)

Source from the content-addressed store, hash-verified

1230
1231// Verify that absent required fields cause Marshal/Unmarshal to return errors.
1232func TestRequiredFieldEnforcement(t *testing.T) {
1233 pb := new(pb2.GoTestField)
1234 _, err := proto.Marshal(pb)
1235 if err == nil {
1236 t.Error("marshal: expected error, got nil")
1237 } else if !isRequiredNotSetError(err) {
1238 t.Errorf("marshal: bad error type: %v", err)
1239 }
1240
1241 // A slightly sneaky, yet valid, proto. It encodes the same required field twice,
1242 // so simply counting the required fields is insufficient.
1243 // field 1, encoding 2, value "hi"
1244 buf := []byte("\x0A\x02hi\x0A\x02hi")
1245 err = proto.Unmarshal(buf, pb)
1246 if err == nil {
1247 t.Error("unmarshal: expected error, got nil")
1248 } else if !isRequiredNotSetError(err) {
1249 t.Errorf("unmarshal: bad error type: %v", err)
1250 }
1251}
1252
1253// Verify that absent required fields in groups cause Marshal/Unmarshal to return errors.
1254func TestRequiredFieldEnforcementGroups(t *testing.T) {

Callers

nothing calls this directly

Calls 4

MarshalFunction · 0.92
UnmarshalFunction · 0.92
isRequiredNotSetErrorFunction · 0.85
ErrorMethod · 0.45

Tested by

no test coverage detected