go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/common/proto/google/descutil/util_test.go (about) 1 // Copyright 2016 The LUCI Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package descutil 16 17 import ( 18 "os" 19 "testing" 20 21 "google.golang.org/protobuf/proto" 22 pb "google.golang.org/protobuf/types/descriptorpb" 23 24 . "github.com/smartystreets/goconvey/convey" 25 ) 26 27 func TestUtil(t *testing.T) { 28 t.Parallel() 29 30 Convey("Util", t, func() { 31 descFileBytes, err := os.ReadFile("./internal/util.desc") 32 So(err, ShouldBeNil) 33 34 var desc pb.FileDescriptorSet 35 err = proto.Unmarshal(descFileBytes, &desc) 36 So(err, ShouldBeNil) 37 38 So(desc.File, ShouldHaveLength, 2) 39 So(desc.File[0].GetName(), ShouldEqual, "google/protobuf/descriptor.proto") 40 file := desc.File[1] 41 So(file.GetName(), ShouldEqual, "go.chromium.org/luci/common/proto/google/descutil/internal/util.proto") 42 43 Convey("Resolve works", func() { 44 names := []string{ 45 "descutil.E1", 46 "descutil.E1.V0", 47 48 "descutil.M1", 49 "descutil.M1.f1", 50 51 "descutil.M2.f1", 52 "descutil.M2.f2", 53 54 "descutil.M3.O1", 55 "descutil.M3.f1", 56 "descutil.M3.O2", 57 58 "descutil.S1", 59 "descutil.S1.R1", 60 "descutil.S2.R2", 61 62 "descutil.NestedMessageParent", 63 "descutil.NestedMessageParent.NestedMessage", 64 "descutil.NestedMessageParent.NestedMessage.f1", 65 "descutil.NestedMessageParent.NestedEnum", 66 "descutil.NestedMessageParent.NestedEnum.V0", 67 } 68 for _, n := range names { 69 Convey(n, func() { 70 actualFile, obj, _ := Resolve(&desc, n) 71 So(actualFile, ShouldEqual, file) 72 So(obj, ShouldNotBeNil) 73 }) 74 } 75 76 Convey("wrong name", func() { 77 actualFile, obj, path := Resolve(&desc, "foo") 78 So(actualFile, ShouldBeNil) 79 So(obj, ShouldBeNil) 80 So(path, ShouldBeNil) 81 }) 82 }) 83 84 Convey("IndexSourceCodeInfo", func() { 85 sourceCodeInfo, err := IndexSourceCodeInfo(file) 86 So(err, ShouldBeNil) 87 _, m1, _ := Resolve(&desc, "descutil.M1") 88 loc := sourceCodeInfo[m1] 89 So(loc, ShouldNotBeNil) 90 So(loc.GetLeadingComments(), ShouldEqual, " M1\n next line.\n") 91 }) 92 }) 93 }