(t *testing.T)
| 5735 | } |
| 5736 | |
| 5737 | func TestCostModel(t *testing.T) { |
| 5738 | model1 := cost.MemberOverload(overloads.StartsWithString, |
| 5739 | cost.EvalCost(cost.Scale(cost.Arg(0), 0.1)), |
| 5740 | ) |
| 5741 | model2 := cost.MemberOverload(overloads.EndsWithString, |
| 5742 | cost.EvalCost(cost.Scale(cost.Arg(0), 0.1)), |
| 5743 | ) |
| 5744 | env, err := NewEnv( |
| 5745 | CostModel(model1), |
| 5746 | CostModel(model2), |
| 5747 | CostSizingStrategy(cost.AggregateSizingStrategy()), |
| 5748 | Variable("str", StringType), |
| 5749 | ) |
| 5750 | if err != nil { |
| 5751 | t.Fatalf("NewEnv failed: %v", err) |
| 5752 | } |
| 5753 | ast, iss := env.Compile(`str.startsWith("prefix") && str.endsWith("suffix")`) |
| 5754 | if iss.Err() != nil { |
| 5755 | t.Fatalf("Compile failed: %v", iss.Err()) |
| 5756 | } |
| 5757 | prg, err := env.Program(ast, CostTracking(nil)) |
| 5758 | if err != nil { |
| 5759 | t.Fatalf("Program failed: %v", err) |
| 5760 | } |
| 5761 | res, valDetails, err := prg.Eval(map[string]any{ |
| 5762 | "str": "prefix_middle_suffix", |
| 5763 | }) |
| 5764 | if err != nil { |
| 5765 | t.Fatalf("Eval failed: %v", err) |
| 5766 | } |
| 5767 | if res != types.True { |
| 5768 | t.Errorf("got %v, want true", res) |
| 5769 | } |
| 5770 | actualCost := *valDetails.ActualCost() |
| 5771 | if actualCost == 0 { |
| 5772 | t.Errorf("expected non-zero actual cost, got %d", actualCost) |
| 5773 | } |
| 5774 | } |
| 5775 | |
| 5776 | // TestCostModelValidation covers rejection of a model whose projection has no runtime counterpart. |
| 5777 | // Environment construction is the last point at which such a model can be reported. |
nothing calls this directly
no test coverage detected