(t *testing.T)
| 35 | ) |
| 36 | |
| 37 | func TestFunctionBindings(t *testing.T) { |
| 38 | sizeFunc, err := NewFunction("size", |
| 39 | MemberOverload("list_size", |
| 40 | []*types.Type{types.NewListType(types.NewTypeParamType("T"))}, types.IntType), |
| 41 | ) |
| 42 | if err != nil { |
| 43 | t.Fatalf("NewFunction() failed: %v", err) |
| 44 | } |
| 45 | bindings, err := sizeFunc.Bindings() |
| 46 | if err != nil { |
| 47 | t.Fatalf("sizeFunc.Bindings() produced an err: %v", err) |
| 48 | } |
| 49 | if len(bindings) != 0 { |
| 50 | t.Errorf("sizeFunc.Bindings() produced %d bindings, wanted none", len(bindings)) |
| 51 | } |
| 52 | sizeFuncDef, err := NewFunction("size", |
| 53 | MemberOverload("list_size", |
| 54 | []*types.Type{types.NewListType(types.NewTypeParamType("T"))}, types.IntType, |
| 55 | UnaryBinding(func(list ref.Val) ref.Val { |
| 56 | sizer := list.(traits.Sizer) |
| 57 | return sizer.Size() |
| 58 | }), |
| 59 | ), |
| 60 | ) |
| 61 | if err != nil { |
| 62 | t.Fatalf("NewFunction() failed: %v", err) |
| 63 | } |
| 64 | sizeMerged, err := sizeFunc.Merge(sizeFuncDef) |
| 65 | if err != nil { |
| 66 | t.Fatalf("Merge() failed: %v", err) |
| 67 | } |
| 68 | bindings, err = sizeMerged.Bindings() |
| 69 | if err != nil { |
| 70 | t.Fatalf("sizeFunc.Bindings() produced an err: %v", err) |
| 71 | } |
| 72 | if len(bindings) != 2 { |
| 73 | t.Errorf("sizeFunc.Bindings() got %d bindings, wanted 2", len(bindings)) |
| 74 | } |
| 75 | empty := types.DefaultTypeAdapter.NativeToValue([]string{}) |
| 76 | in := types.DefaultTypeAdapter.NativeToValue([]string{"1", "2"}) |
| 77 | for _, binding := range bindings { |
| 78 | if binding.Unary == nil { |
| 79 | t.Errorf("binding missing unary implementation: %v", binding.Operator) |
| 80 | continue |
| 81 | } |
| 82 | if binding.Unary(in) != types.Int(2) { |
| 83 | t.Errorf("binding invocation got %v, wanted 2", binding.Unary(in)) |
| 84 | } |
| 85 | if binding.Unary(empty) != types.IntZero { |
| 86 | t.Errorf("binding invocation got %v, wanted 0", binding.Unary(empty)) |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | func TestFunctionVariableArgBindings(t *testing.T) { |
| 92 | splitImpl := func(str, delim string, count int64) ref.Val { |
nothing calls this directly
no test coverage detected