-
Notifications
You must be signed in to change notification settings - Fork 0
/
config_test.go
219 lines (189 loc) · 4.81 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
package config
import (
"reflect"
"strings"
"testing"
"time"
)
// Count of default providers
var provCount = 2
// Additional providers used in tests
type pFull struct{}
type pSimple struct{}
func TestInitializeNewConfigWithCustomProvider(t *testing.T) {
c := New()
assertProviderCount(t, 0, len(c.providers))
c.WithProviders(&pFull{})
assertProviderCount(t, 1, len(c.providers))
}
func TestInitializeNewConfigWithInternalAndCustomProvider(t *testing.T) {
c := New()
assertProviderCount(t, 0, len(c.providers))
c.WithProviders(&Yaml{}, &Env{})
assertProviderCount(t, 2, len(c.providers))
c.WithProviders(&pSimple{})
assertProviderCount(t, 3, len(c.providers))
}
func TestInitializeInternalConfigWithAdditionalProvider(t *testing.T) {
c := Default()
assertProviderCount(t, provCount, len(c.providers))
c.WithProviders(&pFull{})
assertProviderCount(t, provCount+1, len(c.providers))
}
func TestInterfaceKind(t *testing.T) {
err := Parse(testCfg{})
if err == nil {
t.Fatalf("Error expected, but there is none.")
}
if !strings.Contains(err.Error(), "configuration must be a pointer") {
t.Errorf("Interface kind check should fail, but was: %v", err)
}
}
func TestInterfaceValueKind(t *testing.T) {
config := "string"
err := Parse(&config)
if err == nil {
t.Fatalf("Error expected, but there is none.")
}
if !strings.Contains(err.Error(), "value of configuration pointer must be a struct") {
t.Errorf("Interface Value kind check should fail, but was: %v", err)
}
}
func TestParseConfig(t *testing.T) {
c := New()
c.WithProviders(&pFull{})
conf := testCfg{}
err := c.Parse(&conf)
if err != nil {
t.Fatalf("%v\n", err)
}
if conf.StringField != "1234string" {
t.Errorf("Value is '%s', but '1234string' expected", conf.StringField)
}
if conf.IntField != 123 {
t.Errorf("Value is '%d', but '123' expected", conf.IntField)
}
if conf.NestedStruct.StringSlice[1] != "val 2" {
t.Errorf("Value is '%s', but 'val 2' expected", conf.NestedStruct.StringSlice[1])
}
if conf.NestedStruct.AnotherLevel.NestedInt16 != 321 {
t.Errorf("Value is '%d', but '321' expected", conf.NestedStruct.AnotherLevel.NestedInt16)
}
}
func TestSkipNonDefinedValue(t *testing.T) {
c := New()
c.WithProviders(&pFull{}, &pSimple{})
conf := testCfg{}
err := c.Parse(&conf)
if err != nil {
t.Fatalf("%v\n", err)
}
if conf.StringField != "String from simple" {
t.Errorf("Value is '%s', but 'String from simple' expected", conf.StringField)
}
if conf.IntField != 9999 {
t.Errorf("Value is '%d', but '9999' expected", conf.IntField)
}
if conf.NestedStruct.StringSlice[1] != "val 2" {
t.Errorf("Value is '%s', but 'val 2' expected", conf.NestedStruct.StringSlice[1])
}
if conf.NestedStruct.Float32Field != 4545.1212 {
t.Errorf("Value is '%f', but '4545.1212' expected", conf.NestedStruct.Float32Field)
}
if conf.NestedStruct.AnotherLevel.NestedInt16 != 321 {
t.Errorf("Value is '%d', but '321' expected", conf.NestedStruct.AnotherLevel.NestedInt16)
}
}
func assertProviderCount(t *testing.T, expected int, actual int) {
if actual != expected {
t.Fatalf("Configured providers: %d, but %d expected", actual, expected)
}
}
type testCfg struct {
StringField string
IntField int
BoolField bool
DurField time.Duration
NestedStruct struct {
StringSlice []string
Float32Field float32
AnotherLevel struct {
NestedInt16 int16
Uint8Field uint8
}
}
SecondNestedStruct struct {
StringField string
StructField struct {
StringField string
}
SecondStringField string
}
}
func (p *pFull) Provide(config interface{}) error {
cfg := testCfg{
StringField: "1234string",
IntField: 123,
NestedStruct: struct {
StringSlice []string
Float32Field float32
AnotherLevel struct {
NestedInt16 int16
Uint8Field uint8
}
}{
StringSlice: []string{"val 1", "val 2", "val 3"},
AnotherLevel: struct {
NestedInt16 int16
Uint8Field uint8
}{
NestedInt16: 321,
},
},
SecondNestedStruct: struct {
StringField string
StructField struct {
StringField string
}
SecondStringField string
}{
StringField: "nested-123",
StructField: struct {
StringField string
}{
StringField: "inner-nested",
},
SecondStringField: "nested-123",
},
}
parse(config, &cfg)
return nil
}
func (p *pSimple) Provide(config interface{}) error {
cfg := testCfg{
StringField: "String from simple",
IntField: 9999,
NestedStruct: struct {
StringSlice []string
Float32Field float32
AnotherLevel struct {
NestedInt16 int16
Uint8Field uint8
}
}{
Float32Field: 4545.1212,
},
}
parse(config, &cfg)
return nil
}
func parse(config interface{}, cfg *testCfg) {
v := reflect.ValueOf(config).Elem()
vn := reflect.ValueOf(cfg).Elem()
for i := 0; i < v.NumField(); i++ {
f := v.Field(i)
if f.CanSet() {
f.Set(vn.Field(i))
}
}
}