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

Function TestOverrideValidator

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

Source from the content-addressed store, hash-verified

490}
491
492func TestOverrideValidator(t *testing.T) {
493 // Base environment configured with comprehension nesting limit of 2.
494 baseEnv, err := NewEnv(
495 ASTValidators(ValidateComprehensionNestingLimit(2)),
496 )
497 if err != nil {
498 t.Fatalf("NewEnv() failed: %v", err)
499 }
500
501 expr := `[1, 2, 3].map(i, [4, 5, 6].map(j, [7, 8, 9].map(k, i * j * k)))`
502
503 // Fails in base environment with limit 2.
504 _, iss := baseEnv.Compile(expr)
505 if iss.Err() == nil {
506 t.Fatalf("baseEnv.Compile() succeeded, expected nesting limit error")
507 }
508
509 // Extend environment overriding limit to 3.
510 extEnv, err := baseEnv.Extend(
511 ASTValidators(ValidateComprehensionNestingLimit(3)),
512 )
513 if err != nil {
514 t.Fatalf("baseEnv.Extend() failed: %v", err)
515 }
516
517 // Succeeds in extended environment with limit 3.
518 _, iss = extEnv.Compile(expr)
519 if iss.Err() != nil {
520 t.Fatalf("extEnv.Compile() failed: %v", iss.Err())
521 }
522
523 // Verify base environment still fails (immutability check).
524 _, iss = baseEnv.Compile(expr)
525 if iss.Err() == nil {
526 t.Fatalf("baseEnv.Compile() succeeded after Extend, expected baseEnv to remain unchanged")
527 }
528
529 // Extend environment overriding limit to 1 (stricter limit).
530 stricterEnv, err := extEnv.Extend(
531 ASTValidators(ValidateComprehensionNestingLimit(1)),
532 )
533 if err != nil {
534 t.Fatalf("extEnv.Extend() failed: %v", err)
535 }
536
537 expr2 := `[1, 2, 3].exists(i, [4, 5, 6].filter(j, j % i != 0).size() > 0)`
538 // 2 levels deep: succeeds in extEnv (limit 3), fails in stricterEnv (limit 1).
539 _, iss = extEnv.Compile(expr2)
540 if iss.Err() != nil {
541 t.Fatalf("extEnv.Compile() for 2 levels failed: %v", iss.Err())
542 }
543 _, iss = stricterEnv.Compile(expr2)
544 if iss.Err() == nil {
545 t.Fatalf("stricterEnv.Compile() for 2 levels succeeded, expected error")
546 }
547}
548
549func TestOverrideValidatorFromConfig(t *testing.T) {

Callers

nothing calls this directly

Calls 7

CompileMethod · 0.95
ExtendMethod · 0.95
ASTValidatorsFunction · 0.85
ErrMethod · 0.80
NewEnvFunction · 0.70
CompileMethod · 0.65

Tested by

no test coverage detected