-
Notifications
You must be signed in to change notification settings - Fork 0
/
option_test.go
52 lines (41 loc) · 877 Bytes
/
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
package gost
import (
"slices"
"testing"
)
func TestOption(t *testing.T) {
if opt := Some("value"); opt.IsSome() {
if val := opt.Unwrap(); val != "value" {
t.Fatal(val)
}
} else {
t.Fail()
}
if opt := None[string](); !opt.IsNone() {
t.Fail()
}
opt := None[string]()
if val := opt.UnwrapOr("alt"); val != "alt" {
t.Fatal(val)
}
if val := opt.UnwrapOrElse(func() string { return "alt" }); val != "alt" {
t.Fatal(val)
}
if val := opt.UnwrapOrZero(); val != "" {
t.Fatal(val)
}
}
func TestAsOption(t *testing.T) {
val := AsOption(slices.BinarySearch([]int{1, 2, 3, 4, 5}, 4)).UnwrapOr(0)
if val != 3 {
t.Fatal(val)
}
}
func TestOptionMatching(t *testing.T) {
val := AsOption(slices.BinarySearch([]int{1, 2, 3, 4, 5}, 4)).
OnSome(func(v *int) *int { return v }).
OnNone(func() int { return 0 })
if val != 3 {
t.Fatal(val)
}
}