-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoption_test.go
More file actions
192 lines (141 loc) · 3.43 KB
/
option_test.go
File metadata and controls
192 lines (141 loc) · 3.43 KB
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
package option_test
import (
"fmt"
"testing"
"github.com/BooleanCat/option"
"github.com/BooleanCat/option/internal/assert"
)
func ExampleOption_Unwrap() {
fmt.Println(option.Some(4).Unwrap())
// Output: 4
}
func ExampleOption_UnwrapOr() {
fmt.Println(option.Some(4).UnwrapOr(3))
fmt.Println(option.None[int]().UnwrapOr(3))
// Output:
// 4
// 3
}
func ExampleOption_UnwrapOrElse() {
fmt.Println(option.Some(4).UnwrapOrElse(func() int {
return 3
}))
fmt.Println(option.None[int]().UnwrapOrElse(func() int {
return 3
}))
// Output:
// 4
// 3
}
func ExampleOption_UnwrapOrZero() {
fmt.Println(option.Some(4).UnwrapOrZero())
fmt.Println(option.None[int]().UnwrapOrZero())
// Output
// 4
// 0
}
func ExampleOption_IsSome() {
fmt.Println(option.Some(4).IsSome())
fmt.Println(option.None[int]().IsSome())
// Output:
// true
// false
}
func ExampleOption_IsNone() {
fmt.Println(option.Some(4).IsNone())
fmt.Println(option.None[int]().IsNone())
// Output:
// false
// true
}
func ExampleOption_Value() {
value, ok := option.Some(4).Value()
fmt.Println(value)
fmt.Println(ok)
// Output:
// 4
// true
}
func ExampleOption_Expect() {
fmt.Println(option.Some(4).Expect("oops"))
// Output: 4
}
func TestSomeStringer(t *testing.T) {
t.Parallel()
assert.Equal(t, fmt.Sprintf("%s", option.Some("foo")), "Some(foo)") //nolint:staticcheck
assert.Equal(t, fmt.Sprintf("%s", option.Some(42)), "Some(42)") //nolint:staticcheck
}
func TestNoneStringer(t *testing.T) {
t.Parallel()
assert.Equal(t, fmt.Sprintf("%s", option.None[string]()), "None") //nolint:staticcheck
}
func TestSomeUnwrap(t *testing.T) {
t.Parallel()
assert.Equal(t, option.Some(42).Unwrap(), 42)
}
func TestNoneUnwrap(t *testing.T) {
t.Parallel()
defer func() {
assert.Equal(t, fmt.Sprint(recover()), "called `Option.Unwrap()` on a `None` value")
}()
option.None[string]().Unwrap()
t.Error("did not panic")
}
func TestSomeUnwrapOr(t *testing.T) {
t.Parallel()
assert.Equal(t, option.Some(42).UnwrapOr(3), 42)
}
func TestNoneUnwrapOr(t *testing.T) {
t.Parallel()
assert.Equal(t, option.None[int]().UnwrapOr(3), 3)
}
func TestSomeUnwrapOrElse(t *testing.T) {
t.Parallel()
assert.Equal(t, option.Some(42).UnwrapOrElse(func() int { return 41 }), 42)
}
func TestNoneUnwrapOrElse(t *testing.T) {
t.Parallel()
assert.Equal(t, option.None[int]().UnwrapOrElse(func() int { return 41 }), 41)
}
func TestSomeUnwrapOrZero(t *testing.T) {
t.Parallel()
assert.Equal(t, option.Some(42).UnwrapOrZero(), 42)
}
func TestNoneUnwrapOrZero(t *testing.T) {
t.Parallel()
assert.Equal(t, option.None[int]().UnwrapOrZero(), 0)
}
func TestIsSome(t *testing.T) {
t.Parallel()
assert.True(t, option.Some(42).IsSome())
assert.False(t, option.None[int]().IsSome())
}
func TestIsNone(t *testing.T) {
t.Parallel()
assert.False(t, option.Some(42).IsNone())
assert.True(t, option.None[int]().IsNone())
}
func TestSomeValue(t *testing.T) {
t.Parallel()
value, ok := option.Some(42).Value()
assert.Equal(t, value, 42)
assert.True(t, ok)
}
func TestNoneValue(t *testing.T) {
t.Parallel()
value, ok := option.None[int]().Value()
assert.Equal(t, value, 0)
assert.False(t, ok)
}
func TestSomeExpect(t *testing.T) {
t.Parallel()
assert.Equal(t, option.Some(42).Expect("oops"), 42)
}
func TestNoneExpect(t *testing.T) {
t.Parallel()
defer func() {
assert.Equal(t, fmt.Sprint(recover()), "oops")
}()
option.None[int]().Expect("oops")
t.Error("did not panic")
}