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

Function TestValidateComprehensionNestingLimit

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

Source from the content-addressed store, hash-verified

364}
365
366func TestValidateComprehensionNestingLimit(t *testing.T) {
367 env, err := NewEnv(
368 ASTValidators(ValidateComprehensionNestingLimit(2)),
369 )
370 if err != nil {
371 t.Fatalf("NewEnv() failed: %v", err)
372 }
373 tests := []struct {
374 expr string
375 iss string
376 }{
377 {
378 expr: `[1, 2, 3].exists(i, i < 1)`,
379 },
380 {
381 expr: `[1, 2, 3].exists(i, [4, 5, 6].filter(j, j % i != 0).size() > 0)`,
382 },
383 {
384 // three comprehensions, but not three levels deep
385 expr: `[1, 2, 3].exists(i, [4, 5, 6].filter(j, j % i != 0).size() > 0) && [1, 2, 3].exists(i, i < 1)`,
386 },
387 {
388 // the empty iteration range in [].all(k, k) does not impact the actual runtime complexity,
389 // so it does not trip the comprehension limit.
390 expr: `[1, 2, 3].exists(i, [4, 5, 6].filter(j, [].all(k, k) && j % i != 0).size() > 0)`,
391 },
392 {
393 // three comprehensions, three levels deep
394 expr: `[1, 2, 3].map(i, [4, 5, 6].map(j, [7, 8, 9].map(k, i * j * k)))`,
395 iss: `
396 ERROR: <input>:1:48: comprehension exceeds nesting limit
397 | [1, 2, 3].map(i, [4, 5, 6].map(j, [7, 8, 9].map(k, i * j * k)))
398 | ...............................................^`,
399 },
400 }
401 for _, tst := range tests {
402 tc := tst
403 t.Run(tc.expr, func(t *testing.T) {
404 _, iss := env.Compile(tc.expr)
405 if tc.iss != "" {
406 if iss.Err() == nil {
407 t.Fatalf("e.Compile(%v) returned ast, expected error: %v", tc.expr, tc.iss)
408 }
409 if !test.Compare(iss.Err().Error(), tc.iss) {
410 t.Fatalf("e.Compile(%v) returned %v, expected error: %v", tc.expr, iss.Err(), tc.iss)
411 }
412 return
413 }
414 if iss.Err() != nil {
415 t.Fatalf("e.Compile(%v) failed: %v", tc.expr, iss.Err())
416 }
417 })
418 }
419}
420
421func TestExtendedValidations(t *testing.T) {
422 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