Debian Codegraph @

checker/checker_test.go

raw symbols SCIP
1 // Copyright 2018 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 // http://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 checker
16
17 import (
18 "fmt"
19 "strings"
20 "testing"
21 "time"
22
23 "github.com/google/cel-go/common"
24 "github.com/google/cel-go/common/ast"
25 "github.com/google/cel-go/common/containers"
26 "github.com/google/cel-go/common/debug"
27 "github.com/google/cel-go/common/decls"
28 "github.com/google/cel-go/common/stdlib"
29 "github.com/google/cel-go/common/types"
30 "github.com/google/cel-go/parser"
31 "github.com/google/cel-go/test"
32
33 proto2pb "github.com/google/cel-go/test/proto2pb"
34 proto3pb "github.com/google/cel-go/test/proto3pb"
35 )
36
37 func testCases(t testing.TB) []testInfo {
38 t.Helper()
39 return []testInfo{
40 // Const types
41 {
42 in: `"A"`,
43 out: `"A"~string`,
44 outType: types.StringType,
45 },
46 {
47 in: `12`,
48 out: `12~int`,
49 outType: types.IntType,
50 },
51 {
52 in: `12u`,
53 out: `12u~uint`,
54 outType: types.UintType,
55 },
56 {
57 in: `true`,
58 out: `true~bool`,
59 outType: types.BoolType,
60 },
61 {
62 in: `false`,
63 out: `false~bool`,
64 outType: types.BoolType,
65 },
66 {
67 in: `12.23`,
68 out: `12.23~double`,
69 outType: types.DoubleType,
70 },
71 {
72 in: `null`,
73 out: `null~null`,
74 outType: types.NullType,
75 },
76 {
77 in: `b"ABC"`,
78 out: `b"ABC"~bytes`,
79 outType: types.BytesType,
80 },
81 // Ident types
82 {
83 in: `is`,
84 out: `is~string^is`,
85 outType: types.StringType,
86 env: testEnvs(t)["default"],
87 },
88 {
89 in: `ii`,
90 out: `ii~int^ii`,
91 outType: types.IntType,
92 env: testEnvs(t)["default"],
93 },
94 {
95 in: `iu`,
96 out: `iu~uint^iu`,
97 outType: types.UintType,
98 env: testEnvs(t)["default"],
99 },
100 {
101 in: `iz`,
102 out: `iz~bool^iz`,
103 outType: types.BoolType,
104 env: testEnvs(t)["default"],
105 },
106 {
107 in: `id`,
108 out: `id~double^id`,
109 outType: types.DoubleType,
110 env: testEnvs(t)["default"],
111 },
112 {
113 in: `ix`,
114 out: `ix~null^ix`,
115 outType: types.NullType,
116 env: testEnvs(t)["default"],
117 },
118 {
119 in: `ib`,
120 out: `ib~bytes^ib`,
121 outType: types.BytesType,
122 env: testEnvs(t)["default"],
123 },
124 {
125 in: `id`,
126 out: `id~double^id`,
127 outType: types.DoubleType,
128 env: testEnvs(t)["default"],
129 },
130 {
131 in: `[]`,
132 out: `[]~list(dyn)`,
133 outType: types.NewListType(types.DynType),
134 },
135 {
136 in: `[1]`,
137 out: `[1~int]~list(int)`,
138 outType: types.NewListType(types.IntType),
139 },
140 {
141 in: `[1, "A"]`,
142 out: `[1~int, "A"~string]~list(dyn)`,
143 outType: types.NewListType(types.DynType),
144 },
145 {
146 in: `foo`,
147 out: `foo~!error!`,
148 outType: types.ErrorType,
149 err: `
150 ERROR: <input>:1:1: undeclared reference to 'foo' (in container '')
151 | foo
152 | ^`,
153 },
154 // Call resolution
155 {
156 in: `fg_s()`,
157 out: `fg_s()~string^fg_s_0`,
158 outType: types.StringType,
159 env: testEnvs(t)["default"],
160 },
161 {
162 in: `is.fi_s_s()`,
163 out: `is~string^is.fi_s_s()~string^fi_s_s_0`,
164 outType: types.StringType,
165 env: testEnvs(t)["default"],
166 },
167 {
168 in: `1 + 2`,
169 out: `_+_(1~int, 2~int)~int^add_int64`,
170 outType: types.IntType,
171 env: testEnvs(t)["default"],
172 },
173 {
174 in: `1 + ii`,
175 out: `_+_(1~int, ii~int^ii)~int^add_int64`,
176 outType: types.IntType,
177 env: testEnvs(t)["default"],
178 },
179 {
180 in: `[1] + [2]`,
181 out: `_+_([1~int]~list(int), [2~int]~list(int))~list(int)^add_list`,
182 outType: types.NewListType(types.IntType),
183 env: testEnvs(t)["default"],
184 },
185 {
186 in: `[] + [1,2,3,] + [4]`,
187 outType: types.NewListType(types.IntType),
188 out: `
189 _+_(
190 _+_(
191 []~list(int),
192 [1~int, 2~int, 3~int]~list(int))~list(int)^add_list,
193 [4~int]~list(int))
194 ~list(int)^add_list
195 `,
196 },
197 {
198 in: `[1, 2u] + []`,
199 out: `_+_(
200 [
201 1~int,
202 2u~uint
203 ]~list(dyn),
204 []~list(dyn)
205 )~list(dyn)^add_list`,
206 outType: types.NewListType(types.DynType),
207 },
208 {
209 in: `{1:2u, 2:3u}`,
210 outType: types.NewMapType(types.IntType, types.UintType),
211 out: `{1~int : 2u~uint, 2~int : 3u~uint}~map(int, uint)`,
212 },
213 {
214 in: `{"a":1, "b":2}.a`,
215 outType: types.IntType,
216 out: `{"a"~string : 1~int, "b"~string : 2~int}~map(string, int).a~int`,
217 },
218 {
219 in: `{1:2u, 2u:3}`,
220 outType: types.NewMapType(types.DynType, types.DynType),
221 out: `{1~int : 2u~uint, 2u~uint : 3~int}~map(dyn, dyn)`,
222 },
223 {
224 in: `TestAllTypes{single_int32: 1, single_int64: 2}`,
225 container: "google.expr.proto3.test",
226 out: `
227 google.expr.proto3.test.TestAllTypes{
228 single_int32 : 1~int,
229 single_int64 : 2~int
230 }~google.expr.proto3.test.TestAllTypes^google.expr.proto3.test.TestAllTypes`,
231 outType: types.NewObjectType("google.expr.proto3.test.TestAllTypes"),
232 },
233 {
234 in: `TestAllTypes{single_int32: 1u}`,
235 container: "google.expr.proto3.test",
236 err: `
237 ERROR: <input>:1:26: expected type of field 'single_int32' is 'int' but provided type is 'uint'
238 | TestAllTypes{single_int32: 1u}
239 | .........................^`,
240 },
241 {
242 in: `TestAllTypes{single_int32: 1, undefined: 2}`,
243 container: "google.expr.proto3.test",
244 err: `
245 ERROR: <input>:1:40: undefined field 'undefined'
246 | TestAllTypes{single_int32: 1, undefined: 2}
247 | .......................................^`,
248 },
249 {
250 in: `size(x) == x.size()`,
251 out: `
252 _==_(size(x~list(int)^x)~int^size_list, x~list(int)^x.size()~int^list_size)
253 ~bool^equals`,
254 env: testEnv{
255 idents: []*decls.VariableDecl{
256 decls.NewVariable("x", types.NewListType(types.IntType)),
257 },
258 },
259 outType: types.BoolType,
260 },
261 {
262 in: `int(1u) + int(uint("1"))`,
263 out: `
264 _+_(int(1u~uint)~int^uint64_to_int64,
265 int(uint("1"~string)~uint^string_to_uint64)~int^uint64_to_int64)
266 ~int^add_int64`,
267 outType: types.IntType,
268 },
269 {
270 in: `false && !true || false ? 2 : 3`,
271 out: `
272 _?_:_(_||_(_&&_(false~bool, !_(true~bool)~bool^logical_not)~bool^logical_and,
273 false~bool)
274 ~bool^logical_or,
275 2~int,
276 3~int)
277 ~int^conditional
278 `,
279 outType: types.IntType,
280 },
281 {
282 in: `b"abc" + b"def"`,
283 out: `_+_(b"abc"~bytes, b"def"~bytes)~bytes^add_bytes`,
284 outType: types.BytesType,
285 },
286 {
287 in: `1.0 + 2.0 * 3.0 - 1.0 / 2.20202 != 66.6`,
288 out: `
289 _!=_(_-_(_+_(1~double, _*_(2~double, 3~double)~double^multiply_double)
290 ~double^add_double,
291 _/_(1~double, 2.20202~double)~double^divide_double)
292 ~double^subtract_double,
293 66.6~double)
294 ~bool^not_equals`,
295 outType: types.BoolType,
296 },
297 {
298 in: `null == null && null != null`,
299 out: `
300 _&&_(
301 _==_(
302 null~null,
303 null~null
304 )~bool^equals,
305 _!=_(
306 null~null,
307 null~null
308 )~bool^not_equals
309 )~bool^logical_and`,
310 outType: types.BoolType,
311 },
312 {
313 in: `1 == 1 && 2 != 1`,
314 out: `
315 _&&_(
316 _==_(
317 1~int,
318 1~int
319 )~bool^equals,
320 _!=_(
321 2~int,
322 1~int
323 )~bool^not_equals
324 )~bool^logical_and`,
325 outType: types.BoolType,
326 },
327 {
328 in: `1 + 2 * 3 - 1 / 2 == 6 % 1`,
329 out: ` _==_(_-_(_+_(1~int, _*_(2~int, 3~int)~int^multiply_int64)~int^add_int64, _/_(1~int, 2~int)~int^divide_int64)~int^subtract_int64, _%_(6~int, 1~int)~int^modulo_int64)~bool^equals`,
330 outType: types.BoolType,
331 },
332 {
333 in: `"abc" + "def"`,
334 out: `_+_("abc"~string, "def"~string)~string^add_string`,
335 outType: types.StringType,
336 },
337 {
338 in: `1u + 2u * 3u - 1u / 2u == 6u % 1u`,
339 out: `_==_(_-_(_+_(1u~uint, _*_(2u~uint, 3u~uint)~uint^multiply_uint64)
340 ~uint^add_uint64,
341 _/_(1u~uint, 2u~uint)~uint^divide_uint64)
342 ~uint^subtract_uint64,
343 _%_(6u~uint, 1u~uint)~uint^modulo_uint64)
344 ~bool^equals`,
345 outType: types.BoolType,
346 },
347 {
348 in: `x.single_int32 != null`,
349 env: testEnv{
350 idents: []*decls.VariableDecl{
351 decls.NewVariable("x", types.NewObjectType("google.expr.proto3.test.Proto2Message")),
352 },
353 },
354 err: `
355 ERROR: <input>:1:2: unexpected failed resolution of 'google.expr.proto3.test.Proto2Message'
356 | x.single_int32 != null
357 | .^
358 `,
359 },
360 {
361 in: `x.single_value + 1 / x.single_struct.y == 23`,
362 env: testEnv{
363 idents: []*decls.VariableDecl{
364 decls.NewVariable("x", types.NewObjectType("google.expr.proto3.test.TestAllTypes")),
365 },
366 },
367 out: `_==_(
368 _+_(
369 x~google.expr.proto3.test.TestAllTypes^x.single_value~dyn,
370 _/_(
371 1~int,
372 x~google.expr.proto3.test.TestAllTypes^x.single_struct~map(string, dyn).y~dyn
373 )~int^divide_int64
374 )~int^add_int64,
375 23~int
376 )~bool^equals`,
377 outType: types.BoolType,
378 },
379 {
380 in: `x.single_value[23] + x.single_struct['y']`,
381 env: testEnv{
382 idents: []*decls.VariableDecl{
383 decls.NewVariable("x", types.NewObjectType("google.expr.proto3.test.TestAllTypes")),
384 },
385 },
386 out: `_+_(
387 _[_](
388 x~google.expr.proto3.test.TestAllTypes^x.single_value~dyn,
389 23~int
390 )~dyn^index_list|index_map,
391 _[_](
392 x~google.expr.proto3.test.TestAllTypes^x.single_struct~map(string, dyn),
393 "y"~string
394 )~dyn^index_map
395 )~dyn^add_bytes|add_double|add_duration_duration|add_duration_timestamp|add_int64|add_list|add_string|add_timestamp_duration|add_uint64
396 `,
397 outType: types.DynType,
398 },
399 {
400 in: `TestAllTypes.NestedEnum.BAR != 99`,
401 container: "google.expr.proto3.test",
402 out: `_!=_(google.expr.proto3.test.TestAllTypes.NestedEnum.BAR
403 ~int^google.expr.proto3.test.TestAllTypes.NestedEnum.BAR,
404 99~int)
405 ~bool^not_equals`,
406 outType: types.BoolType,
407 },
408 {
409 in: `size([] + [1])`,
410 out: `size(_+_([]~list(int), [1~int]~list(int))~list(int)^add_list)~int^size_list`,
411 outType: types.IntType,
412 env: testEnv{
413 idents: []*decls.VariableDecl{
414 decls.NewVariable("x", types.NewObjectType("google.expr.proto3.test.TestAllTypes")),
415 },
416 },
417 },
418 {
419 in: `x["claims"]["groups"][0].name == "dummy"
420 && x.claims["exp"] == y[1].time
421 && x.claims.structured == {'key': z}
422 && z == 1.0`,
423 out: `_&&_(
424 _&&_(
425 _==_(
426 _[_](
427 _[_](
428 _[_](
429 x~map(string, dyn)^x,
430 "claims"~string
431 )~dyn^index_map,
432 "groups"~string
433 )~list(dyn)^index_map,
434 0~int
435 )~dyn^index_list.name~dyn,
436 "dummy"~string
437 )~bool^equals,
438 _==_(
439 _[_](
440 x~map(string, dyn)^x.claims~dyn,
441 "exp"~string
442 )~dyn^index_map,
443 _[_](
444 y~list(dyn)^y,
445 1~int
446 )~dyn^index_list.time~dyn
447 )~bool^equals
448 )~bool^logical_and,
449 _&&_(
450 _==_(
451 x~map(string, dyn)^x.claims~dyn.structured~dyn,
452 {
453 "key"~string:z~dyn^z
454 }~map(string, dyn)
455 )~bool^equals,
456 _==_(
457 z~dyn^z,
458 1~double
459 )~bool^equals
460 )~bool^logical_and
461 )~bool^logical_and`,
462 env: testEnv{
463 idents: []*decls.VariableDecl{
464 decls.NewVariable("x", types.NewObjectType("google.protobuf.Struct")),
465 decls.NewVariable("y", types.NewObjectType("google.protobuf.ListValue")),
466 decls.NewVariable("z", types.NewObjectType("google.protobuf.Value")),
467 },
468 },
469 outType: types.BoolType,
470 },
471 {
472 in: `x + y`,
473 out: ``,
474 env: testEnv{
475 idents: []*decls.VariableDecl{
476 decls.NewVariable("x", types.NewListType(types.NewObjectType("google.expr.proto3.test.TestAllTypes"))),
477 decls.NewVariable("y", types.NewListType(types.IntType)),
478 },
479 },
480 err: `
481 ERROR: <input>:1:3: found no matching overload for '_+_' applied to '(list(google.expr.proto3.test.TestAllTypes), list(int))'
482 | x + y
483 | ..^
484 `,
485 },
486 {
487 in: `x[1u]`,
488 env: testEnv{
489 idents: []*decls.VariableDecl{
490 decls.NewVariable("x", types.NewListType(types.NewObjectType("google.expr.proto3.test.TestAllTypes"))),
491 },
492 },
493 err: `
494 ERROR: <input>:1:2: found no matching overload for '_[_]' applied to '(list(google.expr.proto3.test.TestAllTypes), uint)'
495 | x[1u]
496 | .^
497 `,
498 },
499 {
500 in: `(x + x)[1].single_int32 == size(x)`,
501 env: testEnv{
502 idents: []*decls.VariableDecl{
503 decls.NewVariable("x", types.NewListType(types.NewObjectType("google.expr.proto3.test.TestAllTypes"))),
504 },
505 },
506 out: `
507 _==_(_[_](_+_(x~list(google.expr.proto3.test.TestAllTypes)^x,
508 x~list(google.expr.proto3.test.TestAllTypes)^x)
509 ~list(google.expr.proto3.test.TestAllTypes)^add_list,
510 1~int)
511 ~google.expr.proto3.test.TestAllTypes^index_list
512 .
513 single_int32
514 ~int,
515 size(x~list(google.expr.proto3.test.TestAllTypes)^x)~int^size_list)
516 ~bool^equals
517 `,
518 outType: types.BoolType,
519 },
520 {
521 in: `x.repeated_int64[x.single_int32] == 23`,
522 env: testEnv{
523 idents: []*decls.VariableDecl{
524 decls.NewVariable("x", types.NewObjectType("google.expr.proto3.test.TestAllTypes")),
525 },
526 },
527 out: `
528 _==_(_[_](x~google.expr.proto3.test.TestAllTypes^x.repeated_int64~list(int),
529 x~google.expr.proto3.test.TestAllTypes^x.single_int32~int)
530 ~int^index_list,
531 23~int)
532 ~bool^equals`,
533 outType: types.BoolType,
534 },
535 {
536 in: `size(x.map_int64_nested_type) == 0`,
537 env: testEnv{
538 idents: []*decls.VariableDecl{
539 decls.NewVariable("x", types.NewObjectType("google.expr.proto3.test.TestAllTypes")),
540 },
541 },
542 out: `
543 _==_(size(x~google.expr.proto3.test.TestAllTypes^x.map_int64_nested_type
544 ~map(int, google.expr.proto3.test.NestedTestAllTypes))
545 ~int^size_map,
546 0~int)
547 ~bool^equals
548 `,
549 outType: types.BoolType,
550 },
551 {
552 in: `x.all(y, y == true)`,
553 env: testEnv{
554 idents: []*decls.VariableDecl{
555 decls.NewVariable("x", types.BoolType),
556 },
557 },
558 out: `
559 __comprehension__(
560 // Variable
561 y,
562 // Target
563 x~bool^x,
564 // Accumulator
565 @result,
566 // Init
567 true~bool,
568 // LoopCondition
569 @not_strictly_false(
570 @result~bool^@result
571 )~bool^not_strictly_false,
572 // LoopStep
573 _&&_(
574 @result~bool^@result,
575 _==_(
576 y~!error!^y,
577 true~bool
578 )~bool^equals
579 )~bool^logical_and,
580 // Result
581 @result~bool^@result)~bool
582 `,
583 err: `ERROR: <input>:1:1: expression of type 'bool' cannot be range of a comprehension (must be list, map, or dynamic)
584 | x.all(y, y == true)
585 | ^`,
586 },
587 {
588 in: `x.repeated_int64.map(x, double(x))`,
589 env: testEnv{
590 idents: []*decls.VariableDecl{
591 decls.NewVariable("x", types.NewObjectType("google.expr.proto3.test.TestAllTypes")),
592 },
593 },
594 out: `
595 __comprehension__(
596 // Variable
597 x,
598 // Target
599 x~google.expr.proto3.test.TestAllTypes^x.repeated_int64~list(int),
600 // Accumulator
601 @result,
602 // Init
603 []~list(double),
604 // LoopCondition
605 true~bool,
606 // LoopStep
607 _+_(
608 @result~list(double)^@result,
609 [
610 double(
611 x~int^x
612 )~double^int64_to_double
613 ]~list(double)
614 )~list(double)^add_list,
615 // Result
616 @result~list(double)^@result)~list(double)
617 `,
618 outType: types.NewListType(types.DoubleType),
619 },
620 {
621 in: `x.repeated_int64.map(x, x > 0, double(x))`,
622 env: testEnv{
623 idents: []*decls.VariableDecl{
624 decls.NewVariable("x", types.NewObjectType("google.expr.proto3.test.TestAllTypes")),
625 },
626 },
627 out: `
628 __comprehension__(
629 // Variable
630 x,
631 // Target
632 x~google.expr.proto3.test.TestAllTypes^x.repeated_int64~list(int),
633 // Accumulator
634 @result,
635 // Init
636 []~list(double),
637 // LoopCondition
638 true~bool,
639 // LoopStep
640 _?_:_(
641 _>_(
642 x~int^x,
643 0~int
644 )~bool^greater_int64,
645 _+_(
646 @result~list(double)^@result,
647 [
648 double(
649 x~int^x
650 )~double^int64_to_double
651 ]~list(double)
652 )~list(double)^add_list,
653 @result~list(double)^@result
654 )~list(double)^conditional,
655 // Result
656 @result~list(double)^@result)~list(double)
657 `,
658 outType: types.NewListType(types.DoubleType),
659 },
660 {
661 in: `x[2].single_int32 == 23`,
662 env: testEnv{
663 idents: []*decls.VariableDecl{
664 decls.NewVariable("x",
665 types.NewMapType(types.StringType,
666 types.NewObjectType("google.expr.proto3.test.TestAllTypes"))),
667 },
668 },
669 err: `
670 ERROR: <input>:1:2: found no matching overload for '_[_]' applied to '(map(string, google.expr.proto3.test.TestAllTypes), int)'
671 | x[2].single_int32 == 23
672 | .^
673 `,
674 },
675 {
676 in: `x["a"].single_int32 == 23`,
677 env: testEnv{
678 idents: []*decls.VariableDecl{
679 decls.NewVariable("x",
680 types.NewMapType(types.StringType,
681 types.NewObjectType("google.expr.proto3.test.TestAllTypes"))),
682 },
683 },
684 out: `
685 _==_(_[_](x~map(string, google.expr.proto3.test.TestAllTypes)^x, "a"~string)
686 ~google.expr.proto3.test.TestAllTypes^index_map
687 .
688 single_int32
689 ~int,
690 23~int)
691 ~bool^equals`,
692 outType: types.BoolType,
693 },
694 {
695 in: `x.single_nested_message.bb == 43 && has(x.single_nested_message)`,
696 env: testEnv{
697 idents: []*decls.VariableDecl{
698 decls.NewVariable("x", types.NewObjectType("google.expr.proto3.test.TestAllTypes")),
699 },
700 },
701
702 // Our implementation code is expanding the macro
703 out: `_&&_(
704 _==_(
705 x~google.expr.proto3.test.TestAllTypes^x.single_nested_message~google.expr.proto3.test.TestAllTypes.NestedMessage.bb~int,
706 43~int
707 )~bool^equals,
708 x~google.expr.proto3.test.TestAllTypes^x.single_nested_message~test-only~~bool
709 )~bool^logical_and`,
710 outType: types.BoolType,
711 },
712 {
713 in: `x.single_nested_message.undefined == x.undefined && has(x.single_int32) && has(x.repeated_int32)`,
714 env: testEnv{
715 idents: []*decls.VariableDecl{
716 decls.NewVariable("x", types.NewObjectType("google.expr.proto3.test.TestAllTypes")),
717 },
718 },
719 err: `
720 ERROR: <input>:1:24: undefined field 'undefined'
721 | x.single_nested_message.undefined == x.undefined && has(x.single_int32) && has(x.repeated_int32)
722 | .......................^
723 ERROR: <input>:1:39: undefined field 'undefined'
724 | x.single_nested_message.undefined == x.undefined && has(x.single_int32) && has(x.repeated_int32)
725 | ......................................^`,
726 },
727 {
728 in: `x.single_nested_message != null`,
729 env: testEnv{
730 idents: []*decls.VariableDecl{
731 decls.NewVariable("x", types.NewObjectType("google.expr.proto3.test.TestAllTypes")),
732 },
733 },
734 out: `
735 _!=_(x~google.expr.proto3.test.TestAllTypes^x.single_nested_message
736 ~google.expr.proto3.test.TestAllTypes.NestedMessage,
737 null~null)
738 ~bool^not_equals
739 `,
740 outType: types.BoolType,
741 },
742 {
743 in: `x.single_int64 != null`,
744 env: testEnv{
745 idents: []*decls.VariableDecl{
746 decls.NewVariable("x", types.NewObjectType("google.expr.proto3.test.TestAllTypes")),
747 },
748 },
749 err: `
750 ERROR: <input>:1:16: found no matching overload for '_!=_' applied to '(int, null)'
751 | x.single_int64 != null
752 | ...............^
753 `,
754 },
755 {
756 in: `x.single_int64_wrapper == null`,
757 env: testEnv{
758 idents: []*decls.VariableDecl{
759 decls.NewVariable("x", types.NewObjectType("google.expr.proto3.test.TestAllTypes")),
760 },
761 },
762 out: `
763 _==_(x~google.expr.proto3.test.TestAllTypes^x.single_int64_wrapper
764 ~wrapper(int),
765 null~null)
766 ~bool^equals
767 `,
768 outType: types.BoolType,
769 },
770 {
771 in: `x.single_bool_wrapper
772 && x.single_bytes_wrapper == b'hi'
773 && x.single_double_wrapper != 2.0
774 && x.single_float_wrapper == 1.0
775 && x.single_int32_wrapper != 2
776 && x.single_int64_wrapper == 1
777 && x.single_string_wrapper == 'hi'
778 && x.single_uint32_wrapper == 1u
779 && x.single_uint64_wrapper != 42u`,
780 env: testEnv{
781 idents: []*decls.VariableDecl{
782 decls.NewVariable("x", types.NewObjectType("google.expr.proto3.test.TestAllTypes")),
783 },
784 },
785 out: `
786 _&&_(
787 _&&_(
788 _&&_(
789 _&&_(
790 x~google.expr.proto3.test.TestAllTypes^x.single_bool_wrapper~wrapper(bool),
791 _==_(
792 x~google.expr.proto3.test.TestAllTypes^x.single_bytes_wrapper~wrapper(bytes),
793 b"hi"~bytes
794 )~bool^equals
795 )~bool^logical_and,
796 _!=_(
797 x~google.expr.proto3.test.TestAllTypes^x.single_double_wrapper~wrapper(double),
798 2~double
799 )~bool^not_equals
800 )~bool^logical_and,
801 _&&_(
802 _==_(
803 x~google.expr.proto3.test.TestAllTypes^x.single_float_wrapper~wrapper(double),
804 1~double
805 )~bool^equals,
806 _!=_(
807 x~google.expr.proto3.test.TestAllTypes^x.single_int32_wrapper~wrapper(int),
808 2~int
809 )~bool^not_equals
810 )~bool^logical_and
811 )~bool^logical_and,
812 _&&_(
813 _&&_(
814 _==_(
815 x~google.expr.proto3.test.TestAllTypes^x.single_int64_wrapper~wrapper(int),
816 1~int
817 )~bool^equals,
818 _==_(
819 x~google.expr.proto3.test.TestAllTypes^x.single_string_wrapper~wrapper(string),
820 "hi"~string
821 )~bool^equals
822 )~bool^logical_and,
823 _&&_(
824 _==_(
825 x~google.expr.proto3.test.TestAllTypes^x.single_uint32_wrapper~wrapper(uint),
826 1u~uint
827 )~bool^equals,
828 _!=_(
829 x~google.expr.proto3.test.TestAllTypes^x.single_uint64_wrapper~wrapper(uint),
830 42u~uint
831 )~bool^not_equals
832 )~bool^logical_and
833 )~bool^logical_and
834 )~bool^logical_and`,
835 outType: types.BoolType,
836 },
837 {
838 in: `x.single_timestamp == google.protobuf.Timestamp{seconds: 20} &&
839 x.single_duration < google.protobuf.Duration{seconds: 10}`,
840 env: testEnv{
841 idents: []*decls.VariableDecl{
842 decls.NewVariable("x", types.NewObjectType("google.expr.proto3.test.TestAllTypes")),
843 },
844 },
845 outType: types.BoolType,
846 },
847 {
848 in: `x.single_bool_wrapper == google.protobuf.BoolValue{value: true}
849 && x.single_bytes_wrapper == google.protobuf.BytesValue{value: b'hi'}
850 && x.single_double_wrapper != google.protobuf.DoubleValue{value: 2.0}
851 && x.single_float_wrapper == google.protobuf.FloatValue{value: 1.0}
852 && x.single_int32_wrapper != google.protobuf.Int32Value{value: -2}
853 && x.single_int64_wrapper == google.protobuf.Int64Value{value: 1}
854 && x.single_string_wrapper == google.protobuf.StringValue{value: 'hi'}
855 && x.single_string_wrapper == google.protobuf.Value{string_value: 'hi'}
856 && x.single_uint32_wrapper == google.protobuf.UInt32Value{value: 1u}
857 && x.single_uint64_wrapper != google.protobuf.UInt64Value{value: 42u}`,
858 env: testEnv{
859 idents: []*decls.VariableDecl{
860 decls.NewVariable("x", types.NewObjectType("google.expr.proto3.test.TestAllTypes")),
861 },
862 },
863 outType: types.BoolType,
864 },
865 {
866 in: `x.repeated_int64.exists(y, y > 10) && y < 5`,
867 env: testEnv{
868 idents: []*decls.VariableDecl{
869 decls.NewVariable("x", types.NewObjectType("google.expr.proto3.test.TestAllTypes")),
870 },
871 },
872 err: `ERROR: <input>:1:39: undeclared reference to 'y' (in container '')
873 | x.repeated_int64.exists(y, y > 10) && y < 5
874 | ......................................^`,
875 },
876 {
877 in: `x.repeated_int64.all(e, e > 0) && x.repeated_int64.exists(e, e < 0) && x.repeated_int64.exists_one(e, e == 0)`,
878 env: testEnv{
879 idents: []*decls.VariableDecl{
880 decls.NewVariable("x", types.NewObjectType("google.expr.proto3.test.TestAllTypes")),
881 },
882 },
883 out: `_&&_(
884 _&&_(
885 __comprehension__(
886 // Variable
887 e,
888 // Target
889 x~google.expr.proto3.test.TestAllTypes^x.repeated_int64~list(int),
890 // Accumulator
891 @result,
892 // Init
893 true~bool,
894 // LoopCondition
895 @not_strictly_false(
896 @result~bool^@result
897 )~bool^not_strictly_false,
898 // LoopStep
899 _&&_(
900 @result~bool^@result,
901 _>_(
902 e~int^e,
903 0~int
904 )~bool^greater_int64
905 )~bool^logical_and,
906 // Result
907 @result~bool^@result)~bool,
908 __comprehension__(
909 // Variable
910 e,
911 // Target
912 x~google.expr.proto3.test.TestAllTypes^x.repeated_int64~list(int),
913 // Accumulator
914 @result,
915 // Init
916 false~bool,
917 // LoopCondition
918 @not_strictly_false(
919 !_(
920 @result~bool^@result
921 )~bool^logical_not
922 )~bool^not_strictly_false,
923 // LoopStep
924 _||_(
925 @result~bool^@result,
926 _<_(
927 e~int^e,
928 0~int
929 )~bool^less_int64
930 )~bool^logical_or,
931 // Result
932 @result~bool^@result)~bool
933 )~bool^logical_and,
934 __comprehension__(
935 // Variable
936 e,
937 // Target
938 x~google.expr.proto3.test.TestAllTypes^x.repeated_int64~list(int),
939 // Accumulator
940 @result,
941 // Init
942 0~int,
943 // LoopCondition
944 true~bool,
945 // LoopStep
946 _?_:_(
947 _==_(
948 e~int^e,
949 0~int
950 )~bool^equals,
951 _+_(
952 @result~int^@result,
953 1~int
954 )~int^add_int64,
955 @result~int^@result
956 )~int^conditional,
957 // Result
958 _==_(
959 @result~int^@result,
960 1~int
961 )~bool^equals)~bool
962 )~bool^logical_and`,
963 outType: types.BoolType,
964 },
965
966 {
967 in: `x.all(e, 0)`,
968 env: testEnv{
969 idents: []*decls.VariableDecl{
970 decls.NewVariable("x", types.NewObjectType("google.expr.proto3.test.TestAllTypes")),
971 },
972 },
973 err: `
974 ERROR: <input>:1:1: expression of type 'google.expr.proto3.test.TestAllTypes' cannot be range of a comprehension (must be list, map, or dynamic)
975 | x.all(e, 0)
976 | ^
977 ERROR: <input>:1:10: expected type 'bool' but found 'int'
978 | x.all(e, 0)
979 | .........^
980 `,
981 },
982 {
983 in: `lists.filter(x, x > 1.5)`,
984 out: `__comprehension__(
985 // Variable
986 x,
987 // Target
988 lists~dyn^lists,
989 // Accumulator
990 @result,
991 // Init
992 []~list(dyn),
993 // LoopCondition
994 true~bool,
995 // LoopStep
996 _?_:_(
997 _>_(
998 x~dyn^x,
999 1.5~double
1000 )~bool^greater_double|greater_int64_double|greater_uint64_double,
1001 _+_(
1002 @result~list(dyn)^@result,
1003 [
1004 x~dyn^x
1005 ]~list(dyn)
1006 )~list(dyn)^add_list,
1007 @result~list(dyn)^@result
1008 )~list(dyn)^conditional,
1009 // Result
1010 @result~list(dyn)^@result)~list(dyn)`,
1011 outType: types.NewListType(types.DynType),
1012 env: testEnv{
1013 idents: []*decls.VariableDecl{
1014 decls.NewVariable("lists", types.DynType),
1015 },
1016 },
1017 },
1018
1019 {
1020 in: `.google.expr.proto3.test.TestAllTypes`,
1021 out: `google.expr.proto3.test.TestAllTypes
1022 ~type(google.expr.proto3.test.TestAllTypes)
1023 ^google.expr.proto3.test.TestAllTypes`,
1024 outType: types.NewTypeTypeWithParam(
1025 types.NewObjectType("google.expr.proto3.test.TestAllTypes")),
1026 },
1027
1028 {
1029 in: `test.TestAllTypes`,
1030 container: "google.expr.proto3",
1031 out: `
1032 google.expr.proto3.test.TestAllTypes
1033 ~type(google.expr.proto3.test.TestAllTypes)
1034 ^google.expr.proto3.test.TestAllTypes
1035 `,
1036 outType: types.NewTypeTypeWithParam(
1037 types.NewObjectType("google.expr.proto3.test.TestAllTypes")),
1038 },
1039
1040 {
1041 in: `1 + x`,
1042 err: `
1043 ERROR: <input>:1:5: undeclared reference to 'x' (in container '')
1044 | 1 + x
1045 | ....^`,
1046 },
1047
1048 {
1049 in: `x == google.protobuf.Any{
1050 type_url:'types.googleapis.com/google.expr.proto3.test.TestAllTypes'
1051 } && x.single_nested_message.bb == 43
1052 || x == google.expr.proto3.test.TestAllTypes{}
1053 || y < x
1054 || x >= x`,
1055 env: testEnv{
1056 idents: []*decls.VariableDecl{
1057 decls.NewVariable("x", types.AnyType),
1058 decls.NewVariable("y", types.NewNullableType(types.IntType)),
1059 },
1060 },
1061 out: `
1062 _||_(
1063 _||_(
1064 _&&_(
1065 _==_(
1066 x~any^x,
1067 google.protobuf.Any{
1068 type_url:"types.googleapis.com/google.expr.proto3.test.TestAllTypes"~string
1069 }~any^google.protobuf.Any
1070 )~bool^equals,
1071 _==_(
1072 x~any^x.single_nested_message~dyn.bb~dyn,
1073 43~int
1074 )~bool^equals
1075 )~bool^logical_and,
1076 _==_(
1077 x~any^x,
1078 google.expr.proto3.test.TestAllTypes{}~google.expr.proto3.test.TestAllTypes^google.expr.proto3.test.TestAllTypes
1079 )~bool^equals
1080 )~bool^logical_or,
1081 _||_(
1082 _<_(
1083 y~wrapper(int)^y,
1084 x~any^x
1085 )~bool^less_int64|less_int64_double|less_int64_uint64,
1086 _>=_(
1087 x~any^x,
1088 x~any^x
1089 )~bool^greater_equals_bool|greater_equals_bytes|greater_equals_double|greater_equals_double_int64|greater_equals_double_uint64|greater_equals_duration|greater_equals_int64|greater_equals_int64_double|greater_equals_int64_uint64|greater_equals_string|greater_equals_timestamp|greater_equals_uint64|greater_equals_uint64_double|greater_equals_uint64_int64
1090 )~bool^logical_or
1091 )~bool^logical_or
1092 `,
1093 outType: types.BoolType,
1094 },
1095
1096 {
1097 in: `x == google.protobuf.Any{
1098 type_url:'types.googleapis.com/google.expr.proto3.test.TestAllTypes'
1099 } && x.single_nested_message.bb == 43
1100 || x == google.expr.proto3.test.TestAllTypes{}
1101 || y < x
1102 || x >= x`,
1103 env: testEnv{
1104 variadicASTs: true,
1105 idents: []*decls.VariableDecl{
1106 decls.NewVariable("x", types.AnyType),
1107 decls.NewVariable("y", types.NewNullableType(types.IntType)),
1108 },
1109 },
1110 out: `
1111 _||_(
1112 _&&_(
1113 _==_(
1114 x~any^x,
1115 google.protobuf.Any{
1116 type_url:"types.googleapis.com/google.expr.proto3.test.TestAllTypes"~string
1117 }~any^google.protobuf.Any
1118 )~bool^equals,
1119 _==_(
1120 x~any^x.single_nested_message~dyn.bb~dyn,
1121 43~int
1122 )~bool^equals
1123 )~bool^logical_and,
1124 _==_(
1125 x~any^x,
1126 google.expr.proto3.test.TestAllTypes{}~google.expr.proto3.test.TestAllTypes^google.expr.proto3.test.TestAllTypes
1127 )~bool^equals,
1128 _<_(
1129 y~wrapper(int)^y,
1130 x~any^x
1131 )~bool^less_int64|less_int64_double|less_int64_uint64,
1132 _>=_(
1133 x~any^x,
1134 x~any^x
1135 )~bool^greater_equals_bool|greater_equals_bytes|greater_equals_double|greater_equals_double_int64|greater_equals_double_uint64|greater_equals_duration|greater_equals_int64|greater_equals_int64_double|greater_equals_int64_uint64|greater_equals_string|greater_equals_timestamp|greater_equals_uint64|greater_equals_uint64_double|greater_equals_uint64_int64
1136 )~bool^logical_or
1137 `,
1138 outType: types.BoolType,
1139 },
1140
1141 {
1142 in: `x`,
1143 container: "container",
1144 env: testEnv{
1145 idents: []*decls.VariableDecl{
1146 decls.NewVariable("container.x", types.NewObjectType("google.expr.proto3.test.TestAllTypes")),
1147 },
1148 },
1149 out: `container.x~google.expr.proto3.test.TestAllTypes^container.x`,
1150 outType: types.NewObjectType("google.expr.proto3.test.TestAllTypes"),
1151 },
1152
1153 {
1154 in: `list == type([1]) && map == type({1:2u})`,
1155 out: `
1156 _&&_(_==_(list~type(list(dyn))^list,
1157 type([1~int]~list(int))~type(list(int))^type)
1158 ~bool^equals,
1159 _==_(map~type(map(dyn, dyn))^map,
1160 type({1~int : 2u~uint}~map(int, uint))~type(map(int, uint))^type)
1161 ~bool^equals)
1162 ~bool^logical_and
1163 `,
1164 outType: types.BoolType,
1165 },
1166
1167 {
1168 in: `myfun(1, true, 3u) + 1.myfun(false, 3u).myfun(true, 42u)`,
1169 env: testEnv{
1170 functions: []*decls.FunctionDecl{
1171 testFunction(t, "myfun",
1172 decls.MemberOverload("myfun_instance",
1173 []*types.Type{types.IntType, types.BoolType, types.UintType}, types.IntType),
1174 decls.Overload("myfun_static",
1175 []*types.Type{types.IntType, types.BoolType, types.UintType}, types.IntType)),
1176 },
1177 },
1178 out: `_+_(
1179 myfun(
1180 1~int,
1181 true~bool,
1182 3u~uint
1183 )~int^myfun_static,
1184 1~int.myfun(
1185 false~bool,
1186 3u~uint
1187 )~int^myfun_instance.myfun(
1188 true~bool,
1189 42u~uint
1190 )~int^myfun_instance
1191 )~int^add_int64`,
1192 outType: types.IntType,
1193 },
1194
1195 {
1196 in: `size(x) > 4`,
1197 env: testEnv{
1198 idents: []*decls.VariableDecl{
1199 decls.NewVariable("x", types.NewObjectType("google.expr.proto3.test.TestAllTypes")),
1200 },
1201 functions: []*decls.FunctionDecl{
1202 testFunction(t, "size",
1203 decls.Overload("size_message",
1204 []*types.Type{types.NewObjectType("google.expr.proto3.test.TestAllTypes")},
1205 types.IntType)),
1206 },
1207 },
1208 outType: types.BoolType,
1209 },
1210
1211 {
1212 in: `x.single_int64_wrapper + 1 != 23`,
1213 env: testEnv{
1214 idents: []*decls.VariableDecl{
1215 decls.NewVariable("x", types.NewObjectType("google.expr.proto3.test.TestAllTypes")),
1216 },
1217 },
1218 out: `
1219 _!=_(_+_(x~google.expr.proto3.test.TestAllTypes^x.single_int64_wrapper
1220 ~wrapper(int),
1221 1~int)
1222 ~int^add_int64,
1223 23~int)
1224 ~bool^not_equals
1225 `,
1226 outType: types.BoolType,
1227 },
1228
1229 {
1230 in: `x.single_int64_wrapper + y != 23`,
1231 env: testEnv{
1232 idents: []*decls.VariableDecl{
1233 decls.NewVariable("x", types.NewObjectType("google.expr.proto3.test.TestAllTypes")),
1234 decls.NewVariable("y", types.NewObjectType("google.protobuf.Int32Value")),
1235 },
1236 },
1237 out: `
1238 _!=_(
1239 _+_(
1240 x~google.expr.proto3.test.TestAllTypes^x.single_int64_wrapper~wrapper(int),
1241 y~wrapper(int)^y
1242 )~int^add_int64,
1243 23~int
1244 )~bool^not_equals
1245 `,
1246 outType: types.BoolType,
1247 },
1248
1249 {
1250 in: `1 in [1, 2, 3]`,
1251 out: `@in(
1252 1~int,
1253 [
1254 1~int,
1255 2~int,
1256 3~int
1257 ]~list(int)
1258 )~bool^in_list`,
1259 outType: types.BoolType,
1260 },
1261
1262 {
1263 in: `1 in dyn([1, 2, 3])`,
1264 out: `@in(
1265 1~int,
1266 dyn(
1267 [
1268 1~int,
1269 2~int,
1270 3~int
1271 ]~list(int)
1272 )~dyn^to_dyn
1273 )~bool^in_list|in_map`,
1274 outType: types.BoolType,
1275 },
1276
1277 {
1278 in: `type(null) == null_type`,
1279 out: `_==_(
1280 type(
1281 null~null
1282 )~type(null)^type,
1283 null_type~type(null)^null_type
1284 )~bool^equals`,
1285 outType: types.BoolType,
1286 },
1287
1288 {
1289 in: `type(type) == type`,
1290 out: `_==_(
1291 type(
1292 type~type(type)^type
1293 )~type(type(type))^type,
1294 type~type(type)^type
1295 )~bool^equals`,
1296 outType: types.BoolType,
1297 },
1298 {
1299 in: `([[[1]], [[2]], [[3]]][0][0] + [2, 3, {'four': {'five': 'six'}}])[3]`,
1300 out: `_[_](
1301 _+_(
1302 _[_](
1303 _[_](
1304 [
1305 [
1306 [
1307 1~int
1308 ]~list(int)
1309 ]~list(list(int)),
1310 [
1311 [
1312 2~int
1313 ]~list(int)
1314 ]~list(list(int)),
1315 [
1316 [
1317 3~int
1318 ]~list(int)
1319 ]~list(list(int))
1320 ]~list(list(list(int))),
1321 0~int
1322 )~list(list(int))^index_list,
1323 0~int
1324 )~list(int)^index_list,
1325 [
1326 2~int,
1327 3~int,
1328 {
1329 "four"~string:{
1330 "five"~string:"six"~string
1331 }~map(string, string)
1332 }~map(string, map(string, string))
1333 ]~list(dyn)
1334 )~list(dyn)^add_list,
1335 3~int
1336 )~dyn^index_list`,
1337 outType: types.DynType,
1338 },
1339 {
1340 in: `[1] + [dyn('string')]`,
1341 out: `_+_(
1342 [
1343 1~int
1344 ]~list(int),
1345 [
1346 dyn(
1347 "string"~string
1348 )~dyn^to_dyn
1349 ]~list(dyn)
1350 )~list(dyn)^add_list`,
1351 outType: types.NewListType(types.DynType),
1352 },
1353 {
1354 in: `[dyn('string')] + [1]`,
1355 out: `_+_(
1356 [
1357 dyn(
1358 "string"~string
1359 )~dyn^to_dyn
1360 ]~list(dyn),
1361 [
1362 1~int
1363 ]~list(int)
1364 )~list(dyn)^add_list`,
1365 outType: types.NewListType(types.DynType),
1366 },
1367 {
1368 in: `[].map(x, [].map(y, x in y && y in x))`,
1369 err: `
1370 ERROR: <input>:1:33: found no matching overload for '@in' applied to '(list(dyn), dyn)'
1371 | [].map(x, [].map(y, x in y && y in x))
1372 | ................................^`,
1373 },
1374 {
1375 in: `args.user["myextension"].customAttributes.filter(x, x.name == "hobbies")`,
1376 out: `__comprehension__(
1377 // Variable
1378 x,
1379 // Target
1380 _[_](
1381 args~map(string, dyn)^args.user~dyn,
1382 "myextension"~string
1383 )~dyn^index_map.customAttributes~dyn,
1384 // Accumulator
1385 @result,
1386 // Init
1387 []~list(dyn),
1388 // LoopCondition
1389 true~bool,
1390 // LoopStep
1391 _?_:_(
1392 _==_(
1393 x~dyn^x.name~dyn,
1394 "hobbies"~string
1395 )~bool^equals,
1396 _+_(
1397 @result~list(dyn)^@result,
1398 [
1399 x~dyn^x
1400 ]~list(dyn)
1401 )~list(dyn)^add_list,
1402 @result~list(dyn)^@result
1403 )~list(dyn)^conditional,
1404 // Result
1405 @result~list(dyn)^@result)~list(dyn)`,
1406 env: testEnv{
1407 idents: []*decls.VariableDecl{
1408 decls.NewVariable("args", types.NewMapType(types.StringType, types.DynType)),
1409 },
1410 },
1411 outType: types.NewListType(types.DynType),
1412 },
1413 {
1414 in: `a.b + 1 == a[0]`,
1415 out: `_==_(
1416 _+_(
1417 a~dyn^a.b~dyn,
1418 1~int
1419 )~int^add_int64,
1420 _[_](
1421 a~dyn^a,
1422 0~int
1423 )~dyn^index_list|index_map
1424 )~bool^equals`,
1425 env: testEnv{
1426 idents: []*decls.VariableDecl{
1427 decls.NewVariable("a", types.NewTypeParamType("T")),
1428 },
1429 },
1430 outType: types.BoolType,
1431 },
1432 {
1433 in: `!has(pb2.single_int64)
1434 && !has(pb2.repeated_int32)
1435 && !has(pb2.map_string_string)
1436 && !has(pb3.single_int64)
1437 && !has(pb3.repeated_int32)
1438 && !has(pb3.map_string_string)`,
1439 env: testEnv{
1440 idents: []*decls.VariableDecl{
1441 decls.NewVariable("pb2", types.NewObjectType("google.expr.proto2.test.TestAllTypes")),
1442 decls.NewVariable("pb3", types.NewObjectType("google.expr.proto3.test.TestAllTypes")),
1443 },
1444 },
1445 out: `
1446 _&&_(
1447 _&&_(
1448 _&&_(
1449 !_(
1450 pb2~google.expr.proto2.test.TestAllTypes^pb2.single_int64~test-only~~bool
1451 )~bool^logical_not,
1452 !_(
1453 pb2~google.expr.proto2.test.TestAllTypes^pb2.repeated_int32~test-only~~bool
1454 )~bool^logical_not
1455 )~bool^logical_and,
1456 !_(
1457 pb2~google.expr.proto2.test.TestAllTypes^pb2.map_string_string~test-only~~bool
1458 )~bool^logical_not
1459 )~bool^logical_and,
1460 _&&_(
1461 _&&_(
1462 !_(
1463 pb3~google.expr.proto3.test.TestAllTypes^pb3.single_int64~test-only~~bool
1464 )~bool^logical_not,
1465 !_(
1466 pb3~google.expr.proto3.test.TestAllTypes^pb3.repeated_int32~test-only~~bool
1467 )~bool^logical_not
1468 )~bool^logical_and,
1469 !_(
1470 pb3~google.expr.proto3.test.TestAllTypes^pb3.map_string_string~test-only~~bool
1471 )~bool^logical_not
1472 )~bool^logical_and
1473 )~bool^logical_and`,
1474 outType: types.BoolType,
1475 },
1476 {
1477 in: `TestAllTypes{}.repeated_nested_message`,
1478 container: "google.expr.proto2.test",
1479 out: `
1480 google.expr.proto2.test.TestAllTypes{}~google.expr.proto2.test.TestAllTypes^
1481 google.expr.proto2.test.TestAllTypes.repeated_nested_message
1482 ~list(google.expr.proto2.test.TestAllTypes.NestedMessage)`,
1483 outType: types.NewListType(
1484 types.NewObjectType(
1485 "google.expr.proto2.test.TestAllTypes.NestedMessage",
1486 ),
1487 ),
1488 },
1489 {
1490 in: `TestAllTypes{}.repeated_nested_message`,
1491 container: "google.expr.proto3.test",
1492 out: `
1493 google.expr.proto3.test.TestAllTypes{}~google.expr.proto3.test.TestAllTypes^
1494 google.expr.proto3.test.TestAllTypes.repeated_nested_message
1495 ~list(google.expr.proto3.test.TestAllTypes.NestedMessage)`,
1496 outType: types.NewListType(
1497 types.NewObjectType(
1498 "google.expr.proto3.test.TestAllTypes.NestedMessage",
1499 ),
1500 ),
1501 },
1502 {
1503 in: `base64.encode('hello')`,
1504 env: testEnv{
1505 functions: []*decls.FunctionDecl{
1506 testFunction(t, "base64.encode",
1507 decls.Overload(
1508 "base64_encode_string",
1509 []*types.Type{types.StringType},
1510 types.StringType)),
1511 },
1512 },
1513 out: `
1514 base64.encode(
1515 "hello"~string
1516 )~string^base64_encode_string`,
1517 outType: types.StringType,
1518 },
1519 {
1520 in: `encode('hello')`,
1521 container: `base64`,
1522 env: testEnv{
1523 functions: []*decls.FunctionDecl{
1524 testFunction(t, "base64.encode",
1525 decls.Overload(
1526 "base64_encode_string",
1527 []*types.Type{types.StringType},
1528 types.StringType)),
1529 },
1530 },
1531 out: `
1532 base64.encode(
1533 "hello"~string
1534 )~string^base64_encode_string`,
1535 outType: types.StringType,
1536 },
1537 {
1538 in: `{}`,
1539 out: `{}~map(dyn, dyn)`,
1540 outType: types.NewMapType(types.DynType, types.DynType),
1541 },
1542 {
1543 in: `set([1, 2, 3])`,
1544 out: `
1545 set(
1546 [
1547 1~int,
1548 2~int,
1549 3~int
1550 ]~list(int)
1551 )~set(int)^set_list`,
1552 env: testEnv{
1553 functions: []*decls.FunctionDecl{
1554 testFunction(t, "set",
1555 decls.Overload("set_list",
1556 []*types.Type{types.NewListType(types.NewTypeParamType("T"))},
1557 types.NewOpaqueType("set", types.NewTypeParamType("T")))),
1558 },
1559 },
1560 outType: types.NewOpaqueType("set", types.IntType),
1561 },
1562 {
1563 in: `set([1, 2]) == set([2, 1])`,
1564 out: `
1565 _==_(
1566 set([1~int, 2~int]~list(int))~set(int)^set_list,
1567 set([2~int, 1~int]~list(int))~set(int)^set_list
1568 )~bool^equals`,
1569 env: testEnv{
1570 functions: []*decls.FunctionDecl{
1571 testFunction(t, "set",
1572 decls.Overload(
1573 "set_list", []*types.Type{types.NewListType(types.NewTypeParamType("T"))},
1574 types.NewOpaqueType("set", types.NewTypeParamType("T")))),
1575 },
1576 },
1577 outType: types.BoolType,
1578 },
1579 {
1580 in: `set([1, 2]) == x`,
1581 out: `
1582 _==_(
1583 set([1~int, 2~int]~list(int))~set(int)^set_list,
1584 x~set(int)^x
1585 )~bool^equals`,
1586 env: testEnv{
1587 idents: []*decls.VariableDecl{
1588 decls.NewVariable("x", types.NewOpaqueType("set", types.NewTypeParamType("T"))),
1589 },
1590 functions: []*decls.FunctionDecl{
1591 testFunction(t, "set",
1592 decls.Overload(
1593 "set_list", []*types.Type{types.NewListType(types.NewTypeParamType("T"))},
1594 types.NewOpaqueType("set", types.NewTypeParamType("T")))),
1595 },
1596 },
1597 outType: types.BoolType,
1598 },
1599 {
1600 in: `int{}`,
1601 err: `
1602 ERROR: <input>:1:4: 'int' is not a message type
1603 | int{}
1604 | ...^
1605 `,
1606 },
1607 {
1608 in: `Msg{}`,
1609 err: `
1610 ERROR: <input>:1:4: undeclared reference to 'Msg' (in container '')
1611 | Msg{}
1612 | ...^
1613 `,
1614 },
1615 {
1616 in: `fun()`,
1617 err: `
1618 ERROR: <input>:1:4: undeclared reference to 'fun' (in container '')
1619 | fun()
1620 | ...^
1621 `,
1622 },
1623 {
1624 in: `'string'.fun()`,
1625 err: `
1626 ERROR: <input>:1:13: undeclared reference to 'fun' (in container '')
1627 | 'string'.fun()
1628 | ............^
1629 `,
1630 },
1631 {
1632 in: `[].length`,
1633 err: `
1634 ERROR: <input>:1:3: type 'list(_var0)' does not support field selection
1635 | [].length
1636 | ..^
1637 `,
1638 },
1639 {
1640 in: `1 <= 1.0 && 1u <= 1.0 && 1.0 <= 1 && 1.0 <= 1u && 1 <= 1u && 1u <= 1`,
1641 opts: []Option{CrossTypeNumericComparisons(false)},
1642 err: `
1643 ERROR: <input>:1:3: found no matching overload for '_<=_' applied to '(int, double)'
1644 | 1 <= 1.0 && 1u <= 1.0 && 1.0 <= 1 && 1.0 <= 1u && 1 <= 1u && 1u <= 1
1645 | ..^
1646 ERROR: <input>:1:16: found no matching overload for '_<=_' applied to '(uint, double)'
1647 | 1 <= 1.0 && 1u <= 1.0 && 1.0 <= 1 && 1.0 <= 1u && 1 <= 1u && 1u <= 1
1648 | ...............^
1649 ERROR: <input>:1:30: found no matching overload for '_<=_' applied to '(double, int)'
1650 | 1 <= 1.0 && 1u <= 1.0 && 1.0 <= 1 && 1.0 <= 1u && 1 <= 1u && 1u <= 1
1651 | .............................^
1652 ERROR: <input>:1:42: found no matching overload for '_<=_' applied to '(double, uint)'
1653 | 1 <= 1.0 && 1u <= 1.0 && 1.0 <= 1 && 1.0 <= 1u && 1 <= 1u && 1u <= 1
1654 | .........................................^
1655 ERROR: <input>:1:53: found no matching overload for '_<=_' applied to '(int, uint)'
1656 | 1 <= 1.0 && 1u <= 1.0 && 1.0 <= 1 && 1.0 <= 1u && 1 <= 1u && 1u <= 1
1657 | ....................................................^
1658 ERROR: <input>:1:65: found no matching overload for '_<=_' applied to '(uint, int)'
1659 | 1 <= 1.0 && 1u <= 1.0 && 1.0 <= 1 && 1.0 <= 1u && 1 <= 1u && 1u <= 1
1660 | ................................................................^
1661 `,
1662 },
1663 {
1664 in: `1 <= 1.0 && 1u <= 1.0 && 1.0 <= 1 && 1.0 <= 1u && 1 <= 1u && 1u <= 1`,
1665 opts: []Option{CrossTypeNumericComparisons(true)},
1666 outType: types.BoolType,
1667 out: `
1668 _&&_(
1669 _&&_(
1670 _&&_(
1671 _<=_(
1672 1~int,
1673 1~double
1674 )~bool^less_equals_int64_double,
1675 _<=_(
1676 1u~uint,
1677 1~double
1678 )~bool^less_equals_uint64_double
1679 )~bool^logical_and,
1680 _<=_(
1681 1~double,
1682 1~int
1683 )~bool^less_equals_double_int64
1684 )~bool^logical_and,
1685 _&&_(
1686 _&&_(
1687 _<=_(
1688 1~double,
1689 1u~uint
1690 )~bool^less_equals_double_uint64,
1691 _<=_(
1692 1~int,
1693 1u~uint
1694 )~bool^less_equals_int64_uint64
1695 )~bool^logical_and,
1696 _<=_(
1697 1u~uint,
1698 1~int
1699 )~bool^less_equals_uint64_int64
1700 )~bool^logical_and
1701 )~bool^logical_and`,
1702 },
1703 {
1704 in: `1 < 1.0 && 1u < 1.0 && 1.0 < 1 && 1.0 < 1u && 1 < 1u && 1u < 1`,
1705 opts: []Option{CrossTypeNumericComparisons(true)},
1706 outType: types.BoolType,
1707 out: `
1708 _&&_(
1709 _&&_(
1710 _&&_(
1711 _<_(
1712 1~int,
1713 1~double
1714 )~bool^less_int64_double,
1715 _<_(
1716 1u~uint,
1717 1~double
1718 )~bool^less_uint64_double
1719 )~bool^logical_and,
1720 _<_(
1721 1~double,
1722 1~int
1723 )~bool^less_double_int64
1724 )~bool^logical_and,
1725 _&&_(
1726 _&&_(
1727 _<_(
1728 1~double,
1729 1u~uint
1730 )~bool^less_double_uint64,
1731 _<_(
1732 1~int,
1733 1u~uint
1734 )~bool^less_int64_uint64
1735 )~bool^logical_and,
1736 _<_(
1737 1u~uint,
1738 1~int
1739 )~bool^less_uint64_int64
1740 )~bool^logical_and
1741 )~bool^logical_and`,
1742 },
1743 {
1744 in: `1 > 1.0 && 1u > 1.0 && 1.0 > 1 && 1.0 > 1u && 1 > 1u && 1u > 1`,
1745 opts: []Option{CrossTypeNumericComparisons(true)},
1746 outType: types.BoolType,
1747 out: `
1748 _&&_(
1749 _&&_(
1750 _&&_(
1751 _>_(
1752 1~int,
1753 1~double
1754 )~bool^greater_int64_double,
1755 _>_(
1756 1u~uint,
1757 1~double
1758 )~bool^greater_uint64_double
1759 )~bool^logical_and,
1760 _>_(
1761 1~double,
1762 1~int
1763 )~bool^greater_double_int64
1764 )~bool^logical_and,
1765 _&&_(
1766 _&&_(
1767 _>_(
1768 1~double,
1769 1u~uint
1770 )~bool^greater_double_uint64,
1771 _>_(
1772 1~int,
1773 1u~uint
1774 )~bool^greater_int64_uint64
1775 )~bool^logical_and,
1776 _>_(
1777 1u~uint,
1778 1~int
1779 )~bool^greater_uint64_int64
1780 )~bool^logical_and
1781 )~bool^logical_and`,
1782 },
1783 {
1784 in: `1 >= 1.0 && 1u >= 1.0 && 1.0 >= 1 && 1.0 >= 1u && 1 >= 1u && 1u >= 1`,
1785 opts: []Option{CrossTypeNumericComparisons(true)},
1786 outType: types.BoolType,
1787 out: `
1788 _&&_(
1789 _&&_(
1790 _&&_(
1791 _>=_(
1792 1~int,
1793 1~double
1794 )~bool^greater_equals_int64_double,
1795 _>=_(
1796 1u~uint,
1797 1~double
1798 )~bool^greater_equals_uint64_double
1799 )~bool^logical_and,
1800 _>=_(
1801 1~double,
1802 1~int
1803 )~bool^greater_equals_double_int64
1804 )~bool^logical_and,
1805 _&&_(
1806 _&&_(
1807 _>=_(
1808 1~double,
1809 1u~uint
1810 )~bool^greater_equals_double_uint64,
1811 _>=_(
1812 1~int,
1813 1u~uint
1814 )~bool^greater_equals_int64_uint64
1815 )~bool^logical_and,
1816 _>=_(
1817 1u~uint,
1818 1~int
1819 )~bool^greater_equals_uint64_int64
1820 )~bool^logical_and
1821 )~bool^logical_and`,
1822 },
1823 {
1824 in: `1 >= 1.0 && 1u >= 1.0 && 1.0 >= 1 && 1.0 >= 1u && 1 >= 1u && 1u >= 1`,
1825 opts: []Option{CrossTypeNumericComparisons(true)},
1826 env: testEnv{variadicASTs: true},
1827 outType: types.BoolType,
1828 out: `
1829 _&&_(
1830 _>=_(
1831 1~int,
1832 1~double
1833 )~bool^greater_equals_int64_double,
1834 _>=_(
1835 1u~uint,
1836 1~double
1837 )~bool^greater_equals_uint64_double,
1838 _>=_(
1839 1~double,
1840 1~int
1841 )~bool^greater_equals_double_int64,
1842 _>=_(
1843 1~double,
1844 1u~uint
1845 )~bool^greater_equals_double_uint64,
1846 _>=_(
1847 1~int,
1848 1u~uint
1849 )~bool^greater_equals_int64_uint64,
1850 _>=_(
1851 1u~uint,
1852 1~int
1853 )~bool^greater_equals_uint64_int64
1854 )~bool^logical_and`,
1855 },
1856 {
1857 in: `[1].map(x, [x, x]).map(x, [x, x])`,
1858 outType: types.NewListType(types.NewListType(types.NewListType(types.IntType))),
1859 out: `__comprehension__(
1860 // Variable
1861 x,
1862 // Target
1863 __comprehension__(
1864 // Variable
1865 x,
1866 // Target
1867 [
1868 1~int
1869 ]~list(int),
1870 // Accumulator
1871 @result,
1872 // Init
1873 []~list(list(int)),
1874 // LoopCondition
1875 true~bool,
1876 // LoopStep
1877 _+_(
1878 @result~list(list(int))^@result,
1879 [
1880 [
1881 x~int^x,
1882 x~int^x
1883 ]~list(int)
1884 ]~list(list(int))
1885 )~list(list(int))^add_list,
1886 // Result
1887 @result~list(list(int))^@result)~list(list(int)),
1888 // Accumulator
1889 @result,
1890 // Init
1891 []~list(list(list(int))),
1892 // LoopCondition
1893 true~bool,
1894 // LoopStep
1895 _+_(
1896 @result~list(list(list(int)))^@result,
1897 [
1898 [
1899 x~list(int)^x,
1900 x~list(int)^x
1901 ]~list(list(int))
1902 ]~list(list(list(int)))
1903 )~list(list(list(int)))^add_list,
1904 // Result
1905 @result~list(list(list(int)))^@result)~list(list(list(int)))
1906 `,
1907 },
1908 {
1909 in: `values.filter(i, i.content != "").map(i, i.content)`,
1910 outType: types.NewListType(types.StringType),
1911 env: testEnv{
1912 idents: []*decls.VariableDecl{
1913 decls.NewVariable("values", types.NewListType(types.NewMapType(types.StringType, types.StringType))),
1914 },
1915 },
1916 out: `__comprehension__(
1917 // Variable
1918 i,
1919 // Target
1920 __comprehension__(
1921 // Variable
1922 i,
1923 // Target
1924 values~list(map(string, string))^values,
1925 // Accumulator
1926 @result,
1927 // Init
1928 []~list(map(string, string)),
1929 // LoopCondition
1930 true~bool,
1931 // LoopStep
1932 _?_:_(
1933 _!=_(
1934 i~map(string, string)^i.content~string,
1935 ""~string
1936 )~bool^not_equals,
1937 _+_(
1938 @result~list(map(string, string))^@result,
1939 [
1940 i~map(string, string)^i
1941 ]~list(map(string, string))
1942 )~list(map(string, string))^add_list,
1943 @result~list(map(string, string))^@result
1944 )~list(map(string, string))^conditional,
1945 // Result
1946 @result~list(map(string, string))^@result)~list(map(string, string)),
1947 // Accumulator
1948 @result,
1949 // Init
1950 []~list(string),
1951 // LoopCondition
1952 true~bool,
1953 // LoopStep
1954 _+_(
1955 @result~list(string)^@result,
1956 [
1957 i~map(string, string)^i.content~string
1958 ]~list(string)
1959 )~list(string)^add_list,
1960 // Result
1961 @result~list(string)^@result)~list(string)`,
1962 },
1963 {
1964 in: `[{}.map(c,c,c)]+[{}.map(c,c,c)]`,
1965 outType: types.NewListType(types.NewListType(types.BoolType)),
1966 out: `_+_(
1967 [
1968 __comprehension__(
1969 // Variable
1970 c,
1971 // Target
1972 {}~map(bool, dyn),
1973 // Accumulator
1974 @result,
1975 // Init
1976 []~list(bool),
1977 // LoopCondition
1978 true~bool,
1979 // LoopStep
1980 _?_:_(
1981 c~bool^c,
1982 _+_(
1983 @result~list(bool)^@result,
1984 [
1985 c~bool^c
1986 ]~list(bool)
1987 )~list(bool)^add_list,
1988 @result~list(bool)^@result
1989 )~list(bool)^conditional,
1990 // Result
1991 @result~list(bool)^@result)~list(bool)
1992 ]~list(list(bool)),
1993 [
1994 __comprehension__(
1995 // Variable
1996 c,
1997 // Target
1998 {}~map(bool, dyn),
1999 // Accumulator
2000 @result,
2001 // Init
2002 []~list(bool),
2003 // LoopCondition
2004 true~bool,
2005 // LoopStep
2006 _?_:_(
2007 c~bool^c,
2008 _+_(
2009 @result~list(bool)^@result,
2010 [
2011 c~bool^c
2012 ]~list(bool)
2013 )~list(bool)^add_list,
2014 @result~list(bool)^@result
2015 )~list(bool)^conditional,
2016 // Result
2017 @result~list(bool)^@result)~list(bool)
2018 ]~list(list(bool))
2019 )~list(list(bool))^add_list`,
2020 },
2021 {
2022 in: "type(testAllTypes.nestedgroup.nested_id) == int",
2023 env: testEnv{
2024 idents: []*decls.VariableDecl{
2025 decls.NewVariable("testAllTypes", types.NewObjectType("google.expr.proto2.test.TestAllTypes")),
2026 },
2027 },
2028 outType: types.BoolType,
2029 out: `_==_(
2030 type(
2031 testAllTypes~google.expr.proto2.test.TestAllTypes^testAllTypes.nestedgroup~google.expr.proto2.test.TestAllTypes.NestedGroup.nested_id~int
2032 )~type(int)^type,
2033 int~type(int)^int
2034 )~bool^equals`,
2035 },
2036 {
2037 in: `a.?b`,
2038 env: testEnv{
2039 optionalSyntax: true,
2040 idents: []*decls.VariableDecl{
2041 decls.NewVariable("a", types.NewMapType(types.StringType, types.StringType)),
2042 },
2043 },
2044 outType: types.NewOptionalType(types.StringType),
2045 out: `_?._(
2046 a~map(string, string)^a,
2047 "b"
2048 )~optional_type(string)^select_optional_field`,
2049 },
2050 {
2051 in: `type(a.?b) == optional_type`,
2052 env: testEnv{
2053 optionalSyntax: true,
2054 idents: []*decls.VariableDecl{
2055 decls.NewVariable("a", types.NewMapType(types.StringType, types.StringType)),
2056 },
2057 },
2058 outType: types.BoolType,
2059 out: `_==_(
2060 type(
2061 _?._(
2062 a~map(string, string)^a,
2063 "b"
2064 )~optional_type(string)^select_optional_field
2065 )~type(optional_type(string))^type,
2066 optional_type~type(optional_type)^optional_type
2067 )~bool^equals`,
2068 },
2069 {
2070 in: `a.b`,
2071 env: testEnv{
2072 idents: []*decls.VariableDecl{
2073 decls.NewVariable("a", types.NewOptionalType(types.NewMapType(types.StringType, types.StringType))),
2074 },
2075 },
2076 outType: types.NewOptionalType(types.StringType),
2077 out: `a~optional_type(map(string, string))^a.b~optional_type(string)`,
2078 },
2079 {
2080 in: `a.dynamic`,
2081 env: testEnv{
2082 idents: []*decls.VariableDecl{
2083 decls.NewVariable("a", types.NewOptionalType(types.DynType)),
2084 },
2085 },
2086 outType: types.NewOptionalType(types.DynType),
2087 out: `a~optional_type(dyn)^a.dynamic~optional_type(dyn)`,
2088 },
2089 {
2090 in: `has(a.dynamic)`,
2091 env: testEnv{
2092 idents: []*decls.VariableDecl{
2093 decls.NewVariable("a", types.NewOptionalType(types.DynType)),
2094 },
2095 },
2096 outType: types.BoolType,
2097 out: `a~optional_type(dyn)^a.dynamic~test-only~~bool`,
2098 },
2099 {
2100 in: `has(a.?b.c)`,
2101 env: testEnv{
2102 optionalSyntax: true,
2103 idents: []*decls.VariableDecl{
2104 decls.NewVariable("a", types.NewOptionalType(types.NewMapType(types.StringType, types.DynType))),
2105 },
2106 },
2107 outType: types.BoolType,
2108 out: `_?._(
2109 a~optional_type(map(string, dyn))^a,
2110 "b"
2111 )~optional_type(dyn)^select_optional_field.c~test-only~~bool`,
2112 },
2113 {
2114 in: `{?'key': {'a': 'b'}.?value}`,
2115 env: testEnv{optionalSyntax: true},
2116 outType: types.NewMapType(types.StringType, types.StringType),
2117 out: `{
2118 ?"key"~string:_?._(
2119 {
2120 "a"~string:"b"~string
2121 }~map(string, string),
2122 "value"
2123 )~optional_type(string)^select_optional_field
2124 }~map(string, string)`,
2125 },
2126 {
2127 in: `{?'key': {'a': 'b'}.?value}.key`,
2128 env: testEnv{optionalSyntax: true},
2129 outType: types.StringType,
2130 out: `{
2131 ?"key"~string:_?._(
2132 {
2133 "a"~string:"b"~string
2134 }~map(string, string),
2135 "value"
2136 )~optional_type(string)^select_optional_field
2137 }~map(string, string).key~string`,
2138 },
2139 {
2140 in: `{?'nested': a.b}`,
2141 env: testEnv{
2142 optionalSyntax: true,
2143 idents: []*decls.VariableDecl{
2144 decls.NewVariable("a", types.NewOptionalType(types.NewMapType(types.StringType, types.StringType))),
2145 },
2146 },
2147 outType: types.NewMapType(types.StringType, types.StringType),
2148 out: `{
2149 ?"nested"~string:a~optional_type(map(string, string))^a.b~optional_type(string)
2150 }~map(string, string)`,
2151 },
2152 {
2153 in: `{?'key': 'hi'}`,
2154 env: testEnv{optionalSyntax: true},
2155 err: `ERROR: <input>:1:10: expected type 'optional_type(string)' but found 'string'
2156 | {?'key': 'hi'}
2157 | .........^`,
2158 },
2159 {
2160 in: `[?a, ?b, 'world']`,
2161 env: testEnv{
2162 optionalSyntax: true,
2163 idents: []*decls.VariableDecl{
2164 decls.NewVariable("a", types.NewOptionalType(types.StringType)),
2165 decls.NewVariable("b", types.NewOptionalType(types.StringType)),
2166 },
2167 },
2168 outType: types.NewListType(types.StringType),
2169 out: `[
2170 a~optional_type(string)^a,
2171 b~optional_type(string)^b,
2172 "world"~string
2173 ]~list(string)`,
2174 },
2175 {
2176 in: `[?'value']`,
2177 env: testEnv{optionalSyntax: true},
2178 err: `ERROR: <input>:1:3: expected type 'optional_type(string)' but found 'string'
2179 | [?'value']
2180 | ..^`,
2181 },
2182 {
2183 in: `TestAllTypes{?single_int32: {}.?i}`,
2184 container: "google.expr.proto2.test",
2185 env: testEnv{optionalSyntax: true},
2186 out: `google.expr.proto2.test.TestAllTypes{
2187 ?single_int32:_?._(
2188 {}~map(dyn, int),
2189 "i"
2190 )~optional_type(int)^select_optional_field
2191 }~google.expr.proto2.test.TestAllTypes^google.expr.proto2.test.TestAllTypes`,
2192 outType: types.NewObjectType(
2193 "google.expr.proto2.test.TestAllTypes",
2194 ),
2195 },
2196 {
2197 in: `TestAllTypes{?single_int32: 1}`,
2198 container: "google.expr.proto2.test",
2199 env: testEnv{optionalSyntax: true},
2200 err: `ERROR: <input>:1:29: expected type 'optional_type(int)' but found 'int'
2201 | TestAllTypes{?single_int32: 1}
2202 | ............................^`,
2203 },
2204 {
2205 in: `undef`,
2206 err: `ERROR: <input>:1:1: undeclared reference to 'undef' (in container '')
2207 | undef
2208 | ^`,
2209 },
2210 {
2211 in: `undef()`,
2212 err: `ERROR: <input>:1:6: undeclared reference to 'undef' (in container '')
2213 | undef()
2214 | .....^`,
2215 },
2216 {
2217 in: `null_int == null || null == null_int || null_msg == null || null == null_msg`,
2218 env: testEnv{
2219 idents: []*decls.VariableDecl{
2220 decls.NewVariable("null_int", types.NewNullableType(types.IntType)),
2221 decls.NewVariable("null_msg", types.NewObjectType("google.expr.proto2.test.TestAllTypes")),
2222 },
2223 },
2224 outType: types.BoolType,
2225 },
2226 {
2227 in: `NotAMessage{}`,
2228 env: testEnv{
2229 idents: []*decls.VariableDecl{
2230 decls.NewVariable("NotAMessage", types.NewNullableType(types.IntType)),
2231 },
2232 },
2233 err: `ERROR: <input>:1:12: undeclared reference to 'NotAMessage' (in container '')
2234 | NotAMessage{}
2235 | ...........^`,
2236 },
2237 {
2238 in: `{}.map(c,[c,type(c)])`,
2239 out: `__comprehension__(
2240 // Variable
2241 c,
2242 // Target
2243 {}~map(dyn, dyn),
2244 // Accumulator
2245 @result,
2246 // Init
2247 []~list(list(dyn)),
2248 // LoopCondition
2249 true~bool,
2250 // LoopStep
2251 _+_(
2252 @result~list(list(dyn))^@result,
2253 [
2254 [
2255 c~dyn^c,
2256 type(
2257 c~dyn^c
2258 )~type(dyn)^type
2259 ]~list(dyn)
2260 ]~list(list(dyn))
2261 )~list(list(dyn))^add_list,
2262 // Result
2263 @result~list(list(dyn))^@result)~list(list(dyn))`,
2264 outType: types.NewListType(types.NewListType(types.DynType)),
2265 },
2266 {
2267 in: `[{'z': 0}].exists(y, y.z == 0)`,
2268 env: testEnv{
2269 idents: []*decls.VariableDecl{
2270 decls.NewVariable("cel.example.y", types.NewMapType(types.StringType, types.IntType)),
2271 },
2272 },
2273 out: `__comprehension__(
2274 // Variable
2275 y,
2276 // Target
2277 [
2278 {
2279 "z"~string:0~int
2280 }~map(string, int)
2281 ]~list(map(string, int)),
2282 // Accumulator
2283 @result,
2284 // Init
2285 false~bool,
2286 // LoopCondition
2287 @not_strictly_false(
2288 !_(
2289 @result~bool^@result
2290 )~bool^logical_not
2291 )~bool^not_strictly_false,
2292 // LoopStep
2293 _||_(
2294 @result~bool^@result,
2295 _==_(
2296 y~map(string, int)^y.z~int,
2297 0~int
2298 )~bool^equals
2299 )~bool^logical_or,
2300 // Result
2301 @result~bool^@result)~bool`,
2302 outType: types.BoolType,
2303 },
2304 {
2305 in: `[{'y': 0}].exists(x, x.y == 0)`,
2306 env: testEnv{
2307 idents: []*decls.VariableDecl{
2308 decls.NewVariable("x", types.NewMapType(types.StringType, types.IntType)),
2309 },
2310 },
2311 out: `__comprehension__(
2312 // Variable
2313 x,
2314 // Target
2315 [
2316 {
2317 "y"~string:0~int
2318 }~map(string, int)
2319 ]~list(map(string, int)),
2320 // Accumulator
2321 @result,
2322 // Init
2323 false~bool,
2324 // LoopCondition
2325 @not_strictly_false(
2326 !_(
2327 @result~bool^@result
2328 )~bool^logical_not
2329 )~bool^not_strictly_false,
2330 // LoopStep
2331 _||_(
2332 @result~bool^@result,
2333 _==_(
2334 x~map(string, int)^x.y~int,
2335 0~int
2336 )~bool^equals
2337 )~bool^logical_or,
2338 // Result
2339 @result~bool^@result)~bool`,
2340 outType: types.BoolType,
2341 },
2342 {
2343 in: `[0].exists(x, x != .x)`,
2344 env: testEnv{
2345 idents: []*decls.VariableDecl{
2346 decls.NewVariable("x", types.IntType),
2347 },
2348 },
2349 out: `__comprehension__(
2350 // Variable
2351 x,
2352 // Target
2353 [
2354 0~int
2355 ]~list(int),
2356 // Accumulator
2357 @result,
2358 // Init
2359 false~bool,
2360 // LoopCondition
2361 @not_strictly_false(
2362 !_(
2363 @result~bool^@result
2364 )~bool^logical_not
2365 )~bool^not_strictly_false,
2366 // LoopStep
2367 _||_(
2368 @result~bool^@result,
2369 _!=_(
2370 x~int^x,
2371 .x~int^.x
2372 )~bool^not_equals
2373 )~bool^logical_or,
2374 // Result
2375 @result~bool^@result)~bool`,
2376 outType: types.BoolType,
2377 },
2378 {
2379 in: `[{'z': 0}].exists(y, .y.z == y.z)`,
2380 env: testEnv{
2381 idents: []*decls.VariableDecl{
2382 decls.NewVariable("y.z", types.IntType),
2383 },
2384 },
2385 out: `__comprehension__(
2386 // Variable
2387 y,
2388 // Target
2389 [
2390 {
2391 "z"~string:0~int
2392 }~map(string, int)
2393 ]~list(map(string, int)),
2394 // Accumulator
2395 @result,
2396 // Init
2397 false~bool,
2398 // LoopCondition
2399 @not_strictly_false(
2400 !_(
2401 @result~bool^@result
2402 )~bool^logical_not
2403 )~bool^not_strictly_false,
2404 // LoopStep
2405 _||_(
2406 @result~bool^@result,
2407 _==_(
2408 .y.z~int^.y.z,
2409 y~map(string, int)^y.z~int
2410 )~bool^equals
2411 )~bool^logical_or,
2412 // Result
2413 @result~bool^@result)~bool`,
2414 outType: types.BoolType,
2415 },
2416 }
2417 }
2418
2419 func testEnvs(t testing.TB) map[string]testEnv {
2420 return map[string]testEnv{
2421 "default": {
2422 functions: []*decls.FunctionDecl{
2423 testFunction(t, "fg_s",
2424 decls.Overload("fg_s_0", []*types.Type{}, types.StringType)),
2425 testFunction(t, "fi_s_s",
2426 decls.MemberOverload("fi_s_s_0",
2427 []*types.Type{types.StringType}, types.StringType)),
2428 },
2429 idents: []*decls.VariableDecl{
2430 decls.NewVariable("is", types.StringType),
2431 decls.NewVariable("ii", types.IntType),
2432 decls.NewVariable("iu", types.UintType),
2433 decls.NewVariable("iz", types.BoolType),
2434 decls.NewVariable("ib", types.BytesType),
2435 decls.NewVariable("id", types.DoubleType),
2436 decls.NewVariable("ix", types.NullType),
2437 },
2438 },
2439 }
2440 }
2441
2442 type testInfo struct {
2443 // in contains the expression to be parsed.
2444 in string
2445
2446 // out contains the output.
2447 out string
2448
2449 // outType is the expected type of the expression
2450 outType *types.Type
2451
2452 // container is the container name to use for test.
2453 container string
2454
2455 // env is the environment to use for testing.
2456 env testEnv
2457
2458 // err is the expected error for negative test cases.
2459 err string
2460
2461 // disableStdEnv indicates whether the standard functions should be disabled.
2462 disableStdEnv bool
2463
2464 // opts is the set of checker Option flags to use when type-checking.
2465 opts []Option
2466 }
2467
2468 type testEnv struct {
2469 idents []*decls.VariableDecl
2470 functions []*decls.FunctionDecl
2471 variadicASTs bool
2472 optionalSyntax bool
2473 }
2474
2475 func TestCheck(t *testing.T) {
2476 p, err := parser.NewParser(
2477 parser.Macros(parser.AllMacros...),
2478 )
2479 if err != nil {
2480 t.Fatalf("parser.NewParser() failed: %v", err)
2481 }
2482
2483 for i, tst := range testCases(t) {
2484 name := fmt.Sprintf("%d %s", i, tst.in)
2485 tc := tst
2486 t.Run(name, func(t *testing.T) {
2487 tcParser := p
2488 if tc.env.optionalSyntax || tc.env.variadicASTs {
2489 tcParser, err = parser.NewParser(
2490 parser.Macros(parser.AllMacros...),
2491 parser.EnableOptionalSyntax(tc.env.optionalSyntax),
2492 parser.EnableVariadicOperatorASTs(tc.env.variadicASTs),
2493 )
2494 if err != nil {
2495 t.Fatalf("parser.NewParser() failed: %v", err)
2496 }
2497 }
2498 // Runs the tests in parallel to ensure that there are no data races
2499 // due to shared mutable state across tests.
2500 t.Parallel()
2501
2502 src := common.NewTextSource(tc.in)
2503 pAst, errors := tcParser.Parse(src)
2504 if len(errors.GetErrors()) > 0 {
2505 t.Fatalf("Unexpected parse errors: %v", errors.ToDisplayString())
2506 }
2507
2508 reg, err := types.NewRegistry(&proto2pb.TestAllTypes{}, &proto3pb.TestAllTypes{})
2509 if err != nil {
2510 t.Fatalf("types.NewRegistry() failed: %v", err)
2511 }
2512 if tc.env.optionalSyntax {
2513 if err := reg.RegisterType(types.OptionalType); err != nil {
2514 t.Fatalf("reg.RegisterType(optional_type) failed: %v", err)
2515 }
2516 }
2517 cont, err := containers.NewContainer(containers.Name(tc.container))
2518 if err != nil {
2519 t.Fatalf("containers.NewContainer() failed: %v", err)
2520 }
2521 opts := []Option{CrossTypeNumericComparisons(true)}
2522 if len(tc.opts) != 0 {
2523 opts = tc.opts
2524 }
2525 env, err := NewEnv(cont, reg, opts...)
2526 if err != nil {
2527 t.Fatalf("NewEnv(cont, reg) failed: %v", err)
2528 }
2529 if !tc.disableStdEnv {
2530 env.AddFunctions(stdlib.Functions()...)
2531 }
2532 if tc.env.idents != nil {
2533 for _, ident := range tc.env.idents {
2534 env.AddIdents(ident)
2535 }
2536 }
2537 if tc.env.functions != nil {
2538 for _, fn := range tc.env.functions {
2539 env.AddFunctions(fn)
2540 }
2541 }
2542
2543 cAst, errors := Check(pAst, src, env)
2544 if len(errors.GetErrors()) > 0 {
2545 errorString := errors.ToDisplayString()
2546 if tc.err != "" {
2547 if !test.Compare(errorString, tc.err) {
2548 t.Error(test.DiffMessage("Error mismatch", errorString, tc.err))
2549 }
2550 } else {
2551 t.Errorf("Unexpected type-check errors: %v", errorString)
2552 }
2553 } else if tc.err != "" {
2554 t.Errorf("Expected error not thrown: %s", tc.err)
2555 }
2556
2557 astIDs := cAst.IDs()
2558 unusedIDs := []int64{}
2559 for id := range cAst.SourceInfo().OffsetRanges() {
2560 if !astIDs[id] {
2561 unusedIDs = append(unusedIDs, id)
2562 }
2563 }
2564 if len(unusedIDs) > 0 {
2565 t.Errorf("SourceInfo has offset range for IDs %v, but no such nodes exists in AST: %s",
2566 unusedIDs, debug.ToDebugStringWithIDs(cAst.Expr()))
2567 }
2568
2569 actual := cAst.GetType(pAst.Expr().ID())
2570 if tc.err == "" {
2571 if actual == nil || !actual.IsEquivalentType(tc.outType) {
2572 t.Error(test.DiffMessage("Type Error", actual, tc.outType))
2573 }
2574 }
2575
2576 if tc.out != "" {
2577 actualStr := Print(cAst.Expr(), cAst)
2578 if !test.Compare(actualStr, tc.out) {
2579 t.Error(test.DiffMessage("Structure error", actualStr, tc.out))
2580 }
2581 }
2582 })
2583 }
2584 }
2585
2586 func BenchmarkCheck(b *testing.B) {
2587 p, err := parser.NewParser(
2588 parser.Macros(parser.AllMacros...),
2589 )
2590 if err != nil {
2591 b.Fatalf("parser.NewParser() failed: %v", err)
2592 }
2593
2594 for i, tst := range testCases(b) {
2595 name := fmt.Sprintf("%d %s", i, tst.in)
2596 tc := tst
2597 b.Run(name, func(b *testing.B) {
2598 tcParser := p
2599 if tc.env.optionalSyntax || tc.env.variadicASTs {
2600 tcParser, err = parser.NewParser(
2601 parser.Macros(parser.AllMacros...),
2602 parser.EnableOptionalSyntax(tc.env.optionalSyntax),
2603 parser.EnableVariadicOperatorASTs(tc.env.variadicASTs),
2604 )
2605 if err != nil {
2606 b.Fatalf("parser.NewParser() failed: %v", err)
2607 }
2608 }
2609 src := common.NewTextSource(tc.in)
2610 pAst, errors := tcParser.Parse(src)
2611 if len(errors.GetErrors()) > 0 {
2612 b.Fatalf("Unexpected parse errors: %v", errors.ToDisplayString())
2613 }
2614 reg, err := types.NewRegistry(&proto2pb.TestAllTypes{}, &proto3pb.TestAllTypes{})
2615 if err != nil {
2616 b.Fatalf("types.NewRegistry() failed: %v", err)
2617 }
2618 if tc.env.optionalSyntax {
2619 if err := reg.RegisterType(types.OptionalType); err != nil {
2620 b.Fatalf("reg.RegisterType(optional_type) failed: %v", err)
2621 }
2622 }
2623 cont, err := containers.NewContainer(containers.Name(tc.container))
2624 if err != nil {
2625 b.Fatalf("containers.NewContainer() failed: %v", err)
2626 }
2627 opts := []Option{CrossTypeNumericComparisons(true)}
2628 if len(tc.opts) != 0 {
2629 opts = tc.opts
2630 }
2631 env, err := NewEnv(cont, reg, opts...)
2632 if err != nil {
2633 b.Fatalf("NewEnv(cont, reg) failed: %v", err)
2634 }
2635 if !tc.disableStdEnv {
2636 env.AddFunctions(stdlib.Functions()...)
2637 }
2638 if tc.env.idents != nil {
2639 for _, ident := range tc.env.idents {
2640 env.AddIdents(ident)
2641 }
2642 }
2643 if tc.env.functions != nil {
2644 for _, fn := range tc.env.functions {
2645 env.AddFunctions(fn)
2646 }
2647 }
2648 b.ResetTimer()
2649 for i := 0; i < b.N; i++ {
2650 cAst, errors := Check(pAst, src, env)
2651 if len(errors.GetErrors()) > 0 {
2652 errorString := errors.ToDisplayString()
2653 if tc.err != "" {
2654 if !test.Compare(errorString, tc.err) {
2655 b.Error(test.DiffMessage("Error mismatch", errorString, tc.err))
2656 }
2657 } else {
2658 b.Errorf("Unexpected type-check errors: %v", errorString)
2659 }
2660 } else if tc.err != "" {
2661 b.Errorf("Expected error not thrown: %s", tc.err)
2662 }
2663
2664 actual := cAst.GetType(pAst.Expr().ID())
2665 if tc.err == "" {
2666 if actual == nil || !actual.IsEquivalentType(tc.outType) {
2667 b.Error(test.DiffMessage("Type Error", actual, tc.outType))
2668 }
2669 }
2670
2671 if tc.out != "" {
2672 actualStr := Print(cAst.Expr(), cAst)
2673 if !test.Compare(actualStr, tc.out) {
2674 b.Error(test.DiffMessage("Structure error", actualStr, tc.out))
2675 }
2676 }
2677 }
2678 })
2679 }
2680 }
2681
2682 func TestAddDuplicateDeclarations(t *testing.T) {
2683 reg, err := types.NewRegistry(&proto2pb.TestAllTypes{}, &proto3pb.TestAllTypes{})
2684 if err != nil {
2685 t.Fatalf("types.NewRegistry() failed: %v", err)
2686 }
2687 env, err := NewEnv(containers.DefaultContainer, reg, CrossTypeNumericComparisons(true))
2688 if err != nil {
2689 t.Fatalf("NewEnv() failed: %v", err)
2690 }
2691 err = env.AddFunctions(stdlib.Functions()...)
2692 if err != nil {
2693 t.Fatalf("env.Add() failed: %v", err)
2694 }
2695 err = env.AddFunctions(stdlib.Functions()...)
2696 if err != nil {
2697 t.Errorf("env.Add() failed with duplicate declarations: %v", err)
2698 }
2699 }
2700
2701 func TestAddEquivalentDeclarations(t *testing.T) {
2702 reg, err := types.NewRegistry(&proto2pb.TestAllTypes{}, &proto3pb.TestAllTypes{})
2703 if err != nil {
2704 t.Fatalf("types.NewRegistry() failed: %v", err)
2705 }
2706 env, err := NewEnv(containers.DefaultContainer, reg, CrossTypeNumericComparisons(true))
2707 if err != nil {
2708 t.Fatalf("NewEnv() failed: %v", err)
2709 }
2710 optIndex := testFunction(t, "optional_index",
2711 decls.Overload("optional_map_key_value",
2712 []*types.Type{
2713 types.NewMapType(types.NewTypeParamType("K"), types.NewTypeParamType("V")),
2714 types.NewTypeParamType("K")},
2715 types.NewOptionalType(types.NewTypeParamType("V"))))
2716 optIndexEquiv := testFunction(t, "optional_index",
2717 decls.Overload("optional_map_key_value",
2718 []*types.Type{
2719 types.NewMapType(types.NewTypeParamType("K"), types.NewTypeParamType("V")),
2720 types.NewTypeParamType("K")},
2721 types.NewOptionalType(types.NewTypeParamType("V"))))
2722 err = env.AddFunctions(optIndex)
2723 if err != nil {
2724 t.Fatalf("env.Add(optIndex) failed: %v", err)
2725 }
2726 err = env.AddFunctions(optIndexEquiv)
2727 if err != nil {
2728 t.Errorf("env.Add(optIndexEquiv) failed: %v", err)
2729 }
2730 }
2731
2732 func TestCheckErrorData(t *testing.T) {
2733 p, err := parser.NewParser(
2734 parser.EnableOptionalSyntax(true),
2735 parser.Macros(parser.AllMacros...),
2736 )
2737 if err != nil {
2738 t.Fatalf("parser.NewParser() failed: %v", err)
2739 }
2740 src := common.NewTextSource(`a || true`)
2741 ast, iss := p.Parse(src)
2742 if len(iss.GetErrors()) != 0 {
2743 t.Fatalf("Parse() failed: %v", iss.ToDisplayString())
2744 }
2745
2746 reg := newTestRegistry(t)
2747 env, err := NewEnv(containers.DefaultContainer, reg)
2748 if err != nil {
2749 t.Fatalf("NewEnv(cont, reg) failed: %v", err)
2750 }
2751 env.AddFunctions(stdlib.Functions()...)
2752 _, iss = Check(ast, src, env)
2753 if len(iss.GetErrors()) != 1 {
2754 t.Fatalf("Check() of a bad expression did produce a single error: %v", iss.ToDisplayString())
2755 }
2756 celErr := iss.GetErrors()[0]
2757 if celErr.ExprID != 1 {
2758 t.Errorf("got exprID %v, wanted 1", celErr.ExprID)
2759 }
2760 if !strings.Contains(celErr.Message, "undeclared reference") {
2761 t.Errorf("got message %v, wanted undeclared reference", celErr.Message)
2762 }
2763 }
2764
2765 func TestCheckInvalidOptSelectMember(t *testing.T) {
2766 fac := ast.NewExprFactory()
2767 target := fac.NewStruct(1, "Foo", nil)
2768 arg1 := fac.NewStruct(2, "Foo", nil)
2769 arg2 := fac.NewLiteral(3, types.String("field"))
2770 call := fac.NewMemberCall(4, "_?._", target, arg1, arg2)
2771
2772 // This is not valid syntax, just for illustration purposes.
2773 src := common.NewTextSource("Foo{}._?._(Foo{}, 'field')")
2774 parsed := ast.NewAST(call, ast.NewSourceInfo(src))
2775 reg := newTestRegistry(t)
2776 env, err := NewEnv(containers.DefaultContainer, reg)
2777 if err != nil {
2778 t.Fatalf("NewEnv(cont, reg) failed: %v", err)
2779 }
2780 _, iss := Check(parsed, src, env)
2781 if !strings.Contains(iss.ToDisplayString(), "incorrect signature. member call") {
2782 t.Errorf("got %s, wanted 'incorrect signature. member call'", iss.ToDisplayString())
2783 }
2784 }
2785
2786 func TestCheckInvalidOptSelectMissingArg(t *testing.T) {
2787 fac := ast.NewExprFactory()
2788 arg1 := fac.NewStruct(1, "Foo", nil)
2789 call := fac.NewCall(2, "_?._", arg1)
2790
2791 // This is not valid syntax, just for illustration purposes.
2792 src := common.NewTextSource("_?._(Foo{})")
2793 parsed := ast.NewAST(call, ast.NewSourceInfo(src))
2794 reg := newTestRegistry(t)
2795 env, err := NewEnv(containers.DefaultContainer, reg)
2796 if err != nil {
2797 t.Fatalf("NewEnv(cont, reg) failed: %v", err)
2798 }
2799 _, iss := Check(parsed, src, env)
2800 if !strings.Contains(iss.ToDisplayString(), "incorrect signature. argument count: 1") {
2801 t.Errorf("got %s, wanted 'incorrect signature. argument count: 1'", iss.ToDisplayString())
2802 }
2803 }
2804
2805 func TestCheckInvalidLiteral(t *testing.T) {
2806 fac := ast.NewExprFactory()
2807 durLiteral := fac.NewLiteral(1, types.Duration{Duration: time.Second})
2808 // This is not valid syntax, just for illustration purposes.
2809 src := common.NewTextSource(`1s`)
2810 parsed := ast.NewAST(durLiteral, ast.NewSourceInfo(src))
2811 reg := newTestRegistry(t)
2812 env, err := NewEnv(containers.DefaultContainer, reg)
2813 if err != nil {
2814 t.Fatalf("NewEnv(cont, reg) failed: %v", err)
2815 }
2816 _, iss := Check(parsed, src, env)
2817 if !strings.Contains(iss.ToDisplayString(), "unexpected literal type") {
2818 t.Errorf("got %s, wanted 'unexpected literal type'", iss.ToDisplayString())
2819 }
2820 }
2821
2822 func testFunction(t testing.TB, name string, opts ...decls.FunctionOpt) *decls.FunctionDecl {
2823 t.Helper()
2824 fn, err := decls.NewFunction(name, opts...)
2825 if err != nil {
2826 t.Fatalf("decls.NewFunction(%s) failed: %v", name, err)
2827 }
2828 return fn
2829 }