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

Function TestOverloadOperandTrait

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

Source from the content-addressed store, hash-verified

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