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

Function TestOptimizeWithSource

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

Source from the content-addressed store, hash-verified

198}
199
200func TestOptimizeWithSource(t *testing.T) {
201 initial := `has(a.b)`
202 replacement := `x["a"]`
203 e := optimizerEnv(t)
204 initialAST, iss := e.Compile(initial)
205 if iss.Err() != nil {
206 t.Fatalf("Compile(%q) failed: %v", initial, iss.Err())
207 }
208 replacementAST, iss := e.Compile(replacement)
209 if iss.Err() != nil {
210 t.Fatalf("Compile(%q) failed: %v", replacement, iss.Err())
211 }
212
213 opt, err := cel.NewStaticOptimizer(
214 &replaceOptimizer{t: t, targetAST: replacementAST.NativeRep()},
215 cel.OptimizeWithSource(replacementAST.Source()),
216 )
217 if err != nil {
218 t.Fatalf("NewStaticOptimizer() failed: %v", err)
219 }
220 optAST, iss := opt.Optimize(e, initialAST)
221 if iss.Err() != nil {
222 t.Fatalf("Optimize() returned an error: %v", iss.Err())
223 }
224
225 if optAST.Source().Content() != replacement {
226 t.Errorf("got source content %q, wanted %q", optAST.Source().Content(), replacement)
227 }
228 sourceInfoPB, err := ast.SourceInfoToProto(optAST.NativeRep().SourceInfo())
229 if err != nil {
230 t.Fatalf("cel.AstToCheckedExpr() failed: %v", err)
231 }
232 wantTextPB := `
233 location: "<input>"
234 line_offsets: 7
235 positions: {
236 key: 1
237 value: 1
238 }
239 positions: {
240 key: 2
241 value: 0
242 }
243 positions: {
244 key: 3
245 value: 2
246 }
247 `
248 var wantSourceInfoPB exprpb.SourceInfo
249 if err := prototext.Unmarshal([]byte(wantTextPB), &wantSourceInfoPB); err != nil {
250 t.Fatalf("prototext.Unmarshal() failed: %v", err)
251 }
252 if !proto.Equal(&wantSourceInfoPB, sourceInfoPB) {
253 t.Errorf("got source info: %s, wanted %s", prototext.Format(sourceInfoPB), wantTextPB)
254 }
255}
256
257func TestStaticOptimizerNilAST(t *testing.T) {

Callers

nothing calls this directly

Calls 12

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

Tested by

no test coverage detected