-
Notifications
You must be signed in to change notification settings - Fork 0
/
option_empty_test.go
108 lines (84 loc) · 1.92 KB
/
option_empty_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
package fluent
import (
"errors"
"testing"
"github.com/stretchr/testify/assert"
)
func Test_OptionEmpty_Present(t *testing.T) {
o := Empty[int]()
assert.False(t, o.IsPresent())
}
func Test_OptionEmpty_Get(t *testing.T) {
o := Empty[int]()
defer func() {
err := recover()
assert.Equal(t, "empty option", err)
}()
o.Get()
t.Error("should panic")
}
func Test_OptionEmpty_Map(t *testing.T) {
o := Empty[int]()
called := new(bool)
*called = false
o = o.Map(func(a int) int {
*called = true
return a * 2
})
assert.False(t, o.IsPresent(), "present")
assert.False(t, *called, "mapper called")
}
func Test_OptionEmpty_OrElse(t *testing.T) {
o := Empty[int]()
expected := 987654321
actual := o.OrElse(expected)
assert.Equal(t, expected, actual)
}
func Test_OptionEmpty_OrElseGet(t *testing.T) {
o := Empty[int]()
expected := 987654321
actual := o.OrElseGet(func() int {
return expected
})
assert.Equal(t, expected, actual)
}
func Test_OptionEmpty_Or(t *testing.T) {
o := Empty[int]()
expected := 987654321
actual := o.Or(func() Option[int] {
return Present(expected)
})
assert.True(t, actual.IsPresent(), "present")
assert.Equal(t, expected, actual.Get())
}
func Test_OptionEmpty_OrError(t *testing.T) {
o := Empty[int]()
err := errors.New("err")
actual := o.OrError(err)
assert.True(t, actual.IsErr(), "IsErr")
assert.Equal(t, err, actual.GetErr(), "error")
}
func Test_OptionEmpty_IfPresent(t *testing.T) {
o := Empty[int]()
called := new(bool)
*called = false
o.IfPresent(func(i int) {
*called = true
})
assert.False(t, *called, "called")
}
func Test_OptionEmpty_Filter(t *testing.T) {
o := Empty[int]()
called := new(bool)
*called = false
o = o.Filter(func(i int) bool {
*called = true
return true
})
assert.False(t, o.IsPresent(), "present")
assert.False(t, *called, "called")
}
func Test_OptionEmpty_String(t *testing.T) {
o := Empty[int]()
assert.NotEmpty(t, o.String())
}