blob: 3ee0cc08e52575749fddf5e53b58cda85f5579ce [file] [log] [blame]
Damien Neil220c2022018-08-15 11:24:18 -07001// Copyright 2018 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
Joe Tsai42fa50d2018-10-15 17:34:43 -07005// +build golden
Damien Neil2dc67182018-09-21 15:03:34 -07006
Damien Neil220c2022018-08-15 11:24:18 -07007package main
8
9import (
Damien Neil162c1272018-10-04 12:42:37 -070010 "bytes"
Damien Neil220c2022018-08-15 11:24:18 -070011 "flag"
Damien Neil162c1272018-10-04 12:42:37 -070012 "io/ioutil"
13 "os"
14 "path/filepath"
Damien Neil220c2022018-08-15 11:24:18 -070015 "testing"
Damien Neil2dc67182018-09-21 15:03:34 -070016
Damien Neil162c1272018-10-04 12:42:37 -070017 "github.com/golang/protobuf/proto"
18 descpb "github.com/golang/protobuf/protoc-gen-go/descriptor"
Damien Neil2dc67182018-09-21 15:03:34 -070019 "github.com/golang/protobuf/v2/internal/protogen/goldentest"
Joe Tsai009e0672018-11-27 18:45:07 -080020 "github.com/golang/protobuf/v2/internal/scalar"
Damien Neil220c2022018-08-15 11:24:18 -070021)
22
23// Set --regenerate to regenerate the golden files.
24var regenerate = flag.Bool("regenerate", false, "regenerate golden files")
25
Damien Neil220c2022018-08-15 11:24:18 -070026func init() {
Damien Neil2dc67182018-09-21 15:03:34 -070027 goldentest.Plugin(main)
Damien Neil220c2022018-08-15 11:24:18 -070028}
29
30func TestGolden(t *testing.T) {
Damien Neil2dc67182018-09-21 15:03:34 -070031 goldentest.Run(t, *regenerate)
Damien Neil220c2022018-08-15 11:24:18 -070032}
Damien Neil162c1272018-10-04 12:42:37 -070033
34func TestAnnotations(t *testing.T) {
35 workdir, err := ioutil.TempDir("", "proto-test")
36 if err != nil {
37 t.Fatal(err)
38 }
39 defer os.RemoveAll(workdir)
40
41 goldentest.Protoc(t, []string{"--go_out=paths=source_relative,annotate_code:" + workdir, "-Itestdata/annotations", "testdata/annotations/annotations.proto"})
42 sourceFile, err := ioutil.ReadFile(filepath.Join(workdir, "annotations.pb.go"))
43 if err != nil {
44 t.Fatal(err)
45 }
46 metaFile, err := ioutil.ReadFile(filepath.Join(workdir, "annotations.pb.go.meta"))
47 if err != nil {
48 t.Fatal(err)
49 }
50 gotInfo := &descpb.GeneratedCodeInfo{}
51 if err := proto.UnmarshalText(string(metaFile), gotInfo); err != nil {
52 t.Fatalf("can't parse meta file: %v", err)
53 }
54
55 wantInfo := &descpb.GeneratedCodeInfo{}
56 for _, want := range []struct {
57 prefix, text, suffix string
58 path []int32
59 }{{
60 "type ", "AnnotationsTestEnum", " int32",
61 []int32{5 /* enum_type */, 0},
62 }, {
63 "\t", "AnnotationsTestEnum_ANNOTATIONS_TEST_ENUM_VALUE", " AnnotationsTestEnum = 0",
64 []int32{5 /* enum_type */, 0, 2 /* value */, 0},
65 }, {
66 "type ", "AnnotationsTestMessage", " struct {",
67 []int32{4 /* message_type */, 0},
68 }, {
69 "\t", "AnnotationsTestField", " ",
70 []int32{4 /* message_type */, 0, 2 /* field */, 0},
71 }, {
72 "func (m *AnnotationsTestMessage) ", "GetAnnotationsTestField", "() string {",
73 []int32{4 /* message_type */, 0, 2 /* field */, 0},
74 }} {
75 s := want.prefix + want.text + want.suffix
76 pos := bytes.Index(sourceFile, []byte(s))
77 if pos < 0 {
78 t.Errorf("source file does not contain: %v", s)
79 continue
80 }
81 begin := pos + len(want.prefix)
82 end := begin + len(want.text)
83 wantInfo.Annotation = append(wantInfo.Annotation, &descpb.GeneratedCodeInfo_Annotation{
84 Path: want.path,
Joe Tsai009e0672018-11-27 18:45:07 -080085 Begin: scalar.Int32(int32(begin)),
86 End: scalar.Int32(int32(end)),
87 SourceFile: scalar.String("annotations.proto"),
Damien Neil162c1272018-10-04 12:42:37 -070088 })
89 }
90 if !proto.Equal(gotInfo, wantInfo) {
91 t.Errorf("unexpected annotations for annotations.proto; got:\n%v\nwant:\n%v",
92 proto.MarshalTextString(gotInfo), proto.MarshalTextString(wantInfo))
93 }
94}