Damien Neil | 220c202 | 2018-08-15 11:24:18 -0700 | [diff] [blame] | 1 | // 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 Tsai | 42fa50d | 2018-10-15 17:34:43 -0700 | [diff] [blame] | 5 | // +build golden |
Damien Neil | 2dc6718 | 2018-09-21 15:03:34 -0700 | [diff] [blame] | 6 | |
Damien Neil | 220c202 | 2018-08-15 11:24:18 -0700 | [diff] [blame] | 7 | package main |
| 8 | |
| 9 | import ( |
Damien Neil | 162c127 | 2018-10-04 12:42:37 -0700 | [diff] [blame] | 10 | "bytes" |
Damien Neil | 220c202 | 2018-08-15 11:24:18 -0700 | [diff] [blame] | 11 | "flag" |
Damien Neil | 162c127 | 2018-10-04 12:42:37 -0700 | [diff] [blame] | 12 | "io/ioutil" |
| 13 | "os" |
| 14 | "path/filepath" |
Damien Neil | 220c202 | 2018-08-15 11:24:18 -0700 | [diff] [blame] | 15 | "testing" |
Damien Neil | 2dc6718 | 2018-09-21 15:03:34 -0700 | [diff] [blame] | 16 | |
Damien Neil | 162c127 | 2018-10-04 12:42:37 -0700 | [diff] [blame] | 17 | "github.com/golang/protobuf/proto" |
| 18 | descpb "github.com/golang/protobuf/protoc-gen-go/descriptor" |
Damien Neil | 2dc6718 | 2018-09-21 15:03:34 -0700 | [diff] [blame] | 19 | "github.com/golang/protobuf/v2/internal/protogen/goldentest" |
Joe Tsai | 009e067 | 2018-11-27 18:45:07 -0800 | [diff] [blame] | 20 | "github.com/golang/protobuf/v2/internal/scalar" |
Damien Neil | 220c202 | 2018-08-15 11:24:18 -0700 | [diff] [blame] | 21 | ) |
| 22 | |
| 23 | // Set --regenerate to regenerate the golden files. |
| 24 | var regenerate = flag.Bool("regenerate", false, "regenerate golden files") |
| 25 | |
Damien Neil | 220c202 | 2018-08-15 11:24:18 -0700 | [diff] [blame] | 26 | func init() { |
Damien Neil | 2dc6718 | 2018-09-21 15:03:34 -0700 | [diff] [blame] | 27 | goldentest.Plugin(main) |
Damien Neil | 220c202 | 2018-08-15 11:24:18 -0700 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | func TestGolden(t *testing.T) { |
Damien Neil | 2dc6718 | 2018-09-21 15:03:34 -0700 | [diff] [blame] | 31 | goldentest.Run(t, *regenerate) |
Damien Neil | 220c202 | 2018-08-15 11:24:18 -0700 | [diff] [blame] | 32 | } |
Damien Neil | 162c127 | 2018-10-04 12:42:37 -0700 | [diff] [blame] | 33 | |
| 34 | func 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 Tsai | 009e067 | 2018-11-27 18:45:07 -0800 | [diff] [blame] | 85 | Begin: scalar.Int32(int32(begin)), |
| 86 | End: scalar.Int32(int32(end)), |
| 87 | SourceFile: scalar.String("annotations.proto"), |
Damien Neil | 162c127 | 2018-10-04 12:42:37 -0700 | [diff] [blame] | 88 | }) |
| 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 | } |