...
1
2
3
4
5 package protodesc
6
7 import (
8 "testing"
9
10 "github.com/google/go-cmp/cmp"
11 "google.golang.org/protobuf/internal/filedesc"
12 "google.golang.org/protobuf/proto"
13 "google.golang.org/protobuf/reflect/protoreflect"
14 "google.golang.org/protobuf/testing/protocmp"
15 "google.golang.org/protobuf/types/descriptorpb"
16 )
17
18 func TestEditionsRequired(t *testing.T) {
19 fd := new(filedesc.Field)
20 fd.L0.ParentFile = filedesc.SurrogateEdition2023
21 fd.L0.FullName = "foo_field"
22 fd.L1.Number = 1337
23 fd.L1.Cardinality = protoreflect.Required
24 fd.L1.Kind = protoreflect.BytesKind
25
26 want := &descriptorpb.FieldDescriptorProto{
27 Name: proto.String("foo_field"),
28 Number: proto.Int32(1337),
29 Label: descriptorpb.FieldDescriptorProto_LABEL_OPTIONAL.Enum(),
30 Type: descriptorpb.FieldDescriptorProto_TYPE_BYTES.Enum(),
31 }
32
33 got := ToFieldDescriptorProto(fd)
34 if diff := cmp.Diff(want, got, protocmp.Transform()); diff != "" {
35 t.Errorf("ToFieldDescriptor: unexpected diff (-want +got):\n%s", diff)
36 }
37 }
38
39 func TestProto2Required(t *testing.T) {
40 fd := new(filedesc.Field)
41 fd.L0.ParentFile = filedesc.SurrogateProto2
42 fd.L0.FullName = "foo_field"
43 fd.L1.Number = 1337
44 fd.L1.Cardinality = protoreflect.Required
45 fd.L1.Kind = protoreflect.BytesKind
46
47 want := &descriptorpb.FieldDescriptorProto{
48 Name: proto.String("foo_field"),
49 Number: proto.Int32(1337),
50 Label: descriptorpb.FieldDescriptorProto_LABEL_REQUIRED.Enum(),
51 Type: descriptorpb.FieldDescriptorProto_TYPE_BYTES.Enum(),
52 }
53
54 got := ToFieldDescriptorProto(fd)
55 if diff := cmp.Diff(want, got, protocmp.Transform()); diff != "" {
56 t.Errorf("ToFieldDescriptor: unexpected diff (-want +got):\n%s", diff)
57 }
58 }
59
60 func TestEditionsDelimited(t *testing.T) {
61 md := new(filedesc.Message)
62 md.L0.ParentFile = filedesc.SurrogateEdition2023
63 md.L0.FullName = "foo_message"
64 fd := new(filedesc.Field)
65 fd.L0.ParentFile = filedesc.SurrogateEdition2023
66 fd.L0.FullName = "foo_field"
67 fd.L1.Number = 1337
68 fd.L1.Cardinality = protoreflect.Optional
69 fd.L1.Kind = protoreflect.GroupKind
70 fd.L1.Message = md
71
72 want := &descriptorpb.FieldDescriptorProto{
73 Name: proto.String("foo_field"),
74 Number: proto.Int32(1337),
75 Label: descriptorpb.FieldDescriptorProto_LABEL_OPTIONAL.Enum(),
76 Type: descriptorpb.FieldDescriptorProto_TYPE_MESSAGE.Enum(),
77 TypeName: proto.String(".foo_message"),
78 }
79
80 got := ToFieldDescriptorProto(fd)
81 if diff := cmp.Diff(want, got, protocmp.Transform()); diff != "" {
82 t.Errorf("ToFieldDescriptor: unexpected diff (-want +got):\n%s", diff)
83 }
84 }
85
86 func TestProto2Group(t *testing.T) {
87 md := new(filedesc.Message)
88 md.L0.ParentFile = filedesc.SurrogateProto2
89 md.L0.FullName = "foo_message"
90 fd := new(filedesc.Field)
91 fd.L0.ParentFile = filedesc.SurrogateProto2
92 fd.L0.FullName = "foo_field"
93 fd.L1.Number = 1337
94 fd.L1.Cardinality = protoreflect.Optional
95 fd.L1.Kind = protoreflect.GroupKind
96 fd.L1.Message = md
97
98 want := &descriptorpb.FieldDescriptorProto{
99 Name: proto.String("foo_field"),
100 Number: proto.Int32(1337),
101 Label: descriptorpb.FieldDescriptorProto_LABEL_OPTIONAL.Enum(),
102 Type: descriptorpb.FieldDescriptorProto_TYPE_GROUP.Enum(),
103 TypeName: proto.String(".foo_message"),
104 }
105
106 got := ToFieldDescriptorProto(fd)
107 if diff := cmp.Diff(want, got, protocmp.Transform()); diff != "" {
108 t.Errorf("ToFieldDescriptor: unexpected diff (-want +got):\n%s", diff)
109 }
110 }
111
View as plain text