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

Function TestComprehension

common/ast/expr_test.go:186–239  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

184}
185
186func TestComprehension(t *testing.T) {
187 fac := ast.NewExprFactory()
188 expr := fac.NewComprehension(1,
189 fac.NewList(2, []ast.Expr{
190 fac.NewLiteral(3, types.Int(1)),
191 fac.NewLiteral(4, types.Int(2)),
192 }, []int32{}),
193 "i",
194 "__result__",
195 fac.NewLiteral(5, types.False),
196 fac.NewLiteral(6, types.True),
197 fac.NewCall(7, "_||_",
198 fac.NewAccuIdent(8),
199 fac.NewCall(9, "<", fac.NewIdent(10, "i"), fac.NewLiteral(11, types.Int(3))),
200 ),
201 fac.NewAccuIdent(12),
202 )
203 comp := expr.AsComprehension()
204 if comp.IterRange().Kind() != ast.ListKind {
205 t.Errorf("IterRange() got %v, wanted list", comp.IterRange().Kind())
206 }
207 if comp.AccuInit().Kind() != ast.LiteralKind {
208 t.Errorf("AccuInit() got %v, wanted literal", comp.AccuInit().Kind())
209 }
210 if comp.LoopCondition().Kind() != ast.LiteralKind {
211 t.Errorf("LoopCondition() got %v, wanted literal", comp.LoopCondition().Kind())
212 }
213 if comp.LoopStep().Kind() != ast.CallKind {
214 t.Errorf("LoopStep() got %v, wanted call", comp.LoopStep().Kind())
215 }
216 if comp.Result().Kind() != ast.IdentKind {
217 t.Errorf("Result() got %v, wanted identifier", comp.Result().Kind())
218 }
219 expr.RenumberIDs(testIDGen(100))
220 if expr.ID() != 101 {
221 t.Errorf("Renumbering an unspecified expression mutated the value: %v", expr)
222 }
223 comp = expr.AsComprehension()
224 if comp.IterRange().ID() != 102 {
225 t.Errorf("IterRange() got %v, wanted list", comp.IterRange().ID())
226 }
227 if comp.AccuInit().ID() != 105 {
228 t.Errorf("AccuInit() got %v, wanted literal", comp.AccuInit().ID())
229 }
230 if comp.LoopCondition().ID() != 106 {
231 t.Errorf("LoopCondition() got %v, wanted literal", comp.LoopCondition().ID())
232 }
233 if comp.LoopStep().ID() != 107 {
234 t.Errorf("LoopStep() got %v, wanted call", comp.LoopStep().ID())
235 }
236 if comp.Result().ID() != 112 {
237 t.Errorf("Result() got %v, wanted identifier", comp.Result().ID())
238 }
239}
240
241func TestComprehensionNil(t *testing.T) {
242 expr := nilTestExpr(t)

Callers

nothing calls this directly

Calls 15

NewComprehensionMethod · 0.95
NewListMethod · 0.95
NewLiteralMethod · 0.95
NewCallMethod · 0.95
NewAccuIdentMethod · 0.95
NewIdentMethod · 0.95
NewExprFactoryFunction · 0.92
IntTypeAlias · 0.92
testIDGenFunction · 0.85
AsComprehensionMethod · 0.65
KindMethod · 0.65
IterRangeMethod · 0.65

Tested by

no test coverage detected