-
Notifications
You must be signed in to change notification settings - Fork 0
/
optional_test.go
50 lines (44 loc) · 1.02 KB
/
optional_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
package optional_test
import (
"testing"
"github.com/benburwell/optional"
)
func TestOf(t *testing.T) {
o := optional.Of("hello")
if !o.Present() {
t.Errorf("expected value to be present, but was not")
}
val, ok := o.Value()
if !ok {
t.Errorf("expected getting value to be OK, but none returned")
}
if val != "hello" {
t.Errorf("expected value to be 'hello', but got %q", val)
}
}
func TestOrElse(t *testing.T) {
t.Run("present", func(t *testing.T) {
actual := optional.Of("a").OrElse("b")
if actual != "a" {
t.Errorf("expected 'a', but got: %s", actual)
}
})
t.Run("absent", func(t *testing.T) {
var s *string
actual := optional.OfNillable(s).OrElse("b")
if actual != "b" {
t.Errorf("expected 'a', but got: %s", actual)
}
})
}
func TestOfNillable(t *testing.T) {
var s *string
if optional.OfNillable(s).Present() {
t.Errorf("nil should not be present")
}
}
func TestEmpty(t *testing.T) {
if optional.Empty[string]().Present() {
t.Errorf("expected empty optional to not be present")
}
}