MCPcopy Create free account
hub / github.com/cel-expr/cel-go / TestStaticOptimizerUpdateExpr

Function TestStaticOptimizerUpdateExpr

cel/optimizer_test.go:34–165  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

32)
33
34func TestStaticOptimizerUpdateExpr(t *testing.T) {
35 expr := `has(a.b)`
36 inlined := `[x, y].filter(i, i.size() > 0)[0].z`
37
38 e := optimizerEnv(t)
39 exprAST, iss := e.Compile(expr)
40 if iss.Err() != nil {
41 t.Fatalf("Compile() failed: %v", iss.Err())
42 }
43
44 inlinedAST, iss := e.Compile(inlined)
45 if iss.Err() != nil {
46 t.Fatalf("Compile() failed: %v", iss.Err())
47 }
48 opt, err := cel.NewStaticOptimizer(&testOptimizer{t: t, inlineExpr: inlinedAST.NativeRep()})
49 if err != nil {
50 t.Fatalf("NewStaticOptimizer() failed: %v", err)
51 }
52 optAST, iss := opt.Optimize(e, exprAST)
53 if iss.Err() != nil {
54 t.Fatalf("Optimize() generated an invalid AST: %v", iss.Err())
55 }
56 optString, err := cel.AstToString(optAST)
57 if err != nil {
58 t.Fatalf("cel.AstToString() failed: %v", err)
59 }
60 sourceInfo := optAST.NativeRep().SourceInfo()
61 sourceInfoPB, err := ast.SourceInfoToProto(sourceInfo)
62 if err != nil {
63 t.Fatalf("cel.AstToCheckedExpr() failed: %v", err)
64 }
65 sourceInfoPB.Positions = nil
66 wantTextPB := `
67 location: "<input>"
68 line_offsets: 9
69 macro_calls: {
70 key: 1
71 value: {
72 call_expr: {
73 function: "has"
74 args: {
75 id: 24
76 select_expr: {
77 operand: {
78 id: 2
79 call_expr: {
80 function: "_[_]"
81 args: {
82 id: 3
83 }
84 args: {
85 id: 20
86 const_expr: {
87 int64_value: 0
88 }
89 }
90 }
91 }

Callers

nothing calls this directly

Calls 10

OptimizeMethod · 0.95
NewStaticOptimizerFunction · 0.92
AstToStringFunction · 0.92
SourceInfoToProtoFunction · 0.92
optimizerEnvFunction · 0.85
ErrMethod · 0.80
NativeRepMethod · 0.80
CompileMethod · 0.65
EqualMethod · 0.65
SourceInfoMethod · 0.45

Tested by

no test coverage detected