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

Function TestEval

cel/cel_test.go:98–151  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

96}
97
98func TestEval(t *testing.T) {
99 env, err := NewEnv(
100 Variable("input", ListType(IntType)),
101 CostEstimatorOptions(
102 checker.OverloadCostEstimate(overloads.TimestampToYear, estimateTimestampToYear),
103 ),
104 )
105 if err != nil {
106 t.Fatalf("NewEnv() failed: %v", err)
107 }
108 tests := []struct {
109 expr string
110 in any
111 }{
112 {
113 expr: `input.size() != 0`,
114 in: map[string]any{"input": []int{1, 2, 3}},
115 },
116 }
117 for i, tst := range tests {
118 tc := tst
119 t.Run(fmt.Sprintf("[%d]", i), func(t *testing.T) {
120 ast, iss := env.Compile(tc.expr)
121 if iss.Err() != nil {
122 t.Fatalf("env.Compile(%s) failed: %v", tc.expr, iss.Err())
123 }
124 ctx := context.Background()
125 prgOpts := []ProgramOption{
126 CostTracking(testRuntimeCostEstimator{}),
127 CostTrackerOptions(
128 interpreter.OverloadCostTracker(overloads.TimestampToYear, trackTimestampToYear),
129 ),
130 EvalOptions(OptOptimize, OptTrackCost),
131 InterruptCheckFrequency(100),
132 }
133 prg, err := env.Program(ast, prgOpts...)
134 if err != nil {
135 t.Fatalf("env.Program() failed: %v", err)
136 }
137 for k := 0; k < 100; k++ {
138 t.Run(fmt.Sprintf("[%d]", k), func(t *testing.T) {
139 t.Parallel()
140 prg.Eval(tc.in)
141 evalCtx, cancel := context.WithTimeout(ctx, time.Minute)
142 defer cancel()
143 _, _, err := prg.ContextEval(evalCtx, tc.in)
144 if err != nil {
145 t.Fatalf("prg.ContextEval() failed: %v", err)
146 }
147 })
148 }
149 })
150 }
151}
152
153func TestAbbrevsCompiled(t *testing.T) {
154 // Test whether abbreviations successfully resolve at type-check time (compile time).

Callers

nothing calls this directly

Calls 14

CompileMethod · 0.95
ProgramMethod · 0.95
OverloadCostEstimateFunction · 0.92
OverloadCostTrackerFunction · 0.92
CostEstimatorOptionsFunction · 0.85
CostTrackingFunction · 0.85
CostTrackerOptionsFunction · 0.85
EvalOptionsFunction · 0.85
InterruptCheckFrequencyFunction · 0.85
ErrMethod · 0.80
NewEnvFunction · 0.70
VariableFunction · 0.70

Tested by

no test coverage detected