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

Function TestLateBoundFunctions

cel/cel_test.go:5892–6052  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

5890 }
5891}
5892func TestLateBoundFunctions(t *testing.T) {
5893 env, err := NewEnv(
5894 Variable("x", IntType),
5895 Variable("y", IntType),
5896 Function("inc",
5897 Overload("inc_int", []*Type{IntType}, IntType, LateFunctionBinding()),
5898 ),
5899 Function("add",
5900 Overload("add_int_int", []*Type{IntType, IntType}, IntType, LateFunctionBinding()),
5901 ),
5902 Function("sum",
5903 Overload("sum_int_int_int", []*Type{IntType, IntType, IntType}, IntType, LateFunctionBinding()),
5904 ),
5905 Function("now_unix",
5906 Overload("now_unix_zero", []*Type{}, IntType, LateFunctionBinding()),
5907 ),
5908 Function("tag",
5909 MemberOverload("tag_int_string", []*Type{IntType, StringType}, IntType, LateFunctionBinding()),
5910 ),
5911 )
5912 if err != nil {
5913 t.Fatalf("NewEnv() failed: %v", err)
5914 }
5915
5916 tests := []struct {
5917 name string
5918 expr string
5919 vars map[string]any
5920 funcs map[string]LateBoundFunction
5921 want ref.Val
5922 wantErr string
5923 }{
5924 {
5925 name: "unary call",
5926 expr: "inc(x)",
5927 vars: map[string]any{"x": 5},
5928 funcs: map[string]LateBoundFunction{
5929 "inc": func(overloadID string, args ...ref.Val) ref.Val {
5930 return types.Int(args[0].(types.Int) + 1)
5931 },
5932 },
5933 want: types.Int(6),
5934 },
5935 {
5936 name: "unary missing from activation",
5937 expr: "inc(x)",
5938 vars: map[string]any{"x": 5},
5939 wantErr: "no such overload: inc",
5940 },
5941 {
5942 name: "unary shadowed by a variable of the same name",
5943 expr: "inc(x)",
5944 vars: map[string]any{"x": 5, "inc": "not a function"},
5945 funcs: map[string]LateBoundFunction{
5946 "inc": func(overloadID string, args ...ref.Val) ref.Val {
5947 return types.Int(args[0].(types.Int) + 1)
5948 },
5949 },

Callers

nothing calls this directly

Calls 15

CompileMethod · 0.95
ProgramMethod · 0.95
IntTypeAlias · 0.92
FunctionVarsFunction · 0.85
ErrMethod · 0.80
NewEnvFunction · 0.70
VariableFunction · 0.70
FunctionFunction · 0.70
OverloadFunction · 0.70
LateFunctionBindingFunction · 0.70
MemberOverloadFunction · 0.70
EvalMethod · 0.65

Tested by

no test coverage detected