...
1
2
3
4
5 package descfmt_test
6
7 import (
8 "reflect"
9 "testing"
10
11 "google.golang.org/protobuf/internal/descfmt"
12 "google.golang.org/protobuf/reflect/protoreflect"
13
14 testpb "google.golang.org/protobuf/internal/testprotos/test"
15 )
16
17
18 func TestDescriptorAccessors(t *testing.T) {
19 ignore := map[string]bool{
20 "ParentFile": true,
21 "Parent": true,
22 "Index": true,
23 "Syntax": true,
24 "Name": true,
25 "FullName": true,
26 "IsPlaceholder": true,
27 "Options": true,
28 "ProtoInternal": true,
29 "ProtoType": true,
30
31 "TextName": true,
32 "HasOptionalKeyword": true,
33 "IsSynthetic": true,
34
35 "SourceLocations": true,
36 "ExtensionRangeOptions": true,
37 "DefaultEnumValue": true,
38 "MapKey": true,
39 "MapValue": true,
40 }
41
42 fileDesc := testpb.File_internal_testprotos_test_test_proto
43 msgDesc := (&testpb.TestAllTypes{}).ProtoReflect().Descriptor()
44 fields := msgDesc.Fields()
45 fieldDesc := fields.ByName("oneof_uint32")
46 oneofDesc := fieldDesc.ContainingOneof()
47 enumDesc := fields.ByName("optional_nested_enum").Enum()
48 enumValueDesc := fields.ByName("default_nested_enum").DefaultEnumValue()
49 services := fileDesc.Services()
50 serviceDesc := services.Get(0)
51 methodDesc := serviceDesc.Methods().Get(0)
52 rmsgDesc := (&testpb.TestNestedExtension{}).ProtoReflect().Descriptor()
53 rfieldDesc := rmsgDesc.Extensions().Get(0)
54 descriptors := map[reflect.Type][]protoreflect.Descriptor{
55 reflect.TypeOf((*protoreflect.FileDescriptor)(nil)).Elem(): {fileDesc},
56 reflect.TypeOf((*protoreflect.MessageDescriptor)(nil)).Elem(): {msgDesc},
57 reflect.TypeOf((*protoreflect.FieldDescriptor)(nil)).Elem(): {fieldDesc, rfieldDesc},
58 reflect.TypeOf((*protoreflect.OneofDescriptor)(nil)).Elem(): {oneofDesc},
59 reflect.TypeOf((*protoreflect.EnumDescriptor)(nil)).Elem(): {enumDesc},
60 reflect.TypeOf((*protoreflect.EnumValueDescriptor)(nil)).Elem(): {enumValueDesc},
61 reflect.TypeOf((*protoreflect.ServiceDescriptor)(nil)).Elem(): {serviceDesc},
62 reflect.TypeOf((*protoreflect.MethodDescriptor)(nil)).Elem(): {methodDesc},
63 }
64 for rt, descs := range descriptors {
65 var m []string
66 for _, desc := range descs {
67
68 descfmt.InternalFormatDescOptForTesting(desc, true, false, func(name string) {
69 m = append(m, name)
70 })
71 }
72
73 got := map[string]bool{}
74 for _, s := range m {
75 got[s] = true
76 }
77 want := map[string]bool{}
78 for i := 0; i < rt.NumMethod(); i++ {
79 want[rt.Method(i).Name] = true
80 }
81
82
83
84 for s := range got {
85 if !want[s] && !ignore[s] {
86 t.Errorf("%v.%v does not exist", rt, s)
87 }
88 }
89
90
91
92
93 for s := range want {
94 if !got[s] && !ignore[s] {
95 t.Errorf("%v.%v is not called by formatter", rt, s)
96 }
97 }
98 }
99 }
100
View as plain text