...

Source file src/google.golang.org/protobuf/internal/impl/legacy_file_test.go

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

     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 impl_test
     6  
     7  import (
     8  	"bytes"
     9  	"compress/gzip"
    10  	"io/ioutil"
    11  	"reflect"
    12  	"testing"
    13  
    14  	"github.com/google/go-cmp/cmp"
    15  
    16  	"google.golang.org/protobuf/internal/impl"
    17  	"google.golang.org/protobuf/internal/pragma"
    18  	"google.golang.org/protobuf/proto"
    19  	"google.golang.org/protobuf/reflect/protodesc"
    20  	"google.golang.org/protobuf/reflect/protoreflect"
    21  
    22  	proto2_20160225 "google.golang.org/protobuf/internal/testprotos/legacy/proto2_20160225_2fc053c5"
    23  	proto2_20160519 "google.golang.org/protobuf/internal/testprotos/legacy/proto2_20160519_a4ab9ec5"
    24  	proto2_20180125 "google.golang.org/protobuf/internal/testprotos/legacy/proto2_20180125_92554152"
    25  	proto2_20180430 "google.golang.org/protobuf/internal/testprotos/legacy/proto2_20180430_b4deda09"
    26  	proto2_20180814 "google.golang.org/protobuf/internal/testprotos/legacy/proto2_20180814_aa810b61"
    27  	proto2_20190205 "google.golang.org/protobuf/internal/testprotos/legacy/proto2_20190205_c823c79e"
    28  	proto3_20160225 "google.golang.org/protobuf/internal/testprotos/legacy/proto3_20160225_2fc053c5"
    29  	proto3_20160519 "google.golang.org/protobuf/internal/testprotos/legacy/proto3_20160519_a4ab9ec5"
    30  	proto3_20180125 "google.golang.org/protobuf/internal/testprotos/legacy/proto3_20180125_92554152"
    31  	proto3_20180430 "google.golang.org/protobuf/internal/testprotos/legacy/proto3_20180430_b4deda09"
    32  	proto3_20180814 "google.golang.org/protobuf/internal/testprotos/legacy/proto3_20180814_aa810b61"
    33  	proto3_20190205 "google.golang.org/protobuf/internal/testprotos/legacy/proto3_20190205_c823c79e"
    34  	"google.golang.org/protobuf/types/descriptorpb"
    35  )
    36  
    37  func mustLoadFileDesc(b []byte, _ []int) protoreflect.FileDescriptor {
    38  	zr, err := gzip.NewReader(bytes.NewReader(b))
    39  	if err != nil {
    40  		panic(err)
    41  	}
    42  	b, err = ioutil.ReadAll(zr)
    43  	if err != nil {
    44  		panic(err)
    45  	}
    46  	p := new(descriptorpb.FileDescriptorProto)
    47  	err = proto.UnmarshalOptions{DiscardUnknown: true}.Unmarshal(b, p)
    48  	if err != nil {
    49  		panic(err)
    50  	}
    51  	fd, err := protodesc.NewFile(p, nil)
    52  	if err != nil {
    53  		panic(err)
    54  	}
    55  	return fd
    56  }
    57  
    58  func TestDescriptor(t *testing.T) {
    59  	var tests []struct{ got, want protoreflect.Descriptor }
    60  
    61  	fileDescP2_20160225 := mustLoadFileDesc(new(proto2_20160225.Message).Descriptor())
    62  	tests = append(tests, []struct{ got, want protoreflect.Descriptor }{{
    63  		got:  impl.LegacyLoadEnumDesc(reflect.TypeOf(proto2_20160225.SiblingEnum(0))),
    64  		want: fileDescP2_20160225.Enums().ByName("SiblingEnum"),
    65  	}, {
    66  		got:  impl.LegacyLoadEnumDesc(reflect.TypeOf(proto2_20160225.Message_ChildEnum(0))),
    67  		want: fileDescP2_20160225.Messages().ByName("Message").Enums().ByName("ChildEnum"),
    68  	}, {
    69  		got:  impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto2_20160225.SiblingMessage))),
    70  		want: fileDescP2_20160225.Messages().ByName("SiblingMessage"),
    71  	}, {
    72  		got:  impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto2_20160225.Message_ChildMessage))),
    73  		want: fileDescP2_20160225.Messages().ByName("Message").Messages().ByName("ChildMessage"),
    74  	}, {
    75  		got:  impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto2_20160225.Message))),
    76  		want: fileDescP2_20160225.Messages().ByName("Message"),
    77  	}, {
    78  		got:  impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto2_20160225.Message_NamedGroup))),
    79  		want: fileDescP2_20160225.Messages().ByName("Message").Messages().ByName("NamedGroup"),
    80  	}, {
    81  		got:  impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto2_20160225.Message_OptionalGroup))),
    82  		want: fileDescP2_20160225.Messages().ByName("Message").Messages().ByName("OptionalGroup"),
    83  	}, {
    84  		got:  impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto2_20160225.Message_RequiredGroup))),
    85  		want: fileDescP2_20160225.Messages().ByName("Message").Messages().ByName("RequiredGroup"),
    86  	}, {
    87  		got:  impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto2_20160225.Message_RepeatedGroup))),
    88  		want: fileDescP2_20160225.Messages().ByName("Message").Messages().ByName("RepeatedGroup"),
    89  	}, {
    90  		got:  impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto2_20160225.Message_OneofGroup))),
    91  		want: fileDescP2_20160225.Messages().ByName("Message").Messages().ByName("OneofGroup"),
    92  	}, {
    93  		got:  impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto2_20160225.Message_ExtensionOptionalGroup))),
    94  		want: fileDescP2_20160225.Messages().ByName("Message").Messages().ByName("ExtensionOptionalGroup"),
    95  	}, {
    96  		got:  impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto2_20160225.Message_ExtensionRepeatedGroup))),
    97  		want: fileDescP2_20160225.Messages().ByName("Message").Messages().ByName("ExtensionRepeatedGroup"),
    98  	}}...)
    99  
   100  	fileDescP3_20160225 := mustLoadFileDesc(new(proto3_20160225.Message).Descriptor())
   101  	tests = append(tests, []struct{ got, want protoreflect.Descriptor }{{
   102  		got:  impl.LegacyLoadEnumDesc(reflect.TypeOf(proto3_20160225.SiblingEnum(0))),
   103  		want: fileDescP3_20160225.Enums().ByName("SiblingEnum"),
   104  	}, {
   105  		got:  impl.LegacyLoadEnumDesc(reflect.TypeOf(proto3_20160225.Message_ChildEnum(0))),
   106  		want: fileDescP3_20160225.Messages().ByName("Message").Enums().ByName("ChildEnum"),
   107  	}, {
   108  		got:  impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto3_20160225.SiblingMessage))),
   109  		want: fileDescP3_20160225.Messages().ByName("SiblingMessage"),
   110  	}, {
   111  		got:  impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto3_20160225.Message_ChildMessage))),
   112  		want: fileDescP3_20160225.Messages().ByName("Message").Messages().ByName("ChildMessage"),
   113  	}, {
   114  		got:  impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto3_20160225.Message))),
   115  		want: fileDescP3_20160225.Messages().ByName("Message"),
   116  	}}...)
   117  
   118  	fileDescP2_20160519 := mustLoadFileDesc(new(proto2_20160519.Message).Descriptor())
   119  	tests = append(tests, []struct{ got, want protoreflect.Descriptor }{{
   120  		got:  impl.LegacyLoadEnumDesc(reflect.TypeOf(proto2_20160519.SiblingEnum(0))),
   121  		want: fileDescP2_20160519.Enums().ByName("SiblingEnum"),
   122  	}, {
   123  		got:  impl.LegacyLoadEnumDesc(reflect.TypeOf(proto2_20160519.Message_ChildEnum(0))),
   124  		want: fileDescP2_20160519.Messages().ByName("Message").Enums().ByName("ChildEnum"),
   125  	}, {
   126  		got:  impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto2_20160519.SiblingMessage))),
   127  		want: fileDescP2_20160519.Messages().ByName("SiblingMessage"),
   128  	}, {
   129  		got:  impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto2_20160519.Message_ChildMessage))),
   130  		want: fileDescP2_20160519.Messages().ByName("Message").Messages().ByName("ChildMessage"),
   131  	}, {
   132  		got:  impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto2_20160519.Message))),
   133  		want: fileDescP2_20160519.Messages().ByName("Message"),
   134  	}, {
   135  		got:  impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto2_20160519.Message_NamedGroup))),
   136  		want: fileDescP2_20160519.Messages().ByName("Message").Messages().ByName("NamedGroup"),
   137  	}, {
   138  		got:  impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto2_20160519.Message_OptionalGroup))),
   139  		want: fileDescP2_20160519.Messages().ByName("Message").Messages().ByName("OptionalGroup"),
   140  	}, {
   141  		got:  impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto2_20160519.Message_RequiredGroup))),
   142  		want: fileDescP2_20160519.Messages().ByName("Message").Messages().ByName("RequiredGroup"),
   143  	}, {
   144  		got:  impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto2_20160519.Message_RepeatedGroup))),
   145  		want: fileDescP2_20160519.Messages().ByName("Message").Messages().ByName("RepeatedGroup"),
   146  	}, {
   147  		got:  impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto2_20160519.Message_OneofGroup))),
   148  		want: fileDescP2_20160519.Messages().ByName("Message").Messages().ByName("OneofGroup"),
   149  	}, {
   150  		got:  impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto2_20160519.Message_ExtensionOptionalGroup))),
   151  		want: fileDescP2_20160519.Messages().ByName("Message").Messages().ByName("ExtensionOptionalGroup"),
   152  	}, {
   153  		got:  impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto2_20160519.Message_ExtensionRepeatedGroup))),
   154  		want: fileDescP2_20160519.Messages().ByName("Message").Messages().ByName("ExtensionRepeatedGroup"),
   155  	}}...)
   156  
   157  	fileDescP3_20160519 := mustLoadFileDesc(new(proto3_20160519.Message).Descriptor())
   158  	tests = append(tests, []struct{ got, want protoreflect.Descriptor }{{
   159  		got:  impl.LegacyLoadEnumDesc(reflect.TypeOf(proto3_20160519.SiblingEnum(0))),
   160  		want: fileDescP3_20160519.Enums().ByName("SiblingEnum"),
   161  	}, {
   162  		got:  impl.LegacyLoadEnumDesc(reflect.TypeOf(proto3_20160519.Message_ChildEnum(0))),
   163  		want: fileDescP3_20160519.Messages().ByName("Message").Enums().ByName("ChildEnum"),
   164  	}, {
   165  		got:  impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto3_20160519.SiblingMessage))),
   166  		want: fileDescP3_20160519.Messages().ByName("SiblingMessage"),
   167  	}, {
   168  		got:  impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto3_20160519.Message_ChildMessage))),
   169  		want: fileDescP3_20160519.Messages().ByName("Message").Messages().ByName("ChildMessage"),
   170  	}, {
   171  		got:  impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto3_20160519.Message))),
   172  		want: fileDescP3_20160519.Messages().ByName("Message"),
   173  	}}...)
   174  
   175  	fileDescP2_20180125 := mustLoadFileDesc(new(proto2_20180125.Message).Descriptor())
   176  	tests = append(tests, []struct{ got, want protoreflect.Descriptor }{{
   177  		got:  impl.LegacyLoadEnumDesc(reflect.TypeOf(proto2_20180125.SiblingEnum(0))),
   178  		want: fileDescP2_20180125.Enums().ByName("SiblingEnum"),
   179  	}, {
   180  		got:  impl.LegacyLoadEnumDesc(reflect.TypeOf(proto2_20180125.Message_ChildEnum(0))),
   181  		want: fileDescP2_20180125.Messages().ByName("Message").Enums().ByName("ChildEnum"),
   182  	}, {
   183  		got:  impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto2_20180125.SiblingMessage))),
   184  		want: fileDescP2_20180125.Messages().ByName("SiblingMessage"),
   185  	}, {
   186  		got:  impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto2_20180125.Message_ChildMessage))),
   187  		want: fileDescP2_20180125.Messages().ByName("Message").Messages().ByName("ChildMessage"),
   188  	}, {
   189  		got:  impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto2_20180125.Message))),
   190  		want: fileDescP2_20180125.Messages().ByName("Message"),
   191  	}, {
   192  		got:  impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto2_20180125.Message_NamedGroup))),
   193  		want: fileDescP2_20180125.Messages().ByName("Message").Messages().ByName("NamedGroup"),
   194  	}, {
   195  		got:  impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto2_20180125.Message_OptionalGroup))),
   196  		want: fileDescP2_20180125.Messages().ByName("Message").Messages().ByName("OptionalGroup"),
   197  	}, {
   198  		got:  impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto2_20180125.Message_RequiredGroup))),
   199  		want: fileDescP2_20180125.Messages().ByName("Message").Messages().ByName("RequiredGroup"),
   200  	}, {
   201  		got:  impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto2_20180125.Message_RepeatedGroup))),
   202  		want: fileDescP2_20180125.Messages().ByName("Message").Messages().ByName("RepeatedGroup"),
   203  	}, {
   204  		got:  impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto2_20180125.Message_OneofGroup))),
   205  		want: fileDescP2_20180125.Messages().ByName("Message").Messages().ByName("OneofGroup"),
   206  	}, {
   207  		got:  impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto2_20180125.Message_ExtensionOptionalGroup))),
   208  		want: fileDescP2_20180125.Messages().ByName("Message").Messages().ByName("ExtensionOptionalGroup"),
   209  	}, {
   210  		got:  impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto2_20180125.Message_ExtensionRepeatedGroup))),
   211  		want: fileDescP2_20180125.Messages().ByName("Message").Messages().ByName("ExtensionRepeatedGroup"),
   212  	}}...)
   213  
   214  	fileDescP3_20180125 := mustLoadFileDesc(new(proto3_20180125.Message).Descriptor())
   215  	tests = append(tests, []struct{ got, want protoreflect.Descriptor }{{
   216  		got:  impl.LegacyLoadEnumDesc(reflect.TypeOf(proto3_20180125.SiblingEnum(0))),
   217  		want: fileDescP3_20180125.Enums().ByName("SiblingEnum"),
   218  	}, {
   219  		got:  impl.LegacyLoadEnumDesc(reflect.TypeOf(proto3_20180125.Message_ChildEnum(0))),
   220  		want: fileDescP3_20180125.Messages().ByName("Message").Enums().ByName("ChildEnum"),
   221  	}, {
   222  		got:  impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto3_20180125.SiblingMessage))),
   223  		want: fileDescP3_20180125.Messages().ByName("SiblingMessage"),
   224  	}, {
   225  		got:  impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto3_20180125.Message_ChildMessage))),
   226  		want: fileDescP3_20180125.Messages().ByName("Message").Messages().ByName("ChildMessage"),
   227  	}, {
   228  		got:  impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto3_20180125.Message))),
   229  		want: fileDescP3_20180125.Messages().ByName("Message"),
   230  	}}...)
   231  
   232  	fileDescP2_20180430 := mustLoadFileDesc(new(proto2_20180430.Message).Descriptor())
   233  	tests = append(tests, []struct{ got, want protoreflect.Descriptor }{{
   234  		got:  impl.LegacyLoadEnumDesc(reflect.TypeOf(proto2_20180430.SiblingEnum(0))),
   235  		want: fileDescP2_20180430.Enums().ByName("SiblingEnum"),
   236  	}, {
   237  		got:  impl.LegacyLoadEnumDesc(reflect.TypeOf(proto2_20180430.Message_ChildEnum(0))),
   238  		want: fileDescP2_20180430.Messages().ByName("Message").Enums().ByName("ChildEnum"),
   239  	}, {
   240  		got:  impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto2_20180430.SiblingMessage))),
   241  		want: fileDescP2_20180430.Messages().ByName("SiblingMessage"),
   242  	}, {
   243  		got:  impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto2_20180430.Message_ChildMessage))),
   244  		want: fileDescP2_20180430.Messages().ByName("Message").Messages().ByName("ChildMessage"),
   245  	}, {
   246  		got:  impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto2_20180430.Message))),
   247  		want: fileDescP2_20180430.Messages().ByName("Message"),
   248  	}, {
   249  		got:  impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto2_20180430.Message_NamedGroup))),
   250  		want: fileDescP2_20180430.Messages().ByName("Message").Messages().ByName("NamedGroup"),
   251  	}, {
   252  		got:  impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto2_20180430.Message_OptionalGroup))),
   253  		want: fileDescP2_20180430.Messages().ByName("Message").Messages().ByName("OptionalGroup"),
   254  	}, {
   255  		got:  impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto2_20180430.Message_RequiredGroup))),
   256  		want: fileDescP2_20180430.Messages().ByName("Message").Messages().ByName("RequiredGroup"),
   257  	}, {
   258  		got:  impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto2_20180430.Message_RepeatedGroup))),
   259  		want: fileDescP2_20180430.Messages().ByName("Message").Messages().ByName("RepeatedGroup"),
   260  	}, {
   261  		got:  impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto2_20180430.Message_OneofGroup))),
   262  		want: fileDescP2_20180430.Messages().ByName("Message").Messages().ByName("OneofGroup"),
   263  	}, {
   264  		got:  impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto2_20180430.Message_ExtensionOptionalGroup))),
   265  		want: fileDescP2_20180430.Messages().ByName("Message").Messages().ByName("ExtensionOptionalGroup"),
   266  	}, {
   267  		got:  impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto2_20180430.Message_ExtensionRepeatedGroup))),
   268  		want: fileDescP2_20180430.Messages().ByName("Message").Messages().ByName("ExtensionRepeatedGroup"),
   269  	}}...)
   270  
   271  	fileDescP3_20180430 := mustLoadFileDesc(new(proto3_20180430.Message).Descriptor())
   272  	tests = append(tests, []struct{ got, want protoreflect.Descriptor }{{
   273  		got:  impl.LegacyLoadEnumDesc(reflect.TypeOf(proto3_20180430.SiblingEnum(0))),
   274  		want: fileDescP3_20180430.Enums().ByName("SiblingEnum"),
   275  	}, {
   276  		got:  impl.LegacyLoadEnumDesc(reflect.TypeOf(proto3_20180430.Message_ChildEnum(0))),
   277  		want: fileDescP3_20180430.Messages().ByName("Message").Enums().ByName("ChildEnum"),
   278  	}, {
   279  		got:  impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto3_20180430.SiblingMessage))),
   280  		want: fileDescP3_20180430.Messages().ByName("SiblingMessage"),
   281  	}, {
   282  		got:  impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto3_20180430.Message_ChildMessage))),
   283  		want: fileDescP3_20180430.Messages().ByName("Message").Messages().ByName("ChildMessage"),
   284  	}, {
   285  		got:  impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto3_20180430.Message))),
   286  		want: fileDescP3_20180430.Messages().ByName("Message"),
   287  	}}...)
   288  
   289  	fileDescP2_20180814 := mustLoadFileDesc(new(proto2_20180814.Message).Descriptor())
   290  	tests = append(tests, []struct{ got, want protoreflect.Descriptor }{{
   291  		got:  impl.LegacyLoadEnumDesc(reflect.TypeOf(proto2_20180814.SiblingEnum(0))),
   292  		want: fileDescP2_20180814.Enums().ByName("SiblingEnum"),
   293  	}, {
   294  		got:  impl.LegacyLoadEnumDesc(reflect.TypeOf(proto2_20180814.Message_ChildEnum(0))),
   295  		want: fileDescP2_20180814.Messages().ByName("Message").Enums().ByName("ChildEnum"),
   296  	}, {
   297  		got:  impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto2_20180814.SiblingMessage))),
   298  		want: fileDescP2_20180814.Messages().ByName("SiblingMessage"),
   299  	}, {
   300  		got:  impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto2_20180814.Message_ChildMessage))),
   301  		want: fileDescP2_20180814.Messages().ByName("Message").Messages().ByName("ChildMessage"),
   302  	}, {
   303  		got:  impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto2_20180814.Message))),
   304  		want: fileDescP2_20180814.Messages().ByName("Message"),
   305  	}, {
   306  		got:  impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto2_20180814.Message_NamedGroup))),
   307  		want: fileDescP2_20180814.Messages().ByName("Message").Messages().ByName("NamedGroup"),
   308  	}, {
   309  		got:  impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto2_20180814.Message_OptionalGroup))),
   310  		want: fileDescP2_20180814.Messages().ByName("Message").Messages().ByName("OptionalGroup"),
   311  	}, {
   312  		got:  impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto2_20180814.Message_RequiredGroup))),
   313  		want: fileDescP2_20180814.Messages().ByName("Message").Messages().ByName("RequiredGroup"),
   314  	}, {
   315  		got:  impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto2_20180814.Message_RepeatedGroup))),
   316  		want: fileDescP2_20180814.Messages().ByName("Message").Messages().ByName("RepeatedGroup"),
   317  	}, {
   318  		got:  impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto2_20180814.Message_OneofGroup))),
   319  		want: fileDescP2_20180814.Messages().ByName("Message").Messages().ByName("OneofGroup"),
   320  	}, {
   321  		got:  impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto2_20180814.Message_ExtensionOptionalGroup))),
   322  		want: fileDescP2_20180814.Messages().ByName("Message").Messages().ByName("ExtensionOptionalGroup"),
   323  	}, {
   324  		got:  impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto2_20180814.Message_ExtensionRepeatedGroup))),
   325  		want: fileDescP2_20180814.Messages().ByName("Message").Messages().ByName("ExtensionRepeatedGroup"),
   326  	}}...)
   327  
   328  	fileDescP3_20180814 := mustLoadFileDesc(new(proto3_20180814.Message).Descriptor())
   329  	tests = append(tests, []struct{ got, want protoreflect.Descriptor }{{
   330  		got:  impl.LegacyLoadEnumDesc(reflect.TypeOf(proto3_20180814.SiblingEnum(0))),
   331  		want: fileDescP3_20180814.Enums().ByName("SiblingEnum"),
   332  	}, {
   333  		got:  impl.LegacyLoadEnumDesc(reflect.TypeOf(proto3_20180814.Message_ChildEnum(0))),
   334  		want: fileDescP3_20180814.Messages().ByName("Message").Enums().ByName("ChildEnum"),
   335  	}, {
   336  		got:  impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto3_20180814.SiblingMessage))),
   337  		want: fileDescP3_20180814.Messages().ByName("SiblingMessage"),
   338  	}, {
   339  		got:  impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto3_20180814.Message_ChildMessage))),
   340  		want: fileDescP3_20180814.Messages().ByName("Message").Messages().ByName("ChildMessage"),
   341  	}, {
   342  		got:  impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto3_20180814.Message))),
   343  		want: fileDescP3_20180814.Messages().ByName("Message"),
   344  	}}...)
   345  
   346  	fileDescP2_20190205 := mustLoadFileDesc(new(proto2_20190205.Message).Descriptor())
   347  	tests = append(tests, []struct{ got, want protoreflect.Descriptor }{{
   348  		got:  impl.LegacyLoadEnumDesc(reflect.TypeOf(proto2_20190205.SiblingEnum(0))),
   349  		want: fileDescP2_20190205.Enums().ByName("SiblingEnum"),
   350  	}, {
   351  		got:  impl.LegacyLoadEnumDesc(reflect.TypeOf(proto2_20190205.Message_ChildEnum(0))),
   352  		want: fileDescP2_20190205.Messages().ByName("Message").Enums().ByName("ChildEnum"),
   353  	}, {
   354  		got:  impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto2_20190205.SiblingMessage))),
   355  		want: fileDescP2_20190205.Messages().ByName("SiblingMessage"),
   356  	}, {
   357  		got:  impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto2_20190205.Message_ChildMessage))),
   358  		want: fileDescP2_20190205.Messages().ByName("Message").Messages().ByName("ChildMessage"),
   359  	}, {
   360  		got:  impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto2_20190205.Message))),
   361  		want: fileDescP2_20190205.Messages().ByName("Message"),
   362  	}, {
   363  		got:  impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto2_20190205.Message_NamedGroup))),
   364  		want: fileDescP2_20190205.Messages().ByName("Message").Messages().ByName("NamedGroup"),
   365  	}, {
   366  		got:  impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto2_20190205.Message_OptionalGroup))),
   367  		want: fileDescP2_20190205.Messages().ByName("Message").Messages().ByName("OptionalGroup"),
   368  	}, {
   369  		got:  impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto2_20190205.Message_RequiredGroup))),
   370  		want: fileDescP2_20190205.Messages().ByName("Message").Messages().ByName("RequiredGroup"),
   371  	}, {
   372  		got:  impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto2_20190205.Message_RepeatedGroup))),
   373  		want: fileDescP2_20190205.Messages().ByName("Message").Messages().ByName("RepeatedGroup"),
   374  	}, {
   375  		got:  impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto2_20190205.Message_OneofGroup))),
   376  		want: fileDescP2_20190205.Messages().ByName("Message").Messages().ByName("OneofGroup"),
   377  	}, {
   378  		got:  impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto2_20190205.Message_ExtensionOptionalGroup))),
   379  		want: fileDescP2_20190205.Messages().ByName("Message").Messages().ByName("ExtensionOptionalGroup"),
   380  	}, {
   381  		got:  impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto2_20190205.Message_ExtensionRepeatedGroup))),
   382  		want: fileDescP2_20190205.Messages().ByName("Message").Messages().ByName("ExtensionRepeatedGroup"),
   383  	}}...)
   384  
   385  	fileDescP3_20190205 := mustLoadFileDesc(new(proto3_20190205.Message).Descriptor())
   386  	tests = append(tests, []struct{ got, want protoreflect.Descriptor }{{
   387  		got:  impl.LegacyLoadEnumDesc(reflect.TypeOf(proto3_20190205.SiblingEnum(0))),
   388  		want: fileDescP3_20190205.Enums().ByName("SiblingEnum"),
   389  	}, {
   390  		got:  impl.LegacyLoadEnumDesc(reflect.TypeOf(proto3_20190205.Message_ChildEnum(0))),
   391  		want: fileDescP3_20190205.Messages().ByName("Message").Enums().ByName("ChildEnum"),
   392  	}, {
   393  		got:  impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto3_20190205.SiblingMessage))),
   394  		want: fileDescP3_20190205.Messages().ByName("SiblingMessage"),
   395  	}, {
   396  		got:  impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto3_20190205.Message_ChildMessage))),
   397  		want: fileDescP3_20190205.Messages().ByName("Message").Messages().ByName("ChildMessage"),
   398  	}, {
   399  		got:  impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto3_20190205.Message))),
   400  		want: fileDescP3_20190205.Messages().ByName("Message"),
   401  	}}...)
   402  
   403  	// TODO: We need a test package to compare descriptors.
   404  	type list interface {
   405  		Len() int
   406  		pragma.DoNotImplement
   407  	}
   408  	opts := cmp.Options{
   409  		cmp.Transformer("", func(x list) []interface{} {
   410  			out := make([]interface{}, x.Len())
   411  			v := reflect.ValueOf(x)
   412  			for i := 0; i < x.Len(); i++ {
   413  				m := v.MethodByName("Get")
   414  				out[i] = m.Call([]reflect.Value{reflect.ValueOf(i)})[0].Interface()
   415  			}
   416  			return out
   417  		}),
   418  		cmp.Transformer("", func(x protoreflect.Descriptor) map[string]interface{} {
   419  			out := make(map[string]interface{})
   420  			v := reflect.ValueOf(x)
   421  			for i := 0; i < v.NumMethod(); i++ {
   422  				name := v.Type().Method(i).Name
   423  				if m := v.Method(i); m.Type().NumIn() == 0 && m.Type().NumOut() == 1 {
   424  					switch name {
   425  					case "ParentFile", "Parent":
   426  						// Ignore parents to avoid recursive cycle.
   427  					case "Index":
   428  						// Ignore index since legacy descriptors have no parent.
   429  					case "Options":
   430  						// Ignore descriptor options since protos are not cmperable.
   431  					case "Enums", "Messages", "Extensions":
   432  						// Ignore nested message and enum declarations since
   433  						// legacy descriptors are all created standalone.
   434  					case "HasJSONName":
   435  						// Ignore this since the semantics of the field has
   436  						// changed across protoc and protoc-gen-go releases.
   437  					case "ContainingOneof", "ContainingMessage", "Enum", "Message":
   438  						// Avoid descending into a dependency to avoid a cycle.
   439  						// Just record the full name if available.
   440  						//
   441  						// TODO: Cycle support in cmp would be useful here.
   442  						v := m.Call(nil)[0]
   443  						if !v.IsNil() {
   444  							out[name] = v.Interface().(protoreflect.Descriptor).FullName()
   445  						}
   446  					default:
   447  						out[name] = m.Call(nil)[0].Interface()
   448  					}
   449  				}
   450  			}
   451  			return out
   452  		}),
   453  		cmp.Transformer("", func(v protoreflect.Value) interface{} {
   454  			return v.Interface()
   455  		}),
   456  	}
   457  
   458  	for _, tt := range tests {
   459  		t.Run(string(tt.want.FullName()), func(t *testing.T) {
   460  			if diff := cmp.Diff(&tt.want, &tt.got, opts); diff != "" {
   461  				t.Errorf("descriptor mismatch (-want, +got):\n%s", diff)
   462  			}
   463  		})
   464  	}
   465  }
   466  

View as plain text