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

Function TestCostTrackingWithStateTracking

cel/cel_test.go:1714–1750  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

1712}
1713
1714func TestCostTrackingWithStateTracking(t *testing.T) {
1715 // Cost tracking and state tracking install separate observers. Every observer has to see
1716 // every evaluation step, whichever combination of them is configured.
1717 env := testEnv(t, Variable("a", StringType))
1718 ast, iss := env.Compile(`a.startsWith("x") && a.contains("yz")`)
1719 if iss.Err() != nil {
1720 t.Fatalf("env.Compile() failed: %v", iss.Err())
1721 }
1722 baseline, _ := evalCostAndState(t, env, ast, CostTracking(nil))
1723 if baseline == 0 {
1724 t.Fatalf("cost tracking alone reported a cost of 0")
1725 }
1726 tests := []struct {
1727 name string
1728 opts []ProgramOption
1729 wantState bool
1730 wantEqCost bool
1731 }{
1732 {name: "cost", opts: []ProgramOption{CostTracking(nil)}, wantEqCost: true},
1733 {name: "cost and state", opts: []ProgramOption{CostTracking(nil), EvalOptions(OptTrackState)},
1734 wantState: true, wantEqCost: true},
1735 {name: "cost and exhaustive", opts: []ProgramOption{CostTracking(nil), EvalOptions(OptExhaustiveEval)},
1736 wantState: true, wantEqCost: true},
1737 }
1738 for _, tst := range tests {
1739 tc := tst
1740 t.Run(tc.name, func(t *testing.T) {
1741 cost, hasState := evalCostAndState(t, env, ast, tc.opts...)
1742 if tc.wantEqCost && cost != baseline {
1743 t.Errorf("actual cost got %d, wanted %d", cost, baseline)
1744 }
1745 if hasState != tc.wantState {
1746 t.Errorf("state tracked got %t, wanted %t", hasState, tc.wantState)
1747 }
1748 })
1749 }
1750}
1751
1752// evalCostAndState evaluates the ast and reports the tracked cost along with whether evaluation
1753// state was recorded.

Callers

nothing calls this directly

Calls 7

evalCostAndStateFunction · 0.85
CostTrackingFunction · 0.85
EvalOptionsFunction · 0.85
ErrMethod · 0.80
testEnvFunction · 0.70
VariableFunction · 0.70
CompileMethod · 0.65

Tested by

no test coverage detected