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

Function TestOverloadOperandTrait

common/decls/decls_test.go:877–919  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

875}
876
877func TestOverloadOperandTrait(t *testing.T) {
878 fn, err := NewFunction("getOrDefault",
879 MemberOverload("get",
880 []*types.Type{types.NewMapType(
881 types.NewTypeParamType("K"), types.NewTypeParamType("V")),
882 types.NewTypeParamType("K"),
883 types.NewTypeParamType("V"),
884 },
885 types.NewTypeParamType("V"),
886 OverloadOperandTrait(traits.ContainerType|traits.IndexerType),
887 FunctionBinding(func(args ...ref.Val) ref.Val {
888 container := args[0].(traits.Container)
889 key := args[1]
890 orValue := args[2]
891 if container.Contains(key) == types.True {
892 return args[0].(traits.Indexer).Get(key)
893 }
894 return orValue
895 }),
896 ),
897 )
898 if err != nil {
899 t.Fatalf("NewFunction() failed: %v", err)
900 }
901 bindings, err := fn.Bindings()
902 if err != nil {
903 t.Fatalf("fn.Binding() failed: %v", err)
904 }
905 m := types.DefaultTypeAdapter.NativeToValue(map[string]string{"hello": "world"})
906 out := bindings[0].Function(m, types.String("hello"), types.String("goodbye"))
907 if out.Equal(types.String("world")) != types.True {
908 t.Errorf("function got %v, wanted 'world'", out)
909 }
910 out = bindings[0].Function(m, types.String("missing"), types.String("goodbye"))
911 if out.Equal(types.String("goodbye")) != types.True {
912 t.Errorf("function got %v, wanted 'goodbye'", out)
913 }
914 noSuchKey := types.NewErr("no such key")
915 out = bindings[0].Function(m, noSuchKey, types.String("goodbye"))
916 if out != noSuchKey {
917 t.Errorf("function got %v, wanted 'no such key'", out)
918 }
919}
920
921func TestFunctionGetTypeParams(t *testing.T) {
922 fn, err := NewFunction("deep_type_params",

Callers

nothing calls this directly

Calls 14

NewMapTypeFunction · 0.92
NewTypeParamTypeFunction · 0.92
StringTypeAlias · 0.92
NewErrFunction · 0.92
BindingsMethod · 0.80
NewFunctionFunction · 0.70
MemberOverloadFunction · 0.70
OverloadOperandTraitFunction · 0.70
FunctionBindingFunction · 0.70
ContainsMethod · 0.65
GetMethod · 0.65
NativeToValueMethod · 0.65

Tested by

no test coverage detected