github.com/whiteCcinn/protobuf-go@v1.0.9/cmd/protoc-gen-go/annotation_test.go (about)

     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 (
     8  	"bytes"
     9  	"io/ioutil"
    10  	"testing"
    11  
    12  	"github.com/whiteCcinn/protobuf-go/encoding/prototext"
    13  	"github.com/whiteCcinn/protobuf-go/internal/genid"
    14  	"github.com/whiteCcinn/protobuf-go/proto"
    15  
    16  	"github.com/whiteCcinn/protobuf-go/types/descriptorpb"
    17  )
    18  
    19  func TestAnnotations(t *testing.T) {
    20  	sourceFile, err := ioutil.ReadFile("testdata/annotations/annotations.pb.go")
    21  	if err != nil {
    22  		t.Fatal(err)
    23  	}
    24  	metaFile, err := ioutil.ReadFile("testdata/annotations/annotations.pb.go.meta")
    25  	if err != nil {
    26  		t.Fatal(err)
    27  	}
    28  	gotInfo := &descriptorpb.GeneratedCodeInfo{}
    29  	if err := prototext.Unmarshal(metaFile, gotInfo); err != nil {
    30  		t.Fatalf("can't parse meta file: %v", err)
    31  	}
    32  
    33  	wantInfo := &descriptorpb.GeneratedCodeInfo{}
    34  	for _, want := range []struct {
    35  		prefix, text, suffix string
    36  		path                 []int32
    37  	}{{
    38  		"type ", "AnnotationsTestEnum", " int32",
    39  		[]int32{int32(genid.FileDescriptorProto_EnumType_field_number), 0},
    40  	}, {
    41  		"\t", "AnnotationsTestEnum_ANNOTATIONS_TEST_ENUM_VALUE", " AnnotationsTestEnum = 0",
    42  		[]int32{int32(genid.FileDescriptorProto_EnumType_field_number), 0, int32(genid.EnumDescriptorProto_Value_field_number), 0},
    43  	}, {
    44  		"type ", "AnnotationsTestMessage", " struct {",
    45  		[]int32{int32(genid.FileDescriptorProto_MessageType_field_number), 0},
    46  	}, {
    47  		"\t", "AnnotationsTestField", " ",
    48  		[]int32{int32(genid.FileDescriptorProto_MessageType_field_number), 0, int32(genid.DescriptorProto_Field_field_number), 0},
    49  	}, {
    50  		"func (x *AnnotationsTestMessage) ", "GetAnnotationsTestField", "() string {",
    51  		[]int32{int32(genid.FileDescriptorProto_MessageType_field_number), 0, int32(genid.DescriptorProto_Field_field_number), 0},
    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)
    61  		wantInfo.Annotation = append(wantInfo.Annotation, &descriptorpb.GeneratedCodeInfo_Annotation{
    62  			Path:       want.path,
    63  			Begin:      proto.Int32(int32(begin)),
    64  			End:        proto.Int32(int32(end)),
    65  			SourceFile: proto.String("cmd/protoc-gen-go/testdata/annotations/annotations.proto"),
    66  		})
    67  	}
    68  	if !proto.Equal(gotInfo, wantInfo) {
    69  		t.Errorf("unexpected annotations for annotations.proto; got:\n%v\nwant:\n%v", gotInfo, wantInfo)
    70  	}
    71  }