forked from samber/mo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
either_test.go
201 lines (161 loc) · 4.25 KB
/
either_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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package mo
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestEitherLeft(t *testing.T) {
is := assert.New(t)
left := Left[int, bool](42)
is.Equal(Either[int, bool]{left: 42, right: false, isLeft: true}, left)
}
func TestEitherRight(t *testing.T) {
is := assert.New(t)
right := Right[int, bool](true)
is.Equal(Either[int, bool]{left: 0, right: true, isLeft: false}, right)
}
func TestEitherIsLeftOrRight(t *testing.T) {
is := assert.New(t)
left := Left[int, bool](42)
right := Right[int, bool](true)
is.True(left.IsLeft())
is.False(left.IsRight())
is.False(right.IsLeft())
is.True(right.IsRight())
}
func TestEitherLeftOrRight(t *testing.T) {
is := assert.New(t)
left := Left[int, bool](42)
right := Right[int, bool](true)
result1, ok1 := left.Left()
result2, ok2 := left.Right()
result3, ok3 := right.Left()
result4, ok4 := right.Right()
is.Equal(42, result1)
is.True(ok1)
is.Equal(false, result2)
is.False(ok2)
is.Equal(0, result3)
is.False(ok3)
is.Equal(true, result4)
is.True(ok4)
}
func TestEitherMustLeftOrRight(t *testing.T) {
is := assert.New(t)
left := Left[int, bool](42)
right := Right[int, bool](true)
is.NotPanics(func() {
is.Equal(42, left.MustLeft())
})
is.Panics(func() {
left.MustRight()
})
is.Panics(func() {
right.MustLeft()
})
is.NotPanics(func() {
is.Equal(true, right.MustRight())
})
}
func TestEitherGetOrElse(t *testing.T) {
is := assert.New(t)
left := Left[int, string](42)
right := Right[int, string]("foobar")
is.Equal(42, left.LeftOrElse(21))
is.Equal(21, right.LeftOrElse(21))
is.Equal("baz", left.RightOrElse("baz"))
is.Equal("foobar", right.RightOrElse("baz"))
}
func TestEitherGetOrEmpty(t *testing.T) {
is := assert.New(t)
left := Left[int, string](42)
right := Right[int, string]("foobar")
is.Equal(42, left.LeftOrEmpty())
is.Equal(0, right.LeftOrEmpty())
is.Equal("", left.RightOrEmpty())
is.Equal("foobar", right.RightOrEmpty())
}
func TestEitherSwap(t *testing.T) {
is := assert.New(t)
left := Left[int, string](42)
right := Right[int, string]("foobar")
is.Equal(Either[string, int]{left: "", right: 42, isLeft: false}, left.Swap())
is.Equal(Either[string, int]{left: "foobar", right: 0, isLeft: true}, right.Swap())
}
func TestEitherForEach(t *testing.T) {
is := assert.New(t)
Left[int, string](42).ForEach(
func(a int) {
is.Equal(42, a)
},
func(b string) {
is.Fail("should not enter here")
},
)
Right[int, string]("foobar").ForEach(
func(a int) {
is.Fail("should not enter here")
},
func(b string) {
is.Equal("foobar", b)
},
)
}
func TestEitherMatch(t *testing.T) {
is := assert.New(t)
e1 := Left[int, string](42).Match(
func(a int) Either[int, string] {
is.Equal(42, a)
return Left[int, string](21)
},
func(b string) Either[int, string] {
is.Fail("should not enter here")
return Left[int, string](1)
},
)
e2 := Right[int, string]("foobar").Match(
func(a int) Either[int, string] {
is.Fail("should not enter here")
return Right[int, string]("baz")
},
func(b string) Either[int, string] {
is.Equal("foobar", b)
return Right[int, string]("plop")
},
)
is.Equal(Either[int, string]{left: 21, right: "", isLeft: true}, e1)
is.Equal(Either[int, string]{left: 0, right: "plop", isLeft: false}, e2)
}
func TestEitherMapLeft(t *testing.T) {
is := assert.New(t)
e1 := Left[int, string](42).MapLeft(
func(a int) Either[int, string] {
is.Equal(42, a)
return Left[int, string](21)
},
)
e2 := Right[int, string]("foobar").MapLeft(
func(a int) Either[int, string] {
is.Fail("should not enter here")
return Right[int, string]("plop")
},
)
is.Equal(Either[int, string]{left: 21, right: "", isLeft: true}, e1)
is.Equal(Either[int, string]{left: 0, right: "foobar", isLeft: false}, e2)
}
func TestEitherMapRight(t *testing.T) {
is := assert.New(t)
e1 := Left[int, string](42).MapRight(
func(b string) Either[int, string] {
is.Fail("should not enter here")
return Left[int, string](21)
},
)
e2 := Right[int, string]("foobar").MapRight(
func(b string) Either[int, string] {
is.Equal("foobar", b)
return Right[int, string]("plop")
},
)
is.Equal(Either[int, string]{left: 42, right: "", isLeft: true}, e1)
is.Equal(Either[int, string]{left: 0, right: "plop", isLeft: false}, e2)
}