Verify that absent required fields cause Marshal/Unmarshal to return errors.
(t *testing.T)
| 1230 | |
| 1231 | // Verify that absent required fields cause Marshal/Unmarshal to return errors. |
| 1232 | func 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. |
| 1254 | func TestRequiredFieldEnforcementGroups(t *testing.T) { |
nothing calls this directly
no test coverage detected