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

Function TestOverrideValidator

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

Source from the content-addressed store, hash-verified

419}
420
421func TestOverrideValidator(t *testing.T) {
422 // Base environment configured with comprehension nesting limit of 2.
423 baseEnv, err := NewEnv(
424 ASTValidators(ValidateComprehensionNestingLimit(2)),
425 )
426 if err != nil {
427 t.Fatalf("NewEnv() failed: %v", err)
428 }
429
430 expr := `[1, 2, 3].map(i, [4, 5, 6].map(j, [7, 8, 9].map(k, i * j * k)))`
431
432 // Fails in base environment with limit 2.
433 _, iss := baseEnv.Compile(expr)
434 if iss.Err() == nil {
435 t.Fatalf("baseEnv.Compile() succeeded, expected nesting limit error")
436 }
437
438 // Extend environment overriding limit to 3.
439 extEnv, err := baseEnv.Extend(
440 ASTValidators(ValidateComprehensionNestingLimit(3)),
441 )
442 if err != nil {
443 t.Fatalf("baseEnv.Extend() failed: %v", err)
444 }
445
446 // Succeeds in extended environment with limit 3.
447 _, iss = extEnv.Compile(expr)
448 if iss.Err() != nil {
449 t.Fatalf("extEnv.Compile() failed: %v", iss.Err())
450 }
451
452 // Verify base environment still fails (immutability check).
453 _, iss = baseEnv.Compile(expr)
454 if iss.Err() == nil {
455 t.Fatalf("baseEnv.Compile() succeeded after Extend, expected baseEnv to remain unchanged")
456 }
457
458 // Extend environment overriding limit to 1 (stricter limit).
459 stricterEnv, err := extEnv.Extend(
460 ASTValidators(ValidateComprehensionNestingLimit(1)),
461 )
462 if err != nil {
463 t.Fatalf("extEnv.Extend() failed: %v", err)
464 }
465
466 expr2 := `[1, 2, 3].exists(i, [4, 5, 6].filter(j, j % i != 0).size() > 0)`
467 // 2 levels deep: succeeds in extEnv (limit 3), fails in stricterEnv (limit 1).
468 _, iss = extEnv.Compile(expr2)
469 if iss.Err() != nil {
470 t.Fatalf("extEnv.Compile() for 2 levels failed: %v", iss.Err())
471 }
472 _, iss = stricterEnv.Compile(expr2)
473 if iss.Err() == nil {
474 t.Fatalf("stricterEnv.Compile() for 2 levels succeeded, expected error")
475 }
476}
477
478func 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