-
Notifications
You must be signed in to change notification settings - Fork 19
/
string_test.go
195 lines (152 loc) · 4.19 KB
/
string_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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package optional
import (
"encoding/json"
"testing"
"github.com/stretchr/testify/assert"
)
func TestString_NewStringFromPtr_NotNil(t *testing.T) {
s := "foo"
p := &s
o := NewStringFromPtr(p)
v, err := o.Get()
assert.True(t, o.Present())
assert.NoError(t, err)
assert.Equal(t, "foo", v)
assert.True(t, p != o.value, "expect return pointer to be different from embedded pointer")
}
func TestString_NewStringFromPtr_Nil(t *testing.T) {
o := NewStringFromPtr(nil)
v, err := o.Get()
assert.False(t, o.Present())
assert.Error(t, err)
assert.Equal(t, "", v)
}
func TestString_Get_Present(t *testing.T) {
o := NewString("foo")
v, err := o.Get()
assert.True(t, o.Present())
assert.NoError(t, err)
assert.Equal(t, "foo", v)
}
func TestString_Get_NotPresent(t *testing.T) {
o := String{}
var zero string
v, err := o.Get()
assert.False(t, o.Present())
assert.Error(t, err)
assert.Equal(t, zero, v)
}
func TestString_MustGet_Present(t *testing.T) {
o := NewString("foo")
v := o.MustGet()
assert.True(t, o.Present())
assert.Equal(t, "foo", v)
}
func TestString_MustGet_NotPresent(t *testing.T) {
o := String{}
assert.Panics(t, func() { _ = o.MustGet() })
assert.False(t, o.Present())
}
func TestString_OrElse_Present(t *testing.T) {
o := NewString("foo")
v := o.OrElse("bar")
assert.True(t, o.Present())
assert.Equal(t, "foo", v)
}
func TestString_OrElse_NotPresent(t *testing.T) {
o := String{}
v := o.OrElse("bar")
assert.False(t, o.Present())
assert.Equal(t, "bar", v)
}
func TestString_If_Present(t *testing.T) {
o := NewString("foo")
canary := false
o.If(func(v string) {
canary = true
})
assert.True(t, o.Present())
assert.True(t, canary)
}
func TestString_If_NotPresent(t *testing.T) {
o := String{}
canary := false
o.If(func(v string) {
canary = true
})
assert.False(t, o.Present())
assert.False(t, canary)
}
func TestString_ToPtr_NotNil(t *testing.T) {
o := NewString("foo")
p := o.ToPtr()
assert.NotNil(t, p)
assert.Equal(t, "foo", *p)
assert.True(t, p != o.value, "expect return pointer to be different from embedded pointer")
}
func TestString_ToPtr_Nil(t *testing.T) {
o := String{}
p := o.ToPtr()
assert.Nil(t, p)
}
func TestString_MarshalJSON(t *testing.T) {
type fields struct {
WithValue String
WithZeroValue String
WithNoValue String
Unused String
}
var instance = fields{
WithValue: NewString("foo"),
WithZeroValue: NewString(""),
WithNoValue: String{},
}
out, err := json.Marshal(instance)
assert.NoError(t, err)
assert.Equal(t, `{"WithValue":"foo","WithZeroValue":"","WithNoValue":null,"Unused":null}`, string(out))
}
func TestString_UnmarshalJSON(t *testing.T) {
type fields struct {
WithValue String
WithZeroValue String
WithNoValue String
Unused String
}
var jsonString = `{"WithValue":"foo","WithZeroValue":"","WithNoValue":null}`
instance := fields{}
err := json.Unmarshal([]byte(jsonString), &instance)
assert.NoError(t, err)
assert.True(t, instance.WithZeroValue.Present())
assert.Equal(t, "foo", *instance.WithValue.value)
assert.True(t, instance.WithZeroValue.Present())
assert.Equal(t, "", *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 TestString_UnmarshalJSON_Overwritten(t *testing.T) {
type fields struct {
WithValue String
WithZeroValue String
WithNoValue String
Unused String
}
var jsonString = `{"WithValue":"foo","WithZeroValue":"","WithNoValue":null}`
instance := fields{
WithValue: NewString("seed_a"),
WithZeroValue: NewString("seed_b"),
WithNoValue: NewString("seed_c"),
Unused: NewString("seed_d"),
}
err := json.Unmarshal([]byte(jsonString), &instance)
assert.NoError(t, err)
assert.True(t, instance.WithValue.Present())
assert.Equal(t, "foo", *instance.WithValue.value)
assert.True(t, instance.WithZeroValue.Present())
assert.Equal(t, "", *instance.WithZeroValue.value)
assert.False(t, instance.WithNoValue.Present())
assert.Nil(t, instance.WithNoValue.value)
assert.True(t, instance.Unused.Present())
assert.Equal(t, "seed_d", *instance.Unused.value)
}