blob: b2754a6df948acdb292a466959d10da466ecb6f8 [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"
Damien Neil2dc67182018-09-21 15:03:34 -070018 "github.com/golang/protobuf/v2/internal/protogen/goldentest"
Joe Tsai009e0672018-11-27 18:45:07 -080019 "github.com/golang/protobuf/v2/internal/scalar"
Joe Tsaie1f8d502018-11-26 18:55:29 -080020
21 descriptorpb "github.com/golang/protobuf/v2/types/descriptor"
Damien Neil220c2022018-08-15 11:24:18 -070022)
23
24// Set --regenerate to regenerate the golden files.
25var regenerate = flag.Bool("regenerate", false, "regenerate golden files")
26
Damien Neil220c2022018-08-15 11:24:18 -070027func init() {
Damien Neil2dc67182018-09-21 15:03:34 -070028 goldentest.Plugin(main)
Damien Neil220c2022018-08-15 11:24:18 -070029}
30
31func TestGolden(t *testing.T) {
Damien Neil2dc67182018-09-21 15:03:34 -070032 goldentest.Run(t, *regenerate)
Damien Neil220c2022018-08-15 11:24:18 -070033}
Damien Neil162c1272018-10-04 12:42:37 -070034
35func TestAnnotations(t *testing.T) {
36 workdir, err := ioutil.TempDir("", "proto-test")
37 if err != nil {
38 t.Fatal(err)
39 }
40 defer os.RemoveAll(workdir)
41
42 goldentest.Protoc(t, []string{"--go_out=paths=source_relative,annotate_code:" + workdir, "-Itestdata/annotations", "testdata/annotations/annotations.proto"})
43 sourceFile, err := ioutil.ReadFile(filepath.Join(workdir, "annotations.pb.go"))
44 if err != nil {
45 t.Fatal(err)
46 }
47 metaFile, err := ioutil.ReadFile(filepath.Join(workdir, "annotations.pb.go.meta"))
48 if err != nil {
49 t.Fatal(err)
50 }
Joe Tsaie1f8d502018-11-26 18:55:29 -080051 gotInfo := &descriptorpb.GeneratedCodeInfo{}
Damien Neil162c1272018-10-04 12:42:37 -070052 if err := proto.UnmarshalText(string(metaFile), gotInfo); err != nil {
53 t.Fatalf("can't parse meta file: %v", err)
54 }
55
Joe Tsaie1f8d502018-11-26 18:55:29 -080056 wantInfo := &descriptorpb.GeneratedCodeInfo{}
Damien Neil162c1272018-10-04 12:42:37 -070057 for _, want := range []struct {
58 prefix, text, suffix string
59 path []int32
60 }{{
61 "type ", "AnnotationsTestEnum", " int32",
62 []int32{5 /* enum_type */, 0},
63 }, {
64 "\t", "AnnotationsTestEnum_ANNOTATIONS_TEST_ENUM_VALUE", " AnnotationsTestEnum = 0",
65 []int32{5 /* enum_type */, 0, 2 /* value */, 0},
66 }, {
67 "type ", "AnnotationsTestMessage", " struct {",
68 []int32{4 /* message_type */, 0},
69 }, {
70 "\t", "AnnotationsTestField", " ",
71 []int32{4 /* message_type */, 0, 2 /* field */, 0},
72 }, {
73 "func (m *AnnotationsTestMessage) ", "GetAnnotationsTestField", "() string {",
74 []int32{4 /* message_type */, 0, 2 /* field */, 0},
75 }} {
76 s := want.prefix + want.text + want.suffix
77 pos := bytes.Index(sourceFile, []byte(s))
78 if pos < 0 {
79 t.Errorf("source file does not contain: %v", s)
80 continue
81 }
82 begin := pos + len(want.prefix)
83 end := begin + len(want.text)
Joe Tsaie1f8d502018-11-26 18:55:29 -080084 wantInfo.Annotation = append(wantInfo.Annotation, &descriptorpb.GeneratedCodeInfo_Annotation{
Damien Neil162c1272018-10-04 12:42:37 -070085 Path: want.path,
Joe Tsai009e0672018-11-27 18:45:07 -080086 Begin: scalar.Int32(int32(begin)),
87 End: scalar.Int32(int32(end)),
88 SourceFile: scalar.String("annotations.proto"),
Damien Neil162c1272018-10-04 12:42:37 -070089 })
90 }
91 if !proto.Equal(gotInfo, wantInfo) {
92 t.Errorf("unexpected annotations for annotations.proto; got:\n%v\nwant:\n%v",
93 proto.MarshalTextString(gotInfo), proto.MarshalTextString(wantInfo))
94 }
95}