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

Function TestResidualAstComplex

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

Source from the content-addressed store, hash-verified

1412}
1413
1414func TestResidualAstComplex(t *testing.T) {
1415 env := testEnv(t,
1416 Variable("resource.name", StringType),
1417 Variable("request.time", TimestampType),
1418 Variable("request.auth.claims", MapType(StringType, StringType)),
1419 )
1420 unkVars, _ := PartialVars(
1421 map[string]any{
1422 "resource.name": "bucket/my-bucket/objects/private",
1423 "request.auth.claims": map[string]string{
1424 "email_verified": "true",
1425 },
1426 },
1427 AttributePattern("request.auth.claims").QualString("email"),
1428 )
1429 ast, iss := env.Compile(
1430 `resource.name.startsWith("bucket/my-bucket") &&
1431 bool(request.auth.claims.email_verified) == true &&
1432 request.auth.claims.email == "wiley@acme.co"`)
1433 if iss.Err() != nil {
1434 t.Fatalf("env.Compile() failed: %v", iss.Err())
1435 }
1436 prg, err := env.Program(ast,
1437 EvalOptions(OptTrackState, OptPartialEval),
1438 )
1439 if err != nil {
1440 t.Fatalf("env.Program() failed: %v", err)
1441 }
1442 out, det, err := prg.Eval(unkVars)
1443 if !types.IsUnknown(out) {
1444 t.Fatalf("got %v, expected unknown", out)
1445 }
1446 if err != nil {
1447 t.Fatal(err)
1448 }
1449 residual, err := env.ResidualAst(ast, det)
1450 if err != nil {
1451 t.Fatal(err)
1452 }
1453 expr, err := AstToString(residual)
1454 if err != nil {
1455 t.Fatal(err)
1456 }
1457 if expr != `request.auth.claims.email == "wiley@acme.co"` {
1458 t.Errorf("got expr: %s, wanted request.auth.claims.email == \"wiley@acme.co\"", expr)
1459 }
1460}
1461
1462func TestResidualAstMacros(t *testing.T) {
1463 tests := []struct {

Callers

nothing calls this directly

Calls 13

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

Tested by

no test coverage detected