blob: beecdfedb2a41a862db03fa0871cd0b71787861e [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
5package main
6
7import (
Damien Neil162c1272018-10-04 12:42:37 -07008 "bytes"
Damien Neil162c1272018-10-04 12:42:37 -07009 "io/ioutil"
Damien Neil220c2022018-08-15 11:24:18 -070010 "testing"
Damien Neil2dc67182018-09-21 15:03:34 -070011
Damien Neil162c1272018-10-04 12:42:37 -070012 "github.com/golang/protobuf/proto"
Joe Tsaica46d8c2019-03-20 16:51:09 -070013 "github.com/golang/protobuf/v2/internal/fieldnum"
Joe Tsai009e0672018-11-27 18:45:07 -080014 "github.com/golang/protobuf/v2/internal/scalar"
Joe Tsaie1f8d502018-11-26 18:55:29 -080015
16 descriptorpb "github.com/golang/protobuf/v2/types/descriptor"
Damien Neil220c2022018-08-15 11:24:18 -070017)
18
Damien Neil162c1272018-10-04 12:42:37 -070019func TestAnnotations(t *testing.T) {
Joe Tsai19058432019-02-27 21:46:29 -080020 sourceFile, err := ioutil.ReadFile("testdata/annotations/annotations.pb.go")
Damien Neil162c1272018-10-04 12:42:37 -070021 if err != nil {
22 t.Fatal(err)
23 }
Joe Tsai19058432019-02-27 21:46:29 -080024 metaFile, err := ioutil.ReadFile("testdata/annotations/annotations.pb.go.meta")
Damien Neil162c1272018-10-04 12:42:37 -070025 if err != nil {
26 t.Fatal(err)
27 }
Joe Tsaie1f8d502018-11-26 18:55:29 -080028 gotInfo := &descriptorpb.GeneratedCodeInfo{}
Damien Neil162c1272018-10-04 12:42:37 -070029 if err := proto.UnmarshalText(string(metaFile), gotInfo); err != nil {
30 t.Fatalf("can't parse meta file: %v", err)
31 }
32
Joe Tsaie1f8d502018-11-26 18:55:29 -080033 wantInfo := &descriptorpb.GeneratedCodeInfo{}
Damien Neil162c1272018-10-04 12:42:37 -070034 for _, want := range []struct {
35 prefix, text, suffix string
36 path []int32
37 }{{
38 "type ", "AnnotationsTestEnum", " int32",
Joe Tsaica46d8c2019-03-20 16:51:09 -070039 []int32{fieldnum.FileDescriptorProto_EnumType, 0},
Damien Neil162c1272018-10-04 12:42:37 -070040 }, {
41 "\t", "AnnotationsTestEnum_ANNOTATIONS_TEST_ENUM_VALUE", " AnnotationsTestEnum = 0",
Joe Tsaica46d8c2019-03-20 16:51:09 -070042 []int32{fieldnum.FileDescriptorProto_EnumType, 0, fieldnum.EnumDescriptorProto_Value, 0},
Damien Neil162c1272018-10-04 12:42:37 -070043 }, {
44 "type ", "AnnotationsTestMessage", " struct {",
Joe Tsaica46d8c2019-03-20 16:51:09 -070045 []int32{fieldnum.FileDescriptorProto_MessageType, 0},
Damien Neil162c1272018-10-04 12:42:37 -070046 }, {
47 "\t", "AnnotationsTestField", " ",
Joe Tsaica46d8c2019-03-20 16:51:09 -070048 []int32{fieldnum.FileDescriptorProto_MessageType, 0, fieldnum.DescriptorProto_Field, 0},
Damien Neil162c1272018-10-04 12:42:37 -070049 }, {
Joe Tsai61968ce2019-04-01 12:59:24 -070050 "func (x *AnnotationsTestMessage) ", "GetAnnotationsTestField", "() string {",
Joe Tsaica46d8c2019-03-20 16:51:09 -070051 []int32{fieldnum.FileDescriptorProto_MessageType, 0, fieldnum.DescriptorProto_Field, 0},
Damien Neil162c1272018-10-04 12:42:37 -070052 }} {
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 Tsaie1f8d502018-11-26 18:55:29 -080061 wantInfo.Annotation = append(wantInfo.Annotation, &descriptorpb.GeneratedCodeInfo_Annotation{
Damien Neil162c1272018-10-04 12:42:37 -070062 Path: want.path,
Joe Tsai009e0672018-11-27 18:45:07 -080063 Begin: scalar.Int32(int32(begin)),
64 End: scalar.Int32(int32(end)),
Joe Tsai19058432019-02-27 21:46:29 -080065 SourceFile: scalar.String("annotations/annotations.proto"),
Damien Neil162c1272018-10-04 12:42:37 -070066 })
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}