isinit_test.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // Copyright 2019 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style.
  3. // license that can be found in the LICENSE file.
  4. package proto_test
  5. import (
  6. "fmt"
  7. "testing"
  8. "google.golang.org/protobuf/internal/scalar"
  9. "google.golang.org/protobuf/proto"
  10. testpb "google.golang.org/protobuf/internal/testprotos/test"
  11. )
  12. func TestIsInitializedErrors(t *testing.T) {
  13. for _, test := range []struct {
  14. m proto.Message
  15. want string
  16. }{
  17. {
  18. &testpb.TestRequired{},
  19. `proto: required field required_field not set`,
  20. },
  21. {
  22. &testpb.TestRequiredForeign{
  23. OptionalMessage: &testpb.TestRequired{},
  24. },
  25. `proto: required field optional_message.required_field not set`,
  26. },
  27. {
  28. &testpb.TestRequiredForeign{
  29. RepeatedMessage: []*testpb.TestRequired{
  30. {RequiredField: scalar.Int32(1)},
  31. {},
  32. },
  33. },
  34. `proto: required field repeated_message[1].required_field not set`,
  35. },
  36. {
  37. &testpb.TestRequiredForeign{
  38. MapMessage: map[int32]*testpb.TestRequired{
  39. 1: {},
  40. },
  41. },
  42. `proto: required field map_message[1].required_field not set`,
  43. },
  44. } {
  45. err := proto.IsInitialized(test.m)
  46. got := "<nil>"
  47. if err != nil {
  48. got = fmt.Sprintf("%q", err)
  49. }
  50. want := fmt.Sprintf("%q", test.want)
  51. if got != want {
  52. t.Errorf("IsInitialized(m):\n got: %v\nwant: %v\nMessage:\n%v", got, want, marshalText(test.m))
  53. }
  54. }
  55. }