2018-08-15 18:24:18 +00:00
|
|
|
// Copyright 2018 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2018-10-04 19:42:37 +00:00
|
|
|
"bytes"
|
|
|
|
"io/ioutil"
|
2018-08-15 18:24:18 +00:00
|
|
|
"testing"
|
2018-09-21 22:03:34 +00:00
|
|
|
|
2019-05-16 22:53:25 +00:00
|
|
|
"google.golang.org/protobuf/encoding/prototext"
|
2019-05-14 06:55:40 +00:00
|
|
|
"google.golang.org/protobuf/internal/fieldnum"
|
2019-05-16 22:53:25 +00:00
|
|
|
"google.golang.org/protobuf/proto"
|
2018-11-27 02:55:29 +00:00
|
|
|
|
2019-05-16 19:47:20 +00:00
|
|
|
"google.golang.org/protobuf/types/descriptorpb"
|
2018-08-15 18:24:18 +00:00
|
|
|
)
|
|
|
|
|
2018-10-04 19:42:37 +00:00
|
|
|
func TestAnnotations(t *testing.T) {
|
2019-02-28 05:46:29 +00:00
|
|
|
sourceFile, err := ioutil.ReadFile("testdata/annotations/annotations.pb.go")
|
2018-10-04 19:42:37 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-02-28 05:46:29 +00:00
|
|
|
metaFile, err := ioutil.ReadFile("testdata/annotations/annotations.pb.go.meta")
|
2018-10-04 19:42:37 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-11-27 02:55:29 +00:00
|
|
|
gotInfo := &descriptorpb.GeneratedCodeInfo{}
|
2019-05-16 22:53:25 +00:00
|
|
|
if err := prototext.Unmarshal(metaFile, gotInfo); err != nil {
|
2018-10-04 19:42:37 +00:00
|
|
|
t.Fatalf("can't parse meta file: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-11-27 02:55:29 +00:00
|
|
|
wantInfo := &descriptorpb.GeneratedCodeInfo{}
|
2018-10-04 19:42:37 +00:00
|
|
|
for _, want := range []struct {
|
|
|
|
prefix, text, suffix string
|
|
|
|
path []int32
|
|
|
|
}{{
|
|
|
|
"type ", "AnnotationsTestEnum", " int32",
|
2019-03-20 23:51:09 +00:00
|
|
|
[]int32{fieldnum.FileDescriptorProto_EnumType, 0},
|
2018-10-04 19:42:37 +00:00
|
|
|
}, {
|
|
|
|
"\t", "AnnotationsTestEnum_ANNOTATIONS_TEST_ENUM_VALUE", " AnnotationsTestEnum = 0",
|
2019-03-20 23:51:09 +00:00
|
|
|
[]int32{fieldnum.FileDescriptorProto_EnumType, 0, fieldnum.EnumDescriptorProto_Value, 0},
|
2018-10-04 19:42:37 +00:00
|
|
|
}, {
|
|
|
|
"type ", "AnnotationsTestMessage", " struct {",
|
2019-03-20 23:51:09 +00:00
|
|
|
[]int32{fieldnum.FileDescriptorProto_MessageType, 0},
|
2018-10-04 19:42:37 +00:00
|
|
|
}, {
|
|
|
|
"\t", "AnnotationsTestField", " ",
|
2019-03-20 23:51:09 +00:00
|
|
|
[]int32{fieldnum.FileDescriptorProto_MessageType, 0, fieldnum.DescriptorProto_Field, 0},
|
2018-10-04 19:42:37 +00:00
|
|
|
}, {
|
2019-04-01 19:59:24 +00:00
|
|
|
"func (x *AnnotationsTestMessage) ", "GetAnnotationsTestField", "() string {",
|
2019-03-20 23:51:09 +00:00
|
|
|
[]int32{fieldnum.FileDescriptorProto_MessageType, 0, fieldnum.DescriptorProto_Field, 0},
|
2018-10-04 19:42:37 +00:00
|
|
|
}} {
|
|
|
|
s := want.prefix + want.text + want.suffix
|
|
|
|
pos := bytes.Index(sourceFile, []byte(s))
|
|
|
|
if pos < 0 {
|
|
|
|
t.Errorf("source file does not contain: %v", s)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
begin := pos + len(want.prefix)
|
|
|
|
end := begin + len(want.text)
|
2018-11-27 02:55:29 +00:00
|
|
|
wantInfo.Annotation = append(wantInfo.Annotation, &descriptorpb.GeneratedCodeInfo_Annotation{
|
2018-10-04 19:42:37 +00:00
|
|
|
Path: want.path,
|
2019-07-10 23:17:16 +00:00
|
|
|
Begin: proto.Int32(int32(begin)),
|
|
|
|
End: proto.Int32(int32(end)),
|
|
|
|
SourceFile: proto.String("annotations/annotations.proto"),
|
2018-10-04 19:42:37 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
if !proto.Equal(gotInfo, wantInfo) {
|
2019-05-16 22:53:25 +00:00
|
|
|
t.Errorf("unexpected annotations for annotations.proto; got:\n%v\nwant:\n%v", gotInfo, wantInfo)
|
2018-10-04 19:42:37 +00:00
|
|
|
}
|
|
|
|
}
|