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 | |
| 5 | package main |
| 6 | |
| 7 | import ( |
Damien Neil | 162c127 | 2018-10-04 12:42:37 -0700 | [diff] [blame] | 8 | "bytes" |
Damien Neil | 162c127 | 2018-10-04 12:42:37 -0700 | [diff] [blame] | 9 | "io/ioutil" |
Damien Neil | 220c202 | 2018-08-15 11:24:18 -0700 | [diff] [blame] | 10 | "testing" |
Damien Neil | 2dc6718 | 2018-09-21 15:03:34 -0700 | [diff] [blame] | 11 | |
Damien Neil | 162c127 | 2018-10-04 12:42:37 -0700 | [diff] [blame] | 12 | "github.com/golang/protobuf/proto" |
Joe Tsai | ca46d8c | 2019-03-20 16:51:09 -0700 | [diff] [blame] | 13 | "github.com/golang/protobuf/v2/internal/fieldnum" |
Joe Tsai | 009e067 | 2018-11-27 18:45:07 -0800 | [diff] [blame] | 14 | "github.com/golang/protobuf/v2/internal/scalar" |
Joe Tsai | e1f8d50 | 2018-11-26 18:55:29 -0800 | [diff] [blame] | 15 | |
| 16 | descriptorpb "github.com/golang/protobuf/v2/types/descriptor" |
Damien Neil | 220c202 | 2018-08-15 11:24:18 -0700 | [diff] [blame] | 17 | ) |
| 18 | |
Damien Neil | 162c127 | 2018-10-04 12:42:37 -0700 | [diff] [blame] | 19 | func TestAnnotations(t *testing.T) { |
Joe Tsai | 1905843 | 2019-02-27 21:46:29 -0800 | [diff] [blame] | 20 | sourceFile, err := ioutil.ReadFile("testdata/annotations/annotations.pb.go") |
Damien Neil | 162c127 | 2018-10-04 12:42:37 -0700 | [diff] [blame] | 21 | if err != nil { |
| 22 | t.Fatal(err) |
| 23 | } |
Joe Tsai | 1905843 | 2019-02-27 21:46:29 -0800 | [diff] [blame] | 24 | metaFile, err := ioutil.ReadFile("testdata/annotations/annotations.pb.go.meta") |
Damien Neil | 162c127 | 2018-10-04 12:42:37 -0700 | [diff] [blame] | 25 | if err != nil { |
| 26 | t.Fatal(err) |
| 27 | } |
Joe Tsai | e1f8d50 | 2018-11-26 18:55:29 -0800 | [diff] [blame] | 28 | gotInfo := &descriptorpb.GeneratedCodeInfo{} |
Damien Neil | 162c127 | 2018-10-04 12:42:37 -0700 | [diff] [blame] | 29 | if err := proto.UnmarshalText(string(metaFile), gotInfo); err != nil { |
| 30 | t.Fatalf("can't parse meta file: %v", err) |
| 31 | } |
| 32 | |
Joe Tsai | e1f8d50 | 2018-11-26 18:55:29 -0800 | [diff] [blame] | 33 | wantInfo := &descriptorpb.GeneratedCodeInfo{} |
Damien Neil | 162c127 | 2018-10-04 12:42:37 -0700 | [diff] [blame] | 34 | for _, want := range []struct { |
| 35 | prefix, text, suffix string |
| 36 | path []int32 |
| 37 | }{{ |
| 38 | "type ", "AnnotationsTestEnum", " int32", |
Joe Tsai | ca46d8c | 2019-03-20 16:51:09 -0700 | [diff] [blame] | 39 | []int32{fieldnum.FileDescriptorProto_EnumType, 0}, |
Damien Neil | 162c127 | 2018-10-04 12:42:37 -0700 | [diff] [blame] | 40 | }, { |
| 41 | "\t", "AnnotationsTestEnum_ANNOTATIONS_TEST_ENUM_VALUE", " AnnotationsTestEnum = 0", |
Joe Tsai | ca46d8c | 2019-03-20 16:51:09 -0700 | [diff] [blame] | 42 | []int32{fieldnum.FileDescriptorProto_EnumType, 0, fieldnum.EnumDescriptorProto_Value, 0}, |
Damien Neil | 162c127 | 2018-10-04 12:42:37 -0700 | [diff] [blame] | 43 | }, { |
| 44 | "type ", "AnnotationsTestMessage", " struct {", |
Joe Tsai | ca46d8c | 2019-03-20 16:51:09 -0700 | [diff] [blame] | 45 | []int32{fieldnum.FileDescriptorProto_MessageType, 0}, |
Damien Neil | 162c127 | 2018-10-04 12:42:37 -0700 | [diff] [blame] | 46 | }, { |
| 47 | "\t", "AnnotationsTestField", " ", |
Joe Tsai | ca46d8c | 2019-03-20 16:51:09 -0700 | [diff] [blame] | 48 | []int32{fieldnum.FileDescriptorProto_MessageType, 0, fieldnum.DescriptorProto_Field, 0}, |
Damien Neil | 162c127 | 2018-10-04 12:42:37 -0700 | [diff] [blame] | 49 | }, { |
Joe Tsai | 61968ce | 2019-04-01 12:59:24 -0700 | [diff] [blame] | 50 | "func (x *AnnotationsTestMessage) ", "GetAnnotationsTestField", "() string {", |
Joe Tsai | ca46d8c | 2019-03-20 16:51:09 -0700 | [diff] [blame] | 51 | []int32{fieldnum.FileDescriptorProto_MessageType, 0, fieldnum.DescriptorProto_Field, 0}, |
Damien Neil | 162c127 | 2018-10-04 12:42:37 -0700 | [diff] [blame] | 52 | }} { |
| 53 | s := want.prefix + want.text + want.suffix |
| 54 | pos := bytes.Index(sourceFile, []byte(s)) |
| 55 | if pos < 0 { |
| 56 | t.Errorf("source file does not contain: %v", s) |
| 57 | continue |
| 58 | } |
| 59 | begin := pos + len(want.prefix) |
| 60 | end := begin + len(want.text) |
Joe Tsai | e1f8d50 | 2018-11-26 18:55:29 -0800 | [diff] [blame] | 61 | wantInfo.Annotation = append(wantInfo.Annotation, &descriptorpb.GeneratedCodeInfo_Annotation{ |
Damien Neil | 162c127 | 2018-10-04 12:42:37 -0700 | [diff] [blame] | 62 | Path: want.path, |
Joe Tsai | 009e067 | 2018-11-27 18:45:07 -0800 | [diff] [blame] | 63 | Begin: scalar.Int32(int32(begin)), |
| 64 | End: scalar.Int32(int32(end)), |
Joe Tsai | 1905843 | 2019-02-27 21:46:29 -0800 | [diff] [blame] | 65 | SourceFile: scalar.String("annotations/annotations.proto"), |
Damien Neil | 162c127 | 2018-10-04 12:42:37 -0700 | [diff] [blame] | 66 | }) |
| 67 | } |
| 68 | if !proto.Equal(gotInfo, wantInfo) { |
| 69 | t.Errorf("unexpected annotations for annotations.proto; got:\n%v\nwant:\n%v", |
| 70 | proto.MarshalTextString(gotInfo), proto.MarshalTextString(wantInfo)) |
| 71 | } |
| 72 | } |