(t *testing.T)
| 1881 | } |
| 1882 | |
| 1883 | func TestCostLimit(t *testing.T) { |
| 1884 | cases := []struct { |
| 1885 | name string |
| 1886 | expr string |
| 1887 | decls []EnvOption |
| 1888 | costLimit uint64 |
| 1889 | in any |
| 1890 | err error |
| 1891 | }{ |
| 1892 | { |
| 1893 | name: "greater", |
| 1894 | expr: `val1 > val2`, |
| 1895 | decls: []EnvOption{ |
| 1896 | Variable("val1", IntType), |
| 1897 | Variable("val2", IntType), |
| 1898 | }, |
| 1899 | in: map[string]any{"val1": 1, "val2": 2}, |
| 1900 | costLimit: 10, |
| 1901 | }, |
| 1902 | { |
| 1903 | name: "greater - error", |
| 1904 | expr: `val1 > val2`, |
| 1905 | decls: []EnvOption{ |
| 1906 | Variable("val1", IntType), |
| 1907 | Variable("val2", IntType), |
| 1908 | }, |
| 1909 | in: map[string]any{"val1": 1, "val2": 2}, |
| 1910 | costLimit: 0, |
| 1911 | err: errors.New("actual cost limit exceeded"), |
| 1912 | }, |
| 1913 | } |
| 1914 | |
| 1915 | for _, tst := range cases { |
| 1916 | tc := tst |
| 1917 | t.Run(tc.name, func(t *testing.T) { |
| 1918 | t.Parallel() |
| 1919 | envOpts := []EnvOption{ |
| 1920 | CostEstimatorOptions( |
| 1921 | checker.OverloadCostEstimate(overloads.TimestampToYear, estimateTimestampToYear), |
| 1922 | ), |
| 1923 | } |
| 1924 | envOpts = append(envOpts, tc.decls...) |
| 1925 | env := testEnv(t, envOpts...) |
| 1926 | ast, iss := env.Compile(tc.expr) |
| 1927 | if iss.Err() != nil { |
| 1928 | t.Fatalf("env.Compile(%v) failed: %v", tc.expr, iss.Err()) |
| 1929 | } |
| 1930 | est, err := env.EstimateCost(ast, testCostEstimator{hints: map[string]uint64{}}) |
| 1931 | if err != nil { |
| 1932 | t.Fatalf("Env.EstimateCost(ast *Ast, estimator checker.CostEstimator) failed to estimate cost: %s\n", err) |
| 1933 | } |
| 1934 | |
| 1935 | checkedAst, iss := env.Check(ast) |
| 1936 | if iss.Err() != nil { |
| 1937 | t.Fatalf(`Env.Check(ast *Ast) failed to check expression: %v`, iss.Err()) |
| 1938 | } |
| 1939 | // Evaluate expression. |
| 1940 | program, err := env.Program(checkedAst, |
nothing calls this directly
no test coverage detected