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

Function TestDynamicDispatch

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

Source from the content-addressed store, hash-verified

2795}
2796
2797func TestDynamicDispatch(t *testing.T) {
2798 env := testEnv(t,
2799 HomogeneousAggregateLiterals(),
2800 Function("first",
2801 MemberOverload("first_list_int", []*Type{ListType(IntType)}, IntType,
2802 UnaryBinding(func(list ref.Val) ref.Val {
2803 l := list.(traits.Lister)
2804 if l.Size() == types.IntZero {
2805 return types.IntZero
2806 }
2807 return l.Get(types.IntZero)
2808 }),
2809 ),
2810 MemberOverload("first_list_double", []*Type{ListType(DoubleType)}, DoubleType,
2811 UnaryBinding(func(list ref.Val) ref.Val {
2812 l := list.(traits.Lister)
2813 if l.Size() == types.IntZero {
2814 return types.Double(0.0)
2815 }
2816 return l.Get(types.IntZero)
2817 }),
2818 ),
2819 MemberOverload("first_list_string", []*Type{ListType(StringType)}, StringType,
2820 UnaryBinding(func(list ref.Val) ref.Val {
2821 l := list.(traits.Lister)
2822 if l.Size() == types.IntZero {
2823 return types.String("")
2824 }
2825 return l.Get(types.IntZero)
2826 }),
2827 ),
2828 MemberOverload("first_list_list_string", []*Type{ListType(ListType(StringType))}, ListType(StringType),
2829 UnaryBinding(func(list ref.Val) ref.Val {
2830 l := list.(traits.Lister)
2831 if l.Size() == types.IntZero {
2832 return types.DefaultTypeAdapter.NativeToValue([]string{})
2833 }
2834 return l.Get(types.IntZero)
2835 }),
2836 ),
2837 ),
2838 )
2839 out, err := interpret(t, env, `
2840 dyn([]).first() == 0
2841 && [1, 2].first() == 1
2842 && [1.0, 2.0].first() == 1.0
2843 && ["hello", "world"].first() == "hello"
2844 && [["hello"], ["world", "!"]].first().first() == "hello"
2845 && [[], ["empty"]].first().first() == ""
2846 && dyn([1, 2]).first() == 1
2847 && dyn([1.0, 2.0]).first() == 1.0
2848 && dyn(["hello", "world"]).first() == "hello"
2849 && dyn([["hello"], ["world", "!"]]).first().first() == "hello"
2850 `, map[string]any{},
2851 )
2852 if err != nil {
2853 t.Fatalf("prg.Eval() failed: %v", err)
2854 }

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