mustProgram compiles an expression and constructs a Program, separating EnvOptions and ProgramOptions.
(t *testing.T, expr string, opts ...any)
| 929 | |
| 930 | // mustProgram compiles an expression and constructs a Program, separating EnvOptions and ProgramOptions. |
| 931 | func mustProgram(t *testing.T, expr string, opts ...any) cel.Program { |
| 932 | t.Helper() |
| 933 | var envOpts []cel.EnvOption |
| 934 | var prgOpts []cel.ProgramOption |
| 935 | var deferredPrgOpts []func(int64) cel.ProgramOption |
| 936 | for _, opt := range opts { |
| 937 | switch o := opt.(type) { |
| 938 | case cel.EnvOption: |
| 939 | envOpts = append(envOpts, o) |
| 940 | case cel.ProgramOption: |
| 941 | prgOpts = append(prgOpts, o) |
| 942 | case func(int64) cel.ProgramOption: |
| 943 | deferredPrgOpts = append(deferredPrgOpts, o) |
| 944 | default: |
| 945 | t.Fatalf("unsupported option type %T", opt) |
| 946 | } |
| 947 | } |
| 948 | env, err := cel.NewEnv(envOpts...) |
| 949 | if err != nil { |
| 950 | t.Fatalf("NewEnv() failed: %v", err) |
| 951 | } |
| 952 | ast, iss := env.Compile(expr) |
| 953 | if iss.Err() != nil { |
| 954 | t.Fatalf("Compile(%q) failed: %v", expr, iss.Err()) |
| 955 | } |
| 956 | rootID := ast.NativeRep().Expr().ID() |
| 957 | for _, deferred := range deferredPrgOpts { |
| 958 | prgOpts = append(prgOpts, deferred(rootID)) |
| 959 | } |
| 960 | prg, err := env.Program(ast, prgOpts...) |
| 961 | if err != nil { |
| 962 | t.Fatalf("Program() failed: %v", err) |
| 963 | } |
| 964 | return prg |
| 965 | } |
| 966 | |
| 967 | type countingObserver struct { |
| 968 | started atomic.Int32 |
no test coverage detected