1
2
3
4
5 package filedesc_test
6
7 import (
8 "bytes"
9 "compress/gzip"
10 "io/ioutil"
11 "testing"
12
13 "google.golang.org/protobuf/proto"
14 "google.golang.org/protobuf/reflect/protodesc"
15 "google.golang.org/protobuf/reflect/protoreflect"
16
17 testpb "google.golang.org/protobuf/internal/testprotos/test"
18 _ "google.golang.org/protobuf/internal/testprotos/test/weak1"
19 "google.golang.org/protobuf/types/descriptorpb"
20 )
21
22 var testFile = new(testpb.TestAllTypes).ProtoReflect().Descriptor().ParentFile()
23
24 func TestInit(t *testing.T) {
25
26
27
28
29
30
31 got := protodesc.ToFileDescriptorProto(testFile)
32
33 want := &descriptorpb.FileDescriptorProto{}
34 zb, _ := (&testpb.TestAllTypes{}).Descriptor()
35 r, _ := gzip.NewReader(bytes.NewBuffer(zb))
36 b, _ := ioutil.ReadAll(r)
37 if err := proto.Unmarshal(b, want); err != nil {
38 t.Fatal(err)
39 }
40
41 if !proto.Equal(got, want) {
42 t.Errorf("protodesc.ToFileDescriptorProto(testpb.Test_protoFile) is not equal to the protoc-generated FileDescriptorProto for internal/testprotos/test/test.proto")
43 }
44
45
46 seen := make(map[protoreflect.FullName]bool)
47 visitFields(want.ProtoReflect(), func(field protoreflect.FieldDescriptor) {
48 seen[field.FullName()] = true
49 })
50 descFile := new(descriptorpb.DescriptorProto).ProtoReflect().Descriptor().ParentFile()
51 descPkg := descFile.Package()
52 ignore := map[protoreflect.FullName]bool{
53
54 descPkg.Append("FileDescriptorProto.source_code_info"): true,
55 descPkg.Append("FileDescriptorProto.syntax"): true,
56
57 descPkg.Append("FileDescriptorProto.edition"): true,
58
59
60 descPkg.Append("FieldDescriptorProto.proto3_optional"): true,
61
62
63
64
65
66 descPkg.Append("OneofDescriptorProto.options"): true,
67 }
68 for _, messageName := range []protoreflect.Name{
69 "FileDescriptorProto",
70 "DescriptorProto",
71 "FieldDescriptorProto",
72 "OneofDescriptorProto",
73 "EnumDescriptorProto",
74 "EnumValueDescriptorProto",
75 } {
76 message := descFile.Messages().ByName(messageName)
77 for i, fields := 0, message.Fields(); i < fields.Len(); i++ {
78 if name := fields.Get(i).FullName(); !seen[name] && !ignore[name] {
79 t.Errorf("No test for descriptor field: %v", name)
80 }
81 }
82 }
83
84
85 mapEntryName := protoreflect.FullName("goproto.proto.test.TestAllTypes.MapInt32Int32Entry")
86 d := testFile.Messages().ByName("TestAllTypes").Fields().ByName("map_int32_int32").Message()
87 if gotName, wantName := d.FullName(), mapEntryName; gotName != wantName {
88 t.Fatalf("looked up wrong descriptor: got %v, want %v", gotName, wantName)
89 }
90 if _, ok := d.(protoreflect.MessageType); ok {
91 t.Errorf("message descriptor for %v must not implement protoreflect.MessageType", mapEntryName)
92 }
93 }
94
95
96 func visitFields(m protoreflect.Message, f func(protoreflect.FieldDescriptor)) {
97 m.Range(func(fd protoreflect.FieldDescriptor, value protoreflect.Value) bool {
98 f(fd)
99 switch fd.Kind() {
100 case protoreflect.MessageKind, protoreflect.GroupKind:
101 if fd.IsList() {
102 for i, list := 0, value.List(); i < list.Len(); i++ {
103 visitFields(list.Get(i).Message(), f)
104 }
105 } else {
106 visitFields(value.Message(), f)
107 }
108 }
109 return true
110 })
111 }
112
113 func TestWeakInit(t *testing.T) {
114
115 fd1 := testFile.Messages().ByName("TestWeak").Fields().ByName("weak_message1")
116 if got, want := fd1.IsWeak(), true; got != want {
117 t.Errorf("field %v: IsWeak() = %v, want %v", fd1.FullName(), got, want)
118 }
119 if got, want := fd1.Message().IsPlaceholder(), false; got != want {
120 t.Errorf("field %v: Message.IsPlaceholder() = %v, want %v", fd1.FullName(), got, want)
121 }
122 if got, want := fd1.Message().Fields().Len(), 1; got != want {
123 t.Errorf("field %v: Message().Fields().Len() == %d, want %d", fd1.FullName(), got, want)
124 }
125
126
127 fd2 := testFile.Messages().ByName("TestWeak").Fields().ByName("weak_message2")
128 if got, want := fd2.IsWeak(), true; got != want {
129 t.Errorf("field %v: IsWeak() = %v, want %v", fd2.FullName(), got, want)
130 }
131 if got, want := fd2.Message().IsPlaceholder(), true; got != want {
132 t.Errorf("field %v: Message.IsPlaceholder() = %v, want %v", fd2.FullName(), got, want)
133 }
134 if got, want := fd2.Message().Fields().Len(), 0; got != want {
135 t.Errorf("field %v: Message().Fields().Len() == %d, want %d", fd2.FullName(), got, want)
136 }
137 }
138
View as plain text