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

Function TestExtendedValidations

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

Source from the content-addressed store, hash-verified

419}
420
421func TestExtendedValidations(t *testing.T) {
422 env, err := NewEnv(
423 Variable("x", types.StringType),
424 ExtendedValidations(),
425 )
426 if err != nil {
427 t.Fatalf("NewEnv(ExtendedValidations()) failed: %v", err)
428 }
429 tests := []struct {
430 expr string
431 iss string
432 }{
433 {
434 expr: `x in ['hello', 0]
435 && duration(x) < duration('1d')
436 && timestamp(x) != timestamp('1000-01-00T00:00:00Z')
437 && x.matches('x++')`,
438 iss: `
439 ERROR: <input>:1:16: expected type 'string' but found 'int'
440 | x in ['hello', 0]
441 | ...............^
442 ERROR: <input>:2:30: invalid duration argument
443 | && duration(x) < duration('1d')
444 | .............................^
445 ERROR: <input>:3:33: invalid timestamp argument
446 | && timestamp(x) != timestamp('1000-01-00T00:00:00Z')
447 | ................................^
448 ERROR: <input>:4:17: invalid matches argument
449 | && x.matches('x++')
450 | ................^`,
451 },
452 }
453 for _, tst := range tests {
454 tc := tst
455 t.Run(tc.expr, func(t *testing.T) {
456 _, iss := env.Compile(tc.expr)
457 if tc.iss != "" {
458 if iss.Err() == nil {
459 t.Fatalf("e.Compile(%v) returned ast, expected error: %v", tc.expr, tc.iss)
460 }
461 if !test.Compare(iss.Err().Error(), tc.iss) {
462 t.Fatalf("e.Compile(%v) returned %v, expected error: %v", tc.expr, iss.Err(), tc.iss)
463 }
464 return
465 }
466 if iss.Err() != nil {
467 t.Fatalf("e.Compile(%v) failed: %v", tc.expr, iss.Err())
468 }
469 })
470 }
471}
472
473func TestValidatorConfig(t *testing.T) {
474 config := newValidatorConfig()

Callers

nothing calls this directly

Calls 7

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

Tested by

no test coverage detected