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

Function TestFunctionBindings

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

Source from the content-addressed store, hash-verified

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