-
Notifications
You must be signed in to change notification settings - Fork 0
/
function_test.go
99 lines (91 loc) · 2.59 KB
/
function_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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package gdk
import (
"fmt"
"reflect"
"testing"
)
func TestNewFunctions(t *testing.T) {
want := make(Functions, 0)
if f := NewFunctions(); !reflect.DeepEqual(f, want) {
t.Fatalf("NewFunctions() = %v, want:%v", f, want)
}
}
func TestFunctionsBind(t *testing.T) {
type args struct {
name string
fn interface{}
}
tests := []struct {
name string
args args
wantErr bool
}{
{name: "bind success", args: args{name: "Add", fn: func(a, b int) int { return a + b }}, wantErr: false},
{name: "panics if the fn type's Kind is not Func", args: args{name: "nil", fn: nil}, wantErr: true},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
f := NewFunctions()
if err := f.Bind(test.args.name, test.args.fn); (err != nil) != test.wantErr {
t.Errorf("f.Bind() err =%v, wantErr:%v", err, test.wantErr)
}
})
}
}
func TestFunctionsCall(t *testing.T) {
type args struct {
name string
params []interface{}
}
tests := []struct {
name string
args args
wantR []reflect.Value
wantErr bool
}{
{name: "call success", args: args{name: "Add", params: []interface{}{1, 2}}, wantR: []reflect.Value{reflect.ValueOf(3)}, wantErr: false},
{name: "function name doesn't exist", args: args{name: "Sum"}, wantErr: true},
{name: "not enough params", args: args{name: "Add", params: []interface{}{1}}, wantErr: true},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
f := NewFunctions()
f.Bind("Add", func(a, b int) int { return a + b })
gotR, err := f.Call(test.args.name, test.args.params...)
if (err != nil) != test.wantErr {
t.Errorf("f.Call() err=%v, wantErr:%v", err, test.wantErr)
}
if fmt.Sprintf("%v", gotR) != fmt.Sprintf("%v", test.wantR) {
t.Errorf("f.Call() gotR:%v, wantR:%v", gotR, test.wantR)
}
})
}
}
type People struct {
Name string `json:"name"`
Age int `json:"age"`
}
func (p People) ShowName() string {
return p.Name
}
func (p People) ShowAge() int {
return p.Age
}
func TestFunctionsCall02(t *testing.T) {
persons := []People{
{"mike", 10},
{"mike", 20},
{"jack", 10},
}
nfuncs := NewFunctions()
for _, person := range persons {
nfuncs.Bind("name", person.ShowName)
nfuncs.Bind("age", person.ShowAge)
if out, err := nfuncs.Call("name"); err != nil || fmt.Sprintf("%v", out[0]) != person.Name {
t.Fatalf("Call invalid, out:%v, expected:%v, err:%v", out, person.Name, err)
}
if out, err := nfuncs.Call("age"); err != nil || fmt.Sprintf("%v", out[0]) != fmt.Sprintf("%v", person.Age) {
t.Fatalf("Call invalid, out:%v, expected:%v, err:%v", out, person.Age, err)
}
}
}