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

Function testCost

ext/network_test.go:912–967  ·  view source on GitHub ↗

testCost asserts the estimated cost at the latest cost model revision and at ModelVersion0, alongside the runtime cost. estimatedCostV0 is supplied only for cases a revision actually moved; omitting it asserts the two revisions agree, so the call sites remain an exact ledger of what a revision chan

(t *testing.T, expr string, estimatedCost cost.CostEstimate, runtimeCost uint64, estimatedCostV0 ...cost.CostEstimate)

Source from the content-addressed store, hash-verified

910// estimatedCostV0 is supplied only for cases a revision actually moved; omitting it asserts the two
911// revisions agree, so the call sites remain an exact ledger of what a revision changed.
912func testCost(t *testing.T, expr string, estimatedCost cost.CostEstimate, runtimeCost uint64, estimatedCostV0 ...cost.CostEstimate) {
913 t.Helper()
914 env, err := cel.NewEnv(Network())
915 if err != nil {
916 t.Fatalf("cel.NewEnv(Network()) failed: %v", err)
917 }
918 parsedAst, iss := env.Parse(expr)
919 if iss.Err() != nil {
920 t.Fatalf("env.Parse(%q) failed: %v", expr, iss.Err())
921 }
922 checkedAst, iss := env.Check(parsedAst)
923 if iss.Err() != nil {
924 t.Fatalf("env.Check(%q) failed: %v", expr, iss.Err())
925 }
926
927 actualEst, err := env.EstimateCost(checkedAst, &noopCostEstimator{})
928 if err != nil {
929 t.Fatalf("env.EstimateCost(%q) failed: %v", expr, err)
930 }
931 if actualEst.Min != estimatedCost.Min || actualEst.Max != estimatedCost.Max {
932 t.Errorf("expected estimated cost %v, got %v for expr %q", estimatedCost, actualEst, expr)
933 }
934
935 wantLegacy := estimatedCost
936 switch len(estimatedCostV0) {
937 case 0:
938 case 1:
939 wantLegacy = estimatedCostV0[0]
940 default:
941 t.Fatalf("testCost() accepts at most one ModelVersion0 expectation, got %d", len(estimatedCostV0))
942 }
943 legacyEst, err := env.EstimateCost(checkedAst, &noopCostEstimator{}, cost.ModelVersion(0))
944 if err != nil {
945 t.Fatalf("env.EstimateCost(%q, version 0) failed: %v", expr, err)
946 }
947 if legacyEst.Min != wantLegacy.Min || legacyEst.Max != wantLegacy.Max {
948 t.Errorf("expected version 0 estimated cost %v, got %v for expr %q", wantLegacy, legacyEst, expr)
949 }
950
951 program, err := env.Program(checkedAst, cel.CostTracking(&noopCostEstimator{}))
952 if err != nil {
953 t.Fatalf("env.Program(%q) failed: %v", expr, err)
954 }
955 _, evalDetails, err := program.Eval(cel.NoVars())
956 if err != nil {
957 t.Fatalf("program.Eval(%q) failed: %v", expr, err)
958 }
959 if evalDetails == nil || evalDetails.ActualCost() == nil {
960 t.Fatalf("evalDetails or actualCost is nil for %q", expr)
961 }
962 if *evalDetails.ActualCost() != runtimeCost {
963 t.Errorf("expected runtime cost %d, got %d for expr %q", runtimeCost, *evalDetails.ActualCost(), expr)
964 }
965 checkCostGoldenV0Estimate(t, env, checkedAst, &noopCostEstimator{})
966 checkCostGoldenV0Actual(t, env, checkedAst, nil)
967}
968
969func TestIPCost(t *testing.T) {

Callers 3

TestNetworkCostFunction · 0.85
TestIPCostFunction · 0.85
TestCIDRCostFunction · 0.85

Calls 14

ParseMethod · 0.95
CheckMethod · 0.95
EstimateCostMethod · 0.95
ProgramMethod · 0.95
NewEnvFunction · 0.92
ModelVersionFunction · 0.92
CostTrackingFunction · 0.92
NoVarsFunction · 0.92
NetworkFunction · 0.85
checkCostGoldenV0ActualFunction · 0.85
ErrMethod · 0.80

Tested by

no test coverage detected