...

Source file src/github.com/golang/protobuf/descriptor/descriptor_test.go

Documentation: github.com/golang/protobuf/descriptor

     1  // Copyright 2020 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package descriptor
     6  
     7  import (
     8  	"testing"
     9  
    10  	"github.com/google/go-cmp/cmp"
    11  	"google.golang.org/protobuf/reflect/protoreflect"
    12  
    13  	descpb "github.com/golang/protobuf/protoc-gen-go/descriptor"
    14  )
    15  
    16  func TestEnumDescriptor(t *testing.T) {
    17  	tests := []struct {
    18  		enum protoreflect.Enum
    19  		idxs []int
    20  		name string
    21  	}{{
    22  		enum: descpb.FieldDescriptorProto_Type(0),
    23  		idxs: []int{
    24  			new(descpb.FieldDescriptorProto).ProtoReflect().Descriptor().Index(),
    25  			new(descpb.FieldDescriptorProto_Type).Descriptor().Index(),
    26  		},
    27  		name: "Type",
    28  	}, {
    29  		enum: descpb.FieldOptions_CType(0),
    30  		idxs: []int{
    31  			new(descpb.FieldOptions).ProtoReflect().Descriptor().Index(),
    32  			new(descpb.FieldOptions_CType).Descriptor().Index(),
    33  		},
    34  		name: "CType",
    35  	}}
    36  
    37  	for _, tt := range tests {
    38  		e := struct{ protoreflect.Enum }{tt.enum} // v2-only enum
    39  
    40  		_, idxs := EnumRawDescriptor(e)
    41  		if diff := cmp.Diff(tt.idxs, idxs); diff != "" {
    42  			t.Errorf("path index mismatch (-want +got):\n%v", diff)
    43  		}
    44  
    45  		_, ed := EnumDescriptorProto(e)
    46  		if ed.GetName() != tt.name {
    47  			t.Errorf("mismatching enum name: got %v, want %v", ed.GetName(), tt.name)
    48  		}
    49  	}
    50  }
    51  
    52  func TestMessageDescriptor(t *testing.T) {
    53  	tests := []struct {
    54  		message protoreflect.ProtoMessage
    55  		idxs    []int
    56  		name    string
    57  	}{{
    58  		message: (*descpb.SourceCodeInfo_Location)(nil),
    59  		idxs: []int{
    60  			new(descpb.SourceCodeInfo).ProtoReflect().Descriptor().Index(),
    61  			new(descpb.SourceCodeInfo_Location).ProtoReflect().Descriptor().Index(),
    62  		},
    63  		name: "Location",
    64  	}, {
    65  		message: (*descpb.FileDescriptorProto)(nil),
    66  		idxs: []int{
    67  			new(descpb.FileDescriptorProto).ProtoReflect().Descriptor().Index(),
    68  		},
    69  		name: "FileDescriptorProto",
    70  	}}
    71  
    72  	for _, tt := range tests {
    73  		m := struct{ protoreflect.ProtoMessage }{tt.message} // v2-only message
    74  
    75  		_, idxs := MessageRawDescriptor(m)
    76  		if diff := cmp.Diff(tt.idxs, idxs); diff != "" {
    77  			t.Errorf("path index mismatch (-want +got):\n%v", diff)
    78  		}
    79  
    80  		_, md := MessageDescriptorProto(m)
    81  		if md.GetName() != tt.name {
    82  			t.Errorf("mismatching message name: got %v, want %v", md.GetName(), tt.name)
    83  		}
    84  	}
    85  }
    86  

View as plain text