-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoperators_test.go
328 lines (301 loc) · 7.58 KB
/
operators_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
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
package rx_go_test
import (
"context"
"fmt"
"github.com/PxyUp/rx_go"
"github.com/stretchr/testify/assert"
"testing"
"time"
)
func TestFind(t *testing.T) {
ch, _ := rx_go.From([]int{1, 2, 3}...).Pipe(rx_go.Find(func(t int) bool {
return t == 3
})).Subscribe()
assert.Equal(t, 3, <-ch)
}
func TestInitialDelay(t *testing.T) {
ch, _ := rx_go.Of[int](1).Pipe(rx_go.InitialDelay[int](time.Second)).Subscribe()
var res []int
start := time.Now()
waiter := make(chan struct{})
go func() {
defer close(waiter)
for v := range ch {
res = append(res, v)
}
}()
assert.Len(t, res, 0)
<-waiter
finish := time.Now()
assert.Len(t, res, 1)
assert.True(t, finish.Sub(start) >= time.Second && finish.Sub(start) <= time.Millisecond*1100)
}
func TestElementAt(t *testing.T) {
ch, _ := rx_go.From([]int{1, 2, 3}...).Pipe(rx_go.ElementAt[int](1)).Subscribe()
assert.Equal(t, 2, <-ch)
}
func TestFinally(t *testing.T) {
done := false
ch, _ := rx_go.From([]int{1, 2, 3}...).Pipe(rx_go.Finally[int](func() {
done = true
})).Subscribe()
var res []int
for val := range ch {
res = append(res, val)
}
assert.Equal(t, []int{1, 2, 3}, res)
assert.True(t, done)
}
func TestSwitch_Complex(t *testing.T) {
ch, _ := rx_go.Switch[int, string](rx_go.From[int]([]int{1, 2, 3}...), func(t int) *rx_go.Observable[string] {
return rx_go.MapTo[time.Time, string](rx_go.NewInterval(time.Second, true).Pipe(rx_go.Take[time.Time](1)), func(_ time.Time) string {
return fmt.Sprintf("OBSERVER %d", t)
})
}).Subscribe()
var res []string
for val := range ch {
res = append(res, val)
}
assert.Equal(t, []string{"OBSERVER 1", "OBSERVER 2", "OBSERVER 3"}, res)
}
func TestRepeat(t *testing.T) {
values := []int{1, 2, 3}
ch, _ := rx_go.From(values...).Pipe(rx_go.Repeat[int](2)).Subscribe()
var res []int
for val := range ch {
res = append(res, val)
}
assert.Equal(t, []int{1, 1, 2, 2, 3, 3}, res)
}
func TestSleep(t *testing.T) {
values := []int{1, 2, 3}
ch, _ := rx_go.From(values...).Pipe(rx_go.Delay[int](time.Second), rx_go.Debounce[int](time.Millisecond*500)).Subscribe()
var res []int
for val := range ch {
res = append(res, val)
}
assert.Equal(t, []int{1, 2, 3}, res)
}
func TestDebounce(t *testing.T) {
values := []int{1, 2, 3}
ch, _ := rx_go.From(values...).Pipe(rx_go.Debounce[int](time.Second)).Subscribe()
var res []int
for val := range ch {
res = append(res, val)
}
assert.Equal(t, []int{3}, res)
}
func TestEndWith(t *testing.T) {
ch, _ := rx_go.From([]int{1}...).Pipe(rx_go.EndWith(2)).Subscribe()
var res []int
for val := range ch {
res = append(res, val)
}
assert.Equal(t, []int{1, 2}, res)
}
func TestStartWith(t *testing.T) {
ch, _ := rx_go.From([]int{1}...).Pipe(rx_go.StartWith(2)).Subscribe()
var res []int
for val := range ch {
res = append(res, val)
}
assert.Equal(t, []int{2, 1}, res)
}
func TestSkipUntil_Never(t *testing.T) {
ch, cancel := rx_go.From([]int{1, 2, 3}...).Pipe(
rx_go.SkipUntil[int, any](rx_go.Never),
).Subscribe()
var res []int
for val := range ch {
res = append(res, val)
}
assert.Nil(t, res)
cancel()
}
func TestSkipUntilCtx_Never(t *testing.T) {
ctx, cancelCtx := context.WithCancel(context.Background())
defer cancelCtx()
ch, cancel := rx_go.From([]int{1, 2, 3}...).Pipe(
rx_go.SkipUntilCtx[int](ctx),
).Subscribe()
var res []int
for val := range ch {
res = append(res, val)
}
assert.Nil(t, res)
cancel()
}
func TestSkipUntilCtx_Emitted(t *testing.T) {
ctx, cancelCtx := context.WithCancel(context.Background())
ch, cancel := rx_go.From([]int{1, 2, 3}...).Pipe(
rx_go.Do(func(value int) {
if value == 2 {
cancelCtx()
}
}),
rx_go.SkipUntilCtx[int](ctx),
).Subscribe()
var res []int
for val := range ch {
res = append(res, val)
}
assert.Equal(t, []int{2, 3}, res)
cancel()
}
func TestSkipUntil_Emitted(t *testing.T) {
ctx, cancelCtx := context.WithCancel(context.Background())
ch, cancel := rx_go.From([]int{1, 2, 3}...).Pipe(
rx_go.AfterCtx[int](ctx),
rx_go.SkipUntil[int, int](rx_go.Of(1).Pipe(rx_go.Do(func(value int) {
cancelCtx()
}))),
).Subscribe()
var res []int
for val := range ch {
res = append(res, val)
}
assert.Equal(t, []int{1, 2, 3}, res)
cancel()
}
func TestSkip(t *testing.T) {
ch, _ := rx_go.From([]int{1, 2, 3}...).Pipe(rx_go.Skip[int](2)).Subscribe()
var res []int
for val := range ch {
res = append(res, val)
}
assert.Equal(t, []int{3}, res)
}
func TestAfterCtx(t *testing.T) {
values := []int{1, 2, 3}
ctx, cancel := context.WithCancel(context.Background())
ch, _ := rx_go.From(values...).Pipe(
rx_go.AfterCtx[int](ctx),
).Subscribe()
cancel()
var res []int
for val := range ch {
res = append(res, val)
}
assert.Equal(t, []int{1, 2, 3}, res)
}
func TestUntilCtx(t *testing.T) {
values := []int{1, 2, 3}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
ch, _ := rx_go.From(values...).Pipe(
rx_go.Do(func(value int) {
if value == 2 {
cancel()
}
}),
rx_go.UntilCtx[int](ctx),
).Subscribe()
var res []int
for val := range ch {
res = append(res, val)
}
assert.Equal(t, []int{1, 2}, res)
}
func TestDo(t *testing.T) {
values := []int{1, 2, 3}
count := 0
ch, _ := rx_go.From(values...).Pipe(
rx_go.Do(func(_ int) {
count += 1
}),
).Subscribe()
var res []int
for val := range ch {
res = append(res, val)
}
assert.Equal(t, []int{1, 2, 3}, res)
assert.Equal(t, 3, count)
}
func TestDistinctWith(t *testing.T) {
values := []int{1, 2, 2, 2, 3, 4, 4, 4, 4}
obs := rx_go.From(values...)
ch, _ := obs.Pipe(rx_go.DistinctWith[int](func(a, b int) bool { return a == b })).Subscribe()
var res []int
for val := range ch {
res = append(res, val)
}
assert.Equal(t, []int{1, 2, 3, 4}, res)
}
func TestDistinct(t *testing.T) {
values := []int{1, 2, 2, 2, 3, 4, 4, 4, 4}
obs := rx_go.From(values...)
ch, _ := obs.Pipe(rx_go.Distinct[int]()).Subscribe()
var res []int
for val := range ch {
res = append(res, val)
}
assert.Equal(t, []int{1, 2, 3, 4}, res)
}
func TestTake(t *testing.T) {
values := []int{1, 2, 3, 4, 5, 6}
obs := rx_go.From(values...)
ch, _ := obs.Pipe(rx_go.Take[int](3)).Subscribe()
var res []int
for val := range ch {
res = append(res, val)
}
assert.Equal(t, []int{1, 2, 3}, res)
}
func TestFirstOne(t *testing.T) {
values := []int{1, 2, 3, 4, 5, 6}
obs := rx_go.From(values...)
ch, _ := obs.Pipe(rx_go.FirstOne[int]()).Subscribe()
var res []int
for val := range ch {
res = append(res, val)
}
assert.Equal(t, []int{1}, res)
}
func TestLastOne(t *testing.T) {
values := []int{1, 2, 3, 4, 5, 6}
obs := rx_go.From(values...)
ch, _ := obs.Pipe(rx_go.LastOne[int]()).Subscribe()
var res []int
for val := range ch {
res = append(res, val)
}
assert.Equal(t, []int{6}, res)
}
func TestFilter(t *testing.T) {
values := []int{1, 2, 3, 4, 5, 6}
obs := rx_go.From(values...)
ch, _ := obs.Pipe(rx_go.Filter[int](func(value int) bool {
return value > 3
})).Subscribe()
var res []int
for val := range ch {
res = append(res, val)
}
assert.Equal(t, []int{4, 5, 6}, res)
}
func TestMap(t *testing.T) {
values := []int{1, 2, 3, 4, 5, 6}
obs := rx_go.From(values...)
ch, _ := obs.Pipe(rx_go.Map[int](func(value int) int {
return value * 3
})).Subscribe()
var res []int
for val := range ch {
res = append(res, val)
}
assert.Equal(t, []int{3, 6, 9, 12, 15, 18}, res)
}
func TestFilterMap(t *testing.T) {
values := []int{1, 2, 3, 4, 5, 6}
obs := rx_go.From(values...)
ch, _ := obs.Pipe(rx_go.Map[int](func(value int) int {
return value * 3
}), rx_go.Filter[int](func(value int) bool {
return value > 16
})).Subscribe()
var res []int
for val := range ch {
res = append(res, val)
}
assert.Equal(t, []int{18}, res)
}