-
Notifications
You must be signed in to change notification settings - Fork 0
/
manager_test.go
50 lines (42 loc) · 1.06 KB
/
manager_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
package event
import (
"reflect"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type myEvent struct{ s string }
func TestTypeOf(t *testing.T) {
assert.Equal(t, typeOf(&myEvent{}), reflect.TypeOf(&myEvent{}))
assert.Equal(t, typeOf(reflect.TypeOf(&myEvent{})), reflect.TypeOf(&myEvent{}))
assert.NotEqual(t, typeOf(reflect.TypeOf(myEvent{})), reflect.TypeOf(&myEvent{}))
}
func TestPriorityAndCorrectType(t *testing.T) {
m := New()
require.False(t, m.HasSubscriber(&myEvent{}))
m.Subscribe(typeOf(&myEvent{}), -1, func(e Event) {
ev := e.(*myEvent)
ev.s += "c"
})
m.Subscribe(&myEvent{}, 1, func(e Event) {
ev := e.(*myEvent)
ev.s += "a"
})
m.Subscribe(typeOf(myEvent{}), 0, func(e Event) {
ev := e.(myEvent)
ev.s += "d"
})
Subscribe(m, 0, func(ev *myEvent) {
ev.s += "b"
})
var noPtr bool
m.Subscribe(myEvent{}, 2, func(e Event) {
_ = e.(myEvent)
noPtr = true
})
e := &myEvent{s: "_"}
m.Fire(e)
require.False(t, noPtr)
require.Equal(t, "_abc", e.s)
require.True(t, m.HasSubscriber(&myEvent{}))
}