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

Function TestStaticOptimizerNewAST

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

Source from the content-addressed store, hash-verified

165}
166
167func TestStaticOptimizerNewAST(t *testing.T) {
168 tests := []string{
169 `[3, 2, 1]`,
170 `[1, 2, 3].all(i, i != 0)`,
171 `cel.bind(m, {"a": 1, "b": 2}, m.filter(k, m[k] > 1))`,
172 }
173 for _, tst := range tests {
174 tc := tst
175 t.Run(tc, func(t *testing.T) {
176 e := optimizerEnv(t)
177 exprAST, iss := e.Compile(tc)
178 if iss.Err() != nil {
179 t.Fatalf("Compile(%q) failed: %v", tc, iss.Err())
180 }
181 opt, err := cel.NewStaticOptimizer(&identityOptimizer{t: t})
182 if err != nil {
183 t.Fatalf("NewStaticOptimizer() failed: %v", err)
184 }
185 optAST, iss := opt.Optimize(e, exprAST)
186 if iss.Err() != nil {
187 t.Fatalf("Optimize() generated an invalid AST: %v", iss.Err())
188 }
189 optString, err := cel.AstToString(optAST)
190 if err != nil {
191 t.Fatalf("cel.AstToString() failed: %v", err)
192 }
193 if tc != optString {
194 t.Errorf("identity optimizer got %q, wanted %q", optString, tc)
195 }
196 })
197 }
198}
199
200func TestOptimizeWithSource(t *testing.T) {
201 initial := `has(a.b)`

Callers

nothing calls this directly

Calls 6

OptimizeMethod · 0.95
NewStaticOptimizerFunction · 0.92
AstToStringFunction · 0.92
optimizerEnvFunction · 0.85
ErrMethod · 0.80
CompileMethod · 0.65

Tested by

no test coverage detected