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

Function TestBinaryBinding

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

Source from the content-addressed store, hash-verified

406}
407
408func TestBinaryBinding(t *testing.T) {
409 e, err := NewCustomEnv(
410 Function("max",
411 Overload("max_int_int", []*Type{IntType, IntType}, IntType,
412 OverloadIsNonStrict(),
413 BinaryBinding(func(arg1, arg2 ref.Val) ref.Val {
414 if types.IsUnknownOrError(arg1) {
415 return arg2
416 }
417 if types.IsUnknownOrError(arg2) {
418 return arg1
419 }
420 i1 := arg1.(types.Int)
421 i2 := arg2.(types.Int)
422 if i1 > i2 {
423 return i1
424 }
425 return i2
426 }),
427 ),
428 ),
429 Variable("x", IntType),
430 Variable("y", IntType),
431 )
432 if err != nil {
433 t.Fatalf("NewCustomEnv() failed: %v", err)
434 }
435 ast, iss := e.Parse("max(x, y)")
436 if iss.Err() != nil {
437 t.Fatalf("Parse(max(x, y))) failed: %v", iss.Err())
438 }
439 prg, err := e.Program(ast)
440 if err != nil {
441 t.Fatalf("Program() failed: %v", err)
442 }
443 out, _, err := prg.Eval(map[string]any{"x": types.NewUnknown(1, nil), "y": 1})
444 if err != nil {
445 t.Fatalf("prg.Eval(x=unk) failed: %v", err)
446 }
447 if !reflect.DeepEqual(out, types.IntOne) {
448 t.Errorf("prg.Eval(x=unk, y=1) returned %v, wanted 1", out)
449 }
450 out, _, err = prg.Eval(map[string]any{"x": 2, "y": types.NewUnknown(2, nil)})
451 if err != nil {
452 t.Fatalf("prg.Eval(x=2, y=unk) failed: %v", err)
453 }
454 if !reflect.DeepEqual(out, types.Int(2)) {
455 t.Errorf("prg.Eval(x=2, y=unk) returned %v, wanted 2", out)
456 }
457 out, _, err = prg.Eval(map[string]any{"x": 2, "y": 1})
458 if err != nil {
459 t.Fatalf("prg.Eval(x=2, y=1) failed: %v", err)
460 }
461 if !reflect.DeepEqual(out, types.Int(2)) {
462 t.Errorf("prg.Eval(x=2, y=1) returned %v, wanted 2", out)
463 }
464
465 _, err = NewCustomEnv(

Callers

nothing calls this directly

Calls 15

IsUnknownOrErrorFunction · 0.92
NewUnknownFunction · 0.92
IntTypeAlias · 0.92
NewCustomEnvFunction · 0.85
ErrMethod · 0.80
ProgramMethod · 0.80
FunctionFunction · 0.70
OverloadFunction · 0.70
OverloadIsNonStrictFunction · 0.70
BinaryBindingFunction · 0.70
VariableFunction · 0.70
ParseMethod · 0.65

Tested by

no test coverage detected