github.com/jhump/protoreflect@v1.16.0/desc/sourceinfo/registry_test.go (about) 1 package sourceinfo_test 2 3 import ( 4 "fmt" 5 "testing" 6 7 "google.golang.org/protobuf/reflect/protoreflect" 8 9 "github.com/jhump/protoreflect/desc/sourceinfo" 10 _ "github.com/jhump/protoreflect/internal/testprotos" 11 "github.com/jhump/protoreflect/internal/testutil" 12 ) 13 14 func TestRegistry(t *testing.T) { 15 fd, err := sourceinfo.GlobalFiles.FindFileByPath("desc_test1.proto") 16 testutil.Ok(t, err) 17 checkFileComments(t, fd) 18 } 19 20 func checkFileComments(t *testing.T, fd protoreflect.FileDescriptor) { 21 srcLocs := fd.SourceLocations() 22 for i := 0; i < fd.Messages().Len(); i++ { 23 checkMessageComments(t, srcLocs, fd.Messages().Get(i)) 24 } 25 for i := 0; i < fd.Enums().Len(); i++ { 26 checkEnumComments(t, srcLocs, fd.Enums().Get(i)) 27 } 28 for i := 0; i < fd.Extensions().Len(); i++ { 29 checkComment(t, srcLocs, fd.Extensions().Get(i)) 30 } 31 for i := 0; i < fd.Services().Len(); i++ { 32 sd := fd.Services().Get(i) 33 checkComment(t, srcLocs, sd) 34 for j := 0; j < sd.Methods().Len(); j++ { 35 mtd := sd.Methods().Get(j) 36 checkComment(t, srcLocs, mtd) 37 } 38 } 39 } 40 41 func checkMessageComments(t *testing.T, srcLocs protoreflect.SourceLocations, md protoreflect.MessageDescriptor) { 42 checkComment(t, srcLocs, md) 43 44 for i := 0; i < md.Fields().Len(); i++ { 45 fld := md.Fields().Get(i) 46 if fld.Kind() == protoreflect.GroupKind { 47 continue // comment is attributed to group message, not field 48 } 49 checkComment(t, srcLocs, fld) 50 } 51 for i := 0; i < md.Oneofs().Len(); i++ { 52 checkComment(t, srcLocs, md.Oneofs().Get(i)) 53 } 54 55 for i := 0; i < md.Messages().Len(); i++ { 56 nmd := md.Messages().Get(i) 57 if nmd.IsMapEntry() { 58 // synthetic map entry messages won't have comments 59 continue 60 } 61 checkMessageComments(t, srcLocs, nmd) 62 } 63 for i := 0; i < md.Enums().Len(); i++ { 64 checkEnumComments(t, srcLocs, md.Enums().Get(i)) 65 } 66 for i := 0; i < md.Extensions().Len(); i++ { 67 checkComment(t, srcLocs, md.Extensions().Get(i)) 68 } 69 } 70 71 func checkEnumComments(t *testing.T, srcLocs protoreflect.SourceLocations, ed protoreflect.EnumDescriptor) { 72 checkComment(t, srcLocs, ed) 73 for i := 0; i < ed.Values().Len(); i++ { 74 evd := ed.Values().Get(i) 75 checkComment(t, srcLocs, evd) 76 } 77 } 78 79 func checkComment(t *testing.T, srcLocs protoreflect.SourceLocations, d protoreflect.Descriptor) { 80 cmt := fmt.Sprintf(" Comment for %s\n", d.Name()) 81 testutil.Eq(t, cmt, srcLocs.ByDescriptor(d).LeadingComments) 82 }