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

Function TestLateBoundFunctionOverloadID

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

Source from the content-addressed store, hash-verified

6212}
6213
6214func TestLateBoundFunctionOverloadID(t *testing.T) {
6215 env, err := NewEnv(
6216 Function("describe",
6217 Overload("describe_int", []*Type{IntType}, StringType, LateFunctionBinding()),
6218 Overload("describe_string", []*Type{StringType}, StringType, LateFunctionBinding()),
6219 MemberOverload("describe_int_member", []*Type{IntType}, StringType, LateFunctionBinding()),
6220 ),
6221 )
6222 if err != nil {
6223 t.Fatalf("NewEnv() failed: %v", err)
6224 }
6225 tests := []struct {
6226 name string
6227 expr string
6228 parsed bool
6229 want string
6230 }{
6231 {name: "global int overload", expr: "describe(1)", want: "describe_int"},
6232 {name: "global string overload", expr: "describe('a')", want: "describe_string"},
6233 {name: "member overload", expr: "(1).describe()", want: "describe_int_member"},
6234 // Without a checked AST the overload is resolved from the runtime argument types and
6235 // the receiver style of the call.
6236 {name: "unchecked global int overload", expr: "describe(1)", parsed: true, want: "describe_int"},
6237 {name: "unchecked global string overload", expr: "describe('a')", parsed: true, want: "describe_string"},
6238 {name: "unchecked member overload", expr: "(1).describe()", parsed: true, want: "describe_int_member"},
6239 }
6240 for _, tc := range tests {
6241 t.Run(tc.name, func(t *testing.T) {
6242 var ast *Ast
6243 var iss *Issues
6244 if tc.parsed {
6245 ast, iss = env.Parse(tc.expr)
6246 } else {
6247 ast, iss = env.Compile(tc.expr)
6248 }
6249 if iss.Err() != nil {
6250 t.Fatalf("Compile(%q) failed: %v", tc.expr, iss.Err())
6251 }
6252 prg, err := env.Program(ast)
6253 if err != nil {
6254 t.Fatalf("Program() failed: %v", err)
6255 }
6256 act, err := FunctionVars(NoVars(), map[string]LateBoundFunction{
6257 "describe": func(overloadID string, args ...ref.Val) ref.Val {
6258 return types.String(overloadID)
6259 },
6260 })
6261 if err != nil {
6262 t.Fatalf("FunctionVars() failed: %v", err)
6263 }
6264 out, _, err := prg.Eval(act)
6265 if err != nil {
6266 t.Fatalf("Eval() failed: %v", err)
6267 }
6268 if out.Equal(types.String(tc.want)) != types.True {
6269 t.Errorf("Eval() got %v, wanted %v", out, tc.want)
6270 }
6271 })

Callers

nothing calls this directly

Calls 14

ParseMethod · 0.95
CompileMethod · 0.95
ErrMethod · 0.95
ProgramMethod · 0.95
StringTypeAlias · 0.92
FunctionVarsFunction · 0.85
NoVarsFunction · 0.85
NewEnvFunction · 0.70
FunctionFunction · 0.70
OverloadFunction · 0.70
LateFunctionBindingFunction · 0.70
MemberOverloadFunction · 0.70

Tested by

no test coverage detected