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

Function TestValidateComprehensionNestingLimit

cel/validator_test.go:294–347  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

292}
293
294func TestValidateComprehensionNestingLimit(t *testing.T) {
295 env, err := NewEnv(
296 ASTValidators(ValidateComprehensionNestingLimit(2)),
297 )
298 if err != nil {
299 t.Fatalf("NewEnv() failed: %v", err)
300 }
301 tests := []struct {
302 expr string
303 iss string
304 }{
305 {
306 expr: `[1, 2, 3].exists(i, i < 1)`,
307 },
308 {
309 expr: `[1, 2, 3].exists(i, [4, 5, 6].filter(j, j % i != 0).size() > 0)`,
310 },
311 {
312 // three comprehensions, but not three levels deep
313 expr: `[1, 2, 3].exists(i, [4, 5, 6].filter(j, j % i != 0).size() > 0) && [1, 2, 3].exists(i, i < 1)`,
314 },
315 {
316 // the empty iteration range in [].all(k, k) does not impact the actual runtime complexity,
317 // so it does not trip the comprehension limit.
318 expr: `[1, 2, 3].exists(i, [4, 5, 6].filter(j, [].all(k, k) && j % i != 0).size() > 0)`,
319 },
320 {
321 // three comprehensions, three levels deep
322 expr: `[1, 2, 3].map(i, [4, 5, 6].map(j, [7, 8, 9].map(k, i * j * k)))`,
323 iss: `
324 ERROR: <input>:1:48: comprehension exceeds nesting limit
325 | [1, 2, 3].map(i, [4, 5, 6].map(j, [7, 8, 9].map(k, i * j * k)))
326 | ...............................................^`,
327 },
328 }
329 for _, tst := range tests {
330 tc := tst
331 t.Run(tc.expr, func(t *testing.T) {
332 _, iss := env.Compile(tc.expr)
333 if tc.iss != "" {
334 if iss.Err() == nil {
335 t.Fatalf("e.Compile(%v) returned ast, expected error: %v", tc.expr, tc.iss)
336 }
337 if !test.Compare(iss.Err().Error(), tc.iss) {
338 t.Fatalf("e.Compile(%v) returned %v, expected error: %v", tc.expr, iss.Err(), tc.iss)
339 }
340 return
341 }
342 if iss.Err() != nil {
343 t.Fatalf("e.Compile(%v) failed: %v", tc.expr, iss.Err())
344 }
345 })
346 }
347}
348
349func TestExtendedValidations(t *testing.T) {
350 env, err := NewEnv(

Callers

nothing calls this directly

Calls 7

CompileMethod · 0.95
CompareFunction · 0.92
ASTValidatorsFunction · 0.85
ErrMethod · 0.80
NewEnvFunction · 0.70
ErrorMethod · 0.45

Tested by

no test coverage detected