-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser_test.go
319 lines (268 loc) · 7.57 KB
/
parser_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
//go:build unittest || !integrationtest
// +build unittest !integrationtest
package proteus_test
import (
"math"
"net/url"
"strings"
"testing"
"time"
"github.com/simplesurance/proteus"
"github.com/simplesurance/proteus/internal/assert"
"github.com/simplesurance/proteus/plog"
"github.com/simplesurance/proteus/sources/cfgtest"
"github.com/simplesurance/proteus/types"
"github.com/simplesurance/proteus/xtypes"
)
func TestDefaultValueAllTypes(t *testing.T) {
testWriter := testWriter{t}
now := time.Now()
testProvider := cfgtest.New(types.ParamValues{})
localhost, _ := url.Parse("https://localhost")
cfg := struct {
Str string `param:",optional"`
I int `param:",optional"`
I8 int8 `param:",optional"`
I16 int16 `param:",optional"`
I32 int32 `param:",optional"`
I64 int64 `param:",optional"`
U uint `param:",optional"`
UI8 uint8 `param:",optional"`
UI16 uint16 `param:",optional"`
UI32 uint32 `param:",optional"`
UI64 uint64 `param:",optional"`
Bool bool `param:",optional"`
Time time.Time `param:",optional"`
Duration time.Duration `param:",optional"`
XStr *xtypes.String `param:",optional"`
XBool *xtypes.Bool `param:",optional"`
XOneOf *xtypes.OneOf `param:",optional"`
XURL *xtypes.URL `param:",optional"`
XRSAPriv *xtypes.RSAPrivateKey `param:",optional"`
}{
Str: "str",
I: math.MinInt,
I8: math.MinInt8,
I16: math.MinInt16,
I32: math.MinInt32,
I64: math.MinInt64,
U: math.MaxUint,
UI8: math.MaxUint8,
UI16: math.MaxUint16,
UI32: math.MaxUint32,
UI64: math.MaxUint64,
Bool: true,
Time: now,
Duration: time.Hour,
XStr: &xtypes.String{
DefaultValue: "def dyn",
},
XBool: &xtypes.Bool{
DefaultValue: true,
},
XOneOf: &xtypes.OneOf{
DefaultValue: "sol",
Choices: []string{"do", "re", "mi", "fa", "sol", "la", "si"},
},
XURL: &xtypes.URL{
DefaultValue: localhost,
},
}
parsed, err := proteus.MustParse(&cfg,
proteus.WithProviders(testProvider),
proteus.WithLogger(plog.TestLogger(t)))
if err != nil {
t.Logf("Unexpected error parsing configuration: %+v", err)
parsed.WriteError(testWriter, err)
t.FailNow()
}
parsed.Usage(testWriter)
assert.Equal(t, cfg.Str, "str")
assert.Equal(t, cfg.I, int(math.MinInt))
assert.Equal(t, cfg.I8, int8(math.MinInt8))
assert.Equal(t, cfg.I16, int16(math.MinInt16))
assert.Equal(t, cfg.I32, int32(math.MinInt32))
assert.Equal(t, cfg.I64, int64(math.MinInt64))
assert.Equal(t, cfg.UI8, uint8(math.MaxUint8))
assert.Equal(t, cfg.U, uint(math.MaxUint))
assert.Equal(t, cfg.UI16, uint16(math.MaxUint16))
assert.Equal(t, cfg.UI32, uint32(math.MaxUint32))
assert.Equal(t, cfg.UI64, uint64(math.MaxUint64))
assert.Equal(t, true, cfg.Bool)
assert.Equal(t, "def dyn", cfg.XStr.Value())
assert.Equal(t, "sol", cfg.XOneOf.Value())
assert.Equal(t, true, cfg.XBool.Value())
assert.Equal(t, localhost, cfg.XURL.Value())
}
// TestEmbeddingParameters asserts that embedding structs result in the values
// being flat.
func TestEmbeddingParameters(t *testing.T) {
testWriter := testWriter{t}
type embeddedConfig struct {
EmbParam string
}
testAppCfg := struct {
embeddedConfig
TestSet struct {
embeddedConfig
FSValue string
}
DevMode bool
}{}
testProvider := cfgtest.New(types.ParamValues{
"": map[string]string{
"devmode": "true",
"embparam": "emb1",
},
"testset": map[string]string{
"fsvalue": "test value",
"embparam": "emb2",
},
})
defer testProvider.Stop()
parsed, err := proteus.MustParse(&testAppCfg,
proteus.WithProviders(testProvider),
proteus.WithLogger(plog.TestLogger(t)))
if err != nil {
t.Logf("Unexpected error parsing configuration: %+v", err)
parsed.WriteError(testWriter, err)
t.FailNow()
}
parsed.Usage(testWriter)
assert.Equal(t, true, testAppCfg.DevMode)
assert.Equal(t, "emb1", testAppCfg.EmbParam)
assert.Equal(t, "test value", testAppCfg.TestSet.FSValue)
assert.Equal(t, "emb2", testAppCfg.TestSet.EmbParam)
}
// TestEmbeddingParamSet asserts that structs including paramsets can be
// embedded as parameter.
func TestEmbeddingParamSet(t *testing.T) {
testWriter := testWriter{t}
type httpConfig struct {
HTTP struct {
IP string
Port uint16
}
}
type logConfig struct {
Log struct {
FileName string
}
}
testAppCfg := struct {
httpConfig
logConfig
}{}
testProvider := cfgtest.New(types.ParamValues{
"http": map[string]string{
"ip": "127.0.0.1",
"port": "42",
},
"log": map[string]string{
"filename": "/dev/null",
},
})
defer testProvider.Stop()
parsed, err := proteus.MustParse(&testAppCfg,
proteus.WithProviders(testProvider),
proteus.WithLogger(plog.TestLogger(t)))
if err != nil {
t.Logf("Unexpected error parsing configuration: %+v", err)
parsed.WriteError(testWriter, err)
t.FailNow()
}
assert.Equal(t, "127.0.0.1", testAppCfg.HTTP.IP)
assert.Equal(t, 42, testAppCfg.HTTP.Port)
assert.Equal(t, "/dev/null", testAppCfg.Log.FileName)
}
func TestParseWithTrim(t *testing.T) {
testWriter := testWriter{t}
params := struct {
TestString string
TestInt int
TestURL *xtypes.URL
}{}
testProvider := cfgtest.New(types.ParamValues{
"": map[string]string{
"teststring": `
value
`,
"testint": `
42
`,
"testurl": `
https://localhost
`,
},
})
defer testProvider.Stop()
parsed, err := proteus.MustParse(¶ms,
proteus.WithProviders(testProvider),
proteus.WithLogger(plog.TestLogger(t)),
proteus.WithValueFormatting(proteus.ValueFormattingOptions{
TrimSpace: true,
}))
if err != nil {
t.Logf("Unexpected error parsing configuration: %+v", err)
parsed.WriteError(testWriter, err)
t.FailNow()
}
assert.Equal(t, "value", params.TestString)
assert.Equal(t, 42, params.TestInt)
assert.Equal(t, "https://localhost", params.TestURL.Value().String())
}
func TestTimeAndDuration(t *testing.T) {
testWriter := testWriter{t}
now := time.Now()
dur := time.Hour + time.Duration(1)
params := struct {
T time.Time
D time.Duration
}{}
testProvider := cfgtest.New(types.ParamValues{"": map[string]string{
"t": now.Format(time.RFC3339Nano),
"d": dur.String(),
}})
defer testProvider.Stop()
parsed, err := proteus.MustParse(¶ms,
proteus.WithProviders(testProvider),
proteus.WithLogger(plog.TestLogger(t)))
if err != nil {
t.Logf("Unexpected error parsing configuration: %+v", err)
parsed.WriteError(testWriter, err)
t.FailNow()
}
assert.EqualFn(t, now, params.T)
assert.Equal(t, dur, params.D)
sb := strings.Builder{}
parsed.Dump(&sb)
t.Log(sb.String())
}
func TestIgnoreInvalidMember(t *testing.T) {
testWriter := testWriter{t}
params := struct {
A int `param:",optional"`
B func() `param:"-"`
}{}
testProvider := cfgtest.New(types.ParamValues{"": map[string]string{}})
defer testProvider.Stop()
parsed, err := proteus.MustParse(¶ms,
proteus.WithProviders(testProvider),
proteus.WithLogger(plog.TestLogger(t)))
if err != nil {
t.Logf("Unexpected error parsing configuration: %+v", err)
parsed.WriteError(testWriter, err)
t.FailNow()
}
sb := strings.Builder{}
parsed.Dump(&sb)
t.Log(sb.String())
t.Logf("Configuration field successfully ignored")
}
type testWriter struct {
t *testing.T
}
func (t testWriter) Write(v []byte) (int, error) {
t.t.Logf("%s", v)
return len(v), nil
}