-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathevent_test.go
180 lines (163 loc) · 3.45 KB
/
event_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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package event
import (
"fmt"
"reflect"
"testing"
)
const nameEvent = "test"
func createEvent() *Dispatcher {
return New()
}
func TestDestroy(t *testing.T) {
t.Parallel()
event := createEvent()
event.Add(nameEvent, func() {}, []interface{}{})
event.Destroy(nameEvent)
if event.existEvent(nameEvent) {
t.Error("event not deleted")
}
}
func TestDestroyNotExist(t *testing.T) {
t.Parallel()
defer func() {
str := recover()
if str != fmt.Sprintf(eventNotExist, "qwer") {
t.Fatalf("Wrong panic message: %s", str)
}
}()
event := createEvent()
event.Destroy(`qwer`)
}
func TestUntie(t *testing.T) {
t.Parallel()
event := createEvent()
closure := func() {}
event.Add(nameEvent, closure, []interface{}{})
event.Untie(closure)
if event.existSubscriber(closure) {
t.Error("Subscriber not deleted")
}
}
func TestAdd(t *testing.T) {
t.Parallel()
event := createEvent()
closure := func() {}
event.Add(nameEvent, closure, []interface{}{})
if event.listeners[nameEvent][0][typing] != "func" {
t.Error("not installed typing")
}
if reflect.ValueOf(event.listeners[nameEvent][0][perform]).Pointer() != reflect.ValueOf(closure).Pointer() {
t.Error("not installed structure")
}
}
func TestFire(t *testing.T) {
t.Parallel()
event := createEvent()
nameString := "exist"
closure := func(test string) string {
return test
}
event.Add(nameEvent, closure, nameString)
event.Fire(nameEvent)
event.Fire(nameEvent, "override")
}
func TestFireNotExist(t *testing.T) {
t.Parallel()
defer func() {
str := recover()
if str != fmt.Sprintf(eventNotExist, "qwer") {
t.Fatalf("Wrong panic message: %s", str)
}
}()
event := createEvent()
event.Fire(`qwer`)
}
func TestFileNotFoundType(t *testing.T) {
t.Parallel()
event := createEvent()
if event.Fire(2).Error() != notFoundName {
t.Error("Expected error")
}
}
func TestGetName(t *testing.T) {
t.Parallel()
type Test struct{}
if GetName(Test{}) != "Test" {
t.Error("GetName structure error")
}
}
func TestExistSubscriber(t *testing.T) {
t.Parallel()
event := createEvent()
closure := func() {}
event.Add(nameEvent, closure, []interface{}{})
if event.listeners[nameEvent][0][typing] != "func" {
t.Error("not installed typing")
}
if !event.existSubscriber(closure) {
t.Error("Subscriber test fail")
}
}
func TestFactoryNames(t *testing.T) {
t.Parallel()
// fot test
type customType int
var digit customType = 2
type customTypeStr string
var str customTypeStr = "test"
cases := []struct {
Value interface{}
ExpectedErr string
ExpectedName string
}{
{
Value: struct {
}{},
ExpectedErr: notFoundName,
ExpectedName: "",
},
{
Value: "test",
ExpectedErr: "",
ExpectedName: "test",
},
{
Value: &struct {
}{},
ExpectedErr: notFoundName,
ExpectedName: "",
},
{
Value: New(),
ExpectedErr: "",
ExpectedName: "Dispatcher",
},
{
Value: *New(),
ExpectedErr: "",
ExpectedName: "Dispatcher",
},
{
Value: digit,
ExpectedErr: "",
ExpectedName: "customType",
},
{
Value: 2,
ExpectedErr: notFoundName,
ExpectedName: "",
},
{
Value: str,
ExpectedErr: "",
ExpectedName: "customTypeStr",
},
}
for _, cas := range cases {
name, err := factoryNames(cas.Value)
if name != cas.ExpectedName && err.Error() != cas.ExpectedErr {
fmt.Println(name, err)
t.Errorf("Expected value name : %s and error : %s", cas.ExpectedName, cas.ExpectedErr)
}
}
}