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

Function TestDynamicDispatch

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

Source from the content-addressed store, hash-verified

3192}
3193
3194func TestDynamicDispatch(t *testing.T) {
3195 env := testEnv(t,
3196 HomogeneousAggregateLiterals(),
3197 Function("first",
3198 MemberOverload("first_list_int", []*Type{ListType(IntType)}, IntType,
3199 UnaryBinding(func(list ref.Val) ref.Val {
3200 l := list.(traits.Lister)
3201 if l.Size() == types.IntZero {
3202 return types.IntZero
3203 }
3204 return l.Get(types.IntZero)
3205 }),
3206 ),
3207 MemberOverload("first_list_double", []*Type{ListType(DoubleType)}, DoubleType,
3208 UnaryBinding(func(list ref.Val) ref.Val {
3209 l := list.(traits.Lister)
3210 if l.Size() == types.IntZero {
3211 return types.Double(0.0)
3212 }
3213 return l.Get(types.IntZero)
3214 }),
3215 ),
3216 MemberOverload("first_list_string", []*Type{ListType(StringType)}, StringType,
3217 UnaryBinding(func(list ref.Val) ref.Val {
3218 l := list.(traits.Lister)
3219 if l.Size() == types.IntZero {
3220 return types.String("")
3221 }
3222 return l.Get(types.IntZero)
3223 }),
3224 ),
3225 MemberOverload("first_list_list_string", []*Type{ListType(ListType(StringType))}, ListType(StringType),
3226 UnaryBinding(func(list ref.Val) ref.Val {
3227 l := list.(traits.Lister)
3228 if l.Size() == types.IntZero {
3229 return types.DefaultTypeAdapter.NativeToValue([]string{})
3230 }
3231 return l.Get(types.IntZero)
3232 }),
3233 ),
3234 ),
3235 )
3236 out, err := interpret(t, env, `
3237 dyn([]).first() == 0
3238 && [1, 2].first() == 1
3239 && [1.0, 2.0].first() == 1.0
3240 && ["hello", "world"].first() == "hello"
3241 && [["hello"], ["world", "!"]].first().first() == "hello"
3242 && [[], ["empty"]].first().first() == ""
3243 && dyn([1, 2]).first() == 1
3244 && dyn([1.0, 2.0]).first() == 1.0
3245 && dyn(["hello", "world"]).first() == "hello"
3246 && dyn([["hello"], ["world", "!"]]).first().first() == "hello"
3247 `, map[string]any{},
3248 )
3249 if err != nil {
3250 t.Fatalf("prg.Eval() failed: %v", err)
3251 }

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