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

Function verifySourceInfoCoverage

policy/compiler_test.go:540–578  ·  view source on GitHub ↗
(t testing.TB, policy *Policy, ast *cel.Ast)

Source from the content-addressed store, hash-verified

538}
539
540func verifySourceInfoCoverage(t testing.TB, policy *Policy, ast *cel.Ast) {
541 t.Helper()
542 info := ast.SourceInfo()
543
544 exprLines := exprLinesFromPolicy(policy)
545 coveredLines := make(map[int]bool)
546 ids := ast.NativeRep().IDs()
547 for id, offset := range info.GetPositions() {
548 if offset <= 0 {
549 t.Errorf("id %d has invalid offset %v", id, offset)
550 }
551 // Check that each position in the SourceInfo corresponds to a valid AST node.
552 if !ids[id] {
553 t.Errorf("id %d not found in AST", id)
554 }
555 loc, found := ast.Source().OffsetLocation(offset)
556 if found {
557 coveredLines[loc.Line()] = true
558 } else {
559 t.Errorf("invalid source location for offset %d", offset)
560 }
561 }
562 // Verify that each source line inside an expression is covered by the at least one node in the
563 // AST.
564 for line := range exprLines {
565 if !coveredLines[line] {
566 t.Errorf("Line %d expected to be covered by SourceInfo, but was not", line)
567 }
568 }
569
570 if t.Failed() {
571 checked, err := cel.AstToCheckedExpr(ast)
572 if err != nil {
573 t.Logf("cel.AstToCheckedExpr() failed: %v", err)
574 } else {
575 t.Logf("AST:\n%s", prototext.Format(checked.GetExpr()))
576 }
577 }
578}
579
580// exprLinesFromPolicy returns a set of line numbers within a policy where expressions (variables,
581// conditions, etc.) are defined.

Callers 2

TestRuleComposerUnnestFunction · 0.85
compileFunction · 0.85

Calls 9

AstToCheckedExprFunction · 0.92
exprLinesFromPolicyFunction · 0.85
NativeRepMethod · 0.80
GetExprMethod · 0.80
IDsMethod · 0.65
OffsetLocationMethod · 0.65
LineMethod · 0.65
SourceInfoMethod · 0.45
SourceMethod · 0.45

Tested by

no test coverage detected