-
Notifications
You must be signed in to change notification settings - Fork 15
/
events_test.go
172 lines (137 loc) · 3.92 KB
/
events_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
package events
import (
"fmt"
"testing"
"time"
)
var testEvents = Events{
"user_created": []Listener{
func(payload ...interface{}) {
fmt.Printf("A new User just created!\n")
},
func(payload ...interface{}) {
fmt.Printf("A new User just created, *from second event listener\n")
},
},
"user_joined": []Listener{func(payload ...interface{}) {
user := payload[0].(string)
room := payload[1].(string)
fmt.Printf("%s joined to room: %s\n", user, room)
}},
"user_left": []Listener{func(payload ...interface{}) {
user := payload[0].(string)
room := payload[1].(string)
fmt.Printf("%s left from the room: %s\n", user, room)
}},
}
func createUser(user string) {
Emit("user_created", user)
}
func joinUserTo(user string, room string) {
Emit("user_joined", user, room)
}
func leaveFromRoom(user string, room string) {
Emit("user_left", user, room)
}
func ExampleEvents() {
// regiter our events to the default event emmiter
for evt, listeners := range testEvents {
On(evt, listeners...)
}
user := "user1"
room := "room1"
createUser(user)
joinUserTo(user, room)
leaveFromRoom(user, room)
// Output:
// A new User just created!
// A new User just created, *from second event listener
// user1 joined to room: room1
// user1 left from the room: room1
}
func TestEvents(t *testing.T) {
e := New()
expectedPayload := "this is my payload"
e.On("my_event", func(payload ...interface{}) {
if len(payload) <= 0 {
t.Fatal("Expected payload but got nothing")
}
if s, ok := payload[0].(string); !ok {
t.Fatalf("Payload is not the correct type, got: %#v", payload[0])
} else if s != expectedPayload {
t.Fatalf("Eexpected %s, got: %s", expectedPayload, s)
}
})
e.Emit("my_event", expectedPayload)
if e.Len() != 1 {
t.Fatalf("Length of the events is: %d, while expecting: %d", e.Len(), 1)
}
if e.Len() != 1 {
t.Fatalf("Length of the listeners is: %d, while expecting: %d", e.ListenerCount("my_event"), 1)
}
e.RemoveAllListeners("my_event")
if e.Len() != 0 {
t.Fatalf("Length of the events is: %d, while expecting: %d", e.Len(), 0)
}
if e.Len() != 0 {
t.Fatalf("Length of the listeners is: %d, while expecting: %d", e.ListenerCount("my_event"), 0)
}
}
func TestEventsOnce(t *testing.T) {
// on default
Clear()
var count = 0
Once("my_event", func(payload ...interface{}) {
if count > 0 {
t.Fatalf("Once's listener fired more than one time! count: %d", count)
}
if payload[0] != "foo" {
t.Fatalf("Once's listener payload is incorrect: %+v", payload)
}
count++
})
if l := ListenerCount("my_event"); l != 1 {
t.Fatalf("Real event's listeners should be: %d but has: %d", 1, l)
}
if l := len(Listeners("my_event")); l != 1 {
t.Fatalf("Real event's listeners (from Listeners) should be: %d but has: %d", 1, l)
}
for i := 0; i < 10; i++ {
Emit("my_event", "foo")
}
time.Sleep(10 * time.Millisecond)
if l := ListenerCount("my_event"); l > 0 {
t.Fatalf("Real event's listeners length count should be: %d but has: %d", 0, l)
}
if l := len(Listeners("my_event")); l > 0 {
t.Fatalf("Real event's listeners length count ( from Listeners) should be: %d but has: %d", 0, l)
}
}
func TestRemoveListener(t *testing.T) {
// on default
e := New()
var count = 0
listener := func(payload ...interface{}) {
if count > 1 {
t.Fatal("Event listener should be removed")
}
count++
}
e.AddListener("my_event", listener)
e.AddListener("my_event", func(payload ...interface{}) {})
e.AddListener("another_event", func(payload ...interface{}) {})
e.Emit("my_event")
if e.RemoveListener("my_event", listener) != true {
t.Fatal("Should return 'true' when removes found listener")
}
if e.RemoveListener("foo_bar", listener) != false {
t.Fatal("Should return 'false' when removes nothing")
}
if e.Len() != 2 {
t.Fatal("Length of all listeners must be 2")
}
if e.ListenerCount("my_event") != 1 {
t.Fatal("Length of 'my_event' event listeners must be 1")
}
e.Emit("my_event")
}