-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathoption_test.go
291 lines (248 loc) · 6.41 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
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
package goption
import (
"testing"
)
// TestUnwrapFail tests that unwrapping none fails..
func TestUnwrapFail(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("Expected to fail unwrapping empty optional")
}
}()
None[struct{}]().Unwrap()
}
// TestUnwrapSuccess tests that unwrapping some succeeds.
func TestUnwrapSuccess(t *testing.T) {
val := Some(3).Unwrap()
if val != 3 {
t.Errorf("Failed unwrapping value, expected 3 but got %v", val)
}
}
// TestUnwrapFail tests that unwrapping a ref on none fails.
func TestUnwrapRefFail(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("Expected to fail unwrapping empty optional")
}
}()
opt := None[struct{}]()
opt.UnwrapRef()
}
// TestUnwrapFail tests that unwrapping a ref on some succeeds.
func TestUnwrapRefSuccess(t *testing.T) {
opt := Some(3)
val := opt.UnwrapRef()
if *val != 3 {
t.Errorf("Failed unwrapping value, expected 3 but got %v", val)
}
}
// TestExpectMessage tests that the custom panic message delivers on expect.
func TestExpectMessage(t *testing.T) {
// This should succeed!
val := Some(3).Expect("not my custom message")
if val != 3 {
t.Errorf("Failed expecting int: %v", val)
}
defer func() {
if r := recover(); r == nil {
t.Errorf("Expected to fail unwrapping empty optional")
} else if r.(string) != "my custom message" {
t.Errorf("Failed setting expect string: %v", r)
}
}()
None[struct{}]().Expect("my custom message")
}
// TestExpectMessage tests that the custom panic message delivers on expect ref.
func TestExpectRefMessage(t *testing.T) {
opt := Some(3)
val := opt.ExpectRef("not my custom message")
if *val != 3 {
t.Errorf("Failed expecting int: %v", *val)
}
defer func() {
if r := recover(); r == nil {
t.Errorf("Expected to fail unwrapping empty optional")
} else if r.(string) != "my custom message" {
t.Errorf("Failed setting expect string: %v", r)
}
}()
None[struct{}]().Expect("my custom message")
}
// TestUnwrapOr tests that or values are returned when unwrap or-ing none.
// Otherwise it expects the underlying optional value.
func TestUnwrapOr(t *testing.T) {
val := None[int]().UnwrapOr(10)
if val != 10 {
t.Errorf("Failed unwrapping optional: %v", val)
}
val = Some(4).UnwrapOr(10)
if val != 4 {
t.Errorf("Failed unwrapping optional: %v", val)
}
}
// TestUnwrapOrDefault tests that default values are returned when unwrap or-ing none.
// Otherwise it expects the underlying optional value.
func TestUnwrapOrDefault(t *testing.T) {
val := None[struct{ FooBar int }]().UnwrapOrDefault()
if val.FooBar != 0 {
t.Errorf("Expected default value of FooBar, got %v", val)
}
val = Some[struct{ FooBar int }](struct{ FooBar int }{FooBar: 11}).UnwrapOrDefault()
if val.FooBar != 11 {
t.Errorf("Failed unwrapping: %v", val)
}
}
// TestOk tests that Ok returns true iff the value is Some.
func TestOk(t *testing.T) {
if !Some(0).Ok() {
t.Errorf("Some must always be present")
}
if None[struct{}]().Ok() {
t.Errorf("None must always be empty")
}
}
func TestGet(t *testing.T) {
val, ok := Some(3).Get()
if !ok {
t.Error("Failed unwrapping value")
}
if val != 3 {
t.Errorf("Failed unwrapping value, expected 3 but got %v", val)
}
_, ok = None[int]().Get()
if ok {
t.Error("Expected empty optional")
}
}
// TestApply tests Applying a function on an optional.
func TestApply(t *testing.T) {
square := func(v float64) float64 {
return v * v
}
val := Apply(Some(3.0), square)
if val.Unwrap() != 9.0 {
t.Errorf("Expected 3^2 = 9, got %v", val)
}
val = Apply(None[float64](), square)
if val.Ok() {
t.Errorf("Expected empty optional")
}
}
func TestDo(t *testing.T) {
val := Do(func() int {
return 1
})
if val.Unwrap() != 1 {
t.Errorf("Expected 1, got %v", val)
}
val = Do(func() int {
var a *int
return *a
})
if val.Ok() {
t.Errorf("Expected empty optional, got %v", val)
}
}
func TestRef(t *testing.T) {
myOption := Some[int](3)
ref := myOption.UnwrapRef()
if *ref != 3 {
t.Errorf("Failed unwrapping option ref, expected 3 but got %d", *ref)
}
*ref = 4
if unwrapped := myOption.Unwrap(); unwrapped != 4 {
t.Errorf("Failed unwrapping option ref, expected 4 but got %d", unwrapped)
}
}
func TestFromRef(t *testing.T) {
empty := FromRef((*int)(nil))
if empty.Ok() {
t.Errorf("FromRef must be empty for nil")
}
val := 10
ptr := &val
myOption := FromRef(ptr)
if !myOption.Ok() {
t.Errorf("FromRef must be present for non-nil value")
}
if unwrapped := myOption.Unwrap(); unwrapped != 10 {
t.Errorf("FromRef must contain dereferenced value, expected 10 but got %d", unwrapped)
}
}
// TestUnwrapRefOr tests that or values are returned when unwrap or-ing none.
// Otherwise it expects the underlying optional value.
func TestUnwrapRefOr(t *testing.T) {
opt := None[int]()
def := 10
val := opt.UnwrapRefOr(&def)
if *val != 10 {
t.Errorf("Failed unwrapping optional: %v", val)
}
opt = Some(4)
val = opt.UnwrapRefOr(&def)
if *val != 4 {
t.Errorf("Failed unwrapping optional: %v", val)
}
}
// TestUnwrapRefOrNil tests that default values are returned when unwrap or-ing none.
// Otherwise it expects the underlying optional value.
func TestUnwrapRefOrNil(t *testing.T) {
opt := None[struct{ FooBar int }]()
val := opt.UnwrapRefOrNil()
if val != nil {
t.Errorf("Expected default value of FooBar, got %v", val)
}
opt = Some[struct{ FooBar int }](struct{ FooBar int }{FooBar: 11})
val = opt.UnwrapRefOrNil()
if val.FooBar != 11 {
t.Errorf("Failed unwrapping: %v", val)
}
}
func TestWithSome(t *testing.T) {
opt := Some(3)
ranFor := 0
for range opt.With() {
ranFor++
}
if ranFor != 1 {
t.Fatalf("Expected to run the loop once, ran %d times", ranFor)
}
}
func TestWithNone(t *testing.T) {
opt := None[int]()
ranFor := 0
for range opt.With() {
ranFor++
}
if ranFor != 0 {
t.Fatalf("Expected not to run the loop, ran %d times", ranFor)
}
}
func TestIsZeroSome(t *testing.T) {
opt := Some(1)
if opt.IsZero() {
t.Fatalf("Expected to not be zero")
}
}
func TestIsZeroNone(t *testing.T) {
opt := None[int]()
if opt.IsZero() {
t.Fatalf("Expected to not be zero")
}
}
type fooZeroer bool
func (f fooZeroer) IsZero() bool {
return bool(f)
}
func TestIsZeroWrappedZero(t *testing.T) {
opt := Some(fooZeroer(true))
if !opt.IsZero() {
t.Fatalf("Expected to be zero")
}
}
func TestIsZeroWrappedNotZero(t *testing.T) {
opt := Some(fooZeroer(false))
if opt.IsZero() {
t.Fatalf("Expected not to be zero")
}
}