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

Function testCost

ext/network_test.go:897–934  ·  view source on GitHub ↗
(t *testing.T, expr string, estimatedCost checker.CostEstimate, runtimeCost uint64)

Source from the content-addressed store, hash-verified

895}
896
897func testCost(t *testing.T, expr string, estimatedCost checker.CostEstimate, runtimeCost uint64) {
898 t.Helper()
899 env, err := cel.NewEnv(Network())
900 if err != nil {
901 t.Fatalf("cel.NewEnv(Network()) failed: %v", err)
902 }
903 parsedAst, iss := env.Parse(expr)
904 if iss.Err() != nil {
905 t.Fatalf("env.Parse(%q) failed: %v", expr, iss.Err())
906 }
907 checkedAst, iss := env.Check(parsedAst)
908 if iss.Err() != nil {
909 t.Fatalf("env.Check(%q) failed: %v", expr, iss.Err())
910 }
911
912 actualEst, err := env.EstimateCost(checkedAst, &noopCostEstimator{})
913 if err != nil {
914 t.Fatalf("env.EstimateCost(%q) failed: %v", expr, err)
915 }
916 if actualEst.Min != estimatedCost.Min || actualEst.Max != estimatedCost.Max {
917 t.Errorf("expected estimated cost %v, got %v for expr %q", estimatedCost, actualEst, expr)
918 }
919
920 program, err := env.Program(checkedAst, cel.CostTracking(&noopCostEstimator{}))
921 if err != nil {
922 t.Fatalf("env.Program(%q) failed: %v", expr, err)
923 }
924 _, evalDetails, err := program.Eval(cel.NoVars())
925 if err != nil {
926 t.Fatalf("program.Eval(%q) failed: %v", expr, err)
927 }
928 if evalDetails == nil || evalDetails.ActualCost() == nil {
929 t.Fatalf("evalDetails or actualCost is nil for %q", expr)
930 }
931 if *evalDetails.ActualCost() != runtimeCost {
932 t.Errorf("expected runtime cost %d, got %d for expr %q", runtimeCost, *evalDetails.ActualCost(), expr)
933 }
934}
935
936func TestIPCost(t *testing.T) {
937 ipv4 := "ip('192.168.0.1')"

Callers 3

TestNetworkCostFunction · 0.85
TestIPCostFunction · 0.85
TestCIDRCostFunction · 0.85

Calls 11

ParseMethod · 0.95
CheckMethod · 0.95
EstimateCostMethod · 0.95
ProgramMethod · 0.95
NewEnvFunction · 0.92
CostTrackingFunction · 0.92
NoVarsFunction · 0.92
NetworkFunction · 0.85
ErrMethod · 0.80
EvalMethod · 0.65
ActualCostMethod · 0.45

Tested by

no test coverage detected