/
config_test.go
334 lines (270 loc) · 9.57 KB
/
config_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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
package conf_test
import (
"fmt"
"os"
"path/filepath"
"strings"
"testing"
"github.com/peyman-abdi/conf"
)
type testEvalFunction struct {
}
var _ conf.EvaluatorFunction = (*testEvalFunction)(nil)
func (t *testEvalFunction) GetFunctionName() string {
return "paramsJoin"
}
func (t *testEvalFunction) Eval(params []string, def interface{}) interface{} {
return strings.Join(params, ":")
}
func TestNew(t *testing.T) {
root, err := os.Executable()
if err != nil {
panic(err)
}
rootDir := filepath.Join(filepath.Dir(root), "..")
t.Log("Searching Config files at: " + rootDir)
_, err = conf.New(filepath.Join(rootDir, "test_configs/valids"), rootDir, []conf.EvaluatorFunction{
new(testEvalFunction),
})
if err != nil {
t.Error(err)
}
}
func TestNotFoundFile(t *testing.T) {
root, err := os.Executable()
if err != nil {
panic(err)
}
rootDir := filepath.Join(filepath.Dir(root), "../does not exist/")
t.Log("Searching Config files at: " + rootDir)
_, err = conf.New(filepath.Join(rootDir, "test_configs"), rootDir, []conf.EvaluatorFunction{
new(testEvalFunction),
})
if err == nil {
t.Error("File not exist but got no error!")
}
}
func TestInvalidFile(t *testing.T) {
root, err := os.Executable()
if err != nil {
panic(err)
}
rootDir := filepath.Join(filepath.Dir(root), "..")
t.Log("Searching Config files at: " + rootDir)
_, err = conf.New(filepath.Join(rootDir, "test_configs/invalids"), rootDir, []conf.EvaluatorFunction{
new(testEvalFunction),
})
if err == nil {
t.Error("File not exist but got no error!")
}
t.Log(err)
}
func TestNestedString(t *testing.T) {
root, err := os.Executable()
if err != nil {
panic(err)
}
rootDir := filepath.Join(filepath.Dir(root), "..")
t.Log("Searching Config files at: " + rootDir)
configure, err := conf.New(filepath.Join(rootDir, "test_configs/valids"), rootDir, []conf.EvaluatorFunction{
new(testEvalFunction),
})
if err != nil {
t.Error(err)
}
testStrings := map[string]string{
"nested.objects[0].name": "First",
"nested.objects[1].name": "Second",
"nested.objects[0].role": "Object",
"nested.objects[1].role": "Object",
"nested.vars.app.inner.string": "Some string goes here",
"nested.vars.app.array[1]": "string element 1",
}
for key, val := range testStrings {
checkString(configure, key, val, t)
}
checkString(configure, "dir.inner.inside.value", "Nested conf in directories", t)
testStrArray := configure.GetStringArray("nested.vars.app.array", []string{})
if len(testStrArray) != 4 || testStrArray[0] != "string element 0" {
t.Errorf("Failed for StringArray at key nested.vars.app.array: %v with len: %d", testStrArray, len(testStrArray))
}
}
func TestNestedIntAndFloats(t *testing.T) {
root, err := os.Executable()
if err != nil {
panic(err)
}
rootDir := filepath.Join(filepath.Dir(root), "..")
t.Log("Searching Config files at: " + rootDir)
configure, err := conf.New(filepath.Join(rootDir, "test_configs/valids"), rootDir, []conf.EvaluatorFunction{
new(testEvalFunction),
})
if err != nil {
t.Error(err)
}
testFloats := map[string]float64{
"nested.vars.app.inner.float": 13.333,
"nested.objects[0].float": 103.33,
"nested.objects[1].float": 203.33,
}
for key, val := range testFloats {
if result := configure.GetFloat(key, 0); result != val {
t.Error(fmt.Sprintf("Looking for \"%f\" in key %s but found %f", val, key, result))
}
}
testInts := map[string]int{
"nested.vars.app.inner.integer": 10,
"nested.objects[0].integer": 100,
"nested.objects[1].integer": 200,
}
for key, val := range testInts {
if result := configure.GetInt(key, 0); result != val {
t.Error(fmt.Sprintf("Looking for \"%d\" in key %s but found %d", val, key, result))
}
}
testBools := map[string]bool{
"nested.vars.app.boolean1": false,
"nested.vars.app.boolean2": true,
"nested.vars.app.boolean3": false,
"nested.vars.app.boolean4": true,
}
for key, val := range testBools {
if result := configure.GetBoolean(key, false); result != val {
t.Error(fmt.Sprintf("Looking for \"%v\" in key %s but found %v", val, key, result))
}
}
arrInt := configure.GetIntArray("nested.vars.intArray", []int{})
if len(arrInt) != 7 || arrInt[0] != 1 || arrInt[1] != 2 {
t.Error(fmt.Sprintf("Looking for int array in key %s but found %v", "nested.vars.intArray", arrInt))
}
arrFloat := configure.GetFloatArray("nested.vars.floatArray", []float64{})
if len(arrFloat) != 5 || arrFloat[0] != 1.1 || arrFloat[1] != 1.2 {
t.Error(fmt.Sprintf("Looking for float array in key %s but found %v", "nested.vars.intArray", arrFloat))
}
if bigInt := configure.GetInt64("nested.vars.bigInt", 0); bigInt != 9223372036854775806 {
t.Errorf("Failed reading big interger int64: %d", bigInt)
}
if bigInt := configure.GetAsString("nested.vars.bigInt", ""); bigInt != "18446744073709551615" {
t.Errorf("Failed reading big interger unsigned int64: %s", bigInt)
}
}
func TestConfig_GetMap(t *testing.T) {
root, err := os.Executable()
if err != nil {
t.Error(err)
}
rootDir := filepath.Join(filepath.Dir(root), "..")
t.Log("Searching Config files at: " + rootDir)
configure, err := conf.New(filepath.Join(rootDir, "test_configs/valids"), rootDir, []conf.EvaluatorFunction{
new(testEvalFunction),
})
if err != nil {
t.Error(err)
}
testMap := configure.GetMap("nested.vars.app", map[string]interface{} {})
if !testMap["boolean2"].(bool) {
t.Error("Testing GetMap Failed, expecting true for map nested.vars.app on key boolean2")
}
}
func TestConfig_GetAsString(t *testing.T) {
root, err := os.Executable()
if err != nil {
t.Error(err)
}
rootDir := filepath.Join(filepath.Dir(root), "..")
t.Log("Searching Config files at: " + rootDir)
configure, err := conf.New(filepath.Join(rootDir, "test_configs/valids"), rootDir, []conf.EvaluatorFunction{
new(testEvalFunction),
})
if err != nil {
t.Error(err)
}
if strIntArr := configure.GetAsString("nested.vars.intArray[1]", "not found"); strIntArr != "2" {
t.Error("Failed for GetAsString on int array returned: " + strIntArr)
}
if strFlArr := configure.GetAsString("nested.vars.floatArray[1]", "not found"); strFlArr != "1.2" {
t.Error("Failed for GetAsString on float array returned: " + strFlArr)
}
if str := configure.GetAsString("nested.vars.small", "not found"); str != "map[v:1 d:2]" && str != "map[d:2 v:1]" {
t.Error("Failed for GetAsString on obj returned: " + str)
}
if str := configure.GetAsString("nested.vars.app.inner.string", "not found"); str != "Some string goes here" {
t.Error("Failed for GetAsString on string returned: " + str)
}
}
func TestConfig_GetAndIsSet(t *testing.T) {
root, err := os.Executable()
if err != nil {
t.Error(err)
}
rootDir := filepath.Join(filepath.Dir(root), "..")
t.Log("Searching Config files at: " + rootDir)
configure, err := conf.New(filepath.Join(rootDir, "test_configs/valids"), rootDir, []conf.EvaluatorFunction{
new(testEvalFunction),
})
if err != nil {
t.Error(err)
}
if configure.IsSet("nested.vars.app.inner.string") {
if val := configure.Get("nested.vars.app.inner.string", "").(string); val != "Some string goes here" {
t.Error("Testing Get failed")
}
} else {
t.Error("Testing IsSet failed; key nested.vars.app.inner.string is present but reported as not found!")
}
}
func TestEnvEvaluator_GetFunctionName(t *testing.T) {
root, err := os.Executable()
if err != nil {
t.Error(err)
}
rootDir := filepath.Join(filepath.Dir(root), "..")
t.Log("Searching Config files at: " + rootDir)
configure, err := conf.New(filepath.Join(rootDir, "test_configs/valids"), rootDir, []conf.EvaluatorFunction{
new(testEvalFunction),
})
if err != nil {
t.Error(err)
}
checkString(configure, "evaluators.env.instance_in_conf_default", "in conf default", t)
checkString(configure, "evaluators.env.server", "Github", t)
checkString(configure, "evaluators.env.port", "2020", t)
checkString(configure, "evaluators.env.val", "vendor", t)
checkString(configure, "evaluators.env.instance", "TEST", t)
checkString(configure, "evaluators.env.sample", "2000", t)
checkString(configure, "evaluators.env.host", "testhost", t)
checkString(configure, "evaluators.testEval", "1:2:3:4:5", t)
checkString(configure, "evaluators.not_to_be_found", "not found", t)
checkString(configure, "evaluators.env.noParam", "not found", t)
}
func Test_DefaultValues(t *testing.T) {
root, err := os.Executable()
if err != nil {
t.Error(err)
}
rootDir := filepath.Join(filepath.Dir(root), "..")
t.Log("Searching Config files at: " + rootDir)
configure, err := conf.New(filepath.Join(rootDir, "test_configs/valids"), rootDir, []conf.EvaluatorFunction{
new(testEvalFunction),
})
if dArr := configure.GetString("nested.vars.app.array[not_a_number]", "default val"); dArr != "default val" {
t.Error("Failed for returning default string for invalid array indexer")
}
if dStr := configure.GetString("do.not.exist", "default string"); dStr != "default string" {
t.Error("Failed for returning default string")
}
if dInt := configure.GetInt("do.not.exist", 2020); dInt != 2020 {
t.Error("Failed for returning default string")
}
if dFloat := configure.GetFloat("do.not.exist", 12.12); dFloat != 12.12 {
t.Error("Failed for returning default string")
}
if dMap := configure.GetMap("do.not.exist", map[string]interface{} { "key": "value" }); dMap["key"] != "value" {
t.Error("Failed for returning default map")
}
}
func checkString(configure *conf.Config, key string, expected string, t *testing.T) {
if value := configure.GetString(key, "not found"); value != expected {
t.Error(fmt.Sprintf("Looking for \"%s\" but found \"%s\" in key: \"%s\"", expected, value, key))
}
}