(t *testing.T)
| 88 | } |
| 89 | |
| 90 | func TestFunctionVariableArgBindings(t *testing.T) { |
| 91 | splitImpl := func(str, delim string, count int64) ref.Val { |
| 92 | return types.DefaultTypeAdapter.NativeToValue(strings.SplitN(str, delim, int(count))) |
| 93 | } |
| 94 | splitFunc, err := NewFunction("split", |
| 95 | MemberOverload("string_split", |
| 96 | []*types.Type{types.StringType}, types.NewListType(types.StringType), |
| 97 | UnaryBinding(func(str ref.Val) ref.Val { |
| 98 | s := str.(types.String) |
| 99 | return splitImpl(string(s), "", -1) |
| 100 | })), |
| 101 | MemberOverload("string_split_string", |
| 102 | []*types.Type{types.StringType, types.StringType}, types.NewListType(types.StringType), |
| 103 | BinaryBinding(func(str, sep ref.Val) ref.Val { |
| 104 | s := str.(types.String) |
| 105 | delim := sep.(types.String) |
| 106 | return splitImpl(string(s), string(delim), -1) |
| 107 | })), |
| 108 | MemberOverload("string_split_string_int", |
| 109 | []*types.Type{types.StringType, types.StringType, types.IntType}, types.NewListType(types.StringType), |
| 110 | FunctionBinding(func(args ...ref.Val) ref.Val { |
| 111 | s := args[0].(types.String) |
| 112 | delim := args[1].(types.String) |
| 113 | count := args[2].(types.Int) |
| 114 | return splitImpl(string(s), string(delim), int64(count)) |
| 115 | })), |
| 116 | ) |
| 117 | if err != nil { |
| 118 | t.Fatalf("NewFunction() failed: %v", err) |
| 119 | } |
| 120 | bindings, err := splitFunc.Bindings() |
| 121 | if err != nil { |
| 122 | t.Fatalf("sizeFunc.Bindings() produced an err: %v", err) |
| 123 | } |
| 124 | if len(bindings) != 4 { |
| 125 | t.Errorf("sizeFunc.Bindings() produced %d bindings, wanted 4", len(bindings)) |
| 126 | } |
| 127 | in := types.String("hi") |
| 128 | sep := types.String("") |
| 129 | out := types.DefaultTypeAdapter.NativeToValue([]string{"h", "i"}) |
| 130 | for _, binding := range bindings { |
| 131 | if binding.Unary != nil { |
| 132 | if binding.Unary(in).Equal(out) != types.True { |
| 133 | t.Errorf("binding invocation got %v, wanted %v", binding.Unary(in), out) |
| 134 | } |
| 135 | celErr := binding.Unary(types.Bytes("hi")) |
| 136 | if !types.IsError(celErr) || !strings.Contains(celErr.(*types.Err).String(), "no such overload") { |
| 137 | t.Errorf("binding.Unary(bytes) got %v, wanted no such overload", celErr) |
| 138 | } |
| 139 | } |
| 140 | if binding.Binary != nil { |
| 141 | if binding.Binary(in, sep).Equal(out) != types.True { |
| 142 | t.Errorf("binding invocation got %v, wanted %v", binding.Binary(in, sep), out) |
| 143 | } |
| 144 | celErr := binding.Binary(types.Bytes("hi"), sep) |
| 145 | if !types.IsError(celErr) || !strings.Contains(celErr.(*types.Err).String(), "no such overload") { |
| 146 | t.Errorf("binding.Binary(bytes, string) got %v, wanted no such overload", celErr) |
| 147 | } |
nothing calls this directly
no test coverage detected