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

Function TestOverrideValidator

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

Source from the content-addressed store, hash-verified

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