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