(t *testing.T)
| 1901 | } |
| 1902 | |
| 1903 | func 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, |
nothing calls this directly
no test coverage detected