File: proto_test.go

package info (click to toggle)
golang-google-protobuf 1.36.7-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental, forky, sid
  • size: 14,996 kB
  • sloc: sh: 94; makefile: 4
file content (110 lines) | stat: -rw-r--r-- 3,520 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Copyright 2018 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package protodesc

import (
	"testing"

	"github.com/google/go-cmp/cmp"
	"google.golang.org/protobuf/internal/filedesc"
	"google.golang.org/protobuf/proto"
	"google.golang.org/protobuf/reflect/protoreflect"
	"google.golang.org/protobuf/testing/protocmp"
	"google.golang.org/protobuf/types/descriptorpb"
)

func TestEditionsRequired(t *testing.T) {
	fd := new(filedesc.Field)
	fd.L0.ParentFile = filedesc.SurrogateEdition2023
	fd.L0.FullName = "foo_field"
	fd.L1.Number = 1337
	fd.L1.Cardinality = protoreflect.Required
	fd.L1.Kind = protoreflect.BytesKind

	want := &descriptorpb.FieldDescriptorProto{
		Name:   proto.String("foo_field"),
		Number: proto.Int32(1337),
		Label:  descriptorpb.FieldDescriptorProto_LABEL_OPTIONAL.Enum(),
		Type:   descriptorpb.FieldDescriptorProto_TYPE_BYTES.Enum(),
	}

	got := ToFieldDescriptorProto(fd)
	if diff := cmp.Diff(want, got, protocmp.Transform()); diff != "" {
		t.Errorf("ToFieldDescriptor: unexpected diff (-want +got):\n%s", diff)
	}
}

func TestProto2Required(t *testing.T) {
	fd := new(filedesc.Field)
	fd.L0.ParentFile = filedesc.SurrogateProto2
	fd.L0.FullName = "foo_field"
	fd.L1.Number = 1337
	fd.L1.Cardinality = protoreflect.Required
	fd.L1.Kind = protoreflect.BytesKind

	want := &descriptorpb.FieldDescriptorProto{
		Name:   proto.String("foo_field"),
		Number: proto.Int32(1337),
		Label:  descriptorpb.FieldDescriptorProto_LABEL_REQUIRED.Enum(),
		Type:   descriptorpb.FieldDescriptorProto_TYPE_BYTES.Enum(),
	}

	got := ToFieldDescriptorProto(fd)
	if diff := cmp.Diff(want, got, protocmp.Transform()); diff != "" {
		t.Errorf("ToFieldDescriptor: unexpected diff (-want +got):\n%s", diff)
	}
}

func TestEditionsDelimited(t *testing.T) {
	md := new(filedesc.Message)
	md.L0.ParentFile = filedesc.SurrogateEdition2023
	md.L0.FullName = "foo_message"
	fd := new(filedesc.Field)
	fd.L0.ParentFile = filedesc.SurrogateEdition2023
	fd.L0.FullName = "foo_field"
	fd.L1.Number = 1337
	fd.L1.Cardinality = protoreflect.Optional
	fd.L1.Kind = protoreflect.GroupKind
	fd.L1.Message = md

	want := &descriptorpb.FieldDescriptorProto{
		Name:     proto.String("foo_field"),
		Number:   proto.Int32(1337),
		Label:    descriptorpb.FieldDescriptorProto_LABEL_OPTIONAL.Enum(),
		Type:     descriptorpb.FieldDescriptorProto_TYPE_MESSAGE.Enum(),
		TypeName: proto.String(".foo_message"),
	}

	got := ToFieldDescriptorProto(fd)
	if diff := cmp.Diff(want, got, protocmp.Transform()); diff != "" {
		t.Errorf("ToFieldDescriptor: unexpected diff (-want +got):\n%s", diff)
	}
}

func TestProto2Group(t *testing.T) {
	md := new(filedesc.Message)
	md.L0.ParentFile = filedesc.SurrogateProto2
	md.L0.FullName = "foo_message"
	fd := new(filedesc.Field)
	fd.L0.ParentFile = filedesc.SurrogateProto2
	fd.L0.FullName = "foo_field"
	fd.L1.Number = 1337
	fd.L1.Cardinality = protoreflect.Optional
	fd.L1.Kind = protoreflect.GroupKind
	fd.L1.Message = md

	want := &descriptorpb.FieldDescriptorProto{
		Name:     proto.String("foo_field"),
		Number:   proto.Int32(1337),
		Label:    descriptorpb.FieldDescriptorProto_LABEL_OPTIONAL.Enum(),
		Type:     descriptorpb.FieldDescriptorProto_TYPE_GROUP.Enum(),
		TypeName: proto.String(".foo_message"),
	}

	got := ToFieldDescriptorProto(fd)
	if diff := cmp.Diff(want, got, protocmp.Transform()); diff != "" {
		t.Errorf("ToFieldDescriptor: unexpected diff (-want +got):\n%s", diff)
	}
}