-
Notifications
You must be signed in to change notification settings - Fork 19
/
int_test.go
180 lines (141 loc) · 3.66 KB
/
int_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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package optional
import (
"encoding/json"
"testing"
"github.com/stretchr/testify/assert"
)
func TestInt_NewIntFromPtr_NotNil(t *testing.T) {
i := 42
p := &i
o := NewIntFromPtr(p)
v, err := o.Get()
assert.True(t, o.Present())
assert.NoError(t, err)
assert.Equal(t, 42, v)
assert.True(t, p != o.value, "expect return pointer to be different from embedded pointer")
}
func TestInt_NewIntFromPtr_Nil(t *testing.T) {
o := NewIntFromPtr(nil)
var zero int
v, err := o.Get()
assert.False(t, o.Present())
assert.Error(t, err)
assert.Equal(t, zero, v)
}
func TestInt_Get_Present(t *testing.T) {
o := NewInt(42)
v, err := o.Get()
assert.True(t, o.Present())
assert.NoError(t, err)
assert.Equal(t, 42, v)
}
func TestInt_Get_NotPresent(t *testing.T) {
o := Int{}
var zero int
v, err := o.Get()
assert.False(t, o.Present())
assert.Error(t, err)
assert.Equal(t, zero, v)
}
func TestInt_MustGet_Present(t *testing.T) {
o := NewInt(42)
v := o.MustGet()
assert.True(t, o.Present())
assert.Equal(t, 42, v)
}
func TestInt_MustGet_NotPresent(t *testing.T) {
o := Int{}
assert.Panics(t, func() { _ = o.MustGet() })
assert.False(t, o.Present())
}
func TestInt_OrElse_Present(t *testing.T) {
o := NewInt(42)
v := o.OrElse(99)
assert.True(t, o.Present())
assert.Equal(t, 42, v)
}
func TestInt_OrElse_NotPresent(t *testing.T) {
o := Int{}
v := o.OrElse(99)
assert.False(t, o.Present())
assert.Equal(t, 99, v)
}
func TestInt_If_Present(t *testing.T) {
o := NewInt(42)
canary := false
o.If(func(v int) {
canary = true
})
assert.True(t, o.Present())
assert.True(t, canary)
}
func TestInt_If_NotPresent(t *testing.T) {
o := Int{}
canary := false
o.If(func(v int) {
canary = true
})
assert.False(t, o.Present())
assert.False(t, canary)
}
func TestInt_MarshalJSON(t *testing.T) {
type fields struct {
WithValue Int
WithZeroValue Int
WithNoValue Int
Unused Int
}
var instance = fields{
WithValue: NewInt(42),
WithZeroValue: NewInt(0),
WithNoValue: Int{},
}
out, err := json.Marshal(instance)
assert.NoError(t, err)
assert.Equal(t, `{"WithValue":42,"WithZeroValue":0,"WithNoValue":null,"Unused":null}`, string(out))
}
func TestInt_UnmarshalJSON(t *testing.T) {
type fields struct {
WithValue Int
WithZeroValue Int
WithNoValue Int
Unused Int
}
var jsonString = `{"WithValue":42,"WithZeroValue":0,"WithNoValue":null}`
instance := fields{}
err := json.Unmarshal([]byte(jsonString), &instance)
assert.NoError(t, err)
assert.True(t, instance.WithZeroValue.Present())
assert.Equal(t, 42, *instance.WithValue.value)
assert.True(t, instance.WithZeroValue.Present())
assert.Equal(t, 0, *instance.WithZeroValue.value)
assert.False(t, instance.WithNoValue.Present())
assert.Nil(t, instance.WithNoValue.value)
assert.False(t, instance.Unused.Present())
assert.Nil(t, instance.Unused.value)
}
func TestInt_UnmarshalJSON_Overwritten(t *testing.T) {
type fields struct {
WithValue Int
WithZeroValue Int
WithNoValue Int
Unused Int
}
var jsonString = `{"WithValue":42,"WithZeroValue":0,"WithNoValue":null}`
instance := fields{
WithValue: NewInt(1),
WithZeroValue: NewInt(2),
WithNoValue: NewInt(3),
Unused: NewInt(4),
}
err := json.Unmarshal([]byte(jsonString), &instance)
assert.NoError(t, err)
assert.True(t, instance.WithValue.Present())
assert.Equal(t, 42, *instance.WithValue.value)
assert.True(t, instance.WithZeroValue.Present())
assert.Equal(t, 0, *instance.WithZeroValue.value)
assert.False(t, instance.WithNoValue.Present())
assert.Nil(t, instance.WithNoValue.value)
assert.True(t, instance.Unused.Present())
assert.Equal(t, 4, *instance.Unused.value)
}