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

Function TestUnaryBinding

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

Source from the content-addressed store, hash-verified

358}
359
360func TestUnaryBinding(t *testing.T) {
361 _, err := NewCustomEnv(
362 Function("dyn",
363 Overload("dyn", []*Type{}, DynType,
364 // wrong arg count
365 UnaryBinding(func(arg ref.Val) ref.Val { return arg }),
366 ),
367 ),
368 )
369 if err == nil || !strings.Contains(err.Error(), "function bound to non-unary overload") {
370 t.Errorf("bad binding did not produce expected error: %v", err)
371 }
372
373 e, err := NewCustomEnv(
374 Function("size",
375 Overload("size_non_strict", []*Type{ListType(DynType)}, IntType,
376 OverloadIsNonStrict(),
377 OverloadOperandTrait(traits.SizerType),
378 UnaryBinding(func(arg ref.Val) ref.Val {
379 if types.IsUnknownOrError(arg) {
380 return arg
381 }
382 return arg.(traits.Sizer).Size()
383 }),
384 ),
385 ),
386 Variable("x", ListType(DynType)),
387 )
388 if err != nil {
389 t.Fatalf("NewCustomEnv() failed: %v", err)
390 }
391 ast, iss := e.Compile("size(x)")
392 if iss.Err() != nil {
393 t.Fatalf("Compile(size(x)) failed: %v", iss.Err())
394 }
395 prg, err := e.Program(ast)
396 if err != nil {
397 t.Fatalf("Program() failed: %v", err)
398 }
399 out, _, err := prg.Eval(map[string]any{"x": types.NewUnknown(1, nil)})
400 if err != nil {
401 t.Fatalf("prg.Eval(x=unk) failed: %v", err)
402 }
403 if !types.NewUnknown(1, nil).Contains(out.(*types.Unknown)) {
404 t.Errorf("prg.Eval(x=unk) returned %v, wanted unknown{1}", out)
405 }
406}
407
408func TestBinaryBinding(t *testing.T) {
409 e, err := NewCustomEnv(

Callers

nothing calls this directly

Calls 15

IsUnknownOrErrorFunction · 0.92
NewUnknownFunction · 0.92
NewCustomEnvFunction · 0.85
ErrMethod · 0.80
ProgramMethod · 0.80
FunctionFunction · 0.70
OverloadFunction · 0.70
UnaryBindingFunction · 0.70
OverloadIsNonStrictFunction · 0.70
OverloadOperandTraitFunction · 0.70
VariableFunction · 0.70
ContainsMethod · 0.65

Tested by

no test coverage detected