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.
|
|
|
|
|
2018-10-16 00:34:43 +00:00
|
|
|
// +build golden
|
2018-09-21 22:03:34 +00:00
|
|
|
|
2018-08-15 18:24:18 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2018-10-04 19:42:37 +00:00
|
|
|
"bytes"
|
2018-08-15 18:24:18 +00:00
|
|
|
"flag"
|
2018-10-04 19:42:37 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2018-08-15 18:24:18 +00:00
|
|
|
"testing"
|
2018-09-21 22:03:34 +00:00
|
|
|
|
2018-10-04 19:42:37 +00:00
|
|
|
"github.com/golang/protobuf/proto"
|
|
|
|
descpb "github.com/golang/protobuf/protoc-gen-go/descriptor"
|
2018-09-21 22:03:34 +00:00
|
|
|
"github.com/golang/protobuf/v2/internal/protogen/goldentest"
|
2018-11-28 02:45:07 +00:00
|
|
|
"github.com/golang/protobuf/v2/internal/scalar"
|
2018-08-15 18:24:18 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Set --regenerate to regenerate the golden files.
|
|
|
|
var regenerate = flag.Bool("regenerate", false, "regenerate golden files")
|
|
|
|
|
|
|
|
func init() {
|
2018-09-21 22:03:34 +00:00
|
|
|
goldentest.Plugin(main)
|
2018-08-15 18:24:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGolden(t *testing.T) {
|
2018-09-21 22:03:34 +00:00
|
|
|
goldentest.Run(t, *regenerate)
|
2018-08-15 18:24:18 +00:00
|
|
|
}
|
2018-10-04 19:42:37 +00:00
|
|
|
|
|
|
|
func TestAnnotations(t *testing.T) {
|
|
|
|
workdir, err := ioutil.TempDir("", "proto-test")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(workdir)
|
|
|
|
|
|
|
|
goldentest.Protoc(t, []string{"--go_out=paths=source_relative,annotate_code:" + workdir, "-Itestdata/annotations", "testdata/annotations/annotations.proto"})
|
|
|
|
sourceFile, err := ioutil.ReadFile(filepath.Join(workdir, "annotations.pb.go"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
metaFile, err := ioutil.ReadFile(filepath.Join(workdir, "annotations.pb.go.meta"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
gotInfo := &descpb.GeneratedCodeInfo{}
|
|
|
|
if err := proto.UnmarshalText(string(metaFile), gotInfo); err != nil {
|
|
|
|
t.Fatalf("can't parse meta file: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
wantInfo := &descpb.GeneratedCodeInfo{}
|
|
|
|
for _, want := range []struct {
|
|
|
|
prefix, text, suffix string
|
|
|
|
path []int32
|
|
|
|
}{{
|
|
|
|
"type ", "AnnotationsTestEnum", " int32",
|
|
|
|
[]int32{5 /* enum_type */, 0},
|
|
|
|
}, {
|
|
|
|
"\t", "AnnotationsTestEnum_ANNOTATIONS_TEST_ENUM_VALUE", " AnnotationsTestEnum = 0",
|
|
|
|
[]int32{5 /* enum_type */, 0, 2 /* value */, 0},
|
|
|
|
}, {
|
|
|
|
"type ", "AnnotationsTestMessage", " struct {",
|
|
|
|
[]int32{4 /* message_type */, 0},
|
|
|
|
}, {
|
|
|
|
"\t", "AnnotationsTestField", " ",
|
|
|
|
[]int32{4 /* message_type */, 0, 2 /* field */, 0},
|
|
|
|
}, {
|
|
|
|
"func (m *AnnotationsTestMessage) ", "GetAnnotationsTestField", "() string {",
|
|
|
|
[]int32{4 /* message_type */, 0, 2 /* 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, &descpb.GeneratedCodeInfo_Annotation{
|
|
|
|
Path: want.path,
|
2018-11-28 02:45:07 +00:00
|
|
|
Begin: scalar.Int32(int32(begin)),
|
|
|
|
End: scalar.Int32(int32(end)),
|
|
|
|
SourceFile: scalar.String("annotations.proto"),
|
2018-10-04 19:42:37 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
if !proto.Equal(gotInfo, wantInfo) {
|
|
|
|
t.Errorf("unexpected annotations for annotations.proto; got:\n%v\nwant:\n%v",
|
|
|
|
proto.MarshalTextString(gotInfo), proto.MarshalTextString(wantInfo))
|
|
|
|
}
|
|
|
|
}
|