forked from samber/mo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoption_test.go
155 lines (116 loc) · 2.89 KB
/
option_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
package mo
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestOptionSome(t *testing.T) {
is := assert.New(t)
is.Equal(Option[int]{value: 42, isPresent: true}, Some(42))
}
func TestOptionNone(t *testing.T) {
is := assert.New(t)
is.Equal(Option[int]{isPresent: false}, None[int]())
}
func TestOptionIsPresent(t *testing.T) {
is := assert.New(t)
is.True(Some(42).IsPresent())
is.False(None[int]().IsPresent())
}
func TestOptionIsAbsent(t *testing.T) {
is := assert.New(t)
is.False(Some(42).IsAbsent())
is.True(None[int]().IsAbsent())
}
func TestOptionSize(t *testing.T) {
is := assert.New(t)
is.Equal(1, Some(42).Size())
is.Equal(0, None[int]().Size())
}
func TestOptionGet(t *testing.T) {
is := assert.New(t)
v1, ok1 := Some(42).Get()
v2, ok2 := None[int]().Get()
is.Equal(42, v1)
is.Equal(true, ok1)
is.Equal(0, v2)
is.Equal(false, ok2)
}
func TestOptionMustGet(t *testing.T) {
is := assert.New(t)
is.NotPanics(func() {
Some(42).MustGet()
})
is.Panics(func() {
None[int]().MustGet()
})
is.Equal(42, Some(42).MustGet())
}
func TestOptionOrElse(t *testing.T) {
is := assert.New(t)
is.Equal(42, Some(42).OrElse(21))
is.Equal(21, None[int]().OrElse(21))
}
func TestOptionOrEmpty(t *testing.T) {
is := assert.New(t)
is.Equal(42, Some(42).OrEmpty())
is.Equal(0, None[int]().OrEmpty())
}
func TestOptionForEach(t *testing.T) {
is := assert.New(t)
tmp := 0
f := func(x int) {
tmp = x
}
None[int]().ForEach(f)
is.Equal(0, tmp)
Some(42).ForEach(f)
is.Equal(42, tmp)
}
func TestOptionMatch(t *testing.T) {
is := assert.New(t)
onValue := func(i int) (int, bool) {
return i * 2, true
}
onNone := func() (int, bool) {
return 0, false
}
opt1 := Some(21).Match(onValue, onNone)
opt2 := None[int]().Match(onValue, onNone)
is.Equal(Option[int]{value: 42, isPresent: true}, opt1)
is.Equal(Option[int]{value: 0, isPresent: false}, opt2)
}
func TestOptionMap(t *testing.T) {
is := assert.New(t)
opt1 := Some(21).Map(func(i int) (int, bool) {
return i * 2, true
})
opt2 := None[int]().Map(func(i int) (int, bool) {
is.Fail("should not be called")
return 42, true
})
is.Equal(Option[int]{value: 42, isPresent: true}, opt1)
is.Equal(Option[int]{value: 0, isPresent: false}, opt2)
}
func TestOptionMapNone(t *testing.T) {
is := assert.New(t)
opt1 := Some(21).MapNone(func() (int, bool) {
is.Fail("should not be called")
return 42, true
})
opt2 := None[int]().MapNone(func() (int, bool) {
return 42, true
})
is.Equal(Option[int]{value: 21, isPresent: true}, opt1)
is.Equal(Option[int]{value: 42, isPresent: true}, opt2)
}
func TestOptionFlatMap(t *testing.T) {
is := assert.New(t)
opt1 := Some(21).FlatMap(func(i int) Option[int] {
return Some(42)
})
opt2 := None[int]().FlatMap(func(i int) Option[int] {
return Some(42)
})
is.Equal(Option[int]{value: 42, isPresent: true}, opt1)
is.Equal(Option[int]{value: 0, isPresent: false}, opt2)
}