-
Notifications
You must be signed in to change notification settings - Fork 0
/
gofig_test.go
710 lines (581 loc) · 16.7 KB
/
gofig_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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
package gofig
import (
"crypto/rand"
"errors"
"fmt"
"math/big"
"testing"
"github.com/ippontech/gofig"
)
/***************
* +-------------------+
* | Test Errors |
* +-------------------+
****************/
var ErrExpectedNoError = func(actual error) error {
return fmt.Errorf("expected no error, got: `%v`", actual)
}
var ErrExpectedError = errors.New("expected an error")
var ErrErrorsDoNotMatch = func(expected, actual error) error {
return fmt.Errorf("expected error: `%v`, got: `%v`", expected, actual)
}
var ErrNotImplemented = errors.New("not implemented")
/***************
* +-------------------+
* | Test Functions |
* +-------------------+
****************/
/*
Test_Init_ is just a general example test to show usage of the library.
Does a few checks in one
*/
func Test_Init_General(t *testing.T) {
t.Setenv("FOO", "true")
t.Setenv("BAR", "10")
t.Setenv("BAZ", "hello")
var fooId gofig.Id
var barId gofig.Id
var bazId gofig.Id
gf, err := gofig.Init([]gofig.InitOpt{
{
Name: "FOO",
Description: "This is a foo. It is used for blah blah blah",
Type: gofig.TypeBool,
Required: true,
IdPtr: &fooId,
},
{
Name: "BAR",
Description: "This is a bar. It is used for blah blah blah",
Type: gofig.TypeInt,
Required: true,
IdPtr: &barId,
},
{
Name: "BAZ",
Description: "This is a baz. It is used for blah blah blah",
Type: gofig.TypeString,
Required: true,
IdPtr: &bazId,
},
})
if err != nil {
t.Error(ErrExpectedNoError(err))
}
// Now you can access the values like this:
foo, errActual := gf.Get(fooId)
if errActual != nil {
fmt.Println(errActual)
}
fmt.Println(foo)
bar, errActual := gf.Get(barId)
if errActual != nil {
fmt.Println(errActual)
}
fmt.Println(bar)
baz, errActual := gf.Get(bazId)
if errActual != nil {
fmt.Println(errActual)
}
fmt.Println(baz)
}
func Test_Init_ErrNil_WhenIntDefatultTypeCorrect(t *testing.T) {
t.Setenv("FOO", "10")
var fooId gofig.Id
_, errActual := gofig.Init([]gofig.InitOpt{
{
Name: "FOO",
Description: "This is a foo. It is used for blah blah blah",
Type: gofig.TypeInt,
Required: false,
Default: 10,
IdPtr: &fooId,
},
})
if errActual != nil {
t.Error(ErrExpectedNoError(errActual))
}
}
func Test_Init_Err_When_IntDefaultTypeIncorrect(t *testing.T) {
var fooId gofig.Id
badInitOpt := gofig.InitOpt{
Name: "FOO",
Description: "This is a foo. It is used for blah blah blah",
Type: gofig.TypeInt,
Required: false,
Default: "incorrect type",
IdPtr: &fooId,
}
_, errActual := gofig.Init([]gofig.InitOpt{
badInitOpt,
})
errExpected := gofig.ErrDefaultValueIsWrongTypeWhenNotRequired(badInitOpt)
if errActual.Error() != errExpected.Error() {
t.Error(ErrErrorsDoNotMatch(errExpected, errActual))
}
}
func Test_Init_ErrNil_When_BoolDefaultTypeCorrect(t *testing.T) {
var fooId gofig.Id
_, errActual := gofig.Init([]gofig.InitOpt{
{
Name: "FOO",
Description: "This is a foo. It is used for blah blah blah",
Type: gofig.TypeBool,
Required: false,
Default: true,
IdPtr: &fooId,
},
})
if errActual != nil {
t.Error(ErrExpectedNoError(errActual))
}
}
func Test_Init_Err_When_BoolDefaultTypeIncorrect(t *testing.T) {
var fooId gofig.Id
badInitOpt := gofig.InitOpt{
Name: "FOO",
Description: "This is a foo. It is used for blah blah blah",
Type: gofig.TypeBool,
Required: false,
Default: "incorrect type",
IdPtr: &fooId,
}
_, errActual := gofig.Init([]gofig.InitOpt{
badInitOpt,
})
errExpected := gofig.ErrDefaultValueIsWrongTypeWhenNotRequired(badInitOpt)
if errActual.Error() != errExpected.Error() {
t.Error(ErrErrorsDoNotMatch(errExpected, errActual))
}
}
func Test_Init_ErrNil_WhenFloatDefaultTypeCorrect(t *testing.T) {
t.Setenv("FOO", "10.0")
var fooId gofig.Id
_, errActual := gofig.Init([]gofig.InitOpt{
{
Name: "FOO",
Description: "This is a foo. It is used for blah blah blah",
Type: gofig.TypeFloat,
Required: false,
Default: 10.0,
IdPtr: &fooId,
},
})
if errActual != nil {
t.Error(ErrExpectedNoError(errActual))
}
}
func Test_Init_Err_When_FloatDefaultTypeIncorrect(t *testing.T) {
var fooId gofig.Id
badInitOpt := gofig.InitOpt{
Name: "FOO",
Description: "This is a foo. It is used for blah blah blah",
Type: gofig.TypeFloat,
Required: false,
Default: "incorrect type",
IdPtr: &fooId,
}
_, errActual := gofig.Init([]gofig.InitOpt{
badInitOpt,
})
errExpected := gofig.ErrDefaultValueIsWrongTypeWhenNotRequired(badInitOpt)
if errActual.Error() != errExpected.Error() {
t.Error(ErrErrorsDoNotMatch(errExpected, errActual))
}
}
func Test_Init_ErrNil_WhenStringDefaultTypeCorrect(t *testing.T) {
var fooId gofig.Id
_, errActual := gofig.Init([]gofig.InitOpt{
{
Name: "FOO",
Description: "This is a foo. It is used for blah blah blah",
Type: gofig.TypeString,
Required: false,
Default: "hello",
IdPtr: &fooId,
},
})
if errActual != nil {
t.Error(ErrExpectedNoError(errActual))
}
}
func Test_Init_Err_When_StringDefaultTypeIncorrect(t *testing.T) {
var fooId gofig.Id
badInitOpt := gofig.InitOpt{
Name: "FOO",
Description: "This is a foo. It is used for blah blah blah",
Type: gofig.TypeString,
Required: false,
Default: 10,
IdPtr: &fooId,
}
_, errActual := gofig.Init([]gofig.InitOpt{
badInitOpt,
})
errExpected := gofig.ErrDefaultValueIsWrongTypeWhenNotRequired(badInitOpt)
if errActual.Error() != errExpected.Error() {
t.Error(ErrErrorsDoNotMatch(errExpected, errActual))
}
}
func Test_Init_Err_When_EnvVarCannotBeConvertedToInt(t *testing.T) {
val := "not an int"
t.Setenv("FOO", val)
var fooId gofig.Id
_, errActual := gofig.Init([]gofig.InitOpt{
{
Name: "FOO",
Description: "This is a foo. It is used for blah blah blah",
Type: gofig.TypeInt,
Required: true,
IdPtr: &fooId,
},
})
if errActual == nil {
t.Error(ErrExpectedError)
}
errExpected := gofig.ErrWrongTypeSetInEnvironment(gofig.InitOpt{
Name: "FOO",
Type: gofig.TypeInt,
Required: true,
}, val)
if errActual.Error() != errExpected.Error() {
t.Error(ErrErrorsDoNotMatch(errExpected, errActual))
}
}
func Test_Init_Err_When_EnvVarCannotBeConvertedToFloat64(t *testing.T) {
val := "not a float"
t.Setenv("FOO", val)
var fooId gofig.Id
_, errActual := gofig.Init([]gofig.InitOpt{
{
Name: "FOO",
Description: "This is a foo. It is used for blah blah blah",
Type: gofig.TypeFloat,
Required: true,
IdPtr: &fooId,
},
})
if errActual == nil {
t.Error(ErrExpectedError)
}
errExpected := gofig.ErrWrongTypeSetInEnvironment(gofig.InitOpt{
Name: "FOO",
Type: gofig.TypeFloat,
Required: true,
}, val)
if errActual.Error() != errExpected.Error() {
t.Error(ErrErrorsDoNotMatch(errExpected, errActual))
}
}
func Test_Init_Err_When_NoInitOptsPassed(t *testing.T) {
_, errActual := gofig.Init([]gofig.InitOpt{})
errExpected := gofig.ErrNoInputOpts
if errActual.Error() != errExpected.Error() {
t.Errorf("expected error: `%v`, got: `%v`", errExpected, errActual)
}
}
/*
Create a bunch of configuration Ids by calling Init with a bunch of correctly defined InitOpts with mixed types. Then test to see if all the values equal expected results.
*/
func Test_Get_ABunchOfCallsSuccess(t *testing.T) {
const n = 30
var ids [n]gofig.Id
var opts [n]gofig.InitOpt
var vals [n]any
// generate a list of random init optsand corresponding environ variable values that were set
for i := 0; i < n; i++ {
opt, val := generateRandomInitOptWithEnvSet(&ids[i], t)
opts[i] = opt
vals[i] = val
}
gf, err := gofig.Init([]gofig.InitOpt{
opts[0],
opts[1],
opts[2],
opts[3],
opts[4],
opts[5],
opts[6],
opts[7],
opts[8],
opts[9],
opts[10],
opts[11],
opts[12],
opts[13],
opts[14],
opts[15],
opts[16],
opts[17],
opts[18],
opts[19],
opts[20],
opts[21],
opts[22],
opts[23],
opts[24],
opts[25],
opts[26],
opts[27],
opts[28],
opts[29],
})
if err != nil {
t.Error(ErrExpectedNoError(err))
}
for i := 0; i < n; i++ {
val, err := gf.Get(ids[i])
if err != nil {
t.Error(err)
}
if val != vals[i] {
t.Errorf("expected: `%v`, got: `%v`", vals[i], val)
}
}
}
func Test_Get_Err_When_GofigNotInitialized(t *testing.T) {
var fooId gofig.Id
gf := gofig.Gofig{}
_, errActual := gf.Get(fooId)
errExpected := gofig.ErrNotInitialized
if errActual.Error() != errExpected.Error() {
t.Error(ErrErrorsDoNotMatch(errExpected, errActual))
}
}
func Test_GetBool_Err_When_GofigNotInitialized(t *testing.T) {
var fooId gofig.Id
gf := gofig.Gofig{}
_, errActual := gf.GetBool(fooId)
errExpected := gofig.ErrNotInitialized
if errActual.Error() != errExpected.Error() {
t.Error(ErrErrorsDoNotMatch(errExpected, errActual))
}
}
func Test_GetInt_Err_When_GofigNotInitialized(t *testing.T) {
var fooId gofig.Id
gf := gofig.Gofig{}
_, errActual := gf.GetInt(fooId)
errExpected := gofig.ErrNotInitialized
if errActual.Error() != errExpected.Error() {
t.Error(ErrErrorsDoNotMatch(errExpected, errActual))
}
}
func Test_GetFloat_Err_When_GofigNotInitialized(t *testing.T) {
var fooId gofig.Id
gf := gofig.Gofig{}
_, errActual := gf.GetFloat(fooId)
errExpected := gofig.ErrNotInitialized
if errActual.Error() != errExpected.Error() {
t.Error(ErrErrorsDoNotMatch(errExpected, errActual))
}
}
func Test_GetString_Err_When_GofigNotInitialized(t *testing.T) {
var fooId gofig.Id
gf := gofig.Gofig{}
_, errActual := gf.GetString(fooId)
errExpected := gofig.ErrNotInitialized
if errActual.Error() != errExpected.Error() {
t.Error(ErrErrorsDoNotMatch(errExpected, errActual))
}
}
func Test_Get_Err_When_InvalidId(t *testing.T) {
t.Setenv("FOO", "true")
var fooId gofig.Id
var invalidId gofig.Id // we never pass this into a call to Init
initOpt := goodBoolInitOpt
initOpt.IdPtr = &fooId
gf, err := gofig.Init([]gofig.InitOpt{initOpt})
if err != nil {
t.Error(ErrExpectedNoError(err))
}
_, errActual := gf.Get(invalidId)
errExpected := gofig.ErrInvalidId
if errActual.Error() != errExpected.Error() {
t.Error(ErrErrorsDoNotMatch(errExpected, errActual))
}
}
func Test_GetBool_Err_When_InvalidId(t *testing.T) {
t.Setenv("FOO", "true")
var fooId gofig.Id
var invalidId gofig.Id // we never pass this into a call to Init
initOpt := goodBoolInitOpt
initOpt.IdPtr = &fooId
gf, err := gofig.Init([]gofig.InitOpt{initOpt})
if err != nil {
t.Error(ErrExpectedNoError(err))
}
_, errActual := gf.GetBool(invalidId)
errExpected := gofig.ErrInvalidId
if errActual.Error() != errExpected.Error() {
t.Error(ErrErrorsDoNotMatch(errExpected, errActual))
}
}
func Test_GetInt_Err_When_InvalidId(t *testing.T) {
t.Setenv("FOO", "10")
var fooId gofig.Id
var invalidId gofig.Id // we never pass this into a call to Init
initOpt := goodIntInitOpt
initOpt.IdPtr = &fooId
gf, err := gofig.Init([]gofig.InitOpt{initOpt})
if err != nil {
t.Error(ErrExpectedNoError(err))
}
_, errActual := gf.GetInt(invalidId)
errExpected := gofig.ErrInvalidId
if errActual.Error() != errExpected.Error() {
t.Error(ErrErrorsDoNotMatch(errExpected, errActual))
}
}
func Test_GetFloat_Err_When_InvalidId(t *testing.T) {
t.Setenv("FOO", "10.0")
var fooId gofig.Id
var invalidId gofig.Id // we never pass this into a call to Init
initOpt := goodFloatInitOpt
initOpt.IdPtr = &fooId
gf, err := gofig.Init([]gofig.InitOpt{initOpt})
if err != nil {
t.Error(ErrExpectedNoError(err))
}
_, errActual := gf.GetFloat(invalidId)
errExpected := gofig.ErrInvalidId
if errActual.Error() != errExpected.Error() {
t.Error(ErrErrorsDoNotMatch(errExpected, errActual))
}
}
func Test_GetString_Err_When_InvalidId(t *testing.T) {
t.Setenv("FOO", "hello")
var fooId gofig.Id
var invalidId gofig.Id // we never pass this into a call to Init
initOpt := goodStringInitOpt
initOpt.IdPtr = &fooId
gf, err := gofig.Init([]gofig.InitOpt{initOpt})
if err != nil {
t.Error(ErrExpectedNoError(err))
}
_, errActual := gf.GetString(invalidId)
errExpected := gofig.ErrInvalidId
if errActual.Error() != errExpected.Error() {
t.Error(ErrErrorsDoNotMatch(errExpected, errActual))
}
}
func Test_DocString_Matches_Expected(t *testing.T) {
expectedDocStr := "FOO\n\tDescription: This is a foo. It is used for blah blah blah\n\tType: bool\n\tRequired: true\nFOO\n\tDescription: This is a foo. It is used for blah blah blah\n\tType: int\n\tRequired: true\nFOO\n\tDescription: This is a foo. It is used for blah blah blah\n\tType: float\n\tRequired: true\nFOO\n\tDescription: This is a foo. It is used for blah blah blah\n\tType: string\n\tRequired: true\n"
initOpts := []gofig.InitOpt{
goodBoolInitOpt,
goodIntInitOpt,
goodFloatInitOpt,
goodStringInitOpt,
}
actualDocStr, err := gofig.DocString(initOpts)
if err != nil {
t.Error(ErrExpectedNoError(err))
}
if actualDocStr != expectedDocStr {
t.Errorf("expected: `%v`, got: `%v`", expectedDocStr, actualDocStr)
}
}
/***************
* +-------------------+
* | helper vars |
* +-------------------+
****************/
var goodBoolInitOpt = gofig.InitOpt{
Name: "FOO",
Description: "This is a foo. It is used for blah blah blah",
Type: gofig.TypeBool,
Required: true,
}
var goodIntInitOpt = gofig.InitOpt{
Name: "FOO",
Description: "This is a foo. It is used for blah blah blah",
Type: gofig.TypeInt,
Required: true,
}
var goodFloatInitOpt = gofig.InitOpt{
Name: "FOO",
Description: "This is a foo. It is used for blah blah blah",
Type: gofig.TypeFloat,
Required: true,
}
var goodStringInitOpt = gofig.InitOpt{
Name: "FOO",
Description: "This is a foo. It is used for blah blah blah",
Type: gofig.TypeString,
Required: true,
}
/**************
* +-------------------+
* | Helper Functions |
* +-------------------+
**************/
func generateRandomStrOfLenN(n int) string {
var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
b := make([]rune, n)
for i := range b {
randLen, _ := rand.Int(rand.Reader, big.NewInt(int64(len(letters))))
b[i] = letters[int(randLen.Int64())]
}
return string(b)
}
// Intn is a shortcut for generating a random integer between 0 and
// max using crypto/rand.
func intn(max int64) int64 {
nBig, err := rand.Int(rand.Reader, big.NewInt(max))
if err != nil {
panic(err)
}
return nBig.Int64()
}
// returns random init opt as well as the value of the random environment variable that was set
func generateRandomInitOptWithEnvSet(id *gofig.Id, t *testing.T) (gofig.InitOpt, any) {
randOptNameLen, _ := rand.Int(rand.Reader, big.NewInt(63))
randOptDescLen, _ := rand.Int(rand.Reader, big.NewInt(63))
randIsRequiredBigInt, _ := rand.Int(rand.Reader, big.NewInt(2))
randTypeBigInt, _ := rand.Int(rand.Reader, big.NewInt(4))
randomOptName := generateRandomStrOfLenN(int(randOptNameLen.Int64()) + 1) // random string between 1 and 64 characters
randomOptDesc := generateRandomStrOfLenN(int(randOptDescLen.Int64()) + 1) // random string between 1 and 64 characters
randomRequired := randIsRequiredBigInt.Int64() == 1
randomType := gofig.GfType(int(randTypeBigInt.Int64()))
var randomVal interface{}
switch randomType {
case gofig.TypeBool:
rval, _ := rand.Int(rand.Reader, big.NewInt(2))
randomVal = rval.Int64() == 1
case gofig.TypeInt:
rval, _ := rand.Int(rand.Reader, big.NewInt(100))
randomVal = int(rval.Int64())
case gofig.TypeFloat:
randomVal = float64(intn(1<<53)) / (1 << 53)
case gofig.TypeString:
rStrLen, _ := rand.Int(rand.Reader, big.NewInt(63))
randomVal = generateRandomStrOfLenN(int(rStrLen.Int64())) // random string between 1 and 64 characters
}
var randomDefault interface{}
if !randomRequired {
switch randomType {
case gofig.TypeBool:
rval, _ := rand.Int(rand.Reader, big.NewInt(2))
randomDefault = rval.Int64() == 1
case gofig.TypeInt:
rval, _ := rand.Int(rand.Reader, big.NewInt(100))
randomDefault = int(rval.Int64())
case gofig.TypeFloat:
randomDefault = float64(intn(1<<53)) / (1 << 53)
case gofig.TypeString:
rStrLen, _ := rand.Int(rand.Reader, big.NewInt(63))
randomDefault = generateRandomStrOfLenN(int(rStrLen.Int64())) // random string between 1 and 64 characters
}
}
opt := gofig.InitOpt{
Name: randomOptName,
Description: randomOptDesc,
Type: randomType,
Required: randomRequired,
Default: randomDefault,
IdPtr: id,
}
// convert randomVal to string depending on randomType and set in environ
envValStr := fmt.Sprintf("%v", randomVal)
t.Setenv(randomOptName, envValStr)
return opt, randomVal
}