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

Function evalWithCEL

ext/strings_test.go:698–751  ·  view source on GitHub ↗
(input string, expectedRuntimeCost uint64, expectedEstimatedCost checker.CostEstimate, t *testing.T)

Source from the content-addressed store, hash-verified

696}
697
698func evalWithCEL(input string, expectedRuntimeCost uint64, expectedEstimatedCost checker.CostEstimate, t *testing.T) string {
699 env, err := cel.NewEnv(Strings())
700 if err != nil {
701 t.Fatalf("cel.NewEnv() failed: %v", err)
702 }
703 expr := fmt.Sprintf(`strings.quote(%q)`, input)
704 parsedAst, issues := env.Parse(expr)
705 if issues.Err() != nil {
706 t.Fatalf("env.Parse() failed: %v", issues.Err())
707 }
708 checkedAst, issues := env.Check(parsedAst)
709 if issues.Err() != nil {
710 t.Fatalf("env.Check() failed: %v", issues.Err())
711 }
712
713 costTracker := &noopCostEstimator{}
714 actualEstimatedCost, err := env.EstimateCost(checkedAst, costTracker)
715 if err != nil {
716 t.Fatal(err)
717 }
718 if expectedEstimatedCost.Min != 0 && expectedEstimatedCost.Max != 0 {
719 if actualEstimatedCost.Min != expectedEstimatedCost.Min && actualEstimatedCost.Max != expectedEstimatedCost.Max {
720 t.Fatalf("expected estimated cost range to be %v, was %v", expectedEstimatedCost, actualEstimatedCost)
721 }
722 }
723
724 program, err := env.Program(checkedAst, cel.CostTracking(costTracker))
725 if err != nil {
726 t.Fatal(err)
727 }
728 out, evalDetails, err := program.Eval(cel.NoVars())
729 if err != nil {
730 t.Fatal(err)
731 }
732 if evalDetails == nil {
733 t.Fatal("evalDetails could not be calculated")
734 } else if evalDetails.ActualCost() == nil {
735 t.Fatal("could not calculate runtime cost")
736 }
737 if expectedRuntimeCost != 0 {
738 if *evalDetails.ActualCost() != expectedRuntimeCost {
739 t.Fatalf("expected runtime cost of %d, got %d", expectedRuntimeCost, *evalDetails.ActualCost())
740 }
741 if expectedEstimatedCost.Min != 0 && expectedEstimatedCost.Max != 0 {
742 if *evalDetails.ActualCost() < expectedEstimatedCost.Min || *evalDetails.ActualCost() > expectedEstimatedCost.Max {
743 t.Fatalf("runtime cost %d outside of expected estimated cost range %v", *evalDetails.ActualCost(), expectedEstimatedCost)
744 }
745 }
746 }
747 if out.Type() != types.StringType {
748 t.Fatalf("expected expr output to be a string, got %s", out.Type().TypeName())
749 }
750 return out.Value().(string)
751}
752
753func TestFunctionsForVersions(t *testing.T) {
754 tests := []struct {

Callers 1

TestQuoteUnquoteFunction · 0.85

Calls 14

ParseMethod · 0.95
CheckMethod · 0.95
EstimateCostMethod · 0.95
ProgramMethod · 0.95
NewEnvFunction · 0.92
CostTrackingFunction · 0.92
NoVarsFunction · 0.92
StringsFunction · 0.85
ErrMethod · 0.80
EvalMethod · 0.65
TypeMethod · 0.65
TypeNameMethod · 0.65

Tested by

no test coverage detected