1*1c12ee1eSDan Willemsen// Copyright 2018 The Go Authors. All rights reserved. 2*1c12ee1eSDan Willemsen// Use of this source code is governed by a BSD-style 3*1c12ee1eSDan Willemsen// license that can be found in the LICENSE file. 4*1c12ee1eSDan Willemsen 5*1c12ee1eSDan Willemsenpackage main 6*1c12ee1eSDan Willemsen 7*1c12ee1eSDan Willemsenimport ( 8*1c12ee1eSDan Willemsen "bytes" 9*1c12ee1eSDan Willemsen "io/ioutil" 10*1c12ee1eSDan Willemsen "testing" 11*1c12ee1eSDan Willemsen 12*1c12ee1eSDan Willemsen "google.golang.org/protobuf/encoding/prototext" 13*1c12ee1eSDan Willemsen "google.golang.org/protobuf/internal/genid" 14*1c12ee1eSDan Willemsen "google.golang.org/protobuf/proto" 15*1c12ee1eSDan Willemsen 16*1c12ee1eSDan Willemsen "google.golang.org/protobuf/types/descriptorpb" 17*1c12ee1eSDan Willemsen) 18*1c12ee1eSDan Willemsen 19*1c12ee1eSDan Willemsenfunc TestAnnotations(t *testing.T) { 20*1c12ee1eSDan Willemsen sourceFile, err := ioutil.ReadFile("testdata/annotations/annotations.pb.go") 21*1c12ee1eSDan Willemsen if err != nil { 22*1c12ee1eSDan Willemsen t.Fatal(err) 23*1c12ee1eSDan Willemsen } 24*1c12ee1eSDan Willemsen metaFile, err := ioutil.ReadFile("testdata/annotations/annotations.pb.go.meta") 25*1c12ee1eSDan Willemsen if err != nil { 26*1c12ee1eSDan Willemsen t.Fatal(err) 27*1c12ee1eSDan Willemsen } 28*1c12ee1eSDan Willemsen gotInfo := &descriptorpb.GeneratedCodeInfo{} 29*1c12ee1eSDan Willemsen if err := prototext.Unmarshal(metaFile, gotInfo); err != nil { 30*1c12ee1eSDan Willemsen t.Fatalf("can't parse meta file: %v", err) 31*1c12ee1eSDan Willemsen } 32*1c12ee1eSDan Willemsen 33*1c12ee1eSDan Willemsen wantInfo := &descriptorpb.GeneratedCodeInfo{} 34*1c12ee1eSDan Willemsen for _, want := range []struct { 35*1c12ee1eSDan Willemsen prefix, text, suffix string 36*1c12ee1eSDan Willemsen path []int32 37*1c12ee1eSDan Willemsen }{{ 38*1c12ee1eSDan Willemsen "type ", "AnnotationsTestEnum", " int32", 39*1c12ee1eSDan Willemsen []int32{int32(genid.FileDescriptorProto_EnumType_field_number), 0}, 40*1c12ee1eSDan Willemsen }, { 41*1c12ee1eSDan Willemsen "\t", "AnnotationsTestEnum_ANNOTATIONS_TEST_ENUM_VALUE", " AnnotationsTestEnum = 0", 42*1c12ee1eSDan Willemsen []int32{int32(genid.FileDescriptorProto_EnumType_field_number), 0, int32(genid.EnumDescriptorProto_Value_field_number), 0}, 43*1c12ee1eSDan Willemsen }, { 44*1c12ee1eSDan Willemsen "type ", "AnnotationsTestMessage", " struct {", 45*1c12ee1eSDan Willemsen []int32{int32(genid.FileDescriptorProto_MessageType_field_number), 0}, 46*1c12ee1eSDan Willemsen }, { 47*1c12ee1eSDan Willemsen "\t", "AnnotationsTestField", " ", 48*1c12ee1eSDan Willemsen []int32{int32(genid.FileDescriptorProto_MessageType_field_number), 0, int32(genid.DescriptorProto_Field_field_number), 0}, 49*1c12ee1eSDan Willemsen }, { 50*1c12ee1eSDan Willemsen "func (x *AnnotationsTestMessage) ", "GetAnnotationsTestField", "() string {", 51*1c12ee1eSDan Willemsen []int32{int32(genid.FileDescriptorProto_MessageType_field_number), 0, int32(genid.DescriptorProto_Field_field_number), 0}, 52*1c12ee1eSDan Willemsen }} { 53*1c12ee1eSDan Willemsen s := want.prefix + want.text + want.suffix 54*1c12ee1eSDan Willemsen pos := bytes.Index(sourceFile, []byte(s)) 55*1c12ee1eSDan Willemsen if pos < 0 { 56*1c12ee1eSDan Willemsen t.Errorf("source file does not contain: %v", s) 57*1c12ee1eSDan Willemsen continue 58*1c12ee1eSDan Willemsen } 59*1c12ee1eSDan Willemsen begin := pos + len(want.prefix) 60*1c12ee1eSDan Willemsen end := begin + len(want.text) 61*1c12ee1eSDan Willemsen wantInfo.Annotation = append(wantInfo.Annotation, &descriptorpb.GeneratedCodeInfo_Annotation{ 62*1c12ee1eSDan Willemsen Path: want.path, 63*1c12ee1eSDan Willemsen Begin: proto.Int32(int32(begin)), 64*1c12ee1eSDan Willemsen End: proto.Int32(int32(end)), 65*1c12ee1eSDan Willemsen SourceFile: proto.String("cmd/protoc-gen-go/testdata/annotations/annotations.proto"), 66*1c12ee1eSDan Willemsen }) 67*1c12ee1eSDan Willemsen } 68*1c12ee1eSDan Willemsen if !proto.Equal(gotInfo, wantInfo) { 69*1c12ee1eSDan Willemsen t.Errorf("unexpected annotations for annotations.proto; got:\n%v\nwant:\n%v", gotInfo, wantInfo) 70*1c12ee1eSDan Willemsen } 71*1c12ee1eSDan Willemsen} 72