isinit_test.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. "strings"
  8. "testing"
  9. "google.golang.org/protobuf/internal/flags"
  10. "google.golang.org/protobuf/proto"
  11. testpb "google.golang.org/protobuf/internal/testprotos/test"
  12. weakpb "google.golang.org/protobuf/internal/testprotos/test/weak1"
  13. )
  14. func TestIsInitializedErrors(t *testing.T) {
  15. type test struct {
  16. m proto.Message
  17. want string
  18. skip bool
  19. }
  20. tests := []test{{
  21. m: &testpb.TestRequired{},
  22. want: `goproto.proto.test.TestRequired.required_field`,
  23. }, {
  24. m: &testpb.TestRequiredForeign{
  25. OptionalMessage: &testpb.TestRequired{},
  26. },
  27. want: `goproto.proto.test.TestRequired.required_field`,
  28. }, {
  29. m: &testpb.TestRequiredForeign{
  30. RepeatedMessage: []*testpb.TestRequired{
  31. {RequiredField: proto.Int32(1)},
  32. {},
  33. },
  34. },
  35. want: `goproto.proto.test.TestRequired.required_field`,
  36. }, {
  37. m: &testpb.TestRequiredForeign{
  38. MapMessage: map[int32]*testpb.TestRequired{
  39. 1: {},
  40. },
  41. },
  42. want: `goproto.proto.test.TestRequired.required_field`,
  43. }, {
  44. m: &testpb.TestWeak{},
  45. want: `<nil>`,
  46. skip: !flags.ProtoLegacy,
  47. }, {
  48. m: func() proto.Message {
  49. m := &testpb.TestWeak{}
  50. m.SetWeakMessage1(&weakpb.WeakImportMessage1{})
  51. return m
  52. }(),
  53. want: `goproto.proto.test.weak.WeakImportMessage1.a`,
  54. skip: !flags.ProtoLegacy,
  55. }, {
  56. m: func() proto.Message {
  57. m := &testpb.TestWeak{}
  58. m.SetWeakMessage1(&weakpb.WeakImportMessage1{
  59. A: proto.Int32(1),
  60. })
  61. return m
  62. }(),
  63. want: `<nil>`,
  64. skip: !flags.ProtoLegacy,
  65. }}
  66. for _, tt := range tests {
  67. t.Run("", func(t *testing.T) {
  68. if tt.skip {
  69. t.SkipNow()
  70. }
  71. err := proto.IsInitialized(tt.m)
  72. got := "<nil>"
  73. if err != nil {
  74. got = fmt.Sprintf("%q", err)
  75. }
  76. if !strings.Contains(got, tt.want) {
  77. t.Errorf("IsInitialized(m):\n got: %v\nwant contains: %v\nMessage:\n%v", got, tt.want, marshalText(tt.m))
  78. }
  79. })
  80. }
  81. }