-
Notifications
You must be signed in to change notification settings - Fork 22
/
publisher_test.go
67 lines (59 loc) · 1.28 KB
/
publisher_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
package fpgo
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestPublisher(t *testing.T) {
var s *Subscription[interface{}]
var p2 *PublisherDef[interface{}]
p := Publisher.New()
actual := 0
expected := 0
assert.Equal(t, expected, actual)
assert.Equal(t, true, s == nil)
actual = 0
expected = 0
s = p.Subscribe(Subscription[interface{}]{
OnNext: func(in interface{}) {
// fmt.Println(*in)
actual, _ = Maybe.Just(in).ToInt()
},
})
assert.Equal(t, expected, actual)
assert.Equal(t, false, s == nil)
actual = 0
expected = 1
p.Publish((1))
assert.Equal(t, expected, actual)
actual = 0
expected = 0
p.Unsubscribe(s)
p.Publish((1))
assert.Equal(t, expected, actual)
p = Publisher.New()
p2 = p.Map(func(in interface{}) interface{} {
v, _ := Maybe.Just(in).ToInt()
return (v + 2)
}).Map(func(in interface{}) interface{} {
v, _ := Maybe.Just(in).ToInt()
return (v + 3)
})
s = p2.Subscribe(Subscription[interface{}]{
OnNext: func(in interface{}) {
actual, _ = Maybe.Just(in).ToInt()
},
})
actual = 0
expected = 6
p.Publish((1))
assert.Equal(t, expected, actual)
actual = 0
expected = 16
p.Publish((11))
assert.Equal(t, expected, actual)
actual = 0
expected = 0
p2.Unsubscribe(s)
p.Publish((1))
assert.Equal(t, expected, actual)
}