mirror of
https://github.com/protocolbuffers/protobuf-go.git
synced 2025-01-07 12:56:47 +00:00
1af1de0c15
Generate a list of descriptor fields from the descriptor.proto itself as an internal package. Use these fields for the internal implementation. Change-Id: Ib1ab0c5c6deb332ba6c8018ef55136b7e5974944 Reviewed-on: https://go-review.googlesource.com/c/164864 Reviewed-by: Herbie Ong <herbie@google.com>
73 lines
2.4 KiB
Go
73 lines
2.4 KiB
Go
// 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 (
|
|
"bytes"
|
|
"io/ioutil"
|
|
"testing"
|
|
|
|
"github.com/golang/protobuf/proto"
|
|
"github.com/golang/protobuf/v2/internal/descfield"
|
|
"github.com/golang/protobuf/v2/internal/scalar"
|
|
|
|
descriptorpb "github.com/golang/protobuf/v2/types/descriptor"
|
|
)
|
|
|
|
func TestAnnotations(t *testing.T) {
|
|
sourceFile, err := ioutil.ReadFile("testdata/annotations/annotations.pb.go")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
metaFile, err := ioutil.ReadFile("testdata/annotations/annotations.pb.go.meta")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
gotInfo := &descriptorpb.GeneratedCodeInfo{}
|
|
if err := proto.UnmarshalText(string(metaFile), gotInfo); err != nil {
|
|
t.Fatalf("can't parse meta file: %v", err)
|
|
}
|
|
|
|
wantInfo := &descriptorpb.GeneratedCodeInfo{}
|
|
for _, want := range []struct {
|
|
prefix, text, suffix string
|
|
path []int32
|
|
}{{
|
|
"type ", "AnnotationsTestEnum", " int32",
|
|
[]int32{descfield.FileDescriptorProto_EnumType, 0},
|
|
}, {
|
|
"\t", "AnnotationsTestEnum_ANNOTATIONS_TEST_ENUM_VALUE", " AnnotationsTestEnum = 0",
|
|
[]int32{descfield.FileDescriptorProto_EnumType, 0, descfield.EnumDescriptorProto_Value, 0},
|
|
}, {
|
|
"type ", "AnnotationsTestMessage", " struct {",
|
|
[]int32{descfield.FileDescriptorProto_MessageType, 0},
|
|
}, {
|
|
"\t", "AnnotationsTestField", " ",
|
|
[]int32{descfield.FileDescriptorProto_MessageType, 0, descfield.DescriptorProto_Field, 0},
|
|
}, {
|
|
"func (m *AnnotationsTestMessage) ", "GetAnnotationsTestField", "() string {",
|
|
[]int32{descfield.FileDescriptorProto_MessageType, 0, descfield.DescriptorProto_Field, 0},
|
|
}} {
|
|
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)
|
|
wantInfo.Annotation = append(wantInfo.Annotation, &descriptorpb.GeneratedCodeInfo_Annotation{
|
|
Path: want.path,
|
|
Begin: scalar.Int32(int32(begin)),
|
|
End: scalar.Int32(int32(end)),
|
|
SourceFile: scalar.String("annotations/annotations.proto"),
|
|
})
|
|
}
|
|
if !proto.Equal(gotInfo, wantInfo) {
|
|
t.Errorf("unexpected annotations for annotations.proto; got:\n%v\nwant:\n%v",
|
|
proto.MarshalTextString(gotInfo), proto.MarshalTextString(wantInfo))
|
|
}
|
|
}
|