Debian Codegraph @

field_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 )
29
30 func TestField(t *testing.T) {
31 proto := `repeated foo.bar lots =1 [option1=a, option2=b, option3="happy"];`
32 p := newParserOn(proto)
33 f := newNormalField()
34 err := f.parse(p)
35 if err != nil {
36 t.Fatal(err)
37 }
38 if got, want := f.Repeated, true; got != want {
39 t.Errorf("got [%v] want [%v]", got, want)
40 }
41 if got, want := f.Type, "foo.bar"; got != want {
42 t.Errorf("got [%v] want [%v]", got, want)
43 }
44 if got, want := f.Name, "lots"; got != want {
45 t.Errorf("got [%v] want [%v]", got, want)
46 }
47 if got, want := len(f.Options), 3; got != want {
48 t.Fatalf("got [%v] want [%v]", got, want)
49 }
50 if got, want := f.Options[0].Name, "option1"; got != want {
51 t.Errorf("got [%v] want [%v]", got, want)
52 }
53 if got, want := f.Options[0].Constant.Source, "a"; got != want {
54 t.Errorf("got [%v] want [%v]", got, want)
55 }
56 if got, want := f.Options[1].Name, "option2"; got != want {
57 t.Errorf("got [%v] want [%v]", got, want)
58 }
59 if got, want := f.Options[1].Constant.Source, "b"; got != want {
60 t.Errorf("got [%v] want [%v]", got, want)
61 }
62 if got, want := f.Options[2].Constant.Source, "happy"; got != want {
63 t.Errorf("got [%v] want [%v]", got, want)
64 }
65 checkParent(f.Options[0], t)
66 }
67
68 func TestFieldNoWhitespace(t *testing.T) {
69 proto := `string s=1[a=b];`
70 p := newParserOn(proto)
71 f := newNormalField()
72 err := f.parse(p)
73 if err != nil {
74 t.Fatal(err)
75 }
76 if got, want := f.Type, "string"; got != want {
77 t.Errorf("got [%v] want [%v]", got, want)
78 }
79 if got, want := f.Sequence, 1; got != want {
80 t.Errorf("got [%v] want [%v]", got, want)
81 }
82 if got, want := f.Options[0].Name, "a"; got != want {
83 t.Errorf("got [%v] want [%v]", got, want)
84 }
85 if got, want := f.Options[0].Constant.Source, "b"; got != want {
86 t.Errorf("got [%v] want [%v]", got, want)
87 }
88 }
89
90 func TestFieldSimple(t *testing.T) {
91 proto := `string optional_string_piece = 24 [ctype=STRING_PIECE];`
92 p := newParserOn(proto)
93 f := newNormalField()
94 err := f.parse(p)
95 if err != nil {
96 t.Fatal(err)
97 }
98 if got, want := f.Type, "string"; got != want {
99 t.Errorf("got [%v] want [%v]", got, want)
100 }
101 if got, want := f.Name, "optional_string_piece"; got != want {
102 t.Errorf("got [%v] want [%v]", got, want)
103 }
104 if got, want := f.Sequence, 24; got != want {
105 t.Errorf("got [%v] want [%v]", got, want)
106 }
107 if got, want := len(f.Options), 1; got != want {
108 t.Fatalf("got [%v] want [%v]", got, want)
109 }
110 if got, want := f.Options[0].Name, "ctype"; got != want {
111 t.Errorf("got [%v] want [%v]", got, want)
112 }
113 if got, want := f.Options[0].Constant.Source, "STRING_PIECE"; got != want {
114 t.Errorf("got [%v] want [%v]", got, want)
115 }
116 }
117
118 func TestFieldSyntaxErrors(t *testing.T) {
119 for i, each := range []string{
120 `repeatet foo.bar lots = 1;`,
121 `string lots === 1;`,
122 } {
123 f := newNormalField()
124 if f.parse(newParserOn(each)) == nil {
125 t.Errorf("uncaught syntax error in test case %d, %#v", i, f)
126 }
127 }
128 }
129
130 func TestMapField(t *testing.T) {
131 proto := ` <string, Project> projects = 3 [foo=bar];`
132 p := newParserOn(proto)
133 f := newMapField()
134 err := f.parse(p)
135 if err != nil {
136 t.Fatal(err)
137 }
138 if got, want := f.KeyType, "string"; got != want {
139 t.Errorf("got [%v] want [%v]", got, want)
140 }
141 if got, want := f.Type, "Project"; got != want {
142 t.Errorf("got [%v] want [%v]", got, want)
143 }
144 if got, want := f.Name, "projects"; got != want {
145 t.Errorf("got [%v] want [%v]", got, want)
146 }
147 if got, want := f.Sequence, 3; got != want {
148 t.Errorf("got [%v] want [%v]", got, want)
149 }
150 checkParent(f.Options[0], t)
151 }
152
153 func TestMapFieldWithDotTypes(t *testing.T) {
154 proto := ` <.Some.How, .Such.Project> projects = 3;`
155 p := newParserOn(proto)
156 f := newMapField()
157 err := f.parse(p)
158 if err != nil {
159 t.Fatal(err)
160 }
161 if got, want := f.KeyType, ".Some.How"; got != want {
162 t.Errorf("got [%v] want [%v]", got, want)
163 }
164 if got, want := f.Type, ".Such.Project"; got != want {
165 t.Errorf("got [%v] want [%v]", got, want)
166 }
167 }
168
169 func TestOptionalWithOption(t *testing.T) {
170 proto := `optional int32 default_int32 = 61 [default = 41 ];`
171 p := newParserOn(proto)
172 f := newNormalField()
173 err := f.parse(p)
174 if err != nil {
175 t.Fatal(err)
176 }
177 if got, want := f.Sequence, 61; got != want {
178 t.Errorf("got [%v] want [%v]", got, want)
179 }
180 o := f.Options[0]
181 if got, want := o.Name, "default"; got != want {
182 t.Errorf("got [%v] want [%v]", got, want)
183 }
184 if got, want := o.Constant.Source, "41"; got != want {
185 t.Errorf("got [%v] want [%v]", got, want)
186 }
187 }
188
189 func TestFieldInlineComment(t *testing.T) {
190 proto := `message Hello {
191 // comment
192 bool foo = 1; // inline comment
193 }`
194 p := newParserOn(proto)
195 def := new(Proto)
196 err := def.parse(p)
197 if err != nil {
198 t.Fatal(err)
199 }
200 m := def.Elements[0].(*Message)
201 if len(m.Elements) != 1 {
202 t.Error("expected one element", m.Elements)
203 }
204 f := m.Elements[0].(*NormalField)
205 if f.InlineComment == nil {
206 t.Error("expected inline comment")
207 }
208 }
209
210 func TestFieldTypeStartsWithDot(t *testing.T) {
211 proto := `.game.Resource foo = 1;`
212 p := newParserOn(proto)
213 f := newNormalField()
214 err := f.parse(p)
215 if err != nil {
216 t.Fatal(err)
217 }
218 foot := f.Field.Type
219 if got, want := foot, ".game.Resource"; got != want {
220 t.Errorf("got [%v] want [%v]", got, want)
221 }
222 }
223
224 func TestMultiLineFieldType(t *testing.T) {
225 src := `google.ads.googleads.v1.enums.ConversionAdjustmentTypeEnum
226 .ConversionAdjustmentType adjustment_type = 5;`
227 p := newParserOn(src)
228 f := newNormalField()
229 err := f.parse(p)
230 if err != nil {
231 t.Fatal(err)
232 }
233 }