-
Notifications
You must be signed in to change notification settings - Fork 29
/
function_util_test.go
78 lines (60 loc) · 1.78 KB
/
function_util_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package toolbox_test
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
"github.com/viant/toolbox"
)
func TestCallFunction(t *testing.T) {
{
var myFunction = func(arg1 string, arg2 int) string {
return fmt.Sprintf("%v%v", arg2, arg1)
}
functionParameters, err := toolbox.BuildFunctionParameters(myFunction, []string{"arg1", "arg2"}, map[string]interface{}{
"arg1": "abc",
"arg2": 100,
})
assert.Nil(t, err)
result := toolbox.CallFunction(myFunction, functionParameters...)
assert.Equal(t, "100abc", result[0])
}
{
var myFunction = func(arg1 string, arg2 ...int) string {
return fmt.Sprintf("%v%v", arg2, arg1)
}
functionParameters, err := toolbox.BuildFunctionParameters(myFunction, []string{"arg1", "arg2"}, map[string]interface{}{
"arg1": "abc",
"arg2": []interface{}{100},
})
assert.Nil(t, err)
result := toolbox.CallFunction(myFunction, functionParameters...)
assert.Equal(t, "[100]abc", result[0])
}
{
var myFunction = func(arg1 string, arg2 ...int) string {
return fmt.Sprintf("%v%v", arg2, arg1)
}
_, err := toolbox.BuildFunctionParameters(myFunction, []string{"arg1", "arg2"}, map[string]interface{}{
"arg1": "abc",
"arg2": 100,
})
assert.NotNil(t, err)
}
}
func Test_GetFunction(t *testing.T) {
var astruct = &AStruct{"ABC"}
var function, err = toolbox.GetFunction(astruct, "Message")
assert.Nil(t, err)
assert.NotNil(t, function)
parameters, err := toolbox.AsCompatibleFunctionParameters(function, []interface{}{"aaa"})
assert.Nil(t, err)
result := toolbox.CallFunction(function, parameters...)
assert.Equal(t, 2, len(result))
assert.Equal(t, "ABC.aaa", result[0])
}
type AStruct struct {
A string
}
func (s *AStruct) Message(a string) (string, error) {
return fmt.Sprintf("%v.%v", s.A, a), nil
}