Debian Codegraph @

option_test.go

raw symbols SCIP
1 // Copyright (c) 2025 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 )
29
30 func TestOptionCases(t *testing.T) {
31 for i, each := range []struct {
32 proto string
33 name string
34 strLit string
35 nonStrLit string
36 }{{
37 `option (full).java_package = "com.example.foo";`,
38 "(full).java_package",
39 "com.example.foo",
40 "",
41 }, {
42 `option Bool = true;`,
43 "Bool",
44 "",
45 "true",
46 }, {
47 `option Float = -3.14E1;`,
48 "Float",
49 "",
50 "-3.14E1",
51 }, {
52 `option (foo_options) = { opt1: 123 opt2: "baz" };`,
53 "(foo_options)",
54 "",
55 "",
56 }, {
57 `option foo = []`,
58 "foo",
59 "",
60 "",
61 }, {
62 `option optimize_for = SPEED;`,
63 "optimize_for",
64 "",
65 "SPEED",
66 }, {
67 "option (my.enum.service.is.like).rpc = 1;",
68 "(my.enum.service.is.like).rpc",
69 "",
70 "1",
71 }, {
72 `option (imported.oss.package).action = "literal-double-quotes";`,
73 "(imported.oss.package).action",
74 "literal-double-quotes",
75 "",
76 }, {
77 `option (imported.oss.package).action = "key:\"literal-double-quotes-escaped\"";`,
78 "(imported.oss.package).action",
79 `key:\"literal-double-quotes-escaped\"`,
80 "",
81 }, {
82 `option (imported.oss.package).action = 'literalsinglequotes';`,
83 "(imported.oss.package).action",
84 "literalsinglequotes",
85 "",
86 }, {
87 `option (imported.oss.package).action = 'single-quotes.with/symbols';`,
88 "(imported.oss.package).action",
89 "single-quotes.with/symbols",
90 "",
91 }, {
92 `option (imported.oss.package).action = 'single-quotes.with//double-slashes';`,
93 "(imported.oss.package).action",
94 "single-quotes.with//double-slashes",
95 "",
96 }, {
97 `option features.(pb.go).api_level = API_OPAQUE;`,
98 "features.(pb.go).api_level",
99 "API_OPAQUE",
100 "",
101 }} {
102 p := newParserOn(each.proto)
103 pr, err := p.Parse()
104 if err != nil {
105 t.Fatal("testcase failed:", i, err)
106 }
107 if got, want := len(pr.Elements), 1; got != want {
108 t.Fatalf("[%d] got [%v] want [%v]", i, got, want)
109 }
110 o := pr.Elements[0].(*Option)
111 if got, want := o.Name, each.name; got != want {
112 t.Errorf("[%d] got [%v] want [%v]", i, got, want)
113 }
114 if len(each.strLit) > 0 {
115 if got, want := o.Constant.Source, each.strLit; got != want {
116 t.Errorf("[%d] got [%v] want [%v]", i, got, want)
117 }
118 }
119 if len(each.nonStrLit) > 0 {
120 if got, want := o.Constant.Source, each.nonStrLit; got != want {
121 t.Errorf("[%d] got [%v] want [%v]", i, got, want)
122 }
123 }
124 if got, want := o.IsEmbedded, false; got != want {
125 t.Errorf("[%d] got [%v] want [%v]", i, got, want)
126 }
127 }
128 }
129
130 func TestOptionComments(t *testing.T) {
131 proto := `
132 // comment
133 option Help = "me"; // inline`
134 p := newParserOn(proto)
135 pr, err := p.Parse()
136 if err != nil {
137 t.Fatal(err)
138 }
139 o := pr.Elements[0].(*Option)
140 if got, want := o.IsEmbedded, false; got != want {
141 t.Errorf("got [%v] want [%v]", got, want)
142 }
143 if got, want := o.Comment != nil, true; got != want {
144 t.Fatalf("got [%v] want [%v]", got, want)
145 }
146 if got, want := o.Comment.Lines[0], " comment"; got != want {
147 t.Fatalf("got [%v] want [%v]", got, want)
148 }
149 if got, want := o.InlineComment != nil, true; got != want {
150 t.Fatalf("got [%v] want [%v]", got, want)
151 }
152 if got, want := o.InlineComment.Lines[0], " inline"; got != want {
153 t.Fatalf("got [%v] want [%v]", got, want)
154 }
155 if got, want := o.Position.Line, 3; got != want {
156 t.Fatalf("got [%v] want [%v]", got, want)
157 }
158 if got, want := o.Comment.Position.Line, 2; got != want {
159 t.Fatalf("got [%v] want [%v]", got, want)
160 }
161 if got, want := o.InlineComment.Position.Line, 3; got != want {
162 t.Fatalf("got [%v] want [%v]", got, want)
163 }
164 }
165
166 func TestAggregateSyntax(t *testing.T) {
167 proto := `
168 // usage:
169 message Bar {
170 // alternative aggregate syntax (uses TextFormat):
171 int32 b = 2 [(foo_options) = {
172 opt1: 123,
173 opt2: "baz"
174 }];
175 }
176 `
177 p := newParserOn(proto)
178 pr, err := p.Parse()
179 if err != nil {
180 t.Fatal(err)
181 }
182 o := pr.Elements[0].(*Message)
183 f := o.Elements[0].(*NormalField)
184 if got, want := len(f.Options), 1; got != want {
185 t.Fatalf("got [%v] want [%v]", got, want)
186 }
187 ac := f.Options[0].Constant.Map
188 if got, want := len(ac), 2; got != want {
189 t.Fatalf("got [%v] want [%v]", got, want)
190 }
191 if got, want := ac["opt1"].Source, "123"; got != want {
192 t.Fatalf("got [%v] want [%v]", got, want)
193 }
194 if got, want := o.Position.Line, 3; got != want {
195 t.Fatalf("got [%v] want [%v]", got, want)
196 }
197 if got, want := o.Comment.Position.String(), "<input>:2:1"; got != want {
198 t.Fatalf("got [%v] want [%v]", got, want)
199 }
200 if got, want := f.Position.String(), "<input>:5:3"; got != want {
201 t.Fatalf("got [%v] want [%v]", got, want)
202 }
203 if got, want := f.Options[0].Constant.Position.Line, 5; got != want {
204 t.Fatalf("got [%v] want [%v]", got, want)
205 }
206 // check for AggregatedConstants
207 list := f.Options[0].AggregatedConstants
208 if got, want := list[0].Source, "123"; got != want {
209 t.Fatalf("got [%v] want [%v]", got, want)
210 }
211 if got, want := list[1].Source, "baz"; got != want {
212 t.Fatalf("got [%v] want [%v]", got, want)
213 }
214 }
215
216 func TestNonPrimitiveOptionComment(t *testing.T) {
217 proto := `
218 // comment
219 option Help = { string_field: "value" }; // inline`
220 p := newParserOn(proto)
221 pr, err := p.Parse()
222 if err != nil {
223 t.Fatal(err)
224 }
225 o := pr.Elements[0].(*Option)
226 if got, want := o.Comment != nil, true; got != want {
227 t.Fatalf("got [%v] want [%v]", got, want)
228 }
229 if got, want := o.Comment.Lines[0], " comment"; got != want {
230 t.Fatalf("got [%v] want [%v]", got, want)
231 }
232 if got, want := o.InlineComment != nil, true; got != want {
233 t.Fatalf("got [%v] want [%v]", got, want)
234 }
235 if got, want := o.InlineComment.Lines[0], " inline"; got != want {
236 t.Fatalf("got [%v] want [%v]", got, want)
237 }
238 }
239
240 func TestFieldCustomOptions(t *testing.T) {
241 proto := `foo.bar lots = 1 [foo={hello:1}, bar=2];`
242 p := newParserOn(proto)
243 f := newNormalField()
244 err := f.parse(p)
245 if err != nil {
246 t.Fatal(err)
247 }
248 if got, want := f.Type, "foo.bar"; got != want {
249 t.Errorf("got [%v] want [%v]", got, want)
250 }
251 if got, want := f.Name, "lots"; got != want {
252 t.Errorf("got [%v] want [%v]", got, want)
253 }
254 if got, want := len(f.Options), 2; got != want {
255 t.Fatalf("got [%v] want [%v]", got, want)
256 }
257 if got, want := f.Options[0].Name, "foo"; got != want {
258 t.Errorf("got [%v] want [%v]", got, want)
259 }
260 if got, want := f.Options[1].Name, "bar"; got != want {
261 t.Errorf("got [%v] want [%v]", got, want)
262 }
263 if got, want := f.Options[1].Constant.Source, "2"; got != want {
264 t.Errorf("got [%v] want [%v]", got, want)
265 }
266 // check for AggregatedConstants
267 if got, want := f.Options[0].AggregatedConstants[0].Name, "hello"; got != want {
268 t.Errorf("got [%v] want [%v]", got, want)
269 }
270 if got, want := f.Options[0].AggregatedConstants[0].PrintsColon, true; got != want {
271 t.Errorf("got [%v] want [%v]", got, want)
272 }
273 if got, want := f.Options[0].AggregatedConstants[0].Source, "1"; got != want {
274 t.Errorf("got [%v] want [%v]", got, want)
275 }
276 }
277
278 func TestIgnoreIllegalEscapeCharsInAggregatedConstants(t *testing.T) {
279 src := `syntax = "proto3";
280 message Person {
281 string name = 3 [(validate.rules).string = {
282 pattern: "^[^\d\s]+( [^\d\s]+)*$",
283 max_bytes: 256,
284 }];
285 }`
286 p := newParserOn(src)
287 d, err := p.Parse()
288 if err != nil {
289 t.Fatal(err)
290 }
291 f := d.Elements[1].(*Message).Elements[0].(*NormalField)
292 if got, want := f.Options[0].Name, "(validate.rules).string"; got != want {
293 t.Errorf("got [%v] want [%v]", got, want)
294 }
295 if got, want := len(f.Options[0].Constant.Map), 2; got != want {
296 t.Errorf("got [%v] want [%v]", got, want)
297 }
298 if got, want := f.Options[0].Constant.Map["pattern"].Source, "^[^\\d\\s]+( [^\\d\\s]+)*$"; got != want {
299 t.Errorf("got [%v] want [%v]", got, want)
300 }
301 if got, want := len(f.Options[0].Constant.OrderedMap), 2; got != want {
302 t.Errorf("got [%v] want [%v]", got, want)
303 }
304 if got, want := f.Options[0].Constant.OrderedMap[0].Name, "pattern"; got != want {
305 t.Errorf("got [%v] want [%v]", got, want)
306 }
307 if got, want := f.Options[0].Constant.OrderedMap[0].Source, "^[^\\d\\s]+( [^\\d\\s]+)*$"; got != want {
308 t.Errorf("got [%v] want [%v]", got, want)
309 }
310 // check for AggregatedConstants
311 if got, want := f.Options[0].AggregatedConstants[0].Name, "pattern"; got != want {
312 t.Errorf("got [%v] want [%v]", got, want)
313 }
314 if got, want := f.Options[0].AggregatedConstants[0].PrintsColon, true; got != want {
315 t.Errorf("got [%v] want [%v]", got, want)
316 }
317 if got, want := f.Options[0].AggregatedConstants[0].Source, "^[^\\d\\s]+( [^\\d\\s]+)*$"; got != want {
318 t.Errorf("got [%v] want [%v]", got, want)
319 }
320 }
321
322 func TestIgnoreIllegalEscapeCharsInConstant(t *testing.T) {
323 src := `syntax = "proto2";
324 message Person {
325 optional string cpp_trigraph = 20 [default = "? \? ?? \?? \??? ??/ ?\?-"];
326 }`
327 p := newParserOn(src)
328 d, err := p.Parse()
329 if err != nil {
330 t.Fatal(err)
331 }
332 f := d.Elements[1].(*Message).Elements[0].(*NormalField)
333 if got, want := f.Options[0].Constant.Source, "? \\? ?? \\?? \\??? ??/ ?\\?-"; got != want {
334 t.Errorf("got [%v] want [%v]", got, want)
335 }
336 }
337
338 func TestFieldCustomOptionExtendedIdent(t *testing.T) {
339 proto := `Type field = 1 [(validate.rules).enum.defined_only = true];`
340 p := newParserOn(proto)
341 f := newNormalField()
342 err := f.parse(p)
343 if err != nil {
344 t.Fatal(err)
345 }
346 if got, want := f.Options[0].Name, "(validate.rules).enum.defined_only"; got != want {
347 t.Errorf("got [%v] want [%v]", got, want)
348 }
349 }
350
351 // issue #50
352 func TestNestedAggregateConstants(t *testing.T) {
353 src := `syntax = "proto3";
354
355 package baz;
356
357 option (foo.bar) = {
358 woot: 100
359 foo {
360 hello: 200
361 hello2: 300
362 bar {
363 hello3: 400
364 }
365 }
366 };`
367 p := newParserOn(src)
368 proto, err := p.Parse()
369 if err != nil {
370 t.Error(err)
371 }
372 option := proto.Elements[2].(*Option)
373 if got, want := option.Name, "(foo.bar)"; got != want {
374 t.Errorf("got [%v] want [%v]", got, want)
375 }
376 if got, want := len(option.Constant.Map), 2; got != want {
377 t.Errorf("got [%v] want [%v]", got, want)
378 }
379 m := option.Constant.Map
380 if got, want := m["woot"].Source, "100"; got != want {
381 t.Errorf("got [%v] want [%v]", got, want)
382 }
383 if got, want := len(m["foo"].Map), 3; got != want {
384 t.Errorf("got [%v] want [%v]", got, want)
385 }
386 m = m["foo"].Map
387 if got, want := len(m["bar"].Map), 1; got != want {
388 t.Errorf("got [%v] want [%v]", got, want)
389 }
390 if got, want := m["bar"].Map["hello3"].Source, "400"; got != want {
391 t.Errorf("got [%v] want [%v]", got, want)
392 }
393 if got, want := option.Constant.OrderedMap[1].Name, "foo"; got != want {
394 t.Errorf("got [%v] want [%v]", got, want)
395 }
396 if got, want := option.Constant.OrderedMap[1].OrderedMap[2].Name, "bar"; got != want {
397 t.Errorf("got [%v] want [%v]", got, want)
398 }
399 if got, want := option.Constant.OrderedMap[1].OrderedMap[2].OrderedMap[0].Source, "400"; got != want {
400 t.Errorf("got [%v] want [%v]", got, want)
401 }
402 if got, want := len(option.AggregatedConstants), 4; got != want {
403 t.Errorf("got [%v] want [%v]", got, want)
404 }
405 // for _, each := range option.AggregatedConstants {
406 // t.Logf("%#v=%v\n", each, each.SourceRepresentation())
407 // }
408 }
409
410 // Issue #59
411 func TestMultiLineOptionAggregateValue(t *testing.T) {
412 src := `rpc ListTransferLogs(ListTransferLogsRequest)
413 returns (ListTransferLogsResponse) {
414 option (google.api.http) = {
415 get: "/v1/{parent=projects/*/locations/*/transferConfigs/*/runs/*}/"
416 "transferLogs"
417 };
418 }`
419 p := newParserOn(src)
420 rpc := new(RPC)
421 p.next()
422 err := rpc.parse(p)
423 if err != nil {
424 t.Error(err)
425 }
426 get := rpc.Options[0].Constant.Map["get"]
427 if got, want := get.Source, "/v1/{parent=projects/*/locations/*/transferConfigs/*/runs/*}/transferLogs"; got != want {
428 t.Errorf("got [%v] want [%v]", got, want)
429 }
430 }
431
432 // issue #76
433 func TestOptionAggregateCanUseKeyword(t *testing.T) {
434 src := `message User {
435 string email = 3 [(validate.field) = {required: true}];
436 }`
437 p := newParserOn(src)
438 _, err := p.Parse()
439 if err != nil {
440 t.Error(err)
441 }
442 }
443
444 // issue #77
445 func TestOptionAggregateWithRepeatedValues(t *testing.T) {
446 src := `message Envelope {
447 int64 not_in = 15 [(validate.rules).int64 = {not_in: [40, 45]}];
448 int64 in = 16 [(validate.rules).int64 = {in: [[1],[2]]}];
449 }`
450 p := newParserOn(src)
451 def, err := p.Parse()
452 if err != nil {
453 t.Error(err)
454 }
455 field := def.Elements[0].(*Message).Elements[0].(*NormalField)
456 notIn := field.Options[0].Constant.Map["not_in"]
457 if got, want := len(notIn.Array), 2; got != want {
458 t.Errorf("got [%v] want [%v]", got, want)
459 }
460 if got, want := notIn.Array[0].Source, "40"; got != want {
461 t.Errorf("got [%v] want [%v]", got, want)
462 }
463 if got, want := notIn.Array[1].Source, "45"; got != want {
464 t.Errorf("got [%v] want [%v]", got, want)
465 }
466 }
467
468 func TestInvalidOptionAggregateWithRepeatedValues(t *testing.T) {
469 src := `message Bogus {
470 int64 a = 1 [a = {not_in: [40 syntax]}];
471 }`
472 p := newParserOn(src)
473 _, err := p.Parse()
474 if err == nil {
475 t.Error("expected syntax error")
476 }
477 }
478
479 // issue #79
480 func TestUseOfSemicolonsInAggregatedConstants(t *testing.T) {
481 src := `rpc Test(Void) returns (Void) {
482 option (google.api.http) = {
483 post: "/api/v1/test";
484 body: "*"; // ignored comment
485 };
486 }`
487 p := newParserOn(src)
488 rpc := new(RPC)
489 p.next()
490 err := rpc.parse(p)
491 if err != nil {
492 t.Fatal(err)
493 }
494 if got, want := len(rpc.Elements), 1; got != want {
495 t.Errorf("got [%v] want [%v]", got, want)
496 }
497 opt := rpc.Elements[0].(*Option)
498 if got, want := len(opt.Constant.Map), 2; got != want {
499 t.Fatalf("got [%v] want [%v]", got, want)
500 }
501 // old access to map
502 if got, want := opt.Constant.Map["body"].Source, "*"; got != want {
503 t.Errorf("got [%v] want [%v]", got, want)
504 }
505 // new access to map
506 body, ok := opt.Constant.OrderedMap.Get("body")
507 if !ok {
508 t.Fatal("expected body key")
509 }
510 if got, want := body.Source, "*"; got != want {
511 t.Errorf("got [%v] want [%v]", got, want)
512 }
513 // for _, each := range opt.Constant.OrderedMap {
514 // t.Log(each)
515 // }
516 }
517
518 func TestParseNestedSelectorInAggregatedConstant(t *testing.T) {
519 src := `rpc Test(Void) returns (Void) {
520 option (google.api.http) = {
521 get: "/api/v1/test"
522 additional_bindings.post: "/api/v1/test"
523 additional_bindings.body: "*"
524 };
525 }`
526 p := newParserOn(src)
527 rpc := new(RPC)
528 p.next()
529 err := rpc.parse(p)
530 if err != nil {
531 t.Fatal(err)
532 }
533 if got, want := rpc.Options[0].Constant.Map["get"].Source, "/api/v1/test"; got != want {
534 t.Errorf("got [%v] want [%v]", got, want)
535 }
536 if got, want := rpc.Options[0].Constant.Map["additional_bindings.post"].Source, "/api/v1/test"; got != want {
537 t.Errorf("got [%v] want [%v]", got, want)
538 }
539 if got, want := rpc.Options[0].AggregatedConstants[2].Name, "additional_bindings.body"; got != want {
540 t.Errorf("got [%v] want [%v]", got, want)
541 }
542 if got, want := rpc.Options[0].AggregatedConstants[2].Source, "*"; got != want {
543 t.Errorf("got [%v] want [%v]", got, want)
544 }
545 }
546
547 func TestParseMultilineStringConstant(t *testing.T) {
548 src := `message Test {
549 string description = 3 [
550 (common.ui_field_desc) = "Description of the account"
551 " domain (e.g. Team,"
552 "Name User Account Directory)."
553 ];
554 }`
555 p := newParserOn(src)
556 m := new(Message)
557 p.next()
558 err := m.parse(p)
559 if err != nil {
560 t.Fatal(err)
561 }
562 s := m.Elements[0].(*NormalField).Options[0].Constant.Source
563 if got, want := s, "Description of the account domain (e.g. Team,Name User Account Directory)."; got != want {
564 t.Errorf("got [%v] want [%v]", got, want)
565 }
566 }
567
568 func TestOptionWithRepeatedMessageValues(t *testing.T) {
569 src := `message Foo {
570 int64 a = 1 [b = {repeated_message_field: [{hello: 1}, {hello: 2}]}];
571 }`
572 p := newParserOn(src)
573 def, err := p.Parse()
574 if err != nil {
575 t.Errorf("expected no error but got %v", err)
576 }
577 opt := def.Elements[0].(*Message).Elements[0].(*NormalField).Options[0]
578 hello, ok := opt.AggregatedConstants[0].Array[0].OrderedMap.Get("hello")
579 if !ok {
580 t.Fail()
581 }
582 if got, want := hello.SourceRepresentation(), "1"; got != want {
583 t.Errorf("got [%v] want [%v]", got, want)
584 }
585 }
586
587 func TestOptionWithRepeatedMessageValuesWithArray(t *testing.T) {
588 src := `message Foo {
589 int64 a = 1 [ (bar.repeated_field_dep_option) =
590 { hello: 1, repeated_dep: [
591 { hello: 1, repeated_bar: [1, 2] },
592 { hello: 3, repeated_bar: [3, 4] } ] } ];
593 }`
594 p := newParserOn(src)
595 def, err := p.Parse()
596 if err != nil {
597 t.Errorf("expected no error but got %v", err)
598 }
599 opt := def.Elements[0].(*Message).Elements[0].(*NormalField).Options[0]
600 hello, ok := opt.Constant.OrderedMap.Get("hello")
601 if !ok {
602 t.Fail()
603 }
604 if got, want := hello.SourceRepresentation(), "1"; got != want {
605 t.Errorf("got [%v] want [%v]", got, want)
606 }
607 repeatedDep, ok := opt.Constant.OrderedMap.Get("repeated_dep")
608 if !ok {
609 t.Fail()
610 }
611 if got, want := len(repeatedDep.Array), 2; got != want {
612 t.Errorf("got [%v] want [%v]", got, want)
613 }
614 hello, ok = repeatedDep.Array[0].OrderedMap.Get("hello")
615 if !ok {
616 t.Fail()
617 }
618 if got, want := hello.SourceRepresentation(), "1"; got != want {
619 t.Errorf("got [%v] want [%v]", got, want)
620 }
621 onetwo, ok := repeatedDep.Array[0].OrderedMap.Get("repeated_bar")
622 if !ok {
623 t.Fail()
624 }
625 if got, want := onetwo.Array[0].Source, "1"; got != want {
626 t.Errorf("got [%v] want [%v]", got, want)
627 }
628 if got, want := onetwo.Array[1].Source, "2"; got != want {
629 t.Errorf("got [%v] want [%v]", got, want)
630 }
631 }
632
633 // https://github.com/emicklei/proto/issues/99
634 func TestFieldCustomOptionLeadingDot(t *testing.T) {
635 proto := `string app_entity_id = 4 [(.common.v1.some_custom_option) = { opt1: true opt2: false }];`
636 p := newParserOn(proto)
637 f := newNormalField()
638 err := f.parse(p)
639 if err != nil {
640 t.Fatal(err)
641 }
642 if got, want := f.Type, "string"; got != want {
643 t.Errorf("got [%v] want [%v]", got, want)
644 }
645 o := f.Options[0]
646 if got, want := o.Name, "(.common.v1.some_custom_option)"; got != want {
647 t.Fatalf("got [%v] want [%v]", got, want)
648 }
649 }
650
651 // https://github.com/emicklei/proto/issues/106
652 func TestEmptyArrayInOptionStructure(t *testing.T) {
653 src := `
654 option (grpc.gateway.protoc_gen_swagger.options.openapiv2_schema) = {
655 json_schema : {
656 title : "Frob a request"
657 description : "blah blah blah"
658 required : [ ]
659 optional:["this"]
660 }
661 };
662 `
663 p := newParserOn(src)
664 p.next()
665 o := new(Option)
666 if err := o.parse(p); err != nil {
667 t.Fatal("testcase parse failed:", err)
668 }
669 s, ok := o.Constant.OrderedMap.Get("json_schema")
670 if !ok {
671 t.Fatal("expected json_schema literal")
672 }
673 // none
674 a, ok := s.OrderedMap.Get("required")
675 if !ok {
676 t.Fatal("expected required literal")
677 }
678 if len(a.Array) != 0 {
679 t.Fatal("expecting empty array")
680 }
681 // one
682 a, ok = s.OrderedMap.Get("optional")
683 if !ok {
684 t.Fatal("expected required literal")
685 }
686 if len(a.Array) != 1 {
687 t.Fatal("expecting one size array")
688 }
689 if got, want := a.Array[0].Source, "this"; got != want {
690 t.Fatalf("got [%s] want [%s]", got, want)
691 }
692 }
693
694 // https://github.com/emicklei/proto/issues/107
695 func TestQuoteNotDroppedInOption(t *testing.T) {
696 src := `string name = 1 [ quote = '<="foo"' ];`
697 f := newNormalField()
698 if err := f.parse(newParserOn(src)); err != nil {
699 t.Fatal(err)
700 }
701 sr := f.Options[0].Constant.SourceRepresentation()
702 if got, want := sr, `'<="foo"'`; got != want {
703 t.Errorf("got [%s] want [%s]", got, want)
704 }
705 }
706
707 func TestWhatYouTypeIsWhatYouGetOptionValue(t *testing.T) {
708 src := `string n = 1 [ quote = 'm"\"/"' ];`
709 f := newNormalField()
710 if err := f.parse(newParserOn(src)); err != nil {
711 t.Fatal(err)
712 }
713 sr := f.Options[0].Constant.SourceRepresentation()
714 if got, want := sr, `'m"\"/"'`; got != want {
715 t.Errorf("got [%s] want [%s]", got, want)
716 }
717 }
718
719 func TestLiteralNoQuoteRuneSet(t *testing.T) {
720 l := Literal{
721 Source: "foo",
722 IsString: true,
723 }
724 if got, want := l.SourceRepresentation(), "\"foo\""; got != want {
725 t.Errorf("got [%s] want [%s]", got, want)
726 }
727 }
728
729 func TestStringValuesParsedAsNumbers(t *testing.T) {
730 src := `VAL0 = 0 [(enum_opt) = '09'];`
731 f := new(EnumField)
732 if err := f.parse(newParserOn(src)); err != nil {
733 t.Fatal(err)
734 }
735 if got, want := f.ValueOption.Constant.Source, "09"; got != want {
736 t.Errorf("got [%v:%T] want [%v:%T]", got, got, want, want)
737 }
738 }
739
740 func TestCommentInsideArray(t *testing.T) {
741 src := `option test = {
742 scope_rules : [
743 // A comment
744 // Another comment
745 {has : [
746 // comment on test
747 "test"
748 // ignored comment at the end
749 ]}
750 ]
751 };
752 `
753 opt, err := newParserOn(src).Parse()
754 if err != nil {
755 t.Fatal(err)
756 }
757 opt2 := opt.Elements[0].(*Option)
758 scp, _ := opt2.Constant.OrderedMap.Get("scope_rules")
759 elem0 := scp.Array[0]
760 t.Log("comment:", elem0.Comment.Lines)
761 has, _ := elem0.OrderedMap.Get("has")
762 if got, want := has.Array[0].Source, "test"; got != want {
763 t.Errorf("got [%v:%T] want [%v:%T]", got, got, want, want)
764 }
765 t.Log("comment:", has.Array[0].Comment.Lines)
766 }
767
768 func TestParseBracedFullIdent(t *testing.T) {
769 src := `features.(pb.go).api_level = API_OPAQUE;`
770 p := newParserOn(src)
771 o := new(Option)
772 o.parseOptionName(p)
773 if got, want := o.Name, "features.(pb.go).api_level"; got != want {
774 t.Errorf("got [%[1]v:%[1]T] want [%[2]v:%[2]T]", got, want)
775 }
776 }
777
778 func TestParseBracedFullIdentWithLeadingDot(t *testing.T) {
779 src := `some.(.like).it.(.hot) = API_OPAQUE;`
780 p := newParserOn(src)
781 o := new(Option)
782 o.parseOptionName(p)
783 if got, want := o.Name, "some.(.like).it.(.hot)"; got != want {
784 t.Errorf("got [%[1]v:%[1]T] want [%[2]v:%[2]T]", got, want)
785 }
786 }