...

Source file src/google.golang.org/protobuf/cmd/protoc-gen-go/annotation_test.go

Documentation: google.golang.org/protobuf/cmd/protoc-gen-go

     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/google/go-cmp/cmp"
    13  	"google.golang.org/protobuf/encoding/prototext"
    14  	"google.golang.org/protobuf/internal/genid"
    15  	"google.golang.org/protobuf/proto"
    16  	"google.golang.org/protobuf/testing/protocmp"
    17  
    18  	"google.golang.org/protobuf/types/descriptorpb"
    19  )
    20  
    21  func TestAnnotations(t *testing.T) {
    22  	sourceFile, err := ioutil.ReadFile("testdata/annotations/annotations.pb.go")
    23  	if err != nil {
    24  		t.Fatal(err)
    25  	}
    26  	metaFile, err := ioutil.ReadFile("testdata/annotations/annotations.pb.go.meta")
    27  	if err != nil {
    28  		t.Fatal(err)
    29  	}
    30  	gotInfo := &descriptorpb.GeneratedCodeInfo{}
    31  	if err := prototext.Unmarshal(metaFile, gotInfo); err != nil {
    32  		t.Fatalf("can't parse meta file: %v", err)
    33  	}
    34  
    35  	wantInfo := &descriptorpb.GeneratedCodeInfo{}
    36  	for _, want := range []struct {
    37  		prefix, text, suffix string
    38  		annotation           *descriptorpb.GeneratedCodeInfo_Annotation
    39  	}{{
    40  		"type ", "AnnotationsTestEnum", " int32",
    41  		&descriptorpb.GeneratedCodeInfo_Annotation{
    42  			Path: []int32{int32(genid.FileDescriptorProto_EnumType_field_number), 0},
    43  		},
    44  	}, {
    45  		"\t", "AnnotationsTestEnum_ANNOTATIONS_TEST_ENUM_VALUE", " AnnotationsTestEnum = 0",
    46  		&descriptorpb.GeneratedCodeInfo_Annotation{
    47  			Path: []int32{int32(genid.FileDescriptorProto_EnumType_field_number), 0, int32(genid.EnumDescriptorProto_Value_field_number), 0},
    48  		},
    49  	}, {
    50  		"type ", "AnnotationsTestMessage", " struct {",
    51  		&descriptorpb.GeneratedCodeInfo_Annotation{
    52  			Path: []int32{int32(genid.FileDescriptorProto_MessageType_field_number), 0},
    53  		},
    54  	}, {
    55  		"\t", "AnnotationsTestField", " ",
    56  		&descriptorpb.GeneratedCodeInfo_Annotation{
    57  			Path: []int32{int32(genid.FileDescriptorProto_MessageType_field_number), 0, int32(genid.DescriptorProto_Field_field_number), 0},
    58  		},
    59  	}, {
    60  		"\t", "XXX_weak_M", " ",
    61  		&descriptorpb.GeneratedCodeInfo_Annotation{
    62  			Path: []int32{int32(genid.FileDescriptorProto_MessageType_field_number), 0, int32(genid.DescriptorProto_Field_field_number), 1},
    63  		},
    64  	}, {
    65  		"func (x *AnnotationsTestMessage) ", "GetAnnotationsTestField", "() string {",
    66  		&descriptorpb.GeneratedCodeInfo_Annotation{
    67  			Path: []int32{int32(genid.FileDescriptorProto_MessageType_field_number), 0, int32(genid.DescriptorProto_Field_field_number), 0},
    68  		},
    69  	}, {
    70  		"func (x *AnnotationsTestMessage) ", "GetM", "() proto.Message {",
    71  		&descriptorpb.GeneratedCodeInfo_Annotation{
    72  			Path: []int32{int32(genid.FileDescriptorProto_MessageType_field_number), 0, int32(genid.DescriptorProto_Field_field_number), 1},
    73  		},
    74  	}, {
    75  		"func (x *AnnotationsTestMessage) ", "SetM", "(v proto.Message) {",
    76  		&descriptorpb.GeneratedCodeInfo_Annotation{
    77  			Path:     []int32{int32(genid.FileDescriptorProto_MessageType_field_number), 0, int32(genid.DescriptorProto_Field_field_number), 1},
    78  			Semantic: descriptorpb.GeneratedCodeInfo_Annotation_SET.Enum(),
    79  		},
    80  	}} {
    81  		s := want.prefix + want.text + want.suffix
    82  		pos := bytes.Index(sourceFile, []byte(s))
    83  		if pos < 0 {
    84  			t.Errorf("source file does not contain: %v", s)
    85  			continue
    86  		}
    87  		begin := pos + len(want.prefix)
    88  		end := begin + len(want.text)
    89  		a := &descriptorpb.GeneratedCodeInfo_Annotation{
    90  			Begin:      proto.Int32(int32(begin)),
    91  			End:        proto.Int32(int32(end)),
    92  			SourceFile: proto.String("cmd/protoc-gen-go/testdata/annotations/annotations.proto"),
    93  		}
    94  		proto.Merge(a, want.annotation)
    95  		wantInfo.Annotation = append(wantInfo.Annotation, a)
    96  	}
    97  	if diff := cmp.Diff(wantInfo, gotInfo, protocmp.Transform()); diff != "" {
    98  		t.Fatalf("unexpected annotations for annotations.proto (-want +got):\n%s", diff)
    99  	}
   100  }
   101  

View as plain text