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

Function mustProgram

cel/program_async_test.go:1088–1122  ·  view source on GitHub ↗

mustProgram compiles an expression and constructs a Program, separating EnvOptions and ProgramOptions.

(t *testing.T, expr string, opts ...any)

Source from the content-addressed store, hash-verified

1086
1087// mustProgram compiles an expression and constructs a Program, separating EnvOptions and ProgramOptions.
1088func mustProgram(t *testing.T, expr string, opts ...any) cel.Program {
1089 t.Helper()
1090 var envOpts []cel.EnvOption
1091 var prgOpts []cel.ProgramOption
1092 var deferredPrgOpts []func(int64) cel.ProgramOption
1093 for _, opt := range opts {
1094 switch o := opt.(type) {
1095 case cel.EnvOption:
1096 envOpts = append(envOpts, o)
1097 case cel.ProgramOption:
1098 prgOpts = append(prgOpts, o)
1099 case func(int64) cel.ProgramOption:
1100 deferredPrgOpts = append(deferredPrgOpts, o)
1101 default:
1102 t.Fatalf("unsupported option type %T", opt)
1103 }
1104 }
1105 env, err := cel.NewEnv(envOpts...)
1106 if err != nil {
1107 t.Fatalf("NewEnv() failed: %v", err)
1108 }
1109 ast, iss := env.Compile(expr)
1110 if iss.Err() != nil {
1111 t.Fatalf("Compile(%q) failed: %v", expr, iss.Err())
1112 }
1113 rootID := ast.NativeRep().Expr().ID()
1114 for _, deferred := range deferredPrgOpts {
1115 prgOpts = append(prgOpts, deferred(rootID))
1116 }
1117 prg, err := env.Program(ast, prgOpts...)
1118 if err != nil {
1119 t.Fatalf("Program() failed: %v", err)
1120 }
1121 return prg
1122}
1123
1124type countingObserver struct {
1125 started atomic.Int32

Calls 7

CompileMethod · 0.95
ProgramMethod · 0.95
NewEnvFunction · 0.92
ErrMethod · 0.80
NativeRepMethod · 0.80
IDMethod · 0.65
ExprMethod · 0.65

Tested by

no test coverage detected