-
Notifications
You must be signed in to change notification settings - Fork 1
/
event_dispatcher_test.go
76 lines (69 loc) · 2.15 KB
/
event_dispatcher_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
// Package eventdispatcher contains a set of tools making up a simple and
// reliable event dispatcher
package eventdispatcher
import (
"fmt"
"github.com/stretchr/testify/assert"
"testing"
)
const (
TestEventName = "test_event"
)
func TestOnOff(t *testing.T) {
assert := assert.New(t)
d := NewDispatcher()
assert.False(d.HasListeners(TestEventName), fmt.Sprintf("No listeners assigned yet for %s!", TestEventName))
l := func(e Event) {
fmt.Sprintf("Event name: %s", e.Name())
}
d.On(TestEventName, l)
assert.True(d.HasListeners(TestEventName), fmt.Sprintf("There should be listeners assigned for %s!", TestEventName))
d.Off(TestEventName, l)
assert.False(d.HasListeners(TestEventName), fmt.Sprintf("No listeners assigned for %s!", TestEventName))
}
func TestOnce(t *testing.T) {
assert := assert.New(t)
d := NewDispatcher()
d.Once(TestEventName, func(e Event) {
fmt.Sprintf("Event name: %s", e.Name())
})
assert.True(d.HasListeners(TestEventName), fmt.Sprintf("There should be one listener assigned for one call for %s!", TestEventName))
e := NewParamsEvent(TestEventName)
d.Dispatch(e)
assert.False(d.HasListeners(TestEventName), fmt.Sprintf("The listener called should unbind itself for %s!", TestEventName))
}
func TestDispatch(t *testing.T) {
assert := assert.New(t)
d := NewDispatcher()
var c int
c = 0
for i := 1; i <= 5; i++ {
d.On(TestEventName, func(e Event) {
c++
})
}
e := NewParamsEvent(TestEventName)
d.Dispatch(e)
assert.Equal(5, c, "Invalid listeners calls number!")
}
func TestOffAll(t *testing.T) {
assert := assert.New(t)
d := NewDispatcher()
var c int
c = 0
for i := 1; i <= 5; i++ {
d.On(TestEventName, func(e Event) {
c++
})
}
assert.True(d.HasListeners(TestEventName), fmt.Sprintf("There should be listeners assigned for %s!", TestEventName))
d.OffAll(TestEventName)
assert.False(d.HasListeners(TestEventName), fmt.Sprintf("All event listeners for %s should be removed!", TestEventName))
}
func TestGetDispatcher(t *testing.T) {
assert := assert.New(t)
d := GetDispatcher(nil)
dn := GetDispatcher(nil)
assert.Equal(d, dn, "The event dispatchers should be the same instance pointers!")
_ = GetDispatcher("foo")
}