-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathopt_test.go
95 lines (70 loc) · 1.82 KB
/
opt_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
package opt
import (
"testing"
"github.com/stretchr/testify/require"
)
func TestSmoke(t *testing.T) {
require.True(t, true)
}
func TestNew(t *testing.T) {
anOptional := New[string]()
_, ok := anOptional.Get()
require.Equal(t, ok, false)
require.False(t, anOptional.Exists())
require.Equal(t, "something", anOptional.GetOrElse("something"))
}
func TestOf(t *testing.T) {
anOptional := Of("hello")
require.True(t, anOptional.Exists())
value, ok := anOptional.Get()
require.Equal(t, true, ok)
require.Equal(t, "hello", value)
require.Equal(t, "hello", anOptional.MustGet())
}
func TestMustGet(t *testing.T) {
anOptional := New[string]()
defer func() {
if r := recover(); r == nil {
t.Errorf("The code did not panic")
}
}()
anOptional.MustGet()
}
func TestIf(t *testing.T) {
anOptional := New[string]()
ranIf := false
If(anOptional, func (value string) bool {
ranIf = true
return true
})
require.False(t, ranIf)
anOptional = Of("hello")
If(anOptional, func (value string) bool {
ranIf = true
return true
})
require.True(t, ranIf)
}
func TestMarshall(t *testing.T) {
anOptional := New[string]()
data, err := anOptional.MarshalJSON()
require.Nil(t, err)
require.Equal(t, `null`, string(data))
anOptional = Of("hello")
data, err = anOptional.MarshalJSON()
require.Nil(t, err)
require.Equal(t, `"hello"`, string(data))
}
func TestUnmarshall(t *testing.T) {
anOptional := New[string]()
err := anOptional.UnmarshalJSON([]byte(`null`))
require.Nil(t, err)
require.False(t, anOptional.Exists())
anOptional = New[string]()
err = anOptional.UnmarshalJSON([]byte(`"hello"`))
require.Nil(t, err)
require.True(t, anOptional.Exists())
require.Equal(t, "hello", anOptional.MustGet())
err = anOptional.UnmarshalJSON([]byte(`"asdjl:1k2l;j'""`))
require.NotNil(t, err)
}