github.com/whiteCcinn/protobuf-go@v1.0.9/internal/filedesc/build_test.go (about) 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 filedesc_test 6 7 import ( 8 "bytes" 9 "compress/gzip" 10 "io/ioutil" 11 "testing" 12 13 "github.com/whiteCcinn/protobuf-go/proto" 14 "github.com/whiteCcinn/protobuf-go/reflect/protodesc" 15 "github.com/whiteCcinn/protobuf-go/reflect/protoreflect" 16 17 testpb "github.com/whiteCcinn/protobuf-go/internal/testprotos/test" 18 _ "github.com/whiteCcinn/protobuf-go/internal/testprotos/test/weak1" 19 "github.com/whiteCcinn/protobuf-go/types/descriptorpb" 20 ) 21 22 var testFile = new(testpb.TestAllTypes).ProtoReflect().Descriptor().ParentFile() 23 24 func TestInit(t *testing.T) { 25 // Compare the FileDescriptorProto for the same test file from two different sources: 26 // 27 // 1. The result of passing the filedesc-produced FileDescriptor through protodesc. 28 // 2. The protoc-generated wire-encoded message. 29 // 30 // This serves as a test of both filedesc and protodesc. 31 got := protodesc.ToFileDescriptorProto(testFile) 32 33 want := &descriptorpb.FileDescriptorProto{} 34 zb, _ := (&testpb.TestAllTypes{}).Descriptor() 35 r, _ := gzip.NewReader(bytes.NewBuffer(zb)) 36 b, _ := ioutil.ReadAll(r) 37 if err := proto.Unmarshal(b, want); err != nil { 38 t.Fatal(err) 39 } 40 41 if !proto.Equal(got, want) { 42 t.Errorf("protodesc.ToFileDescriptorProto(testpb.Test_protoFile) is not equal to the protoc-generated FileDescriptorProto for internal/testprotos/test/test.proto") 43 } 44 45 // Verify that the test proto file provides exhaustive coverage of all descriptor fields. 46 seen := make(map[protoreflect.FullName]bool) 47 visitFields(want.ProtoReflect(), func(field protoreflect.FieldDescriptor) { 48 seen[field.FullName()] = true 49 }) 50 descFile := new(descriptorpb.DescriptorProto).ProtoReflect().Descriptor().ParentFile() 51 descPkg := descFile.Package() 52 ignore := map[protoreflect.FullName]bool{ 53 // The protoreflect descriptors don't include source info. 54 descPkg.Append("FileDescriptorProto.source_code_info"): true, 55 descPkg.Append("FileDescriptorProto.syntax"): true, 56 57 // Impossible to test proto3 optional in a proto2 file. 58 descPkg.Append("FieldDescriptorProto.proto3_optional"): true, 59 60 // TODO: Test oneof and extension options. Testing these requires extending the 61 // options messages (because they contain no user-settable fields), but importing 62 // descriptor.proto from test.proto currently causes an import cycle. Add test 63 // cases when that import cycle has been fixed. 64 descPkg.Append("OneofDescriptorProto.options"): true, 65 } 66 for _, messageName := range []protoreflect.Name{ 67 "FileDescriptorProto", 68 "DescriptorProto", 69 "FieldDescriptorProto", 70 "OneofDescriptorProto", 71 "EnumDescriptorProto", 72 "EnumValueDescriptorProto", 73 } { 74 message := descFile.Messages().ByName(messageName) 75 for i, fields := 0, message.Fields(); i < fields.Len(); i++ { 76 if name := fields.Get(i).FullName(); !seen[name] && !ignore[name] { 77 t.Errorf("No test for descriptor field: %v", name) 78 } 79 } 80 } 81 82 // Verify that message descriptors for map entries have no Go type info. 83 mapEntryName := protoreflect.FullName("goproto.proto.test.TestAllTypes.MapInt32Int32Entry") 84 d := testFile.Messages().ByName("TestAllTypes").Fields().ByName("map_int32_int32").Message() 85 if gotName, wantName := d.FullName(), mapEntryName; gotName != wantName { 86 t.Fatalf("looked up wrong descriptor: got %v, want %v", gotName, wantName) 87 } 88 if _, ok := d.(protoreflect.MessageType); ok { 89 t.Errorf("message descriptor for %v must not implement protoreflect.MessageType", mapEntryName) 90 } 91 } 92 93 // visitFields calls f for every field set in m and its children. 94 func visitFields(m protoreflect.Message, f func(protoreflect.FieldDescriptor)) { 95 m.Range(func(fd protoreflect.FieldDescriptor, value protoreflect.Value) bool { 96 f(fd) 97 switch fd.Kind() { 98 case protoreflect.MessageKind, protoreflect.GroupKind: 99 if fd.IsList() { 100 for i, list := 0, value.List(); i < list.Len(); i++ { 101 visitFields(list.Get(i).Message(), f) 102 } 103 } else { 104 visitFields(value.Message(), f) 105 } 106 } 107 return true 108 }) 109 } 110 111 func TestWeakInit(t *testing.T) { 112 // We do not expect to get a placeholder since weak1 is imported. 113 fd1 := testFile.Messages().ByName("TestWeak").Fields().ByName("weak_message1") 114 if got, want := fd1.IsWeak(), true; got != want { 115 t.Errorf("field %v: IsWeak() = %v, want %v", fd1.FullName(), got, want) 116 } 117 if got, want := fd1.Message().IsPlaceholder(), false; got != want { 118 t.Errorf("field %v: Message.IsPlaceholder() = %v, want %v", fd1.FullName(), got, want) 119 } 120 if got, want := fd1.Message().Fields().Len(), 1; got != want { 121 t.Errorf("field %v: Message().Fields().Len() == %d, want %d", fd1.FullName(), got, want) 122 } 123 124 // We do expect to get a placeholder since weak2 is not imported. 125 fd2 := testFile.Messages().ByName("TestWeak").Fields().ByName("weak_message2") 126 if got, want := fd2.IsWeak(), true; got != want { 127 t.Errorf("field %v: IsWeak() = %v, want %v", fd2.FullName(), got, want) 128 } 129 if got, want := fd2.Message().IsPlaceholder(), true; got != want { 130 t.Errorf("field %v: Message.IsPlaceholder() = %v, want %v", fd2.FullName(), got, want) 131 } 132 if got, want := fd2.Message().Fields().Len(), 0; got != want { 133 t.Errorf("field %v: Message().Fields().Len() == %d, want %d", fd2.FullName(), got, want) 134 } 135 }