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

Function TestResidualAstComplex

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

Source from the content-addressed store, hash-verified

1308}
1309
1310func TestResidualAstComplex(t *testing.T) {
1311 env := testEnv(t,
1312 Variable("resource.name", StringType),
1313 Variable("request.time", TimestampType),
1314 Variable("request.auth.claims", MapType(StringType, StringType)),
1315 )
1316 unkVars, _ := PartialVars(
1317 map[string]any{
1318 "resource.name": "bucket/my-bucket/objects/private",
1319 "request.auth.claims": map[string]string{
1320 "email_verified": "true",
1321 },
1322 },
1323 AttributePattern("request.auth.claims").QualString("email"),
1324 )
1325 ast, iss := env.Compile(
1326 `resource.name.startsWith("bucket/my-bucket") &&
1327 bool(request.auth.claims.email_verified) == true &&
1328 request.auth.claims.email == "wiley@acme.co"`)
1329 if iss.Err() != nil {
1330 t.Fatalf("env.Compile() failed: %v", iss.Err())
1331 }
1332 prg, err := env.Program(ast,
1333 EvalOptions(OptTrackState, OptPartialEval),
1334 )
1335 if err != nil {
1336 t.Fatalf("env.Program() failed: %v", err)
1337 }
1338 out, det, err := prg.Eval(unkVars)
1339 if !types.IsUnknown(out) {
1340 t.Fatalf("got %v, expected unknown", out)
1341 }
1342 if err != nil {
1343 t.Fatal(err)
1344 }
1345 residual, err := env.ResidualAst(ast, det)
1346 if err != nil {
1347 t.Fatal(err)
1348 }
1349 expr, err := AstToString(residual)
1350 if err != nil {
1351 t.Fatal(err)
1352 }
1353 if expr != `request.auth.claims.email == "wiley@acme.co"` {
1354 t.Errorf("got expr: %s, wanted request.auth.claims.email == \"wiley@acme.co\"", expr)
1355 }
1356}
1357
1358func TestResidualAstMacros(t *testing.T) {
1359 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