-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfig_test.go
887 lines (819 loc) · 22.9 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
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
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
package flagtag
import (
"flag"
"os"
"strconv"
"strings"
"testing"
"time"
)
func TestConfigureNil(t *testing.T) {
if Configure(nil) == nil {
t.Fatal("Expected an error, since nil cannot be parsed.")
}
}
func TestConfigureUntaggedStruct(t *testing.T) {
var s = struct {
A int
B uint
C string
}{}
if err := Configure(&s); err != nil {
t.Fatal("Unexpected error: " + err.Error())
}
}
func TestConfigureNilPointer(t *testing.T) {
var c *struct{}
if Configure(c) == nil {
t.Fatal("Expected an error, since pointer is nil.")
}
}
func TestConfigureNonStructPointer(t *testing.T) {
var i = 42
if Configure(&i) == nil {
t.Fatal("Expected an error, since config value is not a struct.")
}
}
func TestEmptyStruct(t *testing.T) {
var s = struct{}{}
if Configure(&s) != nil {
t.Fatal("Expected correct processing of empty struct.")
}
}
func TestNonTaggedStruct(t *testing.T) {
var s = struct {
V string
}{}
if Configure(&s) != nil {
t.Fatal("Expected correct processing of struct without any tags.")
}
}
func TestNonRelevantlyTaggedStruct(t *testing.T) {
var s = struct {
V string `json:"some,value"`
}{}
if Configure(&s) != nil {
t.Fatal("Expected correct processing of struct without any relevant tags.")
}
}
func TestUnexportedTaggedField(t *testing.T) {
var s = struct {
v string `flag:"unexported,unexported,Tagging an unexported value."`
}{}
if Configure(&s) == nil {
t.Fatal("Expected error regarding unexported field, but got nothing.")
}
}
func TestEmptyTagName(t *testing.T) {
var s = struct {
V string `flag:",,"`
}{}
if Configure(&s) == nil {
t.Fatal("Expected error because no flag name was specified.")
}
}
func TestIncompleteTag(t *testing.T) {
var s = struct {
V string `flag:"a"`
}{}
if Configure(&s) != nil {
t.Fatal("Expected correct processing even though tag does not contain all parts.")
}
tag := flag.Lookup("a")
if tag == nil {
t.Fatal("Cannot find configured flag.")
}
if tag.Name != "a" {
t.Error("Expected another tag name.")
}
if tag.DefValue != "" {
t.Error("Expected empty string as default value, since we didn't specify any.")
}
if tag.Usage != "" {
t.Error("Expected empty string as usage information, since we didn't specify any.")
}
}
func TestTagString(t *testing.T) {
var s = struct {
V string `flag:"s,hello world,This sets the string flag."`
}{}
if Configure(&s) != nil {
t.Fatal("Expected correct configuration without any errors.")
}
f := flag.Lookup("s")
if f == nil {
t.Fatal("Could not find configured flag.")
}
if f.Name != "s" {
t.Error("Configured flag has incorrect name.")
}
if f.DefValue != "hello world" {
t.Error("Configured flag has incorrect default value.")
}
if f.Usage != "This sets the string flag." {
t.Error("Configured flag has incorrect usage description.")
}
}
func TestTagBool(t *testing.T) {
var s = struct {
V bool `flag:"b,true,This sets the bool flag."`
}{}
if Configure(&s) != nil {
t.Fatal("Expected correct configuration without any errors.")
}
f := flag.Lookup("b")
if f == nil {
t.Fatal("Could not find configured flag.")
}
if f.Name != "b" {
t.Error("Configured flag has incorrect name.")
}
if f.DefValue != "true" {
t.Error("Configured flag has incorrect default value.")
}
if f.Usage != "This sets the bool flag." {
t.Error("Configured flag has incorrect usage description.")
}
}
func TestTagBoolInvalidDefault(t *testing.T) {
var s = struct {
V bool `flag:"b2,foo,This sets the bool flag."`
}{}
if Configure(&s) == nil {
t.Fatal("Expected error due to incorrect default value.")
}
}
func TestTagFloat64(t *testing.T) {
var s = struct {
V float64 `flag:"f,0.2345,This sets the float flag."`
}{}
if Configure(&s) != nil {
t.Fatal("Expected correct configuration without any errors.")
}
f := flag.Lookup("f")
if f == nil {
t.Fatal("Could not find configured flag.")
}
if f.Name != "f" {
t.Error("Configured flag has incorrect name.")
}
if f.DefValue != "0.2345" {
t.Error("Configured flag has incorrect default value.")
}
if f.Usage != "This sets the float flag." {
t.Error("Configured flag has incorrect usage description.")
}
}
func TestTagFloat64InvalidDefault(t *testing.T) {
var s = struct {
V float64 `flag:"f2,abcde,This sets the float64 flag."`
}{}
if Configure(&s) == nil {
t.Fatal("Expected error due to incorrect default value.")
}
}
func TestTagInt(t *testing.T) {
var s = struct {
V int `flag:"i,64,This sets the int flag."`
}{}
if Configure(&s) != nil {
t.Fatal("Expected correct configuration without any errors.")
}
f := flag.Lookup("i")
if f == nil {
t.Fatal("Could not find configured flag.")
}
if f.Name != "i" {
t.Error("Configured flag has incorrect name.")
}
if f.DefValue != "64" {
t.Error("Configured flag has incorrect default value.")
}
if f.Usage != "This sets the int flag." {
t.Error("Configured flag has incorrect usage description.")
}
}
func TestTagIntInvalidDefault(t *testing.T) {
var s = struct {
V int `flag:"i2,0.33333,This sets the int flag."`
}{}
if Configure(&s) == nil {
t.Fatal("Expected error due to incorrect default value.")
}
}
func TestTagInt64(t *testing.T) {
var s = struct {
V int64 `flag:"i64,-6400000000,This sets the int64 flag."`
}{}
if Configure(&s) != nil {
t.Fatal("Expected correct configuration without any errors.")
}
f := flag.Lookup("i64")
if f == nil {
t.Fatal("Could not find configured flag.")
}
if f.Name != "i64" {
t.Error("Configured flag has incorrect name.")
}
if f.DefValue != "-6400000000" {
t.Error("Configured flag has incorrect default value.")
}
if f.Usage != "This sets the int64 flag." {
t.Error("Configured flag has incorrect usage description.")
}
}
func TestTagInt64InvalidDefault(t *testing.T) {
var s = struct {
V int64 `flag:"i64-2,abcdefgh,This sets the int64 flag."`
}{}
if Configure(&s) == nil {
t.Fatal("Expected error due to incorrect default value.")
}
}
func TestTagUint(t *testing.T) {
var s = struct {
V uint `flag:"u,3200,This sets the uint flag."`
}{}
if Configure(&s) != nil {
t.Fatal("Expected correct configuration without any errors.")
}
f := flag.Lookup("u")
if f == nil {
t.Fatal("Could not find configured flag.")
}
if f.Name != "u" {
t.Error("Configured flag has incorrect name.")
}
if f.DefValue != "3200" {
t.Error("Configured flag has incorrect default value.")
}
if f.Usage != "This sets the uint flag." {
t.Error("Configured flag has incorrect usage description.")
}
}
func TestTagUintInvalidDefault(t *testing.T) {
var s = struct {
V uint `flag:"u2,-200,This sets the uint flag."`
}{}
if Configure(&s) == nil {
t.Fatal("Expected error due to incorrect default value.")
}
}
func TestTagUint64(t *testing.T) {
var s = struct {
V uint64 `flag:"u64,6400000000,This sets the uint64 flag."`
}{}
if Configure(&s) != nil {
t.Fatal("Expected correct configuration without any errors.")
}
f := flag.Lookup("u64")
if f == nil {
t.Fatal("Could not find configured flag.")
}
if f.Name != "u64" {
t.Error("Configured flag has incorrect name.")
}
if f.DefValue != "6400000000" {
t.Error("Configured flag has incorrect default value.")
}
if f.Usage != "This sets the uint64 flag." {
t.Error("Configured flag has incorrect usage description.")
}
}
func TestTagUint64InvalidDefault(t *testing.T) {
var s = struct {
V uint64 `flag:"u64-2,abcdefgh,This sets the uint64 flag."`
}{}
if Configure(&s) == nil {
t.Fatal("Expected error due to incorrect default value.")
}
}
func TestMustConfigure(t *testing.T) {
defer func() {
// We are supposed to get a panic, so silence it.
recover()
}()
var s = struct {
X bool `flag:"x,test,test"`
}{}
MustConfigure(&s)
t.FailNow()
}
func TestConfigureAndParse(t *testing.T) {
var s = struct {
X string `flag:"xx,test,Test 1."`
Y bool `flag:"y,f,Test 2."`
}{}
if err := ConfigureAndParse(&s); err != nil {
t.Fatal("Did not expect error: " + err.Error())
}
if flag.Parsed() == false {
t.Fatal("Expected command line flags to be parsed by now.")
}
}
func TestConfigureAndParseFaulty(t *testing.T) {
var s = struct {
Y bool `flag:"y,bla,Test 2."`
}{}
if err := ConfigureAndParse(&s); err == nil {
t.Fatal("Expected an error but got nothing.")
}
}
func TestMustConfigureAndParseFailing(t *testing.T) {
defer func() {
// We are supposed to get a panic, so silence it.
recover()
}()
var s = struct {
X bool `flag:"xxx,test,test"`
}{}
MustConfigureAndParse(&s)
t.FailNow()
}
func TestMustConfigureAndParseSuccessfully(t *testing.T) {
var s = struct {
X bool `flag:"xxxx,True,test"`
}{}
MustConfigureAndParse(&s)
if !flag.Parsed() {
t.Fatal("Expected an command line flags to be parsed by now.")
}
}
func TestErrorOnInvalidDataType(t *testing.T) {
var s = struct {
Invalid uintptr `flag:"xxxxxx,,"`
}{}
if err := Configure(&s); err == nil {
t.Fatal("Expected error because of unsupported data type.")
}
}
func TestRecursiveStructProcessing(t *testing.T) {
var outer = struct {
Inner struct {
V int `flag:"innerv,1"`
}
}{}
err := Configure(&outer)
if err != nil {
t.Fatal("Unexpected error: " + err.Error())
}
f := flag.Lookup("innerv")
if f == nil {
t.Fatal("Could not find configured flag.")
}
if f.Name != "innerv" {
t.Error("Configured flag has incorrect name.")
}
if f.DefValue != "1" {
t.Error("Configured flag has incorrect default value.")
}
if f.Usage != "" {
t.Error("Configured flag has incorrect usage description.")
}
}
func TestBadInnerStruct(t *testing.T) {
var outer = struct {
Inner struct {
V uint `flag:"innerv,-1"`
}
}{}
err := Configure(&outer)
if err == nil {
t.Fatal("Expected error because of invalid default value.")
}
}
func TestMixedInnerStructProcessing(t *testing.T) {
var outer = struct {
Before uint `flag:"outerBefore,3,some description"`
Blank uint
Inner struct {
Dummy int
Inside string `flag:"innerInside,2,inside information"`
}
After int `flag:"outerAfter,1,final remark"`
}{}
err := Configure(&outer)
if err != nil {
t.Fatal("Unexpected error: " + err.Error())
}
flagBefore := flag.Lookup("outerBefore")
if flagBefore.Name != "outerBefore" || flagBefore.DefValue != "3" || flagBefore.Usage != "some description" {
t.Error("Flag outerBefore data is invalid.")
}
flagInside := flag.Lookup("innerInside")
if flagInside.Name != "innerInside" || flagInside.DefValue != "2" || flagInside.Usage != "inside information" {
t.Error("Flag innerInside data is invalid.")
}
flagAfter := flag.Lookup("outerAfter")
if flagAfter.Name != "outerAfter" || flagAfter.DefValue != "1" || flagAfter.Usage != "final remark" {
t.Error("Flag outerAfter data is invalid.")
}
}
func TestRegisterTypeDerivedFromPrimitive(t *testing.T) {
var s = struct {
D aliasInt `flag:"flagValueAliasInt,-10,Alias of int, still works as primitive int flag."`
}{}
Configure(&s)
flagAlias := flag.Lookup("flagValueAliasInt")
if flagAlias == nil {
t.Fatal("Could not find defined flagValueAliasInt.")
}
if flagAlias.Name != "flagValueAliasInt" || flagAlias.DefValue != "-10" || flagAlias.Usage != "Alias of int, still works as primitive int flag." {
t.Error("Flag flagValueAliasInt data is invalid.")
}
}
type aliasInt int
func TestRegisterValueInterfaceFlag(t *testing.T) {
var s = struct {
D dummyInt `flag:"flagValueDummyInt,,My first flag.Value implementation."`
}{}
err := Configure(&s)
if err != nil {
t.Fatal("Unexpected error: " + err.Error())
}
flagDummyInt := flag.Lookup("flagValueDummyInt")
if flagDummyInt == nil {
t.Fatal("Expected a flag, but got nil.")
}
if flagDummyInt.Name != "flagValueDummyInt" || flagDummyInt.DefValue != "0" || flagDummyInt.Usage != "My first flag.Value implementation." {
t.Fatal("Flag data is invalid.")
}
}
func TestRegisterValueInterfaceFlagNilPointer(t *testing.T) {
var s = struct {
D *dummyInt `flag:"flagValueDummyIntNilPointer,,My first flag.Value implementation."`
}{}
err := Configure(&s)
if err == nil {
t.Fatal("Expected an error since the pointer is nil, but didn't get anything.")
}
}
func TestRegisterValueInterfaceFlagPointer(t *testing.T) {
var s = struct {
D *dummyInt `flag:"flagValueDummyIntPointer,,My first flag.Value implementation."`
}{D: new(dummyInt)}
err := Configure(&s)
if err != nil {
t.Fatal("Unexpected error: " + err.Error())
}
flagDummyInt := flag.Lookup("flagValueDummyIntPointer")
if flagDummyInt == nil {
t.Fatal("Expected a flag, but got nil.")
}
if flagDummyInt.Name != "flagValueDummyIntPointer" || flagDummyInt.DefValue != "0" || flagDummyInt.Usage != "My first flag.Value implementation." {
t.Fatal("Flag data is invalid.")
}
}
func TestRegisterPrimitiveFlagNilPointer(t *testing.T) {
var s = struct {
D *int `flag:"flagValueIntPointer,123,My first primitive pointer flag."`
}{}
err := Configure(&s)
if err == nil {
t.Fatal("Expected an error but got nothing.")
}
}
func TestRegisterPrimitiveFlagPointer(t *testing.T) {
var s = struct {
D *int `flag:"flagValueIntPointer,123,My first primitive pointer flag."`
}{D: new(int)}
*s.D = 123
err := Configure(&s)
if err != nil {
t.Fatal("Unexpected error: " + err.Error())
}
flagInt := flag.Lookup("flagValueIntPointer")
if flagInt == nil {
t.Fatal("Expected a flag, but got nil.")
}
if flagInt.Name != "flagValueIntPointer" || flagInt.DefValue != "123" || flagInt.Usage != "My first primitive pointer flag." {
t.Fatal("Flag data is invalid.")
}
}
func TestRegisterFlagValueThroughInterfaceNil(t *testing.T) {
var s = struct {
D interface{} `flag:"flagValueUnknownInterface,,Value which turns out is flag.Value compatible."`
}{}
err := Configure(&s)
if err == nil {
t.Fatal("Expected error because D is nil.")
}
}
func TestRegisterFlagValueThroughInterfaceValueNilPointer(t *testing.T) {
var v *dummyInt
var s = struct {
D interface{} `flag:"flagValueUnknownInterface,,Value which turns out is flag.Value compatible."`
}{D: v}
err := Configure(&s)
if err == nil {
t.Fatal("Expected error because D is nil.")
}
}
func TestRegisterFlagValueThroughInterfaceValueZeroValue(t *testing.T) {
var v dummyInt
var s = struct {
D interface{} `flag:"flagValueUnknownInterface,,Value which turns out is flag.Value compatible."`
}{D: v}
err := Configure(&s)
if err == nil {
t.Fatal("Expected error because D is nil.")
}
}
func TestRegisterFlagValueThroughInterface(t *testing.T) {
var s = struct {
D interface{} `flag:"flagValueUnknownInterface,,Value which turns out is flag.Value compatible."`
}{D: new(dummyInt)}
err := Configure(&s)
if err != nil {
t.Fatal("Unexpected error: " + err.Error())
}
flagUnknown := flag.Lookup("flagValueUnknownInterface")
if flagUnknown == nil {
t.Fatal("Expected a flag, but got nil.")
}
if flagUnknown.Name != "flagValueUnknownInterface" || flagUnknown.DefValue != "0" || flagUnknown.Usage != "Value which turns out is flag.Value compatible." {
t.Fatal("Flag data is invalid.")
}
}
type dummyInt int
func (d *dummyInt) String() string {
return strconv.Itoa(int(*d))
}
func (d *dummyInt) Set(value string) error {
i, err := strconv.Atoi(value)
if err == nil {
*d = dummyInt(i)
}
return err
}
func TestRegisterDurationNilPointer(t *testing.T) {
var s = struct {
D *time.Duration `flag:"flagDuration,1h,Specify duration"`
}{}
err := Configure(&s)
if err == nil {
t.Fatal("Expected an error because of nil pointer.")
}
}
func TestRegisterDuration(t *testing.T) {
var s = struct {
D time.Duration `flag:"flagDuration,1h,Specify duration"`
}{}
err := Configure(&s)
if err != nil {
t.Fatal("Unexpected error: " + err.Error())
}
flagInterface := flag.Lookup("flagDuration")
if flagInterface == nil {
t.Fatal("Expected a flag, but got nil.")
}
if flagInterface.Name != "flagDuration" || flagInterface.DefValue != "1h0m0s" || flagInterface.Usage != "Specify duration" {
t.Fatal("Flag data is invalid.")
}
}
func TestRegisterDurationBadDefault(t *testing.T) {
var s = struct {
D time.Duration `flag:"flagDuration,1abcde,Specify duration"`
}{}
err := Configure(&s)
if err == nil {
t.Fatal("Expected error because of bad defaults.")
}
}
func TestRegisterDurationPointer(t *testing.T) {
var s = struct {
D *time.Duration `flag:"flagDurationPointer,1h,Specify duration"`
}{D: new(time.Duration)}
err := Configure(&s)
if err != nil {
t.Fatal("Unexpected error: " + err.Error())
}
flagInterface := flag.Lookup("flagDurationPointer")
if flagInterface == nil {
t.Fatal("Expected a flag, but got nil.")
}
if flagInterface.Name != "flagDurationPointer" || flagInterface.DefValue != "1h0m0s" || flagInterface.Usage != "Specify duration" {
t.Fatal("Flag data is invalid.")
}
}
func TestErrInvalidDefault(t *testing.T) {
var s = struct {
D int `flag:"flagInvalidDefault,abcde,Test invalid defaults..."`
}{}
err := Configure(&s)
if err == nil {
t.Fatal("Error was expected but got nothing.")
}
if !strings.HasPrefix(err.Error(), "invalid default value for field 'D' (tag 'flagInvalidDefault'): ") {
t.Fatal("Expected a different error message than was provided.")
}
}
func TestNoFlagOptionsProvided(t *testing.T) {
var s = struct {
D flagoptInt `flag:"flagNoOpts,13,Test flag no options ..."`
}{}
err := Configure(&s)
if err != nil {
t.Fatal("Unexpected error: " + err.Error())
}
if s.D != 42 {
t.Fatal("Expected value 42, because we don't skip the flag.Value interface test.")
}
}
func TestFlagOptionsSkipFlagValueFalseProvided(t *testing.T) {
var s = struct {
D flagoptInt `flag:"flagOptsEmpty,13,Test flag no options ..." flagopt:""`
}{}
err := Configure(&s)
if err != nil {
t.Fatal("Unexpected error:", err.Error())
}
if s.D != 42 {
t.Fatal("Expected value 42, because we don't skip the flag.Value interface test, but got", s.D)
}
}
func TestNoFlagOptionsSkipFlagValueProvided(t *testing.T) {
var s = struct {
D flagoptInt `flag:"flagOptsSkipFlagValue,13,Test flag no options ..." flagopt:"skipFlagValue"`
}{}
err := Configure(&s)
if err != nil {
t.Fatal("Unexpected error: ", err.Error())
}
if s.D != 13 {
t.Fatal("Expected value 13, because we skip the flag.Value interface test, but got", s.D)
}
}
func TestNoFlagValueDefaults(t *testing.T) {
var s = struct {
D flagoptInt `flag:"flagNoFlagValue,,Test flag no options ..."`
}{}
err := Configure(&s)
if err != nil {
t.Fatal("Unexpected error: ", err.Error())
}
if s.D != 0 {
t.Fatal("Expected value 0, because we skip the flag.Value interface test, but got", s.D)
}
}
func TestWithFlagValueDefaults(t *testing.T) {
var s = struct {
D flagoptInt `flag:"flagWithFlagValue,12,Test flag no options ..."`
}{}
err := Configure(&s)
if err != nil {
t.Fatal("Unexpected error: ", err.Error())
}
if s.D != 42 {
t.Fatal("Expected value 0, because we skip the flag.Value interface test, but got", s.D)
}
}
type flagoptInt int
func (o *flagoptInt) String() string {
return strconv.Itoa((int)(*o))
}
func (o *flagoptInt) Set(value string) error {
*o = 42
return nil
}
func TestConfigureAndParseArgs(t *testing.T) {
args := os.Args[:1]
var s = struct {
D int
}{}
var testset = []struct {
data interface{}
args []string
errorExpected bool
}{
{nil, nil, true},
{&s, nil, false},
{nil, args, true},
{&s, args, false},
}
// run tests for cases specified above
for nr, test := range testset {
if err := ConfigureAndParseArgs(test.data, test.args); (err != nil) != test.errorExpected {
t.Error("Test entry", nr, "failed with error", err)
}
}
}
func TestMustConfigureFlagset(t *testing.T) {
os.Args = os.Args[:1]
fs := flag.NewFlagSet("foo", flag.ContinueOnError)
var s = struct {
D int
}{}
var testset = []struct {
data interface{}
flagset *flag.FlagSet
shouldError bool
}{
{nil, nil, true},
{&s, nil, true},
{nil, flag.CommandLine, true},
{nil, fs, true},
{&s, flag.CommandLine, false},
{&s, fs, false},
}
for nr, test := range testset {
if err := ConfigureFlagset(test.data, test.flagset); (err != nil) != test.shouldError {
t.Error("Test entry", nr, "failed ConfigureFlagset with error", err)
}
if err := ConfigureFlagsetAndParse(test.data, test.flagset); (err != nil) != test.shouldError {
t.Error("Test entry", nr, "failed ConfigureFlagsetAndParse with error", err)
}
if recovered := controlledMustConfigureFlagset(test.data, test.flagset); recovered != test.shouldError {
t.Error("Test entry", nr, "failed MustConfigureFlagset with recovery", recovered)
}
if recovered := controlledMustConfigureFlagsetAndParse(test.data, test.flagset); recovered != test.shouldError {
t.Error("Test entry", nr, "failed MustConfigureFlagsetAndParse with recovery", recovered)
}
}
}
func TestMustConfigureAndParseArgs(t *testing.T) {
var s = struct {
D int
}{}
var testset = []struct {
data interface{}
args []string
shouldError bool
}{
{nil, nil, true},
{&s, nil, false},
{nil, []string{}, true},
{&s, []string{}, false},
{nil, []string{"hello"}, true},
{&s, []string{"hello"}, false},
}
for nr, test := range testset {
if err := ConfigureAndParseArgs(test.data, test.args); (err != nil) != test.shouldError {
t.Error("Test entry", nr, "failed ConfigureAndParseArgs with error", err)
}
if recovered := controlledMustConfigureAndParseArgs(test.data, test.args); recovered != test.shouldError {
t.Error("Test entry", nr, "failed MustConfigureFlagset with recovery", recovered)
}
}
}
func TestMustConfigureFlagsetAndParseArgs(t *testing.T) {
var s = struct {
D int
}{}
var fs = flag.NewFlagSet("foo", flag.ContinueOnError)
var testset = []struct {
data interface{}
flagset *flag.FlagSet
args []string
shouldError bool
}{
{nil, nil, nil, true},
{nil, nil, []string{}, true},
{nil, nil, []string{"hello"}, true},
{nil, fs, nil, true},
{nil, fs, []string{}, true},
{nil, fs, []string{"hello"}, true},
{&s, nil, nil, true},
{&s, nil, []string{}, true},
{&s, nil, []string{"hello"}, true},
{&s, fs, nil, false},
{&s, fs, []string{}, false},
{&s, fs, []string{"hello"}, false},
}
for nr, test := range testset {
if err := ConfigureFlagsetAndParseArgs(test.data, test.flagset, test.args); (err != nil) != test.shouldError {
t.Error("Test entry", nr, "failed ConfigureFlagsetAndParseArgs with error", err)
}
if recovered := controlledMustConfigureFlagsetAndParseArgs(test.data, test.flagset, test.args); recovered != test.shouldError {
t.Error("Test entry", nr, "failed MustConfigureFlagsetAndParseArgs with recovery", recovered)
}
}
}
func controlledMustConfigureFlagset(data interface{}, fs *flag.FlagSet) (result bool) {
defer func() {
result = recover() != nil
}()
MustConfigureFlagset(data, fs)
return
}
func controlledMustConfigureFlagsetAndParse(data interface{}, fs *flag.FlagSet) (result bool) {
defer func() {
result = recover() != nil
}()
MustConfigureFlagsetAndParse(data, fs)
return
}
func controlledMustConfigureAndParseArgs(data interface{}, args []string) (result bool) {
defer func() {
result = recover() != nil
}()
MustConfigureAndParseArgs(data, args)
return
}
func controlledMustConfigureFlagsetAndParseArgs(data interface{}, flagset *flag.FlagSet, args []string) (result bool) {
defer func() {
result = recover() != nil
}()
MustConfigureFlagsetAndParseArgs(data, flagset, args)
return
}