Debian Codegraph @

common/ast/navigable_test.go

raw symbols SCIP
1 // Copyright 2023 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 ast_test
16
17 import (
18 "reflect"
19 "testing"
20
21 "google.golang.org/protobuf/proto"
22
23 "github.com/google/cel-go/checker"
24 "github.com/google/cel-go/common"
25 "github.com/google/cel-go/common/ast"
26 "github.com/google/cel-go/common/containers"
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
32 proto3pb "github.com/google/cel-go/test/proto3pb"
33 )
34
35 func TestNavigateAST(t *testing.T) {
36 tests := []struct {
37 expr string
38 descendantCount int
39 callCount int
40 maxDepth int
41 maxID int64
42 }{
43 {
44 expr: `'a' == 'b'`,
45 descendantCount: 3,
46 callCount: 1,
47 maxDepth: 1,
48 maxID: 4,
49 },
50 {
51 expr: `'a'.size()`,
52 descendantCount: 2,
53 callCount: 1,
54 maxDepth: 1,
55 maxID: 3,
56 },
57 {
58 expr: `[1, 2, 3]`,
59 descendantCount: 4,
60 callCount: 0,
61 maxDepth: 1,
62 maxID: 5,
63 },
64 {
65 expr: `[1, 2, 3][0]`,
66 descendantCount: 6,
67 callCount: 1,
68 maxDepth: 2,
69 maxID: 7,
70 },
71 {
72 expr: `{1u: 'hello'}`,
73 descendantCount: 3,
74 callCount: 0,
75 maxDepth: 1,
76 maxID: 5,
77 },
78 {
79 expr: `{'hello': 'world'}.hello`,
80 descendantCount: 4,
81 callCount: 0,
82 maxDepth: 2,
83 maxID: 6,
84 },
85 {
86 expr: `type(1) == int`,
87 descendantCount: 4,
88 callCount: 2,
89 maxDepth: 2,
90 maxID: 5,
91 },
92 {
93 expr: `google.expr.proto3.test.TestAllTypes{single_int32: 1}`,
94 descendantCount: 2,
95 callCount: 0,
96 maxDepth: 1,
97 maxID: 4,
98 },
99 {
100 expr: `[true].exists(i, i)`,
101 descendantCount: 11, // 2 for iter range, 1 for accu init, 4 for loop condition, 3 for loop step, 1 for result
102 callCount: 3, // @not_strictly_false(!result), accu_init || i
103 maxDepth: 3,
104 maxID: 14,
105 },
106 }
107
108 for _, tst := range tests {
109 tc := tst
110 t.Run(tc.expr, func(t *testing.T) {
111 checked := mustTypeCheck(t, tc.expr)
112 nav := ast.NavigateAST(checked)
113 descendants := ast.MatchDescendants(nav, ast.AllMatcher())
114 if len(descendants) != tc.descendantCount {
115 t.Errorf("ast.MatchDescendants(%v) got %d descendants, wanted %d", checked.Expr(), len(descendants), tc.descendantCount)
116 }
117 maxDepth := 0
118 for _, d := range descendants {
119 if d.Depth() > maxDepth {
120 maxDepth = d.Depth()
121 }
122 }
123 if maxDepth != tc.maxDepth {
124 t.Errorf("got max NavigableExpr.Depth() of %d, wanted %d", maxDepth, tc.maxDepth)
125 }
126 maxID := ast.MaxID(checked)
127 if maxID != tc.maxID {
128 t.Errorf("got max id %d, wanted %d", maxID, tc.maxID)
129 }
130 calls := ast.MatchSubset(descendants, ast.KindMatcher(ast.CallKind))
131 if len(calls) != tc.callCount {
132 t.Errorf("ast.MatchSubset(%v) got %d calls, wanted %d", checked.Expr(), len(calls), tc.callCount)
133 }
134 })
135 }
136 }
137
138 func TestExprVisitor(t *testing.T) {
139 tests := []struct {
140 expr string
141 preOrderIDs []int64
142 postOrderIDs []int64
143 }{
144 {
145 // [2] ==, [1] 'a', [3] 'b'
146 expr: `'a' == 'b'`,
147 preOrderIDs: []int64{2, 1, 3},
148 postOrderIDs: []int64{1, 3, 2},
149 },
150 {
151 // [2] size(), [1] 'a'
152 expr: `'a'.size()`,
153 preOrderIDs: []int64{2, 1},
154 postOrderIDs: []int64{1, 2},
155 },
156 {
157 // [3] ==, [1] type(), [2] 1, [4] int
158 expr: `type(1) == int`,
159 preOrderIDs: []int64{3, 1, 2, 4},
160 postOrderIDs: []int64{2, 1, 4, 3},
161 },
162 {
163 // [5] .hello, [1] {}, [3] 'hello', [4] 'world'
164 expr: `{'hello': 'world'}.hello`,
165 preOrderIDs: []int64{5, 1, 3, 4},
166 postOrderIDs: []int64{3, 4, 1, 5},
167 },
168 {
169 // [1] TestAllTypes, [3] 1
170 expr: `google.expr.proto3.test.TestAllTypes{single_int32: 1}`,
171 preOrderIDs: []int64{1, 3},
172 postOrderIDs: []int64{3, 1},
173 },
174 {
175 // [13] comprehension
176 // range: [1] [], [2] true
177 // accuInit: [6] result=false
178 // loopCond: [9] @not_strictly_false, [8] !, [7] result
179 // loopStep: [11] ||, [10] result [5] i
180 // result: [12] result
181 expr: `[true].exists(i, i)`,
182 preOrderIDs: []int64{13, 1, 2, 6, 9, 8, 7, 11, 10, 5, 12},
183 postOrderIDs: []int64{2, 1, 6, 7, 8, 9, 10, 5, 11, 12, 13},
184 },
185 }
186
187 for _, tst := range tests {
188 tc := tst
189 t.Run(tc.expr, func(t *testing.T) {
190 checked := mustTypeCheck(t, tc.expr)
191 root := ast.NavigateAST(checked)
192 // Verify pre order visit behavior
193 preOrderExprIDs := []int64{}
194 navVisitor := ast.NewExprVisitor(func(e ast.Expr) {
195 nav := e.(ast.NavigableExpr)
196 preOrderExprIDs = append(preOrderExprIDs, nav.ID())
197 })
198 ast.PreOrderVisit(root, navVisitor)
199 if !reflect.DeepEqual(tc.preOrderIDs, preOrderExprIDs) {
200 t.Errorf("PreOrderVisit() got %v expressions, wanted %v", tc.preOrderIDs, preOrderExprIDs)
201 }
202
203 // Demonstrate preOrder visit behavior with Children()
204 preOrderExprIDs = []int64{}
205 visited := []ast.NavigableExpr{root}
206 for len(visited) > 0 {
207 e := visited[0]
208 preOrderExprIDs = append(preOrderExprIDs, e.ID())
209 visited = append(e.Children()[:], visited[1:]...)
210 }
211 if !reflect.DeepEqual(tc.preOrderIDs, preOrderExprIDs) {
212 t.Errorf("PreOrderVisit() got %v expressions, wanted %v", tc.preOrderIDs, preOrderExprIDs)
213 }
214
215 // Verify post order visit behavior.
216 postOrderExprIDs := []int64{}
217 navVisitor = ast.NewExprVisitor(func(e ast.Expr) {
218 nav := e.(ast.NavigableExpr)
219 postOrderExprIDs = append(postOrderExprIDs, nav.ID())
220 })
221 ast.PostOrderVisit(root, navVisitor)
222 if !reflect.DeepEqual(tc.postOrderIDs, postOrderExprIDs) {
223 t.Errorf("PostOrderVisit() got %v expressions, wanted %v", tc.postOrderIDs, postOrderExprIDs)
224 }
225 })
226 }
227 }
228
229 func TestNavigableASTNilSafety(t *testing.T) {
230 tests := []struct {
231 name string
232 e ast.NavigableExpr
233 }{
234 {
235 name: "nil expr",
236 e: ast.NavigateAST(ast.NewAST(nil, nil)),
237 },
238 }
239 for _, tst := range tests {
240 tc := tst
241 t.Run(tc.name, func(t *testing.T) {
242 e := tc.e
243 if e.ID() != 0 {
244 t.Errorf("ID() got %d, wanted 0", e.ID())
245 }
246 if e.Kind() != ast.UnspecifiedExprKind {
247 t.Errorf("Kind() got %v, wanted unspecified kind", e.Kind())
248 }
249 if e.Type() != types.DynType {
250 t.Errorf("Type() got %v, wanted types.DynType", e.Type())
251 }
252 if p, found := e.Parent(); found {
253 t.Errorf("Parent() got %v, wanted not found", p)
254 }
255 if len(e.Children()) != 0 {
256 t.Errorf("Children() got %v, wanted none", e.Children())
257 }
258 if e.AsLiteral() != nil {
259 t.Errorf("AsLiteral() got %v, wanted nil", e.AsLiteral())
260 }
261 if e.AsCall() == nil {
262 t.Errorf("AsCall() got nil, wanted non-nil for safe traversal")
263 }
264 if e.AsComprehension() == nil {
265 t.Errorf("AsComprehension() got nil, wanted non-nil for safe traversal")
266 }
267 })
268 }
269 }
270
271 func TestNavigableExpr(t *testing.T) {
272 checkedAST := mustTypeCheck(t, `'a' == 'b'`)
273 navAST := ast.NavigateAST(checkedAST)
274 literals := ast.MatchDescendants(navAST, func(expr ast.NavigableExpr) bool {
275 return expr.Kind() == ast.LiteralKind &&
276 expr.AsLiteral().Equal(types.String("a")) == types.True
277 })
278 if len(literals) != 1 {
279 t.Fatalf("MatchDescendents('a') got %d results, wanted 1", len(literals))
280 }
281 litA := literals[0]
282 if litA.Depth() != 1 {
283 t.Fatalf("litA.Depth() got %d, wanted 1", litA.Depth())
284 }
285 parent, found := litA.Parent()
286 if !found {
287 t.Fatal("litA.Parent() returned nil")
288 }
289 if parent.Kind() != ast.CallKind && parent.AsCall().FunctionName() != "==" {
290 t.Fatalf("litA.Parent() got %v, watned '==' function call", parent)
291 }
292 litAPrime := ast.NavigateExpr(checkedAST, litA)
293 if litAPrime.Depth() != litA.Depth() {
294 t.Errorf("litAPrime.Depth() != litA.Depth(), got %d, wanted %d", litAPrime.Depth(), litA.Depth())
295 }
296 }
297
298 func TestNavigableCallExprMember(t *testing.T) {
299 checked := mustTypeCheck(t, `'hello'.size()`)
300 expr := ast.NavigateAST(checked)
301 if expr.Kind() != ast.CallKind {
302 t.Errorf("Kind() got %v, wanted CallKind", expr.Kind())
303 }
304 call := expr.AsCall()
305 if call.FunctionName() != "size" {
306 t.Errorf("FunctionName() got %s, wanted size", call.FunctionName())
307 }
308 if call.Target() == nil {
309 t.Fatalf("Target() got nil, wanted non-nil")
310 }
311 if len(call.Args()) != 0 {
312 t.Errorf("Args() got %v, wanted 0", call.Args())
313 }
314 target := call.Target()
315 if target.Kind() != ast.LiteralKind {
316 t.Errorf("Kind() got %v, wanted literal", target.Kind())
317 }
318 if target.AsLiteral().Equal(types.String("hello")) != types.True {
319 t.Errorf("AsLiteral() got %v, wanted 'hello'", target.AsLiteral())
320 }
321 if p, found := target.(ast.NavigableExpr).Parent(); !found || p != expr {
322 t.Errorf("Parent() got %v, wanted %v", p, expr)
323 }
324 sizeFn := ast.MatchDescendants(expr, ast.FunctionMatcher("size"))
325 if len(sizeFn) != 1 {
326 t.Errorf("ast.MatchDescendants() size function returned %v, wanted 1", sizeFn)
327 }
328 constantValues := ast.MatchDescendants(expr, ast.ConstantValueMatcher())
329 if len(constantValues) != 1 {
330 t.Fatalf("ast.MatchDescendants() constant values returned %v, wanted 1 value", constantValues)
331 }
332 if constantValues[0].AsLiteral().Equal(types.String("hello")) != types.True {
333 t.Errorf("constantValues[0] got %v, wanted 'hello'", constantValues[0])
334 }
335 }
336
337 func TestNavigableCallExprGlobal(t *testing.T) {
338 checked := mustTypeCheck(t, `size('hello')`)
339 expr := ast.NavigateAST(checked)
340 if expr.Kind() != ast.CallKind {
341 t.Errorf("Kind() got %v, wanted CallKind", expr.Kind())
342 }
343 call := expr.AsCall()
344 if call.FunctionName() != "size" {
345 t.Errorf("FunctionName() got %s, wanted size", call.FunctionName())
346 }
347 if call.IsMemberFunction() {
348 t.Fatal("IsMemberFunction() returned true, wanted false")
349 }
350 if len(call.Args()) != 1 {
351 t.Errorf("Args() got %v, wanted 1", call.Args())
352 }
353 arg := call.Args()[0]
354 if arg.Kind() != ast.LiteralKind {
355 t.Errorf("Kind() got %v, wanted literal", arg.Kind())
356 }
357 if arg.AsLiteral().Equal(types.String("hello")) != types.True {
358 t.Errorf("AsLiteral() got %v, wanted 'hello'", arg.AsLiteral())
359 }
360 if p, found := arg.(ast.NavigableExpr).Parent(); !found || p != expr {
361 t.Errorf("Parent() got %v, wanted %v", p, expr)
362 }
363 sizeFn := ast.MatchDescendants(expr, ast.FunctionMatcher("size"))
364 if len(sizeFn) != 1 {
365 t.Errorf("ast.MatchDescendants() size function returned %v, wanted 1", sizeFn)
366 }
367 constantValues := ast.MatchDescendants(expr, ast.ConstantValueMatcher())
368 if len(constantValues) != 1 {
369 t.Fatalf("ast.MatchDescendants() constant values returned %v, wanted 1 value", constantValues)
370 }
371 if constantValues[0].AsLiteral().Equal(types.String("hello")) != types.True {
372 t.Errorf("constantValues[0] got %v, wanted 'hello'", constantValues[0])
373 }
374 }
375
376 func TestNavigableListExpr(t *testing.T) {
377 checked := mustTypeCheck(t, `[[1], [2]]`)
378 expr := ast.NavigateAST(checked)
379 if expr.Kind() != ast.ListKind {
380 t.Errorf("Kind() got %v, wanted ListKind", expr.Kind())
381 }
382 list := expr.AsList()
383 if list.Size() != 2 {
384 t.Errorf("Size() got %d, wanted 2", list.Size())
385 }
386 if len(list.OptionalIndices()) != 0 {
387 t.Errorf("OptionalIndicies() returned %v, wanted none", list.OptionalIndices())
388 }
389 if len(list.Elements()) != 2 {
390 t.Errorf("Elements() returned %v, wanted 2", list.Elements())
391 }
392 constantValues := ast.MatchDescendants(expr, ast.ConstantValueMatcher())
393 if len(constantValues) != 5 {
394 t.Errorf("ast.MatchDescendants() constant values returned %v, wanted 5", constantValues)
395 }
396 constantLists := ast.MatchSubset(constantValues, ast.KindMatcher(ast.ListKind))
397 if len(constantLists) != 3 {
398 t.Errorf("ast.MatchSubset() constant lists returned %v, wanted 3", constantLists)
399 }
400 literals := ast.MatchSubset(constantValues, ast.KindMatcher(ast.LiteralKind))
401 if len(literals) != 2 {
402 t.Errorf("ast.MatchSubset() literals returned %v, wanted 2", literals)
403 }
404 }
405
406 func TestNavigableMapExpr(t *testing.T) {
407 checked := mustTypeCheck(t, `{'hello': 1}`)
408 expr := ast.NavigateAST(checked)
409 if expr.Kind() != ast.MapKind {
410 t.Errorf("Kind() got %v, wanted MapKind", expr.Kind())
411 }
412 m := expr.AsMap()
413 if m.Size() != 1 {
414 t.Errorf("Size() got %d, wanted 1", m.Size())
415 }
416 if len(m.Entries()) != 1 {
417 t.Errorf("Entries() returned %v, wanted 1", m.Entries())
418 }
419 e := m.Entries()[0]
420 entry := e.AsMapEntry()
421 if entry.IsOptional() {
422 t.Error("IsOptional() returned true, wanted false")
423 }
424 if entry.Key().AsLiteral().Equal(types.String("hello")) != types.True {
425 t.Errorf("Key() returned %v, wanted 'hello'", entry.Key().AsLiteral())
426 }
427 if entry.Value().AsLiteral().Equal(types.Int(1)) != types.True {
428 t.Errorf("Value() returned %v, wanted 1", entry.Value().AsLiteral())
429 }
430 descendants := ast.MatchDescendants(expr, ast.AllMatcher())
431 if len(descendants) != 3 {
432 t.Errorf("ast.MatchDescendants() returned %v, wanted 3", descendants)
433 }
434 literals := ast.MatchSubset(descendants, ast.KindMatcher(ast.LiteralKind))
435 if len(literals) != 2 {
436 t.Errorf("ast.MatchSubset() literals returned %v, wanted 2", literals)
437 }
438 }
439
440 func TestNavigableStructExpr(t *testing.T) {
441 checked := mustTypeCheck(t, `google.expr.proto3.test.TestAllTypes{single_int32: 1}`)
442 expr := ast.NavigateAST(checked)
443 if expr.Kind() != ast.StructKind {
444 t.Errorf("Kind() got %v, wanted StructKind", expr.Kind())
445 }
446 s := expr.AsStruct()
447 if s.TypeName() != "google.expr.proto3.test.TestAllTypes" {
448 t.Errorf("TypeName() got %s, wanted TestAllTypes", s.TypeName())
449 }
450 if len(s.Fields()) != 1 {
451 t.Errorf("Fields() returned %v, wanted 1", s.Fields())
452 }
453 f := s.Fields()[0]
454 field := f.AsStructField()
455 if field.IsOptional() {
456 t.Error("IsOptional() returned true, wanted false")
457 }
458 if field.Name() != "single_int32" {
459 t.Errorf("Name() returned %s, wanted 'single_int32'", field.Name())
460 }
461 if field.Value().AsLiteral().Equal(types.Int(1)) != types.True {
462 t.Errorf("Value() returned %v, wanted 1", field.Value().AsLiteral())
463 }
464 descendants := ast.MatchDescendants(expr, ast.AllMatcher())
465 if len(descendants) != 2 {
466 t.Errorf("ast.MatchDescendants() returned %v, wanted 2", descendants)
467 }
468 literals := ast.MatchSubset(descendants, ast.KindMatcher(ast.LiteralKind))
469 if len(literals) != 1 {
470 t.Errorf("ast.MatchSubset() literals returned %v, wanted 1", literals)
471 }
472 if literals[0].AsLiteral().Equal(types.Int(1)) != types.True {
473 t.Errorf("Value().AsLiteral() got %v, wanted 1", literals[0].AsLiteral())
474 }
475 }
476
477 func TestNavigableComprehensionExpr(t *testing.T) {
478 checked := mustTypeCheck(t, `[true].exists(i, i)`)
479 expr := ast.NavigateAST(checked)
480 if expr.Kind() != ast.ComprehensionKind {
481 t.Errorf("Kind() got %v, wanted ComprehensionKind", expr.Kind())
482 }
483 comp := expr.AsComprehension()
484 iterRange := comp.IterRange()
485 if len(ast.MatchSubset([]ast.NavigableExpr{iterRange.(ast.NavigableExpr)}, ast.ConstantValueMatcher())) != 1 {
486 t.Errorf("IterRange() returned a non-constant list")
487 }
488 if comp.IterVar() != "i" {
489 t.Errorf("IterVar() got %s, wanted 'i'", comp.IterVar())
490 }
491 if comp.HasIterVar2() {
492 t.Error("HasIterVar2() returned true, wanted false")
493 }
494 if comp.IterVar2() != "" {
495 t.Errorf("IterVar2() returned %s, wanted empty string", comp.IterVar2())
496 }
497 if comp.AccuVar() != "@result" {
498 t.Errorf("AccuVar() got %s, wanted '@result'", comp.AccuVar())
499 }
500 if comp.AccuInit().AsLiteral() != types.False {
501 t.Errorf("AccuInit() returned %v, wanted false", comp.AccuInit().AsLiteral())
502 }
503 if comp.Result().Kind() != ast.IdentKind {
504 t.Errorf("Result() returned %v, wanted ident", comp.Result())
505 }
506 if comp.LoopCondition().Kind() != ast.CallKind {
507 t.Errorf("LoopCondition() returned %v, wanted call", comp.LoopCondition())
508 }
509 if comp.LoopStep().Kind() != ast.CallKind {
510 t.Errorf("LoopStep() returned %v, wanted call", comp.LoopStep())
511 }
512 if comp.Result().AsIdent() != "@result" {
513 t.Errorf("AsIdent() returned %v, wanted @result", comp.Result().AsIdent())
514 }
515 }
516
517 func TestNavigableSelectExpr(t *testing.T) {
518 checked := mustTypeCheck(t, `msg.single_int32`)
519 expr := ast.NavigateAST(checked)
520 if expr.Kind() != ast.SelectKind {
521 t.Errorf("Kind() got %v, wanted SelectKind", expr.Kind())
522 }
523 sel := expr.AsSelect()
524 if sel.FieldName() != "single_int32" {
525 t.Errorf("FieldName() got %s, wanted single_int32", sel.FieldName())
526 }
527 if sel.Operand().Kind() != ast.IdentKind {
528 t.Fatalf("Operand() kind got %v, wanted ident", sel.Operand().Kind())
529 }
530 if sel.Operand().AsIdent() != "msg" {
531 t.Errorf("Operand() got %v, wanted ident 'msg'", sel.Operand())
532 }
533 if sel.IsTestOnly() {
534 t.Error("IsTestOnly() got true, wanted false")
535 }
536 }
537
538 func TestNavigableSelectExpr_TestOnly(t *testing.T) {
539 checked := mustTypeCheck(t, `has(msg.single_int32)`)
540 expr := ast.NavigateAST(checked)
541 if expr.Kind() != ast.SelectKind {
542 t.Errorf("Kind() got %v, wanted SelectKind", expr.Kind())
543 }
544 sel := expr.AsSelect()
545 if sel.FieldName() != "single_int32" {
546 t.Errorf("FieldName() got %s, wanted single_int32", sel.FieldName())
547 }
548 if sel.Operand().Kind() != ast.IdentKind {
549 t.Fatalf("Operand() kind got %v, wanted ident", sel.Operand().Kind())
550 }
551 if sel.Operand().AsIdent() != "msg" {
552 t.Errorf("Operand() got %v, wanted ident 'msg'", sel.Operand())
553 }
554 if !sel.IsTestOnly() {
555 t.Error("IsTestOnly() got false, wanted true")
556 }
557 }
558
559 func mustTypeCheck(t testing.TB, expr string) *ast.AST {
560 t.Helper()
561 p, err := parser.NewParser(
562 parser.Macros(parser.AllMacros...),
563 parser.EnableOptionalSyntax(true),
564 parser.PopulateMacroCalls(true))
565 if err != nil {
566 t.Fatalf("parser.NewParser() failed: %v", err)
567 }
568 exprSrc := common.NewTextSource(expr)
569 parsed, iss := p.Parse(exprSrc)
570 if len(iss.GetErrors()) != 0 {
571 t.Fatalf("Parse(%s) failed: %s", expr, iss.ToDisplayString())
572 }
573 reg := newTestRegistry(t, &proto3pb.TestAllTypes{})
574 env := newTestEnv(t, containers.DefaultContainer, reg)
575 checked, iss := checker.Check(parsed, exprSrc, env)
576 if len(iss.GetErrors()) != 0 {
577 t.Fatalf("Check(%s) failed: %s", expr, iss.ToDisplayString())
578 }
579 return checked
580 }
581
582 func newTestRegistry(t testing.TB, msgs ...proto.Message) *types.Registry {
583 t.Helper()
584 reg, err := types.NewRegistry(msgs...)
585 if err != nil {
586 t.Fatalf("types.NewRegistry(%v) failed: %v", msgs, err)
587 }
588 return reg
589 }
590
591 func newTestEnv(t testing.TB, cont *containers.Container, reg *types.Registry) *checker.Env {
592 t.Helper()
593 env, err := checker.NewEnv(cont, reg, checker.CrossTypeNumericComparisons(true))
594 if err != nil {
595 t.Fatalf("checker.NewEnv(%v, %v) failed: %v", cont, reg, err)
596 }
597 err = env.AddFunctions(stdlib.Functions()...)
598 if err != nil {
599 t.Fatalf("env.Add(stdlib.Functions()...) failed: %v", err)
600 }
601 env.AddIdents(decls.NewVariable("msg", types.NewObjectType("google.expr.proto3.test.TestAllTypes")))
602 return env
603 }