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

Function TestDynamicDispatch

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

Source from the content-addressed store, hash-verified

2775}
2776
2777func TestDynamicDispatch(t *testing.T) {
2778 env := testEnv(t,
2779 HomogeneousAggregateLiterals(),
2780 Function("first",
2781 MemberOverload("first_list_int", []*Type{ListType(IntType)}, IntType,
2782 UnaryBinding(func(list ref.Val) ref.Val {
2783 l := list.(traits.Lister)
2784 if l.Size() == types.IntZero {
2785 return types.IntZero
2786 }
2787 return l.Get(types.IntZero)
2788 }),
2789 ),
2790 MemberOverload("first_list_double", []*Type{ListType(DoubleType)}, DoubleType,
2791 UnaryBinding(func(list ref.Val) ref.Val {
2792 l := list.(traits.Lister)
2793 if l.Size() == types.IntZero {
2794 return types.Double(0.0)
2795 }
2796 return l.Get(types.IntZero)
2797 }),
2798 ),
2799 MemberOverload("first_list_string", []*Type{ListType(StringType)}, StringType,
2800 UnaryBinding(func(list ref.Val) ref.Val {
2801 l := list.(traits.Lister)
2802 if l.Size() == types.IntZero {
2803 return types.String("")
2804 }
2805 return l.Get(types.IntZero)
2806 }),
2807 ),
2808 MemberOverload("first_list_list_string", []*Type{ListType(ListType(StringType))}, ListType(StringType),
2809 UnaryBinding(func(list ref.Val) ref.Val {
2810 l := list.(traits.Lister)
2811 if l.Size() == types.IntZero {
2812 return types.DefaultTypeAdapter.NativeToValue([]string{})
2813 }
2814 return l.Get(types.IntZero)
2815 }),
2816 ),
2817 ),
2818 )
2819 out, err := interpret(t, env, `
2820 dyn([]).first() == 0
2821 && [1, 2].first() == 1
2822 && [1.0, 2.0].first() == 1.0
2823 && ["hello", "world"].first() == "hello"
2824 && [["hello"], ["world", "!"]].first().first() == "hello"
2825 && [[], ["empty"]].first().first() == ""
2826 && dyn([1, 2]).first() == 1
2827 && dyn([1.0, 2.0]).first() == 1.0
2828 && dyn(["hello", "world"]).first() == "hello"
2829 && dyn([["hello"], ["world", "!"]]).first().first() == "hello"
2830 `, map[string]any{},
2831 )
2832 if err != nil {
2833 t.Fatalf("prg.Eval() failed: %v", err)
2834 }

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