blob: 7463215bab34e9e70f1a1298037e45a45bf01ee1 [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 Tsai009e0672018-11-27 18:45:07 -080013 "github.com/golang/protobuf/v2/internal/scalar"
Joe Tsaie1f8d502018-11-26 18:55:29 -080014
15 descriptorpb "github.com/golang/protobuf/v2/types/descriptor"
Damien Neil220c2022018-08-15 11:24:18 -070016)
17
Damien Neil162c1272018-10-04 12:42:37 -070018func TestAnnotations(t *testing.T) {
Joe Tsai19058432019-02-27 21:46:29 -080019 sourceFile, err := ioutil.ReadFile("testdata/annotations/annotations.pb.go")
Damien Neil162c1272018-10-04 12:42:37 -070020 if err != nil {
21 t.Fatal(err)
22 }
Joe Tsai19058432019-02-27 21:46:29 -080023 metaFile, err := ioutil.ReadFile("testdata/annotations/annotations.pb.go.meta")
Damien Neil162c1272018-10-04 12:42:37 -070024 if err != nil {
25 t.Fatal(err)
26 }
Joe Tsaie1f8d502018-11-26 18:55:29 -080027 gotInfo := &descriptorpb.GeneratedCodeInfo{}
Damien Neil162c1272018-10-04 12:42:37 -070028 if err := proto.UnmarshalText(string(metaFile), gotInfo); err != nil {
29 t.Fatalf("can't parse meta file: %v", err)
30 }
31
Joe Tsaie1f8d502018-11-26 18:55:29 -080032 wantInfo := &descriptorpb.GeneratedCodeInfo{}
Damien Neil162c1272018-10-04 12:42:37 -070033 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 Tsaie1f8d502018-11-26 18:55:29 -080060 wantInfo.Annotation = append(wantInfo.Annotation, &descriptorpb.GeneratedCodeInfo_Annotation{
Damien Neil162c1272018-10-04 12:42:37 -070061 Path: want.path,
Joe Tsai009e0672018-11-27 18:45:07 -080062 Begin: scalar.Int32(int32(begin)),
63 End: scalar.Int32(int32(end)),
Joe Tsai19058432019-02-27 21:46:29 -080064 SourceFile: scalar.String("annotations/annotations.proto"),
Damien Neil162c1272018-10-04 12:42:37 -070065 })
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}