This repository was archived by the owner on Oct 25, 2023. It is now read-only.
forked from markphelps/optional
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathint_test.go
158 lines (123 loc) · 3.2 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
package optional
import (
"encoding/json"
"testing"
"github.com/stretchr/testify/assert"
)
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)
}