1
2
3
4
5 package proto_test
6
7 import (
8 "fmt"
9 "strings"
10 "testing"
11
12 "google.golang.org/protobuf/encoding/prototext"
13 "google.golang.org/protobuf/internal/flags"
14 "google.golang.org/protobuf/proto"
15
16 testpb "google.golang.org/protobuf/internal/testprotos/test"
17 weakpb "google.golang.org/protobuf/internal/testprotos/test/weak1"
18 testeditionspb "google.golang.org/protobuf/internal/testprotos/testeditions"
19 )
20
21 func TestCheckInitializedErrors(t *testing.T) {
22 type test struct {
23 m proto.Message
24 want string
25 skip bool
26 }
27 tests := []test{{
28 m: &testpb.TestRequired{},
29 want: `goproto.proto.test.TestRequired.required_field`,
30 }, {
31 m: &testpb.TestRequiredForeign{
32 OptionalMessage: &testpb.TestRequired{},
33 },
34 want: `goproto.proto.test.TestRequired.required_field`,
35 }, {
36 m: &testpb.TestRequiredForeign{
37 RepeatedMessage: []*testpb.TestRequired{
38 {RequiredField: proto.Int32(1)},
39 {},
40 },
41 },
42 want: `goproto.proto.test.TestRequired.required_field`,
43 }, {
44 m: &testpb.TestRequiredForeign{
45 MapMessage: map[int32]*testpb.TestRequired{
46 1: {},
47 },
48 },
49 want: `goproto.proto.test.TestRequired.required_field`,
50 }, {
51 m: &testeditionspb.TestRequired{},
52 want: `goproto.proto.testeditions.TestRequired.required_field`,
53 }, {
54 m: &testeditionspb.TestRequiredForeign{
55 OptionalMessage: &testeditionspb.TestRequired{},
56 },
57 want: `goproto.proto.testeditions.TestRequired.required_field`,
58 }, {
59 m: &testeditionspb.TestRequiredForeign{
60 RepeatedMessage: []*testeditionspb.TestRequired{
61 {RequiredField: proto.Int32(1)},
62 {},
63 },
64 },
65 want: `goproto.proto.testeditions.TestRequired.required_field`,
66 }, {
67 m: &testeditionspb.TestRequiredForeign{
68 MapMessage: map[int32]*testeditionspb.TestRequired{
69 1: {},
70 },
71 },
72 want: `goproto.proto.testeditions.TestRequired.required_field`,
73 }, {
74 m: &testpb.TestWeak{},
75 want: `<nil>`,
76 skip: !flags.ProtoLegacy,
77 }, {
78 m: func() proto.Message {
79 m := &testpb.TestWeak{}
80 m.SetWeakMessage1(&weakpb.WeakImportMessage1{})
81 return m
82 }(),
83 want: `goproto.proto.test.weak.WeakImportMessage1.a`,
84 skip: !flags.ProtoLegacy,
85 }, {
86 m: func() proto.Message {
87 m := &testpb.TestWeak{}
88 m.SetWeakMessage1(&weakpb.WeakImportMessage1{
89 A: proto.Int32(1),
90 })
91 return m
92 }(),
93 want: `<nil>`,
94 skip: !flags.ProtoLegacy,
95 }}
96
97 for _, tt := range tests {
98 t.Run("", func(t *testing.T) {
99 if tt.skip {
100 t.SkipNow()
101 }
102
103 err := proto.CheckInitialized(tt.m)
104 got := "<nil>"
105 if err != nil {
106 got = fmt.Sprintf("%q", err)
107 }
108 if !strings.Contains(got, tt.want) {
109 t.Errorf("CheckInitialized(m):\n got: %v\nwant contains: %v\nMessage:\n%v", got, tt.want, prototext.Format(tt.m))
110 }
111 })
112 }
113 }
114
View as plain text