(t *testing.T)
| 1985 | } |
| 1986 | |
| 1987 | func TestCostLimit(t *testing.T) { |
| 1988 | cases := []struct { |
| 1989 | name string |
| 1990 | expr string |
| 1991 | decls []EnvOption |
| 1992 | costLimit uint64 |
| 1993 | in any |
| 1994 | err error |
| 1995 | }{ |
| 1996 | { |
| 1997 | name: "greater", |
| 1998 | expr: `val1 > val2`, |
| 1999 | decls: []EnvOption{ |
| 2000 | Variable("val1", IntType), |
| 2001 | Variable("val2", IntType), |
| 2002 | }, |
| 2003 | in: map[string]any{"val1": 1, "val2": 2}, |
| 2004 | costLimit: 10, |
| 2005 | }, |
| 2006 | { |
| 2007 | name: "greater - error", |
| 2008 | expr: `val1 > val2`, |
| 2009 | decls: []EnvOption{ |
| 2010 | Variable("val1", IntType), |
| 2011 | Variable("val2", IntType), |
| 2012 | }, |
| 2013 | in: map[string]any{"val1": 1, "val2": 2}, |
| 2014 | costLimit: 0, |
| 2015 | err: errors.New("actual cost limit exceeded"), |
| 2016 | }, |
| 2017 | } |
| 2018 | |
| 2019 | for _, tst := range cases { |
| 2020 | tc := tst |
| 2021 | t.Run(tc.name, func(t *testing.T) { |
| 2022 | t.Parallel() |
| 2023 | envOpts := []EnvOption{ |
| 2024 | CostEstimatorOptions( |
| 2025 | checker.OverloadCostEstimate(overloads.TimestampToYear, estimateTimestampToYear), |
| 2026 | ), |
| 2027 | } |
| 2028 | envOpts = append(envOpts, tc.decls...) |
| 2029 | env := testEnv(t, envOpts...) |
| 2030 | ast, iss := env.Compile(tc.expr) |
| 2031 | if iss.Err() != nil { |
| 2032 | t.Fatalf("env.Compile(%v) failed: %v", tc.expr, iss.Err()) |
| 2033 | } |
| 2034 | est, err := env.EstimateCost(ast, testCostEstimator{hints: map[string]uint64{}}) |
| 2035 | if err != nil { |
| 2036 | t.Fatalf("Env.EstimateCost(ast *Ast, estimator checker.CostEstimator) failed to estimate cost: %s\n", err) |
| 2037 | } |
| 2038 | |
| 2039 | checkedAst, iss := env.Check(ast) |
| 2040 | if iss.Err() != nil { |
| 2041 | t.Fatalf(`Env.Check(ast *Ast) failed to check expression: %v`, iss.Err()) |
| 2042 | } |
| 2043 | // Evaluate expression. |
| 2044 | program, err := env.Program(checkedAst, |
nothing calls this directly
no test coverage detected