-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevent_test.go
78 lines (62 loc) · 1.58 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
package event_test
import (
"testing"
"github.com/gvre/event"
)
type UserCreatedEvent struct {
ID int
Name string
}
func TestUserCreatedEvent(t *testing.T) {
id := 1
name := "user"
dispatcher := event.NewDispatcher()
dispatcher.On("user.created", func(ev interface{}) bool {
if e, ok := ev.(*UserCreatedEvent); ok {
if e.ID != id {
t.Errorf("Invalid user id, got: %d, want: %d", e.ID, id)
}
if e.Name != name {
t.Errorf("Invalid user name, got: %q, want: %q", e.Name, name)
}
}
return true
})
ev := &UserCreatedEvent{
ID: id,
Name: name,
}
dispatcher.Dispatch("user.created", ev)
}
func TestStopPropagation(t *testing.T) {
var totalExecutions int
dispatcher := event.NewDispatcher()
dispatcher.On("user.created", func(ev interface{}) bool {
totalExecutions++
return false
})
dispatcher.On("user.created", func(ev interface{}) bool {
totalExecutions++
return true
})
ev := &UserCreatedEvent{}
dispatcher.Dispatch("user.created", ev)
expectedExecutions := 1
if totalExecutions != expectedExecutions {
t.Errorf("Invalid number of executions, got: %d, want: %d", totalExecutions, expectedExecutions)
}
}
func TestWildcardListener(t *testing.T) {
var totalExecutions int
dispatcher := event.NewDispatcher()
dispatcher.On("user.*", func(ev interface{}) bool {
totalExecutions++
return true
})
ev := &UserCreatedEvent{}
dispatcher.Dispatch("user.created", ev)
expectedExecutions := 1
if totalExecutions != expectedExecutions {
t.Errorf("Invalid number of executions, got: %d, want: %d", totalExecutions, expectedExecutions)
}
}