-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathgoemitter_test.go
51 lines (38 loc) · 896 Bytes
/
goemitter_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
package Emitter
import (
"reflect"
"testing"
)
func TestRemoveListener(t *testing.T) {
emitter := Construct()
counter := 0
fn1 := func(args ...interface{}) {
counter++
}
fn2 := func(args ...interface{}) {
counter++
}
emitter.On("testevent", fn1)
emitter.On("testevent", fn2)
emitter.RemoveListener("testevent", fn1)
emitter.EmitSync("testevent")
listenersCount := emitter.ListenersCount("testevent")
expect(t, 1, listenersCount)
expect(t, 1, counter)
}
func TestOnce(t *testing.T) {
emitter := Construct()
counter := 0
fn := func(args ...interface{}) {
counter++
}
emitter.Once("testevent", fn)
emitter.EmitSync("testevent")
emitter.EmitSync("testevent")
expect(t, 1, counter)
}
func expect(t *testing.T, a interface{}, b interface{}) {
if a != b {
t.Errorf("Expected %v (type %v) - Got %v (type %v)", b, reflect.TypeOf(b), a, reflect.TypeOf(a))
}
}