(t *testing.T)
| 1684 | } |
| 1685 | |
| 1686 | func TestCostTrackingWithStateTracking(t *testing.T) { |
| 1687 | // Cost tracking and state tracking install separate observers. Every observer has to see |
| 1688 | // every evaluation step, whichever combination of them is configured. |
| 1689 | env := testEnv(t, Variable("a", StringType)) |
| 1690 | ast, iss := env.Compile(`a.startsWith("x") && a.contains("yz")`) |
| 1691 | if iss.Err() != nil { |
| 1692 | t.Fatalf("env.Compile() failed: %v", iss.Err()) |
| 1693 | } |
| 1694 | baseline, _ := evalCostAndState(t, env, ast, CostTracking(nil)) |
| 1695 | if baseline == 0 { |
| 1696 | t.Fatalf("cost tracking alone reported a cost of 0") |
| 1697 | } |
| 1698 | tests := []struct { |
| 1699 | name string |
| 1700 | opts []ProgramOption |
| 1701 | wantState bool |
| 1702 | wantEqCost bool |
| 1703 | }{ |
| 1704 | {name: "cost", opts: []ProgramOption{CostTracking(nil)}, wantEqCost: true}, |
| 1705 | {name: "cost and state", opts: []ProgramOption{CostTracking(nil), EvalOptions(OptTrackState)}, |
| 1706 | wantState: true, wantEqCost: true}, |
| 1707 | {name: "cost and exhaustive", opts: []ProgramOption{CostTracking(nil), EvalOptions(OptExhaustiveEval)}, |
| 1708 | wantState: true, wantEqCost: true}, |
| 1709 | } |
| 1710 | for _, tst := range tests { |
| 1711 | tc := tst |
| 1712 | t.Run(tc.name, func(t *testing.T) { |
| 1713 | cost, hasState := evalCostAndState(t, env, ast, tc.opts...) |
| 1714 | if tc.wantEqCost && cost != baseline { |
| 1715 | t.Errorf("actual cost got %d, wanted %d", cost, baseline) |
| 1716 | } |
| 1717 | if hasState != tc.wantState { |
| 1718 | t.Errorf("state tracked got %t, wanted %t", hasState, tc.wantState) |
| 1719 | } |
| 1720 | }) |
| 1721 | } |
| 1722 | } |
| 1723 | |
| 1724 | // evalCostAndState evaluates the ast and reports the tracked cost along with whether evaluation |
| 1725 | // state was recorded. |
nothing calls this directly
no test coverage detected