-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgoptional_test.go
115 lines (88 loc) · 1.56 KB
/
goptional_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
package goptional_test
import (
"testing"
"github.com/duplabe/goptional"
)
func TestNone(t *testing.T) {
none := goptional.None[bool]()
if !none.IsEmpty() {
t.Fatalf(`None is not empty`)
}
if none.IsPresent() {
t.Fatalf(`None is present`)
}
val, ok := none.Get()
if ok {
t.Fatalf(`Get is ok`)
}
if val {
t.Fatalf(`Val is not false`)
}
if !none.GetOr(true) {
t.Fatalf(`GetOr false`)
}
if none.GetOrZero() {
t.Fatalf(`GetOrZero true`)
}
var flag bool
none.IfPresent(func(_ bool) {
flag = true
})
if flag {
t.Fatalf(`IfPresent callback was called`)
}
defer func() {
if r := recover(); r == nil {
t.Errorf("The code did not panic")
}
}()
none.GetOrPanic()
}
func TestSome(t *testing.T) {
some := goptional.Some(42)
if some.IsEmpty() {
t.Fatalf(`Some is empty`)
}
if !some.IsPresent() {
t.Fatalf(`Some is not present`)
}
val, ok := some.Get()
if !ok {
t.Fatalf(`Get is not ok`)
}
if val == 0 {
t.Fatalf(`Val is 0`)
}
if some.GetOr(100) != 42 {
t.Fatalf(`GetOr is not 42`)
}
if some.GetOrZero() != 42 {
t.Fatalf(`GetOrZero is not 42`)
}
var flag bool
some.IfPresent(func(val int) {
if val == 0 {
t.Fatalf(`IfPresent val is 0`)
}
flag = true
})
if !flag {
t.Fatalf(`IfPresent callback was not called`)
}
defer func() {
if r := recover(); r != nil {
t.Errorf("The code paniced")
}
}()
some.GetOrPanic()
}
func TestOf(t *testing.T) {
val := 42
var p *int
if (goptional.Of(&val)).IsEmpty() {
t.Fatalf(`empty`)
}
if (goptional.Of(p)).IsPresent() {
t.Fatalf(`present`)
}
}