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

Function TestDynamicDispatch

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

Source from the content-addressed store, hash-verified

3048}
3049
3050func TestDynamicDispatch(t *testing.T) {
3051 env := testEnv(t,
3052 HomogeneousAggregateLiterals(),
3053 Function("first",
3054 MemberOverload("first_list_int", []*Type{ListType(IntType)}, IntType,
3055 UnaryBinding(func(list ref.Val) ref.Val {
3056 l := list.(traits.Lister)
3057 if l.Size() == types.IntZero {
3058 return types.IntZero
3059 }
3060 return l.Get(types.IntZero)
3061 }),
3062 ),
3063 MemberOverload("first_list_double", []*Type{ListType(DoubleType)}, DoubleType,
3064 UnaryBinding(func(list ref.Val) ref.Val {
3065 l := list.(traits.Lister)
3066 if l.Size() == types.IntZero {
3067 return types.Double(0.0)
3068 }
3069 return l.Get(types.IntZero)
3070 }),
3071 ),
3072 MemberOverload("first_list_string", []*Type{ListType(StringType)}, StringType,
3073 UnaryBinding(func(list ref.Val) ref.Val {
3074 l := list.(traits.Lister)
3075 if l.Size() == types.IntZero {
3076 return types.String("")
3077 }
3078 return l.Get(types.IntZero)
3079 }),
3080 ),
3081 MemberOverload("first_list_list_string", []*Type{ListType(ListType(StringType))}, ListType(StringType),
3082 UnaryBinding(func(list ref.Val) ref.Val {
3083 l := list.(traits.Lister)
3084 if l.Size() == types.IntZero {
3085 return types.DefaultTypeAdapter.NativeToValue([]string{})
3086 }
3087 return l.Get(types.IntZero)
3088 }),
3089 ),
3090 ),
3091 )
3092 out, err := interpret(t, env, `
3093 dyn([]).first() == 0
3094 && [1, 2].first() == 1
3095 && [1.0, 2.0].first() == 1.0
3096 && ["hello", "world"].first() == "hello"
3097 && [["hello"], ["world", "!"]].first().first() == "hello"
3098 && [[], ["empty"]].first().first() == ""
3099 && dyn([1, 2]).first() == 1
3100 && dyn([1.0, 2.0]).first() == 1.0
3101 && dyn(["hello", "world"]).first() == "hello"
3102 && dyn([["hello"], ["world", "!"]]).first().first() == "hello"
3103 `, map[string]any{},
3104 )
3105 if err != nil {
3106 t.Fatalf("prg.Eval() failed: %v", err)
3107 }

Callers

nothing calls this directly

Calls 11

DoubleTypeAlias · 0.92
StringTypeAlias · 0.92
interpretFunction · 0.85
testEnvFunction · 0.70
FunctionFunction · 0.70
MemberOverloadFunction · 0.70
UnaryBindingFunction · 0.70
SizeMethod · 0.65
GetMethod · 0.65
NativeToValueMethod · 0.65

Tested by

no test coverage detected