-
Notifications
You must be signed in to change notification settings - Fork 24
/
emitter_test.go
154 lines (118 loc) · 2.85 KB
/
emitter_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
package emission
import (
"testing"
)
func TestAddListener(t *testing.T) {
event := "test"
emitter := NewEmitter().
AddListener(event, func() {})
if 1 != len(emitter.events[event]) {
t.Error("Failed to add listener to the emitter.")
}
}
func TestEmit(t *testing.T) {
event := "test"
flag := true
NewEmitter().
AddListener(event, func() { flag = !flag }).
Emit(event)
if flag {
t.Error("Emit failed to call listener to unset flag.")
}
}
func TestEmitSync(t *testing.T) {
event := "test"
flag := true
NewEmitter().
AddListener(event, func() { flag = !flag }).
EmitSync(event)
if flag {
t.Error("EmitSync failed to call listener to unset flag.")
}
}
func TestEmitWithMultipleListeners(t *testing.T) {
event := "test"
invoked := 0
NewEmitter().
AddListener(event, func() {
invoked = invoked + 1
}).
AddListener(event, func() {
invoked = invoked + 1
}).
Emit(event)
if invoked != 2 {
t.Error("Emit failed to call all listeners.")
}
}
func TestRemoveListener(t *testing.T) {
event := "test"
listener := func() {}
emitter := NewEmitter().
AddListener(event, listener).
RemoveListener(event, listener)
if 0 != len(emitter.events[event]) {
t.Error("Failed to remove listener from the emitter.")
}
}
func TestOnce(t *testing.T) {
event := "test"
flag := true
NewEmitter().
Once(event, func() { flag = !flag }).
Emit(event).
Emit(event)
if flag {
t.Error("Once called listener multiple times reseting the flag.")
}
}
func TestRecoveryWith(t *testing.T) {
event := "test"
flag := true
NewEmitter().
AddListener(event, func() { panic(event) }).
RecoverWith(func(event, listener interface{}, err error) { flag = !flag }).
Emit(event)
if flag {
t.Error("Listener supplied to RecoverWith was not called to unset flag on panic.")
}
}
func TestRemoveOnce(t *testing.T) {
event := "test"
flag := false
fn := func() { flag = !flag }
NewEmitter().
Once(event, fn).
RemoveListener(event, fn).
Emit(event)
if flag {
t.Error("Failed to remove Listener for Once")
}
}
func TestCountListener(t *testing.T) {
event := "test"
emitter := NewEmitter().
AddListener(event, func() {})
if 1 != emitter.GetListenerCount(event) {
t.Error("Failed to get listener count from emitter.")
}
if 0 != emitter.GetListenerCount("fake") {
t.Error("Failed to get listener count from emitter.")
}
}
type SomeType struct{}
func (*SomeType) Receiver(evt string) {}
func TestRemoveStructMethod(t *testing.T) {
event := "test"
listener := &SomeType{}
emitter := NewEmitter().AddListener(event, listener.Receiver)
emitter.RemoveListener(event, listener.Receiver)
if 0 != emitter.GetListenerCount(event) {
t.Error("Failed to remove listener from emitter.")
}
}
func TestRemoveDoubleListener(t *testing.T) {
event := "test"
fn1 := func() {}
NewEmitter().On(event, fn1).On(event, fn1).RemoveListener(event, fn1)
}