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

Function TestFunctionBindings

common/decls/decls_test.go:36–88  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

34)
35
36func TestFunctionBindings(t *testing.T) {
37 sizeFunc, err := NewFunction("size",
38 MemberOverload("list_size",
39 []*types.Type{types.NewListType(types.NewTypeParamType("T"))}, types.IntType),
40 )
41 if err != nil {
42 t.Fatalf("NewFunction() failed: %v", err)
43 }
44 bindings, err := sizeFunc.Bindings()
45 if err != nil {
46 t.Fatalf("sizeFunc.Bindings() produced an err: %v", err)
47 }
48 if len(bindings) != 0 {
49 t.Errorf("sizeFunc.Bindings() produced %d bindings, wanted none", len(bindings))
50 }
51 sizeFuncDef, err := NewFunction("size",
52 MemberOverload("list_size",
53 []*types.Type{types.NewListType(types.NewTypeParamType("T"))}, types.IntType,
54 UnaryBinding(func(list ref.Val) ref.Val {
55 sizer := list.(traits.Sizer)
56 return sizer.Size()
57 }),
58 ),
59 )
60 if err != nil {
61 t.Fatalf("NewFunction() failed: %v", err)
62 }
63 sizeMerged, err := sizeFunc.Merge(sizeFuncDef)
64 if err != nil {
65 t.Fatalf("Merge() failed: %v", err)
66 }
67 bindings, err = sizeMerged.Bindings()
68 if err != nil {
69 t.Fatalf("sizeFunc.Bindings() produced an err: %v", err)
70 }
71 if len(bindings) != 2 {
72 t.Errorf("sizeFunc.Bindings() got %d bindings, wanted 2", len(bindings))
73 }
74 empty := types.DefaultTypeAdapter.NativeToValue([]string{})
75 in := types.DefaultTypeAdapter.NativeToValue([]string{"1", "2"})
76 for _, binding := range bindings {
77 if binding.Unary == nil {
78 t.Errorf("binding missing unary implementation: %v", binding.Operator)
79 continue
80 }
81 if binding.Unary(in) != types.Int(2) {
82 t.Errorf("binding invocation got %v, wanted 2", binding.Unary(in))
83 }
84 if binding.Unary(empty) != types.IntZero {
85 t.Errorf("binding invocation got %v, wanted 0", binding.Unary(empty))
86 }
87 }
88}
89
90func TestFunctionVariableArgBindings(t *testing.T) {
91 splitImpl := func(str, delim string, count int64) ref.Val {

Callers

nothing calls this directly

Calls 11

NewListTypeFunction · 0.92
NewTypeParamTypeFunction · 0.92
IntTypeAlias · 0.92
BindingsMethod · 0.80
MergeMethod · 0.80
NewFunctionFunction · 0.70
MemberOverloadFunction · 0.70
UnaryBindingFunction · 0.70
SizeMethod · 0.65
NativeToValueMethod · 0.65
UnaryMethod · 0.65

Tested by

no test coverage detected