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

Function TestExtendedValidations

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

Source from the content-addressed store, hash-verified

347}
348
349func TestExtendedValidations(t *testing.T) {
350 env, err := NewEnv(
351 Variable("x", types.StringType),
352 ExtendedValidations(),
353 )
354 if err != nil {
355 t.Fatalf("NewEnv(ExtendedValidations()) failed: %v", err)
356 }
357 tests := []struct {
358 expr string
359 iss string
360 }{
361 {
362 expr: `x in ['hello', 0]
363 && duration(x) < duration('1d')
364 && timestamp(x) != timestamp('1000-01-00T00:00:00Z')
365 && x.matches('x++')`,
366 iss: `
367 ERROR: <input>:1:16: expected type 'string' but found 'int'
368 | x in ['hello', 0]
369 | ...............^
370 ERROR: <input>:2:30: invalid duration argument
371 | && duration(x) < duration('1d')
372 | .............................^
373 ERROR: <input>:3:33: invalid timestamp argument
374 | && timestamp(x) != timestamp('1000-01-00T00:00:00Z')
375 | ................................^
376 ERROR: <input>:4:17: invalid matches argument
377 | && x.matches('x++')
378 | ................^`,
379 },
380 }
381 for _, tst := range tests {
382 tc := tst
383 t.Run(tc.expr, func(t *testing.T) {
384 _, iss := env.Compile(tc.expr)
385 if tc.iss != "" {
386 if iss.Err() == nil {
387 t.Fatalf("e.Compile(%v) returned ast, expected error: %v", tc.expr, tc.iss)
388 }
389 if !test.Compare(iss.Err().Error(), tc.iss) {
390 t.Fatalf("e.Compile(%v) returned %v, expected error: %v", tc.expr, iss.Err(), tc.iss)
391 }
392 return
393 }
394 if iss.Err() != nil {
395 t.Fatalf("e.Compile(%v) failed: %v", tc.expr, iss.Err())
396 }
397 })
398 }
399}
400
401func TestValidatorConfig(t *testing.T) {
402 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