-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathezconf_test.go
186 lines (160 loc) · 4.91 KB
/
ezconf_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
package ezconf
import (
"fmt"
"log/slog"
"os"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
type allKinds struct {
MyInt int
MyUint uint
MyFloat float64
MyBool bool
MyString string
MyDatetime time.Time
}
type allTypes struct {
MyInt int
MyInt8 int8
MyInt16 int16
MyInt32 int32
MyInt64 int64
MyUint uint
MyUint8 uint8
MyUint16 uint16
MyUint32 uint32
MyUint64 uint64
MyFloat32 float32
MyFloat64 float64
MyBool bool
MyString string
MyDatetime time.Time
MyLogLevel slog.Level
}
func toFields(t *testing.T, s interface{}) *ezFields {
fields, err := buildFields(s)
if err != nil {
t.Errorf("error building fields for %+v: %s", s, err)
t.FailNow()
}
return fields
}
func TestCamelToSnake(t *testing.T) {
tests := []struct {
camel string
snake string
}{
{"CamelCase", "camel_case"},
{"AWSAccessKey", "aws_access_key"},
{"S3Region", "s3_region"},
{"EC2Region", "ec2_region"},
{"Route53", "route53"},
{"AWS", "aws"},
{"snake_case", "snake_case"},
{"Snake_Camel", "snake_camel"},
{"CamelCaseA", "camel_case_a"},
{"CamelABCCaseDEF", "camel_abc_case_def"},
}
for _, tc := range tests {
snake := CamelToSnake(tc.camel)
assert.Equal(t, tc.snake, snake, "to snake mismatch for %s", tc.camel)
}
}
func TestSetValue(t *testing.T) {
at := allTypes{}
fields := toFields(t, &at)
tests := []struct {
key string
value string
hasErr bool
expected string
}{
{"my_int", "-48", false, "-48"},
{"my_int", "wat", true, ""},
{"my_int8", "-48", false, "-48"},
{"my_int8", "wat", true, ""},
{"my_int16", "-48", false, "-48"},
{"my_int16", "wat", true, ""},
{"my_int32", "-48", false, "-48"},
{"my_int32", "wat", true, ""},
{"my_int64", "-48", false, "-48"},
{"my_int64", "wat", true, ""},
{"my_uint", "48", false, "48"},
{"my_uint", "wat", true, ""},
{"my_uint8", "48", false, "48"},
{"my_uint8", "wat", true, ""},
{"my_uint16", "48", false, "48"},
{"my_uint16", "wat", true, ""},
{"my_uint32", "48", false, "48"},
{"my_uint32", "wat", true, ""},
{"my_uint64", "48", false, "48"},
{"my_uint64", "wat", true, ""},
{"my_float32", "12", false, "12"},
{"my_float32", "wat", true, ""},
{"my_float64", "12", false, "12"},
{"my_float64", "wat", true, ""},
{"my_bool", "true", false, "true"},
{"my_bool", "wat", true, ""},
{"my_string", "foozap", false, "foozap"},
{"my_datetime", "15:45:05", false, "0000-01-01 15:45:05 +0000 UTC"},
{"my_datetime", "2018-04-03", false, "2018-04-03 00:00:00 +0000 UTC"},
{"my_datetime", "2018-04-03T05:30:00Z", false, "2018-04-03 05:30:00 +0000 UTC"},
{"my_datetime", "2018-04-03T05:30:00.123+07:00", false, "2018-04-03 05:30:00.123 +0700 +0700"},
{"my_datetime", "notdate", true, ""},
{"my_log_level", "info", false, "INFO"},
{"my_log_level", "ERROR", false, "ERROR"},
{"my_log_level", "crazy", true, ""},
{"unknown", "", true, ""},
}
for _, tc := range tests {
values := map[string]ezValue{tc.key: {tc.key, tc.value}}
err := setValues(fields, values)
if !tc.hasErr && err != nil {
assert.NoError(t, err, "unexpected error setting %s to %s", tc.key, tc.value)
}
if tc.hasErr && err == nil {
assert.Error(t, err, "expected error setting %s to %s", tc.key, tc.value)
}
field, found := fields.fields[tc.key]
if found && !tc.hasErr && err == nil {
strValue := fmt.Sprintf("%v", field.Value())
assert.Equal(t, tc.expected, strValue)
}
}
}
func TestEndToEnd(t *testing.T) {
at := &allTypes{}
conf := NewLoader(at, "foo", "description", []string{"testdata/missing.toml", "testdata/fields.toml", "testdata/simple.toml"})
conf.args = []string{"-my-int=48", "-my-log-level=error", "-debug-conf"}
err := conf.Load()
assert.NoError(t, err)
assert.Equal(t, 48, at.MyInt)
assert.Equal(t, slog.LevelError, at.MyLogLevel)
}
func TestPriority(t *testing.T) {
at := &allTypes{MyInt: 16}
conf := NewLoader(at, "foo", "description", []string{"testdata/missing.toml", "testdata/fields.toml", "testdata/simple.toml"})
conf.args = []string{}
conf.Load()
assert.Equal(t, 96, at.MyInt)
// override with environment variable
conf = NewLoader(at, "foo", "description", []string{"testdata/missing.toml", "testdata/fields.toml", "testdata/simple.toml"})
conf.args = []string{}
os.Setenv("FOO_MY_INT", "48")
conf.Load()
assert.Equal(t, 48, at.MyInt)
// override with args
conf = NewLoader(at, "foo", "description", []string{"testdata/missing.toml", "testdata/fields.toml", "testdata/simple.toml"})
conf.args = []string{"-my-int=56"}
os.Setenv("FOO_MY_INT", "48")
conf.Load()
assert.Equal(t, 56, at.MyInt)
// clear our env, args should take precedence now even though we are setting to the same as our new default
os.Setenv("FOO_MY_INT", "")
conf = NewLoader(at, "foo", "description", []string{"testdata/missing.toml", "testdata/fields.toml", "testdata/simple.toml"})
conf.args = []string{"-my-int=56"}
conf.Load()
assert.Equal(t, 56, at.MyInt)
}