-
Notifications
You must be signed in to change notification settings - Fork 338
/
optionalsingle_test.go
60 lines (52 loc) · 1.57 KB
/
optionalsingle_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
package rxgo
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
"go.uber.org/goleak"
)
func Test_OptionalSingle_Get_Item(t *testing.T) {
defer goleak.VerifyNone(t)
var os OptionalSingle = &OptionalSingleImpl{iterable: Just(1)()}
get, err := os.Get()
assert.NoError(t, err)
assert.Equal(t, 1, get.V)
}
func Test_OptionalSingle_Get_Empty(t *testing.T) {
defer goleak.VerifyNone(t)
var os OptionalSingle = &OptionalSingleImpl{iterable: Empty()}
get, err := os.Get()
assert.NoError(t, err)
assert.Equal(t, OptionalSingleEmpty, get)
}
func Test_OptionalSingle_Get_Error(t *testing.T) {
defer goleak.VerifyNone(t)
var os OptionalSingle = &OptionalSingleImpl{iterable: Just(errFoo)()}
get, err := os.Get()
assert.NoError(t, err)
assert.Equal(t, errFoo, get.E)
}
func Test_OptionalSingle_Get_ContextCanceled(t *testing.T) {
defer goleak.VerifyNone(t)
ctx, cancel := context.WithCancel(context.Background())
var os OptionalSingle = &OptionalSingleImpl{iterable: Never()}
cancel()
_, err := os.Get(WithContext(ctx))
assert.Equal(t, ctx.Err(), err)
}
func Test_OptionalSingle_Map(t *testing.T) {
defer goleak.VerifyNone(t)
single := Just(1)().Max(func(_, _ interface{}) int {
return 1
}).Map(func(_ context.Context, i interface{}) (interface{}, error) {
return i.(int) + 1, nil
})
Assert(context.Background(), t, single, HasItem(2), HasNoError())
}
func Test_OptionalSingle_Observe(t *testing.T) {
defer goleak.VerifyNone(t)
os := JustItem(1).Filter(func(i interface{}) bool {
return i == 1
})
Assert(context.Background(), t, os, HasItem(1), HasNoError())
}