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

Function TestLateBoundFunctionNonStrict

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

Source from the content-addressed store, hash-verified

6370}
6371
6372func TestLateBoundFunctionNonStrict(t *testing.T) {
6373 env, err := NewEnv(
6374 Variable("x", IntType),
6375 Function("observe",
6376 Overload("observe_int", []*Type{IntType}, StringType,
6377 LateFunctionBinding(), OverloadIsNonStrict()),
6378 ),
6379 )
6380 if err != nil {
6381 t.Fatalf("NewEnv() failed: %v", err)
6382 }
6383 ast, iss := env.Compile("observe(x)")
6384 if iss.Err() != nil {
6385 t.Fatalf("Compile() failed: %v", iss.Err())
6386 }
6387 prg, err := env.Program(ast, EvalOptions(OptPartialEval))
6388 if err != nil {
6389 t.Fatalf("Program() failed: %v", err)
6390 }
6391 // A non-strict overload accepts unknown arguments, so the signature check must not reject
6392 // an operand which is unknown rather than the declared type.
6393 sawUnknown := false
6394 partial, err := PartialVars(map[string]any{}, AttributePattern("x"))
6395 if err != nil {
6396 t.Fatalf("PartialVars() failed: %v", err)
6397 }
6398 act, err := FunctionVars(partial, map[string]LateBoundFunction{
6399 "observe": func(overloadID string, args ...ref.Val) ref.Val {
6400 sawUnknown = types.IsUnknown(args[0])
6401 return types.String("observed")
6402 },
6403 })
6404 if err != nil {
6405 t.Fatalf("FunctionVars() failed: %v", err)
6406 }
6407 out, _, err := prg.Eval(act)
6408 if err != nil {
6409 t.Fatalf("Eval() failed: %v", err)
6410 }
6411 if !sawUnknown {
6412 t.Error("non-strict late binding was not invoked with the unknown argument")
6413 }
6414 if out.Equal(types.String("observed")) != types.True {
6415 t.Errorf("Eval() got %v, wanted 'observed'", out)
6416 }
6417}
6418
6419// lateBoundComprehensionEnv declares the late-bound functions exercised by the comprehension
6420// tests. Comprehensions install a nested scope (a folder) on top of the caller's activation, so

Callers

nothing calls this directly

Calls 15

CompileMethod · 0.95
ProgramMethod · 0.95
IsUnknownFunction · 0.92
StringTypeAlias · 0.92
EvalOptionsFunction · 0.85
PartialVarsFunction · 0.85
FunctionVarsFunction · 0.85
ErrMethod · 0.80
NewEnvFunction · 0.70
VariableFunction · 0.70
FunctionFunction · 0.70
OverloadFunction · 0.70

Tested by

no test coverage detected