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

Function TestFunctionBinding

cel/decls_test.go:478–526  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

476}
477
478func TestFunctionBinding(t *testing.T) {
479 e, err := NewCustomEnv(
480 Variable("unk", DynType),
481 Variable("err", DynType),
482 Function("dyn",
483 Overload("dyn", []*Type{DynType}, DynType),
484 SingletonUnaryBinding(func(arg ref.Val) ref.Val {
485 return arg
486 })),
487 Function("max",
488 Overload("max_int", []*Type{IntType}, IntType,
489 UnaryBinding(func(arg ref.Val) ref.Val { return arg })),
490 Overload("max_int_int", []*Type{IntType, IntType}, IntType,
491 BinaryBinding(func(lhs, rhs ref.Val) ref.Val {
492 if lhs.(types.Int).Compare(rhs) == types.IntNegOne {
493 return rhs
494 }
495 return lhs
496 })),
497 Overload("max_int_int_int", []*Type{IntType, IntType, IntType}, IntType,
498 FunctionBinding(func(args ...ref.Val) ref.Val {
499 max := types.Int(math.MinInt64)
500 for _, arg := range args {
501 i := arg.(types.Int)
502 if i > max {
503 max = i
504 }
505 }
506 return max
507 })),
508 ),
509 )
510 if err != nil {
511 t.Fatalf("NewCustomEnv() failed: %v", err)
512 }
513
514 for _, tc := range dispatchTests {
515 tc := tc
516 t.Run(fmt.Sprintf("Parse(%s)", tc.expr), func(t *testing.T) {
517 testParse(t, e, tc.expr, tc.out)
518 })
519 }
520 for _, tc := range dispatchTests {
521 tc := tc
522 t.Run(fmt.Sprintf("Compile(%s)", tc.expr), func(t *testing.T) {
523 testCompile(t, e, tc.expr, tc.out)
524 })
525 }
526}
527
528func TestFunctionDisableDeclaration(t *testing.T) {
529 e, err := NewCustomEnv(

Callers

nothing calls this directly

Calls 12

IntTypeAlias · 0.92
NewCustomEnvFunction · 0.85
testParseFunction · 0.85
testCompileFunction · 0.85
VariableFunction · 0.70
FunctionFunction · 0.70
OverloadFunction · 0.70
SingletonUnaryBindingFunction · 0.70
UnaryBindingFunction · 0.70
BinaryBindingFunction · 0.70
FunctionBindingFunction · 0.70
CompareMethod · 0.65

Tested by

no test coverage detected