This repository has been archived by the owner on Sep 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 840
/
handler_test.go
122 lines (100 loc) · 2.65 KB
/
handler_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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package socketio
import (
"fmt"
"reflect"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestNewEventFunc(t *testing.T) {
tests := []struct {
f interface{}
ok bool
argTypes []interface{}
}{
{1, false, []interface{}{}},
{func() {}, false, []interface{}{}},
{func(int) {}, false, []interface{}{}},
{func() error { return nil }, false, []interface{}{}},
{func(Conn) {}, true, []interface{}{}},
{func(Conn, int) {}, true, []interface{}{1}},
{func(Conn, int) error { return nil }, true, []interface{}{1}},
}
for _, test := range tests {
t.Run(fmt.Sprintf("%#v", test.argTypes), func(t *testing.T) {
should := assert.New(t)
must := require.New(t)
defer func() {
r := recover()
must.Equal(test.ok, r == nil)
}()
h := newEventFunc(test.f)
must.Equal(len(test.argTypes), len(h.argTypes))
for i := range h.argTypes {
should.Equal(reflect.TypeOf(test.argTypes[i]), h.argTypes[i])
}
})
}
}
func TestNewAckFunc(t *testing.T) {
tests := []struct {
f interface{}
ok bool
argTypes []interface{}
}{
{1, false, []interface{}{}},
{func() {}, true, []interface{}{}},
{func(int) {}, true, []interface{}{1}},
{func(int) error { return nil }, true, []interface{}{1}},
}
for _, test := range tests {
t.Run(fmt.Sprintf("%#v", test.argTypes), func(t *testing.T) {
should := assert.New(t)
must := require.New(t)
defer func() {
r := recover()
must.Equal(test.ok, r == nil)
}()
h := newAckFunc(test.f)
must.Equal(len(test.argTypes), len(h.argTypes))
for i := range h.argTypes {
should.Equal(reflect.TypeOf(test.argTypes[i]), h.argTypes[i])
}
})
}
}
func TestHandlerCall(t *testing.T) {
tests := []struct {
f interface{}
args []interface{}
ok bool
rets []interface{}
}{
{func() {}, []interface{}{1}, false, nil},
{func() {}, nil, true, nil},
{func(int) {}, []interface{}{1}, true, nil},
{func() int { return 1 }, nil, true, []interface{}{1}},
{func(int) int { return 1 }, []interface{}{1}, true, []interface{}{1}},
}
for _, test := range tests {
t.Run(fmt.Sprintf("%#v", test.f), func(t *testing.T) {
should := assert.New(t)
must := require.New(t)
h := newAckFunc(test.f)
args := make([]reflect.Value, len(test.args))
for i := range args {
args[i] = reflect.ValueOf(test.args[i])
}
retV, err := h.Call(args)
must.Equal(test.ok, err == nil)
if len(retV) == len(test.rets) && len(test.rets) == 0 {
return
}
rets := make([]interface{}, len(retV))
for i := range rets {
rets[i] = retV[i].Interface()
}
should.Equal(test.rets, rets)
})
}
}