-
Notifications
You must be signed in to change notification settings - Fork 0
/
field_test.go
284 lines (239 loc) · 5.59 KB
/
field_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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
package conf_test
import (
"reflect"
"testing"
"time"
"github.com/rsb/conf"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestField_EnvVariable_NoPrefix(t *testing.T) {
f := conf.Field{
EnvVar: "FOO",
Tag: conf.Tag{
NoPrefix: true,
},
}
assert.Equal(t, "FOO", f.EnvVariable())
}
func TestField_EnvVariable_WithPrefix(t *testing.T) {
f := conf.Field{
Prefix: "MY_PREFIX",
EnvVar: "FOO",
Tag: conf.Tag{},
}
assert.Equal(t, "MY_PREFIX_FOO", f.EnvVariable())
}
func TestField_DefaultValue(t *testing.T) {
tests := []struct {
name string
field conf.Field
isCheck bool
value string
}{
{
name: "with default value",
isCheck: true,
value: "some default value",
field: conf.Field{
Tag: conf.Tag{
Default: "some default value",
IsDefault: true,
},
},
},
{
name: "without default value",
isCheck: false,
value: "",
field: conf.Field{
Tag: conf.Tag{},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := tt.field.DefaultValue()
check := tt.field.IsDefault()
assert.Equal(t, tt.value, result)
assert.Equal(t, tt.isCheck, check)
})
}
}
func TestField_ParamStoreKey(t *testing.T) {
tests := []struct {
name string
field conf.Field
isCheck bool
value string
}{
{
name: "Param store is inside the tag",
isCheck: true,
value: "foo-bar",
field: conf.Field{
Tag: conf.Tag{
PStoreVar: "foo-bar",
},
},
},
{
name: "Param store is not inside the tag",
isCheck: false,
value: "",
field: conf.Field{
Tag: conf.Tag{},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := tt.field.ParamStoreKey()
check := tt.field.IsParamStore()
assert.Equal(t, tt.value, result)
assert.Equal(t, tt.isCheck, check)
})
}
}
func TestField_ViperKey(t *testing.T) {
tests := []struct {
name string
field conf.Field
isCheck bool
value string
}{
{
name: "Viper is inside the tag",
isCheck: true,
value: "foo-bar",
field: conf.Field{
Tag: conf.Tag{
CLIFlag: "foo-bar",
},
},
},
{
name: "Viper is not inside the tag",
isCheck: false,
value: "",
field: conf.Field{
Tag: conf.Tag{},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := tt.field.CLIFlag()
check := tt.field.IsCLI()
assert.Equal(t, tt.value, result)
assert.Equal(t, tt.isCheck, check)
})
}
}
func TestField_IsRequired(t *testing.T) {
field := conf.Field{Tag: conf.Tag{Required: true}}
assert.True(t, field.IsRequired())
field = conf.Field{Tag: conf.Tag{}}
assert.False(t, field.IsRequired())
}
func TestFields_InvalidSpecFailure(t *testing.T) {
type InvalidConfig struct {
Value1 string `conf:"env:FOO"`
}
var config1 InvalidConfig
_, err := conf.Fields(config1, "MY_APP")
require.Error(t, err, "conf.Fields is expected to fail")
assert.Equal(t, conf.InvalidSpecFailure, err)
type InvalidConfigString string
var config2 InvalidConfigString
_, err = conf.Fields(&config2, "MY_APP")
require.Error(t, err, "conf.Fields is expected to fail")
assert.Equal(t, conf.InvalidSpecFailure, err)
}
func TestFields_AllIgnores(t *testing.T) {
type ConfigAllIgnore struct {
Value1 string `conf:"-"`
Value2 int `conf:"-"`
Value3 bool `conf:"-"`
}
var config ConfigAllIgnore
result, err := conf.Fields(&config, "MY_APP")
require.NoError(t, err, "config.Fields is not expected to fail")
assert.Empty(t, result)
}
func TestFields_ReflectPtr_Success(t *testing.T) {
type MyConfig struct {
FieldA *string `conf:"env:Field"`
}
var config MyConfig
result, err := conf.Fields(&config)
require.NoError(t, err, "conf.Fields is not expected to fail")
require.Len(t, result, 1)
f := result[0]
assert.True(t, f.ReflectValue.IsNil())
}
func TestFields_ReflectStructPtr_Success(t *testing.T) {
type Foo struct {
Item int
}
type MyConfig struct {
FieldA *Foo `conf:"env:Field"`
}
var config MyConfig
result, err := conf.Fields(&config)
require.NoError(t, err, "conf.Fields is not expected to fail")
require.Len(t, result, 1)
f := result[0]
assert.Equal(t, "int", f.ReflectValue.Kind().String())
}
func TestFields_EmbeddedStruct_Failure(t *testing.T) {
type Foo struct {
IsFlag bool `conf:"env:,default:xys"`
}
type MyConfig struct {
Foo
FieldA string `conf:"env:Field"`
}
var config MyConfig
_, err := conf.Fields(&config)
require.Error(t, err, "conf.Fields is expected to fail")
assert.Contains(t, err.Error(), "Fields failed for embedded struct")
}
type Foo struct {
IsFlag bool `conf:"env:IS_FLAG,default:true"`
}
func (f *Foo) Decode(_ string) error {
f.IsFlag = true
return nil
}
func TestFields_EmbeddedStruct_Success(t *testing.T) {
type MyConfig struct {
Foo
FieldA string `conf:"env:Field"`
}
var config MyConfig
result, err := conf.Fields(&config)
require.NoError(t, err, "conf.Fields is not expected to fail")
require.Len(t, result, 2)
a := result[0]
assert.Equal(t, "Foo", a.Name)
b := result[1]
assert.Equal(t, "FieldA", b.Name)
}
/*
env:aaa; default:a,b,c,d; xyx;
*/
func TestTextUnmarshaler(t *testing.T) {
config := struct {
TimeValue time.Time
}{}
timeValue := "2016-08-16T18:57:05Z"
field := reflect.ValueOf(&config).Elem().Field(0)
tu := conf.TextUnmarshaler(field)
require.NotNil(t, tu)
err := tu.UnmarshalText([]byte(timeValue))
require.NoError(t, err, "tu.UnmarshalText is not expected to fail")
var expected time.Time
err = expected.UnmarshalText([]byte(timeValue))
assert.Equal(t, expected, config.TimeValue)
}