(t *testing.T)
| 874 | } |
| 875 | |
| 876 | func 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 | |
| 920 | func TestFunctionGetTypeParams(t *testing.T) { |
| 921 | fn, err := NewFunction("deep_type_params", |
nothing calls this directly
no test coverage detected