-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevent_test.go
More file actions
158 lines (137 loc) · 3.4 KB
/
event_test.go
File metadata and controls
158 lines (137 loc) · 3.4 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package xterm
import (
"testing"
"github.com/google/go-cmp/cmp"
)
func TestEventEmitterFire(t *testing.T) {
type Expectation struct {
Results []int
}
type TestCase struct {
Name string
Listeners int
FireValue int
Expected Expectation
}
tests := []TestCase{
{"no listeners", 0, 5, Expectation{Results: nil}},
{"single listener", 1, 3, Expectation{Results: []int{30}}},
{"two listeners", 2, 3, Expectation{Results: []int{30, 300}}},
}
for _, tc := range tests {
t.Run(tc.Name, func(t *testing.T) {
var e EventEmitter[int]
var results []int
if tc.Listeners >= 1 {
e.Event(func(v int) { results = append(results, v*10) })
}
if tc.Listeners >= 2 {
e.Event(func(v int) { results = append(results, v*100) })
}
e.Fire(tc.FireValue)
got := Expectation{Results: results}
if diff := cmp.Diff(tc.Expected, got); diff != "" {
t.Errorf("(-want +got):\n%s", diff)
}
})
}
}
func TestEventEmitterUnsubscribe(t *testing.T) {
type Expectation struct {
CallCount int
}
var e EventEmitter[string]
count := 0
d := e.Event(func(string) { count++ })
e.Fire("a")
d.Dispose()
e.Fire("b")
got := Expectation{CallCount: count}
expected := Expectation{CallCount: 1}
if diff := cmp.Diff(expected, got); diff != "" {
t.Errorf("(-want +got):\n%s", diff)
}
}
func TestEventEmitterDispose(t *testing.T) {
type Expectation struct {
CallCount int
HasListeners bool
}
var e EventEmitter[int]
count := 0
e.Event(func(int) { count++ })
e.Dispose()
e.Fire(1)
d := e.Event(func(int) { count++ })
e.Fire(2)
d.Dispose()
got := Expectation{CallCount: count, HasListeners: e.HasListeners()}
expected := Expectation{CallCount: 0, HasListeners: false}
if diff := cmp.Diff(expected, got); diff != "" {
t.Errorf("(-want +got):\n%s", diff)
}
}
func TestEventEmitterSelfUnsubscribeDuringFire(t *testing.T) {
type Expectation struct {
FirstFire []string
SecondFire []string
}
var e EventEmitter[int]
var target *[]string
firstResults := []string{}
secondResults := []string{}
target = &firstResults
var d1 Disposable
d1 = e.Event(func(int) {
*target = append(*target, "first")
d1.Dispose()
})
e.Event(func(int) {
*target = append(*target, "second")
})
e.Fire(1)
target = &secondResults
e.Fire(2)
got := Expectation{FirstFire: firstResults, SecondFire: secondResults}
expected := Expectation{
FirstFire: []string{"first", "second"},
SecondFire: []string{"second"},
}
if diff := cmp.Diff(expected, got); diff != "" {
t.Errorf("(-want +got):\n%s", diff)
}
}
func TestEventEmitterHasListeners(t *testing.T) {
type Expectation struct {
Before bool
After bool
AfterUnsub bool
}
var e EventEmitter[int]
before := e.HasListeners()
d := e.Event(func(int) {})
after := e.HasListeners()
d.Dispose()
afterUnsub := e.HasListeners()
got := Expectation{Before: before, After: after, AfterUnsub: afterUnsub}
expected := Expectation{Before: false, After: true, AfterUnsub: false}
if diff := cmp.Diff(expected, got); diff != "" {
t.Errorf("(-want +got):\n%s", diff)
}
}
func TestEventEmitterDoubleDispose(t *testing.T) {
type Expectation struct {
CallCount int
}
var e EventEmitter[int]
count := 0
e.Event(func(int) { count++ })
e.Dispose()
e.Dispose()
e.Fire(1)
got := Expectation{CallCount: count}
expected := Expectation{CallCount: 0}
if diff := cmp.Diff(expected, got); diff != "" {
t.Errorf("(-want +got):\n%s", diff)
}
}