This repository has been 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
/
string_test.go
158 lines (123 loc) · 3.4 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
package optional
import (
"encoding/json"
"testing"
"github.com/stretchr/testify/assert"
)
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_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)
}