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

Function exprLinesFromPolicy

policy/compiler_test.go:582–618  ·  view source on GitHub ↗

exprLinesFromPolicy returns a set of line numbers within a policy where expressions (variables, conditions, etc.) are defined.

(policy *Policy)

Source from the content-addressed store, hash-verified

580// exprLinesFromPolicy returns a set of line numbers within a policy where expressions (variables,
581// conditions, etc.) are defined.
582func exprLinesFromPolicy(policy *Policy) map[int]bool {
583 lines := make(map[int]bool)
584 addExpectedLines := func(vs ValueString) {
585 if offset, found := policy.SourceInfo().GetOffsetRange(vs.ID); found {
586 startLoc, foundStart := policy.Source().OffsetLocation(offset.Start)
587 // Multiline strings can span multiple lines, but the SourceInfo will only contain the start
588 // position of the expression. So just skip the check if the expression contains a multiline
589 // string literal.
590 hasMultiline := strings.Contains(vs.Value, "'''") || strings.Contains(vs.Value, "\"\"\"")
591 if foundStart && !hasMultiline {
592 numLines := strings.Count(vs.Value, "\n")
593 for i := 0; i <= numLines; i++ {
594 lines[startLoc.Line()+i] = true
595 }
596 }
597 }
598 }
599 var traverseRule func(r *Rule)
600 traverseRule = func(r *Rule) {
601 for _, v := range r.Variables() {
602 addExpectedLines(v.Expression())
603 }
604 for _, m := range r.Matches() {
605 if !strings.HasPrefix(m.Condition().Value, "true") {
606 addExpectedLines(m.Condition())
607 }
608 if m.HasOutput() {
609 addExpectedLines(m.Output())
610 }
611 if m.HasRule() {
612 traverseRule(m.Rule())
613 }
614 }
615 }
616 traverseRule(policy.Rule())
617 return lines
618}
619
620func TestCompileYAMLPolicy_Aggregate(t *testing.T) {
621 type testEval struct {

Callers 1

verifySourceInfoCoverageFunction · 0.85

Calls 14

GetOffsetRangeMethod · 0.80
ExpressionMethod · 0.80
HasOutputMethod · 0.80
HasRuleMethod · 0.80
OffsetLocationMethod · 0.65
ContainsMethod · 0.65
LineMethod · 0.65
SourceInfoMethod · 0.45
SourceMethod · 0.45
VariablesMethod · 0.45
MatchesMethod · 0.45
ConditionMethod · 0.45

Tested by

no test coverage detected