-
Notifications
You must be signed in to change notification settings - Fork 0
/
optional_test.go
164 lines (134 loc) · 2.71 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
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
package optional //nolint:testpackage // required, because accessing to private fields
import (
"encoding/json"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestEmptyValues(t *testing.T) {
t.Parallel()
table := []struct {
name string
jsonBytes []byte
exp Val[[]string]
}{
{
name: "empty_input",
jsonBytes: []byte("{}"),
exp: Val[[]string]{
hasVal: false,
value: nil,
},
},
{
name: "null_value",
jsonBytes: []byte(`{"v": null}`),
exp: Val[[]string]{
hasVal: false,
value: nil,
},
},
}
for i := range table {
row := table[i]
t.Run(row.name, func(t *testing.T) {
t.Parallel()
var obj struct {
V Val[[]string] `json:"v"`
}
assert.NoError(t, json.Unmarshal(row.jsonBytes, &obj))
assert.Equal(t, row.exp, obj.V)
})
}
}
func TestNewFromPointer(t *testing.T) {
t.Parallel()
val := new(string)
*val = "value"
opt := NewFromPointer(val)
assert.True(t, opt.HasVal())
assert.Equal(t, "value", opt.Val())
val = nil
opt = NewFromPointer(val)
assert.False(t, opt.HasVal())
assert.Equal(t, "", opt.Val())
}
func TestNotEmptyValues(t *testing.T) {
t.Parallel()
table := []struct {
jsonBytes []byte
exp Val[[]string]
}{
{
jsonBytes: []byte(`{"v": []}`),
exp: Val[[]string]{
hasVal: true,
value: []string{},
},
},
{
jsonBytes: []byte(`{"v": ["hi"]}`),
exp: Val[[]string]{
hasVal: true,
value: []string{"hi"},
},
},
}
for i := range table {
row := table[i]
t.Run("", func(t *testing.T) {
t.Parallel()
var obj struct {
V Val[[]string] `json:"v"`
}
assert.NoError(t, json.Unmarshal(row.jsonBytes, &obj))
assert.Equal(t, row.exp, obj.V)
})
}
}
func TestNewEmpty(t *testing.T) {
t.Parallel()
assert.Equal(t, Val[string]{
hasVal: true,
value: "hello",
}, New("hello"))
assert.Equal(t, Val[int]{
hasVal: true,
value: 0,
}, New(0))
assert.Equal(t, Val[string]{
hasVal: false,
value: "",
}, Empty[string]())
assert.Equal(t, Val[int]{
hasVal: false,
value: 0,
}, Empty[int]())
}
func TestSet(t *testing.T) {
t.Parallel()
fOk := func(val Val[int]) {
t.Helper()
t.Run("", func(t *testing.T) {
require.Equal(t, false, val.HasVal())
val.Set(42)
require.Equal(t, true, val.HasVal())
require.Equal(t, 42, val.Val())
val.Set(-42)
require.Equal(t, true, val.HasVal())
require.Equal(t, -42, val.Val())
})
}
fOk(Empty[int]())
var val Val[int]
fOk(val)
}
func TestReset(t *testing.T) {
t.Parallel()
val := New(42)
require.Equal(t, true, val.HasVal())
require.Equal(t, 42, val.Val())
val.Reset()
require.Equal(t, false, val.HasVal())
require.Equal(t, 0, val.Val())
}