1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 package proto
25
26 import "testing"
27
28 func TestService(t *testing.T) {
29 proto := `service AccountService {
30 // comment
31 rpc CreateAccount (CreateAccount) returns (ServiceFault); // inline comment
32 rpc GetAccounts (stream Int64) returns (Account) {} // inline comment2
33 rpc Health(google.protobuf.Empty) returns (google.protobuf.Empty) {} // inline comment3
34 }`
35 pr, err := newParserOn(proto).Parse()
36 if err != nil {
37 t.Fatal(err)
38 }
39 srv := collect(pr).Services()[0]
40 if got, want := len(srv.Elements), 3; got != want {
41 t.Fatalf("got [%v] want [%v]", got, want)
42 }
43 if got, want := srv.Position.String(), "<input>:1:1"; got != want {
44 t.Fatalf("got [%v] want [%v]", got, want)
45 }
46 rpc1 := srv.Elements[0].(*RPC)
47 if got, want := rpc1.Name, "CreateAccount"; got != want {
48 t.Fatalf("got [%v] want [%v]", got, want)
49 }
50 if got, want := rpc1.Doc().Message(), " comment"; got != want {
51 t.Fatalf("got [%v] want [%v]", got, want)
52 }
53 if got, want := rpc1.InlineComment.Message(), " inline comment"; got != want {
54 t.Fatalf("got [%v] want [%v]", got, want)
55 }
56 if got, want := rpc1.Position.Line, 3; got != want {
57 t.Fatalf("got [%v] want [%v]", got, want)
58 }
59 rpc2 := srv.Elements[1].(*RPC)
60 if got, want := rpc2.Name, "GetAccounts"; got != want {
61 t.Errorf("got [%v] want [%v]", got, want)
62 }
63 rpc3 := srv.Elements[2].(*RPC)
64 if got, want := rpc3.Name, "Health"; got != want {
65 t.Errorf("got [%v] want [%v]", got, want)
66 }
67 if rpc2.InlineComment == nil {
68 t.Fatal("missing inline comment 2")
69 }
70 if got, want := rpc2.InlineComment.Message(), " inline comment2"; got != want {
71 t.Fatalf("got [%v] want [%v]", got, want)
72 }
73 if rpc3.InlineComment == nil {
74 t.Fatal("missing inline comment 3")
75 }
76 if got, want := rpc3.InlineComment.Message(), " inline comment3"; got != want {
77 t.Fatalf("got [%v] want [%v]", got, want)
78 }
79 }
80
81 func TestRPCWithOptionAggregateSyntax(t *testing.T) {
82 proto := `service AccountService {
83 // CreateAccount
84 rpc CreateAccount (CreateAccount) returns (ServiceFault){
85 // test_ident
86 option (test_ident) = {
87 test: "test"
88 test2:"test2"
89 }; // inline test_ident
90 }
91 }`
92 pr, err := newParserOn(proto).Parse()
93 if err != nil {
94 t.Fatal(err)
95 }
96 srv := collect(pr).Services()[0]
97 if got, want := len(srv.Elements), 1; got != want {
98 t.Fatalf("got [%v] want [%v]", got, want)
99 }
100 rpc1 := srv.Elements[0].(*RPC)
101 if got, want := len(rpc1.Elements), 2; got != want {
102 t.Errorf("got [%v] want [%v]", got, want)
103 }
104 com := rpc1.Elements[0].(*Comment)
105 if got, want := com.Message(), " test_ident"; got != want {
106 t.Errorf("got [%v] want [%v]", got, want)
107 }
108 opt := rpc1.Elements[1].(*Option)
109 if got, want := opt.Name, "(test_ident)"; got != want {
110 t.Errorf("got [%v] want [%v]", got, want)
111 }
112 if got, want := opt.InlineComment != nil, true; got != want {
113 t.Fatalf("got [%v] want [%v]", got, want)
114 }
115 if got, want := opt.InlineComment.Message(), " inline test_ident"; got != want {
116 t.Errorf("got [%v] want [%v]", got, want)
117 }
118 if got, want := len(opt.Constant.Map), 2; got != want {
119 t.Fatalf("got [%v] want [%v]", got, want)
120 }
121
122 if got, want := len(rpc1.Options), 1; got != want {
123 t.Errorf("got len Options %v want %v", got, want)
124 }
125 }
126
127 func TestServiceWithOption(t *testing.T) {
128 src := `service AnyService {
129 option secure = true;
130 }`
131 p := newParserOn(src)
132 p.next()
133 svc := new(Service)
134 err := svc.parse(p)
135 if err != nil {
136 t.Fatal(err)
137 }
138 if got, want := svc.Elements[0].(*Option).Name, "secure"; got != want {
139 t.Errorf("got [%v] want [%v]", got, want)
140 }
141 checkParent(svc.Elements[0].(*Option), t)
142 }
143
144 func TestRPCWithOneLineCommentInOptionBlock(t *testing.T) {
145 proto := `service AccountService {
146 rpc CreateAccount (CreateAccount) returns (ServiceFault) {
147 // test comment
148 }
149 }`
150 _, err := newParserOn(proto).Parse()
151 if err != nil {
152 t.Fatal(err)
153 }
154 }
155
156 func TestRPCWithMultiLineCommentInOptionBlock(t *testing.T) {
157 proto := `service AccountService {
158 rpc CreateAccount (CreateAccount) returns (ServiceFault) {
159 // test comment
160 // test comment
161 }
162 }`
163 def, err := newParserOn(proto).Parse()
164 if err != nil {
165 t.Fatal(err)
166 }
167 s := def.Elements[0].(*Service)
168 r := s.Elements[0].(*RPC)
169 if got, want := len(r.Elements), 1; got != want {
170 t.Errorf("got [%v] want [%v]", got, want)
171 }
172 c := r.Elements[0].(*Comment)
173 if got, want := len(c.Lines), 2; got != want {
174 t.Errorf("got [%v] want [%v]", got, want)
175 }
176 }
177
178 func TestRPCWithTypeThatHasLeadingDot(t *testing.T) {
179 src := `service Dummy {
180 rpc DeleteProgram (ProgramIdentifier) returns (.google.protobuf.Empty) {}
181 }`
182 _, err := newParserOn(src).Parse()
183 if err != nil {
184 t.Fatal(err)
185 }
186 }
187
188 func TestServiceInlineCommentBeforeBody(t *testing.T) {
189 src := `service BarService // BarService
190 // with another line
191 {
192 rpc Foo (Void) returns (Magic) // FooRPC {
193 // yet another line
194 }
195 }
196 `
197 p := newParserOn(src)
198 svc := new(Service)
199 p.next()
200 if err := svc.parse(p); err != nil {
201 t.Fatal(err)
202 }
203 nestedComment := svc.Elements[0].(*Comment)
204 if nestedComment == nil {
205 t.Fatal("expected comment present")
206 }
207 if got, want := len(nestedComment.Lines), 2; got != want {
208 t.Errorf("got %d want %d lines", got, want)
209 }
210 }
211
View as plain text