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

Function TestCostLimit

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

Source from the content-addressed store, hash-verified

1901}
1902
1903func TestCostLimit(t *testing.T) {
1904 cases := []struct {
1905 name string
1906 expr string
1907 decls []EnvOption
1908 costLimit uint64
1909 in any
1910 err error
1911 }{
1912 {
1913 name: "greater",
1914 expr: `val1 > val2`,
1915 decls: []EnvOption{
1916 Variable("val1", IntType),
1917 Variable("val2", IntType),
1918 },
1919 in: map[string]any{"val1": 1, "val2": 2},
1920 costLimit: 10,
1921 },
1922 {
1923 name: "greater - error",
1924 expr: `val1 > val2`,
1925 decls: []EnvOption{
1926 Variable("val1", IntType),
1927 Variable("val2", IntType),
1928 },
1929 in: map[string]any{"val1": 1, "val2": 2},
1930 costLimit: 0,
1931 err: errors.New("actual cost limit exceeded"),
1932 },
1933 }
1934
1935 for _, tst := range cases {
1936 tc := tst
1937 t.Run(tc.name, func(t *testing.T) {
1938 t.Parallel()
1939 envOpts := []EnvOption{
1940 CostEstimatorOptions(
1941 checker.OverloadCostEstimate(overloads.TimestampToYear, estimateTimestampToYear),
1942 ),
1943 }
1944 envOpts = append(envOpts, tc.decls...)
1945 env := testEnv(t, envOpts...)
1946 ast, iss := env.Compile(tc.expr)
1947 if iss.Err() != nil {
1948 t.Fatalf("env.Compile(%v) failed: %v", tc.expr, iss.Err())
1949 }
1950 est, err := env.EstimateCost(ast, testCostEstimator{hints: map[string]uint64{}})
1951 if err != nil {
1952 t.Fatalf("Env.EstimateCost(ast *Ast, estimator checker.CostEstimator) failed to estimate cost: %s\n", err)
1953 }
1954
1955 checkedAst, iss := env.Check(ast)
1956 if iss.Err() != nil {
1957 t.Fatalf(`Env.Check(ast *Ast) failed to check expression: %v`, iss.Err())
1958 }
1959 // Evaluate expression.
1960 program, err := env.Program(checkedAst,

Callers

nothing calls this directly

Calls 15

OverloadCostEstimateFunction · 0.92
OverloadCostTrackerFunction · 0.92
CostEstimatorOptionsFunction · 0.85
CostTrackingFunction · 0.85
CostTrackerOptionsFunction · 0.85
CostLimitFunction · 0.85
NewMethod · 0.80
ErrMethod · 0.80
EstimateCostMethod · 0.80
CheckMethod · 0.80
ProgramMethod · 0.80
VariableFunction · 0.70

Tested by

no test coverage detected