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"
|
2024-04-22 22:15:31 +00:00
|
|
|
"os"
|
2018-08-15 18:24:18 +00:00
|
|
|
"testing"
|
2018-09-21 22:03:34 +00:00
|
|
|
|
2023-04-27 15:39:18 +00:00
|
|
|
"github.com/google/go-cmp/cmp"
|
2019-05-16 22:53:25 +00:00
|
|
|
"google.golang.org/protobuf/encoding/prototext"
|
2020-05-26 18:21:59 +00:00
|
|
|
"google.golang.org/protobuf/internal/genid"
|
2019-05-16 22:53:25 +00:00
|
|
|
"google.golang.org/protobuf/proto"
|
2023-04-27 15:39:18 +00:00
|
|
|
"google.golang.org/protobuf/testing/protocmp"
|
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) {
|
2024-04-22 22:15:31 +00:00
|
|
|
sourceFile, err := os.ReadFile("testdata/annotations/annotations.pb.go")
|
2018-10-04 19:42:37 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2024-04-22 22:15:31 +00:00
|
|
|
metaFile, err := os.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
|
2023-04-27 15:39:18 +00:00
|
|
|
annotation *descriptorpb.GeneratedCodeInfo_Annotation
|
2018-10-04 19:42:37 +00:00
|
|
|
}{{
|
|
|
|
"type ", "AnnotationsTestEnum", " int32",
|
2023-04-27 15:39:18 +00:00
|
|
|
&descriptorpb.GeneratedCodeInfo_Annotation{
|
|
|
|
Path: []int32{int32(genid.FileDescriptorProto_EnumType_field_number), 0},
|
|
|
|
},
|
2018-10-04 19:42:37 +00:00
|
|
|
}, {
|
|
|
|
"\t", "AnnotationsTestEnum_ANNOTATIONS_TEST_ENUM_VALUE", " AnnotationsTestEnum = 0",
|
2023-04-27 15:39:18 +00:00
|
|
|
&descriptorpb.GeneratedCodeInfo_Annotation{
|
|
|
|
Path: []int32{int32(genid.FileDescriptorProto_EnumType_field_number), 0, int32(genid.EnumDescriptorProto_Value_field_number), 0},
|
|
|
|
},
|
2018-10-04 19:42:37 +00:00
|
|
|
}, {
|
|
|
|
"type ", "AnnotationsTestMessage", " struct {",
|
2023-04-27 15:39:18 +00:00
|
|
|
&descriptorpb.GeneratedCodeInfo_Annotation{
|
|
|
|
Path: []int32{int32(genid.FileDescriptorProto_MessageType_field_number), 0},
|
|
|
|
},
|
2018-10-04 19:42:37 +00:00
|
|
|
}, {
|
|
|
|
"\t", "AnnotationsTestField", " ",
|
2023-04-27 15:39:18 +00:00
|
|
|
&descriptorpb.GeneratedCodeInfo_Annotation{
|
|
|
|
Path: []int32{int32(genid.FileDescriptorProto_MessageType_field_number), 0, int32(genid.DescriptorProto_Field_field_number), 0},
|
|
|
|
},
|
|
|
|
}, {
|
|
|
|
"\t", "XXX_weak_M", " ",
|
|
|
|
&descriptorpb.GeneratedCodeInfo_Annotation{
|
|
|
|
Path: []int32{int32(genid.FileDescriptorProto_MessageType_field_number), 0, int32(genid.DescriptorProto_Field_field_number), 1},
|
|
|
|
},
|
2018-10-04 19:42:37 +00:00
|
|
|
}, {
|
2019-04-01 19:59:24 +00:00
|
|
|
"func (x *AnnotationsTestMessage) ", "GetAnnotationsTestField", "() string {",
|
2023-04-27 15:39:18 +00:00
|
|
|
&descriptorpb.GeneratedCodeInfo_Annotation{
|
|
|
|
Path: []int32{int32(genid.FileDescriptorProto_MessageType_field_number), 0, int32(genid.DescriptorProto_Field_field_number), 0},
|
|
|
|
},
|
|
|
|
}, {
|
|
|
|
"func (x *AnnotationsTestMessage) ", "GetM", "() proto.Message {",
|
|
|
|
&descriptorpb.GeneratedCodeInfo_Annotation{
|
|
|
|
Path: []int32{int32(genid.FileDescriptorProto_MessageType_field_number), 0, int32(genid.DescriptorProto_Field_field_number), 1},
|
|
|
|
},
|
|
|
|
}, {
|
|
|
|
"func (x *AnnotationsTestMessage) ", "SetM", "(v proto.Message) {",
|
|
|
|
&descriptorpb.GeneratedCodeInfo_Annotation{
|
|
|
|
Path: []int32{int32(genid.FileDescriptorProto_MessageType_field_number), 0, int32(genid.DescriptorProto_Field_field_number), 1},
|
|
|
|
Semantic: descriptorpb.GeneratedCodeInfo_Annotation_SET.Enum(),
|
|
|
|
},
|
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)
|
2023-04-27 15:39:18 +00:00
|
|
|
a := &descriptorpb.GeneratedCodeInfo_Annotation{
|
2019-07-10 23:17:16 +00:00
|
|
|
Begin: proto.Int32(int32(begin)),
|
|
|
|
End: proto.Int32(int32(end)),
|
2020-02-04 23:03:30 +00:00
|
|
|
SourceFile: proto.String("cmd/protoc-gen-go/testdata/annotations/annotations.proto"),
|
2023-04-27 15:39:18 +00:00
|
|
|
}
|
|
|
|
proto.Merge(a, want.annotation)
|
|
|
|
wantInfo.Annotation = append(wantInfo.Annotation, a)
|
2018-10-04 19:42:37 +00:00
|
|
|
}
|
2023-04-27 15:39:18 +00:00
|
|
|
if diff := cmp.Diff(wantInfo, gotInfo, protocmp.Transform()); diff != "" {
|
|
|
|
t.Fatalf("unexpected annotations for annotations.proto (-want +got):\n%s", diff)
|
2018-10-04 19:42:37 +00:00
|
|
|
}
|
|
|
|
}
|