-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpipe_test.go
112 lines (89 loc) · 1.96 KB
/
pipe_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
package observable
import (
"github.com/stretchr/testify/assert"
"testing"
)
func TestFilterMiddleware_Pipe_Filter(t *testing.T) {
obs := Observable{}
var called bool
obs.Pipe(
Filter(func(i interface{}) bool {
return i != nil
}),
).Subscribe(func(data interface{}) {
called = data.(bool)
})
obs.Next(true)
assert.True(t, called, "listener was not called")
}
func TestFilterMiddleware_Pipe_FilterNot(t *testing.T) {
obs := Observable{}
obs.Pipe(
Filter(func(i interface{}) bool {
return i != nil
}),
).Subscribe(func(i interface{}) {
assert.Fail(t, "listener must not be called")
})
obs.Next(nil)
}
func TestFirstMiddleware_Pipe(t *testing.T) {
obs := Observable{}
var numCalled int
obs.Pipe(
First(),
).Subscribe(func(i interface{}) {
numCalled++
})
obs.Next(true)
obs.Next(false)
assert.Equal(t, 1, numCalled, "must unsubscribe after first call")
}
func TestMapMiddleware_Pipe(t *testing.T) {
obs := Observable{}
obs.Pipe(
Map(func(i interface{}) interface{} {
return true
}),
).Subscribe(func(i interface{}) {
assert.True(t, i.(bool), "must be transformed to 'true'")
})
obs.Next(nil)
}
func TestMiddlewares(t *testing.T) {
obs := Observable{}
numCalled := 0
obs.Pipe(
Filter(func(i interface{}) bool {
return i == 10
}),
Map(func(i interface{}) interface{} {
num := i.(int)
return num * 2
}),
First(),
).Subscribe(func(i interface{}) {
numCalled++
assert.Equal(t, 20, i)
})
obs.Next(1) // Filtered
obs.Next(nil) // Filtered
obs.Next(10) // OK
obs.Next(10) // Unsubscribed
assert.Equal(t, 1, numCalled, "must unsubscribe after first event")
}
func TestTakeUntilMiddleware_Pipe(t *testing.T) {
obs := &Observable{}
numCalled := 0
trigger := &Observable{}
obs.Pipe(
TakeUntil(trigger),
).Subscribe(func(i interface{}) {
numCalled++
})
obs.Next(nil)
obs.Next(nil)
trigger.Next(nil)
obs.Next(nil)
assert.Equal(t, 2, numCalled, "must unsubscribe when trigger is called")
}