Debian Codegraph @

comment_test.go

raw symbols SCIP
1 // Copyright (c) 2017 Ernest Micklei
2 //
3 // MIT License
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining
6 // a copy of this software and associated documentation files (the
7 // "Software"), to deal in the Software without restriction, including
8 // without limitation the rights to use, copy, modify, merge, publish,
9 // distribute, sublicense, and/or sell copies of the Software, and to
10 // permit persons to whom the Software is furnished to do so, subject to
11 // the following conditions:
12 //
13 // The above copyright notice and this permission notice shall be
14 // included in all copies or substantial portions of the Software.
15 //
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
24 package proto
25
26 import (
27 "testing"
28 "text/scanner"
29 )
30
31 var startPosition = scanner.Position{Line: 1, Column: 1}
32
33 func TestCreateComment(t *testing.T) {
34 c0 := newComment(startPosition, "")
35 if got, want := len(c0.Lines), 1; got != want {
36 t.Errorf("got [%v] want [%v]", got, want)
37 }
38 c1 := newComment(startPosition, `hello
39 world`)
40 if got, want := len(c1.Lines), 2; got != want {
41 t.Errorf("got [%v] want [%v]", got, want)
42 }
43 if got, want := c1.Lines[0], "hello"; got != want {
44 t.Errorf("got [%v] want [%v]", got, want)
45 }
46 if got, want := c1.Lines[1], "world"; got != want {
47 t.Errorf("got [%v] want [%v]", got, want)
48 }
49 if got, want := c1.Cstyle, false; got != want {
50 t.Errorf("got [%v] want [%v]", got, want)
51 }
52 }
53
54 func TestTakeLastComment(t *testing.T) {
55 c0 := newComment(startPosition, "hi")
56 c1 := newComment(startPosition, "there")
57 _, l := takeLastCommentIfEndsOnLine([]Visitee{c0, c1}, 1)
58 if got, want := len(l), 1; got != want {
59 t.Fatalf("got [%v] want [%v]", got, want)
60 }
61 if got, want := l[0], c0; got != want {
62 t.Errorf("got [%v] want [%v]", c1, want)
63 }
64 }
65
66 func TestParseCommentWithEmptyLinesIndentAndTripleSlash(t *testing.T) {
67 proto := `
68 // comment 1
69 // comment 2
70 //
71 // comment 3
72 /// comment 4`
73 p := newParserOn(proto)
74 def, err := p.Parse()
75 if err != nil {
76 t.Fatal(err)
77 }
78 //spew.Dump(def)
79 if got, want := len(def.Elements), 1; got != want {
80 t.Fatalf("got [%v] want [%v]", got, want)
81 }
82
83 if got, want := len(def.Elements[0].(*Comment).Lines), 5; got != want {
84 t.Fatalf("got [%v] want [%v]", got, want)
85 }
86 if got, want := def.Elements[0].(*Comment).Lines[4], " comment 4"; got != want {
87 t.Fatalf("got [%v] want [%v]", got, want)
88 }
89 if got, want := def.Elements[0].(*Comment).Position.Line, 2; got != want {
90 t.Fatalf("got [%d] want [%d]", got, want)
91 }
92 if got, want := def.Elements[0].(*Comment).Cstyle, false; got != want {
93 t.Fatalf("got [%v] want [%v]", got, want)
94 }
95 }
96
97 func TestParseCStyleComment(t *testing.T) {
98 proto := `
99 /*comment 1
100 comment 2
101
102 comment 3
103 comment 4
104 */`
105 p := newParserOn(proto)
106 def, err := p.Parse()
107 if err != nil {
108 t.Fatal(err)
109 }
110 if got, want := len(def.Elements), 1; got != want {
111 t.Fatalf("got [%v] want [%v]", got, want)
112 }
113
114 if got, want := len(def.Elements[0].(*Comment).Lines), 6; got != want {
115 t.Fatalf("got [%v] want [%v]", got, want)
116 }
117 if got, want := def.Elements[0].(*Comment).Lines[3], "comment 3"; got != want {
118 t.Fatalf("got [%v] want [%v]", got, want)
119 }
120 if got, want := def.Elements[0].(*Comment).Lines[4], " comment 4"; got != want {
121 t.Fatalf("got [%v] want [%v]", got, want)
122 }
123 if got, want := def.Elements[0].(*Comment).Cstyle, true; got != want {
124 t.Fatalf("got [%v] want [%v]", got, want)
125 }
126 }
127
128 func TestParseCStyleCommentWithIndent(t *testing.T) {
129 proto := `
130 /*comment 1
131 comment 2
132
133 comment 3
134 comment 4
135 */`
136 p := newParserOn(proto)
137 def, err := p.Parse()
138 if err != nil {
139 t.Fatal(err)
140 }
141 if got, want := len(def.Elements), 1; got != want {
142 t.Fatalf("got [%v] want [%v]", got, want)
143 }
144
145 if got, want := len(def.Elements[0].(*Comment).Lines), 6; got != want {
146 t.Fatalf("got [%v] want [%v]", got, want)
147 }
148 if got, want := def.Elements[0].(*Comment).Lines[0], "comment 1"; got != want {
149 t.Fatalf("got [%v] want [%v]", got, want)
150 }
151 if got, want := def.Elements[0].(*Comment).Lines[3], "\tcomment 3"; got != want {
152 t.Fatalf("got [%v] want [%v]", got, want)
153 }
154 if got, want := def.Elements[0].(*Comment).Lines[4], "\t comment 4"; got != want {
155 t.Fatalf("got [%v] want [%v]", got, want)
156 }
157 if got, want := def.Elements[0].(*Comment).Cstyle, true; got != want {
158 t.Fatalf("got [%v] want [%v]", got, want)
159 }
160 }
161
162 func TestParseCStyleOneLineComment(t *testing.T) {
163 proto := `/* comment 1 */`
164 p := newParserOn(proto)
165 def, err := p.Parse()
166 if err != nil {
167 t.Fatal(err)
168 }
169 if got, want := len(def.Elements), 1; got != want {
170 t.Fatalf("got [%v] want [%v]", got, want)
171 }
172
173 if got, want := len(def.Elements[0].(*Comment).Lines), 1; got != want {
174 t.Fatalf("got [%v] want [%v]", got, want)
175 }
176 if got, want := def.Elements[0].(*Comment).Lines[0], " comment 1 "; got != want {
177 t.Errorf("got [%v] want [%v]", got, want)
178 }
179 if got, want := def.Elements[0].(*Comment).Cstyle, true; got != want {
180 t.Errorf("got [%v] want [%v]", got, want)
181 }
182 }
183
184 func TestParseCStyleInlineComment(t *testing.T) {
185 proto := `message Foo {
186 int64 hello = 1; /*
187 comment 1
188 */
189 }`
190 p := newParserOn(proto)
191 def := new(Proto)
192 err := def.parse(p)
193 if err != nil {
194 t.Fatal(err)
195 }
196 m := def.Elements[0].(*Message)
197 if len(m.Elements) != 1 {
198 t.Fatal("expected one element", m.Elements)
199 }
200 f := m.Elements[0].(*NormalField)
201 comment := f.InlineComment
202 if comment == nil {
203 t.Fatal("no inline comment")
204 }
205 if got, want := len(comment.Lines), 3; got != want {
206 t.Fatalf("got [%v] want [%v]", got, want)
207 }
208 if got, want := comment.Lines[0], ""; got != want {
209 t.Errorf("got [%v] want [%v]", got, want)
210 }
211 if got, want := comment.Lines[1], " comment 1"; got != want {
212 t.Errorf("got [%v] want [%v]", got, want)
213 }
214 if got, want := comment.Cstyle, true; got != want {
215 t.Errorf("got [%v] want [%v]", got, want)
216 }
217 }
218
219 func TestParseCommentWithTripleSlash(t *testing.T) {
220 proto := `
221 /// comment 1
222 `
223 p := newParserOn(proto)
224 def, err := p.Parse()
225 if err != nil {
226 t.Fatal(err)
227 }
228 if got, want := len(def.Elements), 1; got != want {
229 t.Fatalf("got [%v] want [%v]", got, want)
230 }
231 if got, want := def.Elements[0].(*Comment).ExtraSlash, true; got != want {
232 t.Fatalf("got [%v] want [%v]", got, want)
233 }
234 if got, want := def.Elements[0].(*Comment).Lines[0], " comment 1"; got != want {
235 t.Fatalf("got [%v] want [%v]", got, want)
236 }
237 if got, want := def.Elements[0].(*Comment).Position.Line, 2; got != want {
238 t.Fatalf("got [%d] want [%d]", got, want)
239 }
240 }
241
242 func TestCommentAssociation(t *testing.T) {
243 src := `
244 // foo1
245 // foo2
246
247 // bar
248
249 syntax = "proto3";
250
251 // baz
252
253 // bat1
254 // bat2
255 package bat;
256
257 // Oneway is the return type to use for an rpc method if
258 // the method should be generated as oneway.
259 message Oneway {
260 bool ack = 1;
261 }`
262 p := newParserOn(src)
263 def, err := p.Parse()
264 if err != nil {
265 t.Fatal(err)
266 }
267 if got, want := len(def.Elements), 6; got != want {
268 t.Fatalf("got [%v] want [%v]", got, want)
269 }
270 pkg := def.Elements[4].(*Package)
271 if got, want := pkg.Comment.Message(), " bat1"; got != want {
272 t.Fatalf("got [%v] want [%v]", got, want)
273 }
274 if got, want := len(pkg.Comment.Lines), 2; got != want {
275 t.Fatalf("got [%v] want [%v]", got, want)
276 }
277 if got, want := pkg.Comment.Lines[1], " bat2"; got != want {
278 t.Fatalf("got [%v] want [%v]", got, want)
279 }
280 if got, want := len(def.Elements[5].(*Message).Comment.Lines), 2; got != want {
281 t.Fatalf("got [%v] want [%v]", got, want)
282 }
283 }
284
285 func TestCommentInOptionValue(t *testing.T) {
286 src := `syntax = "proto3";
287 message Foo {
288 string bar = 1 [
289 // comment
290 // me
291 deprecated=true
292 ];
293 }`
294 p := newParserOn(src)
295 def, err := p.Parse()
296 if err != nil {
297 t.Fatal(err)
298 }
299 o := def.Elements[1].(*Message).elements()[0].(*NormalField).Options[0]
300 if got, want := o.Name, "deprecated"; got != want {
301 t.Fatalf("got [%v] want [%v]", got, want)
302 }
303 if got, want := def.Elements[1].(*Message).elements()[0].(*NormalField).IsDeprecated(), true; got != want {
304 t.Errorf("got [%v]:%T want [%v]:%T", got, got, want, want)
305 }
306 if got, want := len(o.Comment.Lines), 2; got != want {
307 t.Fatalf("got [%v] want [%v]", got, want)
308 }
309 if got, want := o.Comment.Lines[1], " me"; got != want {
310 t.Fatalf("got [%v] want [%v]", got, want)
311 }
312 }
313
314 func TestNormalFieldInlineComment(t *testing.T) {
315 src := `message Example {
316 /* i'm */ optional /* a comment */ bool /* too */ field /* hard */ = /* to */ 1 /* read */;
317 }`
318 p := newParserOn(src)
319 def, err := p.Parse()
320 if err != nil {
321 t.Fatal(err)
322 }
323 m := def.Elements[0].(*Message)
324 f := m.Elements[1].(*NormalField)
325 lines := f.Field.InlineComment.Lines
326 if got, want := len(lines), 5; got != want {
327 t.Fatalf("got [%v:%T] want [%v:%T]", got, got, want, want)
328 }
329 if got, want := lines[0], " a comment "; got != want {
330 t.Errorf("got [%v:%T] want [%v:%T]", got, got, want, want)
331 }
332 if got, want := lines[1], " too "; got != want {
333 t.Errorf("got [%v:%T] want [%v:%T]", got, got, want, want)
334 }
335 if got, want := lines[4], " read "; got != want {
336 t.Errorf("got [%v:%T] want [%v:%T]", got, got, want, want)
337 }
338 }