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

Function TestResidualAstModified

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

Source from the content-addressed store, hash-verified

2247}
2248
2249func TestResidualAstModified(t *testing.T) {
2250 env := testEnv(t,
2251 Variable("x", MapType(StringType, IntType)),
2252 Variable("y", IntType),
2253 )
2254 ast, iss := env.Parse("x == y")
2255 if iss.Err() != nil {
2256 t.Fatalf("env.Parse() failed: %v", iss.Err())
2257 }
2258 prg, err := env.Program(ast,
2259 EvalOptions(OptTrackState, OptPartialEval),
2260 )
2261 if err != nil {
2262 t.Fatalf("env.Program() failed: %v", err)
2263 }
2264 for _, x := range []int{123, 456} {
2265 vars, _ := PartialVars(map[string]any{
2266 "x": x,
2267 }, AttributePattern("y"))
2268 out, det, err := prg.Eval(vars)
2269 if !types.IsUnknown(out) {
2270 t.Fatalf("got %v, expected unknown", out)
2271 }
2272 if err != nil {
2273 t.Fatal(err)
2274 }
2275 residual, err := env.ResidualAst(ast, det)
2276 if err != nil {
2277 t.Fatal(err)
2278 }
2279 orig, err := AstToString(ast)
2280 if err != nil {
2281 t.Fatal(err)
2282 }
2283 if orig != "x == y" {
2284 t.Errorf("parsed ast: got expr: %s, wanted x == y", orig)
2285 }
2286 expr, err := AstToString(residual)
2287 if err != nil {
2288 t.Fatal(err)
2289 }
2290 want := fmt.Sprintf("%d == y", x)
2291 if expr != want {
2292 t.Errorf("residual ast: got expr: %s, wanted %s", expr, want)
2293 }
2294 }
2295}
2296
2297func TestContextProto(t *testing.T) {
2298 descriptor := new(proto3pb.TestAllTypes).ProtoReflect().Descriptor()

Callers

nothing calls this directly

Calls 12

IsUnknownFunction · 0.92
EvalOptionsFunction · 0.85
PartialVarsFunction · 0.85
AstToStringFunction · 0.85
ErrMethod · 0.80
ProgramMethod · 0.80
ResidualAstMethod · 0.80
testEnvFunction · 0.70
VariableFunction · 0.70
AttributePatternFunction · 0.70
ParseMethod · 0.65
EvalMethod · 0.65

Tested by

no test coverage detected