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

Function TestDynamicDispatch

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

Source from the content-addressed store, hash-verified

2990}
2991
2992func TestDynamicDispatch(t *testing.T) {
2993 env := testEnv(t,
2994 HomogeneousAggregateLiterals(),
2995 Function("first",
2996 MemberOverload("first_list_int", []*Type{ListType(IntType)}, IntType,
2997 UnaryBinding(func(list ref.Val) ref.Val {
2998 l := list.(traits.Lister)
2999 if l.Size() == types.IntZero {
3000 return types.IntZero
3001 }
3002 return l.Get(types.IntZero)
3003 }),
3004 ),
3005 MemberOverload("first_list_double", []*Type{ListType(DoubleType)}, DoubleType,
3006 UnaryBinding(func(list ref.Val) ref.Val {
3007 l := list.(traits.Lister)
3008 if l.Size() == types.IntZero {
3009 return types.Double(0.0)
3010 }
3011 return l.Get(types.IntZero)
3012 }),
3013 ),
3014 MemberOverload("first_list_string", []*Type{ListType(StringType)}, StringType,
3015 UnaryBinding(func(list ref.Val) ref.Val {
3016 l := list.(traits.Lister)
3017 if l.Size() == types.IntZero {
3018 return types.String("")
3019 }
3020 return l.Get(types.IntZero)
3021 }),
3022 ),
3023 MemberOverload("first_list_list_string", []*Type{ListType(ListType(StringType))}, ListType(StringType),
3024 UnaryBinding(func(list ref.Val) ref.Val {
3025 l := list.(traits.Lister)
3026 if l.Size() == types.IntZero {
3027 return types.DefaultTypeAdapter.NativeToValue([]string{})
3028 }
3029 return l.Get(types.IntZero)
3030 }),
3031 ),
3032 ),
3033 )
3034 out, err := interpret(t, env, `
3035 dyn([]).first() == 0
3036 && [1, 2].first() == 1
3037 && [1.0, 2.0].first() == 1.0
3038 && ["hello", "world"].first() == "hello"
3039 && [["hello"], ["world", "!"]].first().first() == "hello"
3040 && [[], ["empty"]].first().first() == ""
3041 && dyn([1, 2]).first() == 1
3042 && dyn([1.0, 2.0]).first() == 1.0
3043 && dyn(["hello", "world"]).first() == "hello"
3044 && dyn([["hello"], ["world", "!"]]).first().first() == "hello"
3045 `, map[string]any{},
3046 )
3047 if err != nil {
3048 t.Fatalf("prg.Eval() failed: %v", err)
3049 }

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