Debian Codegraph @

repl/typefmt_test.go

raw symbols SCIP
1 // Copyright 2022 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package repl
16
17 import (
18 "testing"
19
20 "google.golang.org/protobuf/proto"
21
22 exprpb "google.golang.org/genproto/googleapis/api/expr/v1alpha1"
23 )
24
25 func TestUnparseType(t *testing.T) {
26 var testCases = []struct {
27 exprType *exprpb.Type
28 wantFmt string
29 }{
30 {
31 exprType: &exprpb.Type{},
32 wantFmt: "<unknown type>",
33 },
34 {
35 exprType: &exprpb.Type{TypeKind: &exprpb.Type_Dyn{}},
36 wantFmt: "dyn",
37 },
38 {
39 exprType: &exprpb.Type{TypeKind: &exprpb.Type_Null{}},
40 wantFmt: "null",
41 },
42 {
43 exprType: &exprpb.Type{TypeKind: &exprpb.Type_Primitive{Primitive: exprpb.Type_BOOL}},
44 wantFmt: "bool",
45 },
46 {
47 exprType: &exprpb.Type{TypeKind: &exprpb.Type_Primitive{Primitive: exprpb.Type_INT64}},
48 wantFmt: "int",
49 },
50 {
51 exprType: &exprpb.Type{TypeKind: &exprpb.Type_Primitive{Primitive: exprpb.Type_UINT64}},
52 wantFmt: "uint",
53 },
54 {
55 exprType: &exprpb.Type{TypeKind: &exprpb.Type_Primitive{Primitive: exprpb.Type_DOUBLE}},
56 wantFmt: "double",
57 },
58 {
59 exprType: &exprpb.Type{TypeKind: &exprpb.Type_Primitive{Primitive: exprpb.Type_STRING}},
60 wantFmt: "string",
61 },
62 {
63 exprType: &exprpb.Type{TypeKind: &exprpb.Type_Primitive{Primitive: exprpb.Type_BYTES}},
64 wantFmt: "bytes",
65 },
66 {
67 exprType: &exprpb.Type{TypeKind: &exprpb.Type_Primitive{Primitive: exprpb.Type_PRIMITIVE_TYPE_UNSPECIFIED}},
68 wantFmt: "primitive_type_unspecified",
69 },
70 {
71 exprType: &exprpb.Type{TypeKind: &exprpb.Type_WellKnown{WellKnown: exprpb.Type_DURATION}},
72 wantFmt: "duration",
73 },
74 {
75 exprType: &exprpb.Type{TypeKind: &exprpb.Type_WellKnown{WellKnown: exprpb.Type_TIMESTAMP}},
76 wantFmt: "timestamp",
77 },
78 {
79 exprType: &exprpb.Type{TypeKind: &exprpb.Type_WellKnown{WellKnown: exprpb.Type_ANY}},
80 wantFmt: "any",
81 },
82 {
83 exprType: &exprpb.Type{TypeKind: &exprpb.Type_WellKnown{WellKnown: exprpb.Type_WELL_KNOWN_TYPE_UNSPECIFIED}},
84 wantFmt: "well_known:WELL_KNOWN_TYPE_UNSPECIFIED",
85 },
86 {
87 exprType: &exprpb.Type{TypeKind: &exprpb.Type_MapType_{MapType: &exprpb.Type_MapType{
88 KeyType: &exprpb.Type{TypeKind: &exprpb.Type_Primitive{Primitive: exprpb.Type_STRING}},
89 ValueType: &exprpb.Type{TypeKind: &exprpb.Type_WellKnown{WellKnown: exprpb.Type_TIMESTAMP}},
90 }}},
91 wantFmt: "map(string, timestamp)",
92 },
93 {
94 exprType: &exprpb.Type{TypeKind: &exprpb.Type_ListType_{
95 ListType: &exprpb.Type_ListType{ElemType: &exprpb.Type{TypeKind: &exprpb.Type_Primitive{Primitive: exprpb.Type_DOUBLE}}}}},
96 wantFmt: "list(double)",
97 },
98 {
99 exprType: &exprpb.Type{TypeKind: &exprpb.Type_Type{
100 Type: &exprpb.Type{TypeKind: &exprpb.Type_Primitive{Primitive: exprpb.Type_DOUBLE}}}},
101 wantFmt: "type(double)",
102 },
103 {
104 exprType: &exprpb.Type{TypeKind: &exprpb.Type_Wrapper{
105 Wrapper: exprpb.Type_UINT64}},
106 wantFmt: "wrapper(uint)",
107 },
108 {
109 exprType: &exprpb.Type{TypeKind: &exprpb.Type_Error{}},
110 wantFmt: "!error!",
111 },
112 {
113 exprType: &exprpb.Type{TypeKind: &exprpb.Type_MessageType{
114 MessageType: "com.example.Message",
115 }},
116 wantFmt: "com.example.Message",
117 },
118 {
119 exprType: &exprpb.Type{TypeKind: &exprpb.Type_TypeParam{
120 TypeParam: "T",
121 }},
122 wantFmt: "T",
123 },
124 {
125 exprType: &exprpb.Type{TypeKind: &exprpb.Type_Function{
126 Function: &exprpb.Type_FunctionType{
127 ResultType: &exprpb.Type{TypeKind: &exprpb.Type_Primitive{Primitive: exprpb.Type_DOUBLE}},
128 ArgTypes: []*exprpb.Type{
129 {TypeKind: &exprpb.Type_Primitive{Primitive: exprpb.Type_DOUBLE}},
130 {TypeKind: &exprpb.Type_Primitive{Primitive: exprpb.Type_DOUBLE}},
131 },
132 },
133 }},
134 wantFmt: "(double, double) -> double",
135 },
136 {
137 exprType: &exprpb.Type{TypeKind: &exprpb.Type_AbstractType_{
138 AbstractType: &exprpb.Type_AbstractType{
139 Name: "MyAbstractParamType",
140 ParameterTypes: []*exprpb.Type{
141 {TypeKind: &exprpb.Type_Primitive{Primitive: exprpb.Type_DOUBLE}},
142 {TypeKind: &exprpb.Type_Primitive{Primitive: exprpb.Type_STRING}},
143 },
144 },
145 }},
146 wantFmt: "MyAbstractParamType(double, string)",
147 },
148 {
149 exprType: &exprpb.Type{TypeKind: &exprpb.Type_AbstractType_{
150 AbstractType: &exprpb.Type_AbstractType{
151 Name: "MyAbstractType",
152 },
153 }},
154 wantFmt: "MyAbstractType()",
155 },
156 {
157 exprType: &exprpb.Type{TypeKind: &exprpb.Type_MapType_{MapType: &exprpb.Type_MapType{
158 KeyType: &exprpb.Type{TypeKind: &exprpb.Type_Primitive{Primitive: exprpb.Type_STRING}},
159 ValueType: &exprpb.Type{TypeKind: &exprpb.Type_ListType_{
160 ListType: &exprpb.Type_ListType{ElemType: &exprpb.Type{TypeKind: &exprpb.Type_Primitive{Primitive: exprpb.Type_DOUBLE}}}}},
161 }}},
162 wantFmt: "map(string, list(double))",
163 },
164 }
165
166 for _, tc := range testCases {
167 fmt := UnparseType(tc.exprType)
168 if fmt != tc.wantFmt {
169 t.Errorf("expected: %s got: %s for type: %v", tc.wantFmt, fmt, tc.exprType)
170 }
171 }
172
173 }
174
175 func TestParseType(t *testing.T) {
176 var testCases = []struct {
177 fmt string
178 wantExprType *exprpb.Type
179 }{
180 {
181 fmt: "int",
182 wantExprType: &exprpb.Type{TypeKind: &exprpb.Type_Primitive{Primitive: exprpb.Type_INT64}},
183 },
184 {
185 fmt: "uint",
186 wantExprType: &exprpb.Type{TypeKind: &exprpb.Type_Primitive{Primitive: exprpb.Type_UINT64}},
187 },
188 {
189 fmt: "double",
190 wantExprType: &exprpb.Type{TypeKind: &exprpb.Type_Primitive{Primitive: exprpb.Type_DOUBLE}},
191 },
192 {
193 fmt: "string",
194 wantExprType: &exprpb.Type{TypeKind: &exprpb.Type_Primitive{Primitive: exprpb.Type_STRING}},
195 },
196 {
197 fmt: "bytes",
198 wantExprType: &exprpb.Type{TypeKind: &exprpb.Type_Primitive{Primitive: exprpb.Type_BYTES}},
199 },
200 {
201 fmt: "bool",
202 wantExprType: &exprpb.Type{TypeKind: &exprpb.Type_Primitive{Primitive: exprpb.Type_BOOL}},
203 },
204 {
205 fmt: "wrapper(int)",
206 wantExprType: &exprpb.Type{TypeKind: &exprpb.Type_Wrapper{Wrapper: exprpb.Type_INT64}},
207 },
208 {
209 fmt: "wrapper(uint)",
210 wantExprType: &exprpb.Type{TypeKind: &exprpb.Type_Wrapper{Wrapper: exprpb.Type_UINT64}},
211 },
212 {
213 fmt: "wrapper(double)",
214 wantExprType: &exprpb.Type{TypeKind: &exprpb.Type_Wrapper{Wrapper: exprpb.Type_DOUBLE}},
215 },
216 {
217 fmt: "wrapper(string)",
218 wantExprType: &exprpb.Type{TypeKind: &exprpb.Type_Wrapper{Wrapper: exprpb.Type_STRING}},
219 },
220 {
221 fmt: "wrapper(bytes)",
222 wantExprType: &exprpb.Type{TypeKind: &exprpb.Type_Wrapper{Wrapper: exprpb.Type_BYTES}},
223 },
224 {
225 fmt: "wrapper(bool)",
226 wantExprType: &exprpb.Type{TypeKind: &exprpb.Type_Wrapper{Wrapper: exprpb.Type_BOOL}},
227 },
228 {
229 fmt: "dyn",
230 wantExprType: &exprpb.Type{TypeKind: &exprpb.Type_Dyn{}},
231 },
232 {
233 fmt: "null",
234 wantExprType: &exprpb.Type{TypeKind: &exprpb.Type_Null{}},
235 },
236 {
237 fmt: "google.protobuf.Any",
238 wantExprType: &exprpb.Type{TypeKind: &exprpb.Type_WellKnown{WellKnown: exprpb.Type_ANY}},
239 },
240 {
241 fmt: "google.protobuf.Timestamp",
242 wantExprType: &exprpb.Type{TypeKind: &exprpb.Type_WellKnown{WellKnown: exprpb.Type_TIMESTAMP}},
243 },
244 {
245 fmt: "google.protobuf.Duration",
246 wantExprType: &exprpb.Type{TypeKind: &exprpb.Type_WellKnown{WellKnown: exprpb.Type_DURATION}},
247 },
248 {
249 fmt: "any",
250 wantExprType: &exprpb.Type{TypeKind: &exprpb.Type_WellKnown{WellKnown: exprpb.Type_ANY}},
251 },
252 {
253 fmt: "timestamp",
254 wantExprType: &exprpb.Type{TypeKind: &exprpb.Type_WellKnown{WellKnown: exprpb.Type_TIMESTAMP}},
255 },
256 {
257 fmt: "duration",
258 wantExprType: &exprpb.Type{TypeKind: &exprpb.Type_WellKnown{WellKnown: exprpb.Type_DURATION}},
259 },
260 {
261 fmt: "map(string, int)",
262 wantExprType: &exprpb.Type{TypeKind: &exprpb.Type_MapType_{
263 MapType: &exprpb.Type_MapType{
264 KeyType: &exprpb.Type{TypeKind: &exprpb.Type_Primitive{Primitive: exprpb.Type_STRING}},
265 ValueType: &exprpb.Type{TypeKind: &exprpb.Type_Primitive{Primitive: exprpb.Type_INT64}},
266 }}},
267 },
268 {
269 fmt: "map(string, map(string, int))",
270 wantExprType: &exprpb.Type{TypeKind: &exprpb.Type_MapType_{
271 MapType: &exprpb.Type_MapType{
272 KeyType: &exprpb.Type{TypeKind: &exprpb.Type_Primitive{Primitive: exprpb.Type_STRING}},
273 ValueType: &exprpb.Type{TypeKind: &exprpb.Type_MapType_{
274 MapType: &exprpb.Type_MapType{
275 KeyType: &exprpb.Type{TypeKind: &exprpb.Type_Primitive{Primitive: exprpb.Type_STRING}},
276 ValueType: &exprpb.Type{TypeKind: &exprpb.Type_Primitive{Primitive: exprpb.Type_INT64}},
277 }}},
278 }}},
279 },
280
281 {
282 fmt: "list(int)",
283 wantExprType: &exprpb.Type{TypeKind: &exprpb.Type_ListType_{
284 ListType: &exprpb.Type_ListType{ElemType: &exprpb.Type{TypeKind: &exprpb.Type_Primitive{Primitive: exprpb.Type_INT64}}}}},
285 },
286 {
287 fmt: "list(list(int))",
288 wantExprType: &exprpb.Type{TypeKind: &exprpb.Type_ListType_{
289 ListType: &exprpb.Type_ListType{ElemType: &exprpb.Type{TypeKind: &exprpb.Type_ListType_{
290 ListType: &exprpb.Type_ListType{ElemType: &exprpb.Type{TypeKind: &exprpb.Type_Primitive{Primitive: exprpb.Type_INT64}}}}}}}},
291 },
292 {
293 fmt: ".com.example.Message",
294 wantExprType: &exprpb.Type{TypeKind: &exprpb.Type_MessageType{MessageType: ".com.example.Message"}},
295 },
296 {
297 fmt: "type(int)",
298 wantExprType: &exprpb.Type{TypeKind: &exprpb.Type_Type{
299 Type: &exprpb.Type{TypeKind: &exprpb.Type_Primitive{Primitive: exprpb.Type_INT64}}}},
300 },
301 {
302 fmt: "type(type(wrapper(int)))",
303 wantExprType: &exprpb.Type{TypeKind: &exprpb.Type_Type{
304 Type: &exprpb.Type{TypeKind: &exprpb.Type_Type{
305 Type: &exprpb.Type{TypeKind: &exprpb.Type_Wrapper{Wrapper: exprpb.Type_INT64}}}}}},
306 },
307 {
308 fmt: "optional_type",
309 wantExprType: &exprpb.Type{TypeKind: &exprpb.Type_AbstractType_{
310 AbstractType: &exprpb.Type_AbstractType{
311 Name: "optional_type",
312 ParameterTypes: []*exprpb.Type{
313 {TypeKind: &exprpb.Type_Dyn{}},
314 }}}},
315 },
316 {
317 fmt: "optional_type(string)",
318 wantExprType: &exprpb.Type{TypeKind: &exprpb.Type_AbstractType_{
319 AbstractType: &exprpb.Type_AbstractType{
320 Name: "optional_type",
321 ParameterTypes: []*exprpb.Type{
322 {TypeKind: &exprpb.Type_Primitive{Primitive: exprpb.Type_STRING}},
323 }}}},
324 },
325 {
326 fmt: "MyAbstractType(string)",
327 wantExprType: &exprpb.Type{TypeKind: &exprpb.Type_AbstractType_{
328 AbstractType: &exprpb.Type_AbstractType{
329 Name: "MyAbstractType",
330 ParameterTypes: []*exprpb.Type{
331 {TypeKind: &exprpb.Type_Primitive{Primitive: exprpb.Type_STRING}},
332 }}}},
333 },
334 }
335
336 for _, tc := range testCases {
337 exprType, err := ParseType(tc.fmt)
338 if err != nil {
339 t.Fatalf("ParseType(%s) failed: %v", tc.fmt, err)
340 }
341 if !proto.Equal(exprType, tc.wantExprType) {
342 t.Errorf("ParseType(%s) got %s, wanted %s", tc.fmt, exprType, tc.wantExprType)
343 }
344 }
345 }
346
347 func TestParseTypeErrors(t *testing.T) {
348 var testCases = []struct {
349 fmt string
350 }{{
351 fmt: "list()",
352 },
353 {
354 fmt: "list(int",
355 },
356 {
357 fmt: "list",
358 },
359 {
360 fmt: "list(int, int)",
361 },
362 {
363 fmt: "wrapper(int, double)",
364 },
365 {
366 fmt: "wrapper(map(int, int))",
367 },
368 {
369 fmt: "in",
370 },
371 {
372 fmt: "x?",
373 },
374 {
375 fmt: "map(int)",
376 },
377 {
378 fmt: "map",
379 },
380 {
381 fmt: "map(string, )",
382 },
383 {
384 fmt: "map(string, int",
385 },
386 }
387
388 for _, tc := range testCases {
389 exprType, err := ParseType(tc.fmt)
390 if err == nil {
391 t.Errorf("ParseType(%s) got %s, wanted error", tc.fmt, exprType)
392 }
393 }
394
395 }