protobuf-go/internal/genid/detect_test.go
Joe Tsai e0b77db13b internal/genid: add new package for generated identifiers
The genid package unifies the genname, fieldnum, and detectknown
packages into a single package.

Whenever possible use the generated constants rather than
hard-coded literals. This makes it easier to search the entire
module for special logic that deal with well-known types.

Change-Id: I13beff1f4149444a0c0b9e607ebf759657f000f4
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/235301
Reviewed-by: Herbie Ong <herbie@google.com>
2020-05-29 07:08:23 +00:00

73 lines
2.4 KiB
Go

// Copyright 2020 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 genid_test
import (
"testing"
"google.golang.org/protobuf/internal/genid"
"google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/types/descriptorpb"
"google.golang.org/protobuf/types/known/anypb"
"google.golang.org/protobuf/types/known/apipb"
"google.golang.org/protobuf/types/known/durationpb"
"google.golang.org/protobuf/types/known/emptypb"
"google.golang.org/protobuf/types/known/fieldmaskpb"
"google.golang.org/protobuf/types/known/sourcecontextpb"
"google.golang.org/protobuf/types/known/structpb"
"google.golang.org/protobuf/types/known/timestamppb"
"google.golang.org/protobuf/types/known/typepb"
"google.golang.org/protobuf/types/known/wrapperspb"
"google.golang.org/protobuf/types/pluginpb"
)
func TestWhich(t *testing.T) {
tests := []struct {
in protoreflect.FileDescriptor
want genid.ProtoFile
}{
{descriptorpb.File_google_protobuf_descriptor_proto, genid.Unknown_file},
{pluginpb.File_google_protobuf_compiler_plugin_proto, genid.Unknown_file},
{anypb.File_google_protobuf_any_proto, genid.Any_file},
{timestamppb.File_google_protobuf_timestamp_proto, genid.Timestamp_file},
{durationpb.File_google_protobuf_duration_proto, genid.Duration_file},
{wrapperspb.File_google_protobuf_wrappers_proto, genid.Wrappers_file},
{structpb.File_google_protobuf_struct_proto, genid.Struct_file},
{fieldmaskpb.File_google_protobuf_field_mask_proto, genid.FieldMask_file},
{apipb.File_google_protobuf_api_proto, genid.Api_file},
{typepb.File_google_protobuf_type_proto, genid.Type_file},
{sourcecontextpb.File_google_protobuf_source_context_proto, genid.SourceContext_file},
{emptypb.File_google_protobuf_empty_proto, genid.Empty_file},
}
for _, tt := range tests {
rangeDescriptors(tt.in, func(d protoreflect.Descriptor) {
got := genid.WhichFile(d.FullName())
if got != tt.want {
t.Errorf("Which(%s) = %v, want %v", d.FullName(), got, tt.want)
}
})
}
}
func rangeDescriptors(d interface {
Enums() protoreflect.EnumDescriptors
Messages() protoreflect.MessageDescriptors
}, f func(protoreflect.Descriptor)) {
for i := 0; i < d.Enums().Len(); i++ {
ed := d.Enums().Get(i)
f(ed)
}
for i := 0; i < d.Messages().Len(); i++ {
md := d.Messages().Get(i)
if md.IsMapEntry() {
continue
}
f(md)
rangeDescriptors(md, f)
}
}