-
Notifications
You must be signed in to change notification settings - Fork 0
/
events_test.go
86 lines (72 loc) · 1.41 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
package eventmgr
import "testing"
var test2Tracker = 0
func handler1(event string, info InfoMap) {
t := info["test"].(*testing.T)
if info["k"].(int) != 3 {
t.Error(
"For", event,
"expected", 3,
"got", info["k"],
)
}
}
func handler2First(event string, info InfoMap) {
t := info["test"].(*testing.T)
if test2Tracker != 0 {
t.Error(
"For", event,
"expected", 0,
"got", test2Tracker,
)
} else {
test2Tracker += 3
}
}
func handler2Second(event string, info InfoMap) {
t := info["test"].(*testing.T)
if test2Tracker != 3 {
t.Error(
"For", event,
"expected", 3,
"got", test2Tracker,
)
} else {
test2Tracker--
}
}
func handler2Third(event string, info InfoMap) {
t := info["test"].(*testing.T)
if test2Tracker != 2 {
t.Error(
"For", event,
"expected", 2,
"got", test2Tracker,
)
} else {
test2Tracker += 5
}
}
func TestAttachDispatch(t *testing.T) {
var manager EventManager
// test dispatching
manager.Attach("test1", handler1, 0)
info := NewInfoMap()
info["k"] = 3
info["test"] = t
manager.Dispatch("test1", info)
// test priority dispatching
manager.Attach("test2", handler2Second, 8)
manager.Attach("test2", handler2First, 3)
manager.Attach("test2", handler2Third, 16)
info = make(InfoMap)
info["test"] = t
manager.Dispatch("test2", info)
if test2Tracker != 7 {
t.Error(
"For", "test2 end",
"expected", 7,
"got", test2Tracker,
)
}
}