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

Function TestContextEval

cel/cel_test.go:1237–1274  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

1235}
1236
1237func TestContextEval(t *testing.T) {
1238 env := testEnv(t, Variable("items", ListType(IntType)))
1239 ast, iss := env.Compile("items.map(i, i * 2).filter(i, i >= 50).size()")
1240 if iss.Err() != nil {
1241 t.Fatalf("env.Compile(expr) failed: %v", iss.Err())
1242 }
1243 prg, err := env.Program(ast, EvalOptions(OptOptimize|OptTrackState), InterruptCheckFrequency(100))
1244 if err != nil {
1245 t.Fatalf("env.Program() failed: %v", err)
1246 }
1247
1248 ctx := context.TODO()
1249 items := make([]int64, 2000)
1250 for i := int64(0); i < 2000; i++ {
1251 items[i] = i
1252 }
1253 out, _, err := prg.ContextEval(ctx, map[string]any{"items": items})
1254 if err != nil {
1255 t.Fatalf("prg.ContextEval() failed: %v", err)
1256 }
1257 if out != types.Int(1975) {
1258 t.Errorf("prg.ContextEval() got %v, wanted 1975", out)
1259 }
1260
1261 evalCtx, cancel := context.WithTimeout(ctx, time.Microsecond)
1262 defer cancel()
1263
1264 out, _, err = prg.ContextEval(evalCtx, map[string]any{"items": items})
1265 if err == nil {
1266 t.Errorf("Got result %v, wanted timeout error", out)
1267 }
1268 if err != nil && !errors.Is(err, context.DeadlineExceeded) {
1269 t.Errorf("Got %v, wanted context deadline exceeded", err)
1270 }
1271 if err != nil && !strings.Contains(err.Error(), "operation interrupted") {
1272 t.Errorf("Got %v, wanted error containing 'operation interrupted'", err)
1273 }
1274}
1275
1276func TestContextEvalUnknowns(t *testing.T) {
1277 env, err := NewEnv(

Callers

nothing calls this directly

Calls 12

IntTypeAlias · 0.92
EvalOptionsFunction · 0.85
InterruptCheckFrequencyFunction · 0.85
ErrMethod · 0.80
ProgramMethod · 0.80
testEnvFunction · 0.70
VariableFunction · 0.70
CompileMethod · 0.65
ContextEvalMethod · 0.65
ContainsMethod · 0.65
IsMethod · 0.45
ErrorMethod · 0.45

Tested by

no test coverage detected