...

Source file src/google.golang.org/protobuf/internal/descfmt/desc_test.go

Documentation: google.golang.org/protobuf/internal/descfmt

     1  // Copyright 2018 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 descfmt_test
     6  
     7  import (
     8  	"reflect"
     9  	"testing"
    10  
    11  	"google.golang.org/protobuf/internal/descfmt"
    12  	"google.golang.org/protobuf/reflect/protoreflect"
    13  
    14  	testpb "google.golang.org/protobuf/internal/testprotos/test"
    15  )
    16  
    17  // TestDescriptorAccessors tests that descriptorAccessors is up-to-date.
    18  func TestDescriptorAccessors(t *testing.T) {
    19  	ignore := map[string]bool{
    20  		"ParentFile":    true,
    21  		"Parent":        true,
    22  		"Index":         true,
    23  		"Syntax":        true,
    24  		"Name":          true,
    25  		"FullName":      true,
    26  		"IsPlaceholder": true,
    27  		"Options":       true,
    28  		"ProtoInternal": true,
    29  		"ProtoType":     true,
    30  
    31  		"TextName":           true, // derived from other fields
    32  		"HasOptionalKeyword": true, // captured by HasPresence
    33  		"IsSynthetic":        true, // captured by HasPresence
    34  
    35  		"SourceLocations":       true, // specific to FileDescriptor
    36  		"ExtensionRangeOptions": true, // specific to MessageDescriptor
    37  		"DefaultEnumValue":      true, // specific to FieldDescriptor
    38  		"MapKey":                true, // specific to FieldDescriptor
    39  		"MapValue":              true, // specific to FieldDescriptor
    40  	}
    41  
    42  	fileDesc := testpb.File_internal_testprotos_test_test_proto
    43  	msgDesc := (&testpb.TestAllTypes{}).ProtoReflect().Descriptor()
    44  	fields := msgDesc.Fields()
    45  	fieldDesc := fields.ByName("oneof_uint32")
    46  	oneofDesc := fieldDesc.ContainingOneof()
    47  	enumDesc := fields.ByName("optional_nested_enum").Enum()
    48  	enumValueDesc := fields.ByName("default_nested_enum").DefaultEnumValue()
    49  	services := fileDesc.Services()
    50  	serviceDesc := services.Get(0)
    51  	methodDesc := serviceDesc.Methods().Get(0)
    52  	rmsgDesc := (&testpb.TestNestedExtension{}).ProtoReflect().Descriptor()
    53  	rfieldDesc := rmsgDesc.Extensions().Get(0)
    54  	descriptors := map[reflect.Type][]protoreflect.Descriptor{
    55  		reflect.TypeOf((*protoreflect.FileDescriptor)(nil)).Elem():      {fileDesc},
    56  		reflect.TypeOf((*protoreflect.MessageDescriptor)(nil)).Elem():   {msgDesc},
    57  		reflect.TypeOf((*protoreflect.FieldDescriptor)(nil)).Elem():     {fieldDesc, rfieldDesc},
    58  		reflect.TypeOf((*protoreflect.OneofDescriptor)(nil)).Elem():     {oneofDesc},
    59  		reflect.TypeOf((*protoreflect.EnumDescriptor)(nil)).Elem():      {enumDesc},
    60  		reflect.TypeOf((*protoreflect.EnumValueDescriptor)(nil)).Elem(): {enumValueDesc},
    61  		reflect.TypeOf((*protoreflect.ServiceDescriptor)(nil)).Elem():   {serviceDesc},
    62  		reflect.TypeOf((*protoreflect.MethodDescriptor)(nil)).Elem():    {methodDesc},
    63  	}
    64  	for rt, descs := range descriptors {
    65  		var m []string
    66  		for _, desc := range descs {
    67  
    68  			descfmt.InternalFormatDescOptForTesting(desc, true, false, func(name string) {
    69  				m = append(m, name)
    70  			})
    71  		}
    72  
    73  		got := map[string]bool{}
    74  		for _, s := range m {
    75  			got[s] = true
    76  		}
    77  		want := map[string]bool{}
    78  		for i := 0; i < rt.NumMethod(); i++ {
    79  			want[rt.Method(i).Name] = true
    80  		}
    81  
    82  		// Check if descriptorAccessors contains a non-existent accessor.
    83  		// If this test fails, remove the accessor from descriptorAccessors.
    84  		for s := range got {
    85  			if !want[s] && !ignore[s] {
    86  				t.Errorf("%v.%v does not exist", rt, s)
    87  			}
    88  		}
    89  
    90  		// Check if there are new protoreflect interface methods that are not
    91  		// handled by the formatter. If this fails, either add the method to
    92  		// ignore or add them to descriptorAccessors.
    93  		for s := range want {
    94  			if !got[s] && !ignore[s] {
    95  				t.Errorf("%v.%v is not called by formatter", rt, s)
    96  			}
    97  		}
    98  	}
    99  }
   100  

View as plain text