blob: e9f656c2dd7409ef4dabb4d6ee42b926aba1903c [file] [log] [blame]
// 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)
}
}