-
Notifications
You must be signed in to change notification settings - Fork 0
/
validators_test.go
1002 lines (915 loc) · 24.9 KB
/
validators_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
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package tri
import (
"time"
"testing"
)
func MakeTestHandler() func(*Tri) int {
return func(*Tri) int { return 0 }
}
func TestBrief(t *testing.T) {
// one item only
tb1 := Brief{
"item1", "item2",
}
e := tb1.Validate()
if e == nil {
t.Error("validator allowed more than one")
}
// string typed item
tb2 := Brief{
1,
}
e = tb2.Validate()
if e == nil {
t.Error("validator permitted other than a string")
}
// string length < 80
tb3 := Brief{
"123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890",
}
e = tb3.Validate()
if e == nil {
t.Error("validator permitted over 80 characters")
}
// no control characters
tb4 := Brief{
"this should not have a cr at the end\n",
}
e = tb4.Validate()
if e == nil {
t.Error("validator permitted over 80 characters")
}
tb5 := Brief{
"this should not have a cr at the end\n",
}
e = tb5.Validate()
if e == nil {
t.Error("validator permitted over 80 characters")
}
tb6 := Brief{
"this is ok",
}
e = tb6.Validate()
if e != nil {
t.Error("validator rejected correct input")
}
}
func TestCommand(t *testing.T) {
// not empty
tc0 := Command{}
if e := tc0.Validate(); e == nil {
t.Error("validator accepted empty Command")
}
//string in index 0
tc1 := Command{1}
if e := tc1.Validate(); e == nil {
t.Error("validator accepted non-string name")
}
//string is valid name (letters only)
tc2 := Command{"!"}
if e := tc2.Validate(); e == nil {
t.Error("validator accepted invalid name")
}
//more than one brief not allowed
tc3 := Command{"name", Brief{""}, Brief{""}}
if e := tc3.Validate(); e == nil {
t.Error("validator accepted more than one Brief")
}
// brief is invalid
tc4 := Command{"name", Brief{}}
if e := tc4.Validate(); e == nil {
t.Error("validator accepted invalid Brief")
}
//more than one handler not allowed
tc5 := Command{"name", Brief{""}, MakeTestHandler(), MakeTestHandler()}
if e := tc5.Validate(); e == nil {
t.Error("validator accepted more than one handler")
}
// Handler not nil
isnil := MakeTestHandler()
_ = isnil
isnil = nil
tc6 := Command{"name", isnil}
if e := tc6.Validate(); e == nil {
t.Error("validator accepted nil MakeTestHandler()")
}
// no more than one Short
tc7 := Command{"name", Short{'a'}, Short{'b'}}
if e := tc7.Validate(); e == nil {
t.Error("validator accepted more than one Short")
}
// invalid Short
tc8 := Command{"name", Short{}}
if e := tc8.Validate(); e == nil {
t.Error("validator accepted invalid Short")
}
// no more than one Usage
tc9 := Command{"name", Usage{""}, Usage{""}}
if e := tc9.Validate(); e == nil {
t.Error("validator accepted more than one Short")
}
// invalid Usage
tc10 := Command{"name", Usage{}}
if e := tc10.Validate(); e == nil {
t.Error("validator invalid Usage")
}
// no more than one Help
tc11 := Command{"name", Help{""}, Help{""}}
if e := tc11.Validate(); e == nil {
t.Error("validator accepted more than one Short")
}
// invalid Help
tc12 := Command{"name", Help{}}
if e := tc12.Validate(); e == nil {
t.Error("validator invalid Usage")
}
// no more than one Examples
tc13 := Command{"name", Examples{"", ""}, Examples{"", ""}}
if e := tc13.Validate(); e == nil {
t.Error("validator accepted more than one Short")
}
// invalid Examples
tc14 := Command{"name", Examples{}}
if e := tc14.Validate(); e == nil {
t.Error("validator invalid Usage")
}
//invalid Var
tc15 := Command{"name", Var{}}
if e := tc15.Validate(); e == nil {
t.Error("validator accepted invalid Var")
}
//invalid Trigger
tc16 := Command{"name", Trigger{}}
if e := tc16.Validate(); e == nil {
t.Error("validator accepted invalid Trigger")
}
//Brief field present
tc17 := Command{"name", MakeTestHandler(), Help{"aaaaa"}}
// t.Log(spew.Sdump(tc17), tc17.Validate())
if e := tc17.Validate(); e == nil {
t.Error("validator accepted Command without a Brief")
}
// handler present
tc18 := Command{"name", Brief{""}, Help{"aaaa"}}
if e := tc18.Validate(); e == nil {
t.Error("validator accepted Command without a handler")
}
//invalid typed element
tc19 := Command{"name", Brief{""}, MakeTestHandler(), 1}
if e := tc19.Validate(); e == nil {
t.Error("validator accepted Command with a invalid typed eleement")
}
// no errors!
tc20 := Command{"name", Brief{""}, MakeTestHandler()}
if e := tc20.Validate(); e != nil {
t.Error("validator rejected valid Command")
}
}
func TestCommands(t *testing.T) {
tcc1 := Commands{Command{"name", Brief{""}, MakeTestHandler(), 1}}
if e := tcc1.Validate(); e == nil {
t.Error("validator accepted Commands with invalid element")
}
tcc2 := Commands{Command{"name", Brief{""}, MakeTestHandler()}}
if e := tcc2.Validate(); e != nil {
t.Error("validator rejected valid Commands")
}
}
func TestDefault(t *testing.T) {
// only one item
td1 := Default{
"item1", "item2",
}
e := td1.Validate()
if e == nil {
t.Error("validator allowed more than one")
}
// no error!
td2 := Default{1}
e = td2.Validate()
if e != nil {
t.Error("validator rejected valid Default")
}
}
func TestDefaultCommand(t *testing.T) {
// only one item
tdc1 := DefaultCommand{
"item1", "item2",
}
e := tdc1.Validate()
if e == nil {
t.Error("validator allowed more than one")
}
// item is string
tdc2 := DefaultCommand{
1,
}
e = tdc2.Validate()
if e == nil {
t.Error("validator permitted other than a string")
}
// item is a ValidName
tdc3 := DefaultCommand{
"abc123",
}
e = tdc3.Validate()
if e == nil {
t.Error("validator permitted an invalid name")
}
// item is a valid
tdc4 := DefaultCommand{
"abc",
}
e = tdc4.Validate()
if e != nil {
t.Error("validator rejected a valid name")
}
}
func TestDefaultOn(t *testing.T) {
// must be empty
tdo1 := DefaultOn{1}
e := tdo1.Validate()
if e == nil {
t.Error("validator permitted content in DefaultOn")
}
// is empty
tdo2 := DefaultOn{}
e = tdo2.Validate()
if e != nil {
t.Error("validator rejected valid empty DefaultOn")
}
}
func TestExamples(t *testing.T) {
// must not be empty
te1 := Examples{}
e := te1.Validate()
if e == nil {
t.Error("validator invalid empty Examples")
}
// must have pairs of elements
te2 := Examples{"1", 2, 3.0}
e = te2.Validate()
if e == nil {
t.Error("validator permitted odd number of items in Examples")
}
// elements must be strings
te3 := Examples{"1", 2}
e = te3.Validate()
if e == nil {
t.Error("validator permitted non-string in Examples")
}
// string length < 80
te4 := Examples{
"--max=0", "123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890",
}
e = te4.Validate()
if e == nil {
t.Error("validator permitted over 80 characters")
}
// string length < 80
te5 := Examples{
"a1234567890123456789012345678901234567890", "123",
}
e = te5.Validate()
if e == nil {
t.Error("validator permitted over 80 characters")
}
// even numbered (first in pair) elements have no control characters
te6 := Examples{
"--max=0", "aaa\n",
}
e = te6.Validate()
if e == nil {
t.Error("validator permitted control character in explainer")
}
// no error
te7 := Examples{
"--max=0", "aaaaaaaa",
}
e = te7.Validate()
if e != nil {
t.Error("validator rejected valid example")
}
}
func TestGroup(t *testing.T) {
// contains only one element
tg1 := Group{1, 1}
tg2 := Group{}
e := tg1.Validate()
if e == nil {
t.Error("validator accepted more than one")
}
e = tg2.Validate()
if e == nil {
t.Error("validator accepted no elements")
}
// element is a string
tg3 := Group{1}
e = tg3.Validate()
if e == nil {
t.Error("validator accepted non string elemeent")
}
// string is a ValidName
tg4 := Group{"abc123"}
e = tg4.Validate()
if e == nil {
t.Error("validator accepted invalid name")
}
// no error!
tg5 := Group{"abc"}
e = tg5.Validate()
if e != nil {
t.Error("validator rejected valid name")
}
}
func TestHelp(t *testing.T) {
// contains only one element
th1 := Help{1, 1}
e := th1.Validate()
if e == nil {
t.Error("validator accepted more than one")
}
th2 := Help{}
e = th2.Validate()
if e == nil {
t.Error("validator accepted no elements")
}
// element is a string
th3 := Help{1.0}
e = th3.Validate()
if e == nil {
t.Error("validator accepted non-string")
}
// no error!
th4 := Help{"this is a test with cr\nand other things"}
e = th4.Validate()
if e != nil {
t.Error("validator rejected valid element")
}
}
func TestRunAfter(t *testing.T) {
// may not contain anything
tra1 := RunAfter{
"",
}
e := tra1.Validate()
if e == nil {
t.Error("validator accepted content in RunAfter")
}
// no error
tra2 := RunAfter{}
e = tra2.Validate()
if e != nil {
t.Error("validator rejected valid RunAfter")
}
}
func TestShort(t *testing.T) {
// contains only one element
ts1 := Short{'1', 2}
e := ts1.Validate()
if e == nil {
t.Error("validator accepted more than one item")
}
// element is a rune (single character/unicode point)
ts2 := Short{1}
e = ts2.Validate()
if e == nil {
t.Error("validator accepted a non-rune element")
}
// element is a letter or number
ts3 := Short{'!'}
e = ts3.Validate()
if e == nil {
t.Error("validator accepted non alphanumeric element")
}
// no error!
ts4 := Short{'a'}
e = ts4.Validate()
if e != nil {
t.Error("validator rejected a valid short element")
}
}
func TestSlot(t *testing.T) {
// slots are all the same type (pointer to said type)
a := 1
b := "string"
ts1 := Slot{&a, &b}
e := ts1.Validate()
if e == nil {
t.Error("validator accepted heteregenous types")
}
// slots are all pointers
c := 2
ts2 := Slot{a, c}
e = ts2.Validate()
if e == nil {
t.Error("validator accepted heteregenous types")
}
// no error!
ts3 := Slot{&a, &c}
e = ts3.Validate()
if e != nil {
t.Error("validator rejected valid contents")
}
}
func TestTerminates(t *testing.T) {
// must be empty
tt1 := Terminates{1}
e := tt1.Validate()
if e == nil {
t.Error("validator permitted content")
}
// is empty
tt2 := Terminates{}
e = tt2.Validate()
if e != nil {
t.Error("validator rejected valid empty Terminates")
}
}
func TestTri(t *testing.T) {
// contains at least 3 elements
ttr1 := Tri{1, 1}
if e := ttr1.Validate(); e == nil {
t.Error("Trigger must contain at least 3 elements")
}
// first element is a string
ttr2 := Tri{1, 1, 1}
if e := ttr2.Validate(); e == nil {
t.Error("first element must be a string")
}
// string is a ValidName
ttr3 := Tri{"a ", 1, 1}
if e := ttr3.Validate(); e == nil {
t.Error("validator accepted invalid name")
}
// contains (only) one Brief
ttr4 := Tri{"aaaa", Brief{""}, Brief{""}}
if e := ttr4.Validate(); e == nil {
t.Error("validator accepted more than one Brief")
}
// contains (only) one Version
ttr5 := Tri{"aaaa", Version{1, 1, 1}, Version{1, 1, 1}, Brief{"aaaa"}}
if e := ttr5.Validate(); e == nil {
t.Error("validator accepted more than one Version")
}
// contains (only) one Commands
ttr6 := Tri{"aaaa", Commands{}, Commands{}, Brief{"aaaa"}}
if e := ttr6.Validate(); e == nil {
t.Error("validator accepted more than one Commands")
}
// contains no more than one DefaultCommand
ttr7 := Tri{"aaaa", DefaultCommand{"commname"}, DefaultCommand{"commname"}, Brief{"aaaa"}, Commands{Command{"commname"}}}
if e := ttr7.Validate(); e == nil {
t.Error("validator accepted more than one DefaultCommand")
}
// DefaultCommand with no Commands array
ttr8 := Tri{"aaaa", DefaultCommand{"aaaa"}, Brief{"aaaa"}}
if e := ttr8.Validate(); e == nil {
t.Error("validator accepted DefaultCommand with no Commands present")
}
// DefaultCommand's name appears in also present Commands array
ttr9 := Tri{"aaaa", DefaultCommand{"commname"}, Brief{"aaaa"}, Commands{Command{"NOTcommname"}}}
if e := ttr9.Validate(); e == nil {
t.Error("validator accepted more than one DefaultCommand")
}
// contains invalid Var
ttr10 := Tri{"aaaa", Version{1, 1, 1}, Brief{"aaaa"}, Var{}}
if e := ttr10.Validate(); e == nil {
t.Error("validator accepted invalid Var")
}
// contains invalid Trigger
ttr11 := Tri{"aaaa", Version{1, 1, 1}, Brief{"aaaa"}, Trigger{}}
if e := ttr11.Validate(); e == nil {
t.Error("validator accepted invalid Trigger")
}
// contains invalid DefaultCommand
ttr12 := Tri{"aaaa", Version{1, 1, 1}, DefaultCommand{}, Brief{"aaaa"}, Commands{Command{"commname"}}}
if e := ttr12.Validate(); e == nil {
t.Error("validator accepted invalid DefaultCommand")
}
// contains invalid Command in Commands
ttr13 := Tri{"aaaa", Version{1, 1, 1}, Brief{"aaaa"}, Commands{Command{}}}
if e := ttr13.Validate(); e == nil {
t.Error("validator accepted invalid Command element in Commands")
}
// only contains element from set of possible elements
ttr14 := Tri{"aaaa", Version{1, 1, 1}, Brief{"aaaa"}, 1}
if e := ttr14.Validate(); e == nil {
t.Error("validator accepted invalid element in Tri")
}
// contains invalid Brief
ttr15 := Tri{"aaaa", Version{1, 1, 1}, Brief{1}}
if e := ttr15.Validate(); e == nil {
t.Error("validator accepted invalid Brief")
}
// contains invalid Version
ttr16 := Tri{"aaaa", Version{1, 1, 1, 1}, Brief{"valid brief"}}
if e := ttr16.Validate(); e == nil {
t.Error("validator accepted invalid Version")
}
// Brief is missing
ttr17 := Tri{"aaaa", DefaultCommand{"commname"}, Version{1, 1, 1}, Commands{Command{"commname", Brief{"valid brief"}, MakeTestHandler()}}}
if e := ttr17.Validate(); e == nil {
t.Error("validator accepted missing Brief")
}
// Version is missing
ttr18 := Tri{"aaaa", DefaultCommand{"commname"}, Brief{"valid brief"},
Commands{
Command{
"commname",
Brief{"valid brief"},
MakeTestHandler(),
},
},
}
if e := ttr18.Validate(); e == nil {
t.Error("validator accepted missing Version")
}
// no error!
ttr21 := Tri{"aaaa", DefaultCommand{"commname"}, Brief{"valid brief"},
Commands{
Command{
"commname",
Brief{"valid brief"},
MakeTestHandler(),
},
},
Version{1, 1, 1},
}
if e := ttr21.Validate(); e != nil {
t.Error("validator rejected valid Tri")
}
}
func TestTrigger(t *testing.T) {
// contains at least 3 elements
tt1 := Trigger{1, 1}
if e := tt1.Validate(); e == nil {
t.Error("Trigger must contain at least 3 elements")
}
// first is string
tt2 := Trigger{1, 1, 1}
if e := tt2.Validate(); e == nil {
t.Error("first element must be a string")
}
// name is ValidName
tt3 := Trigger{"a ", 1, 1}
if e := tt3.Validate(); e == nil {
t.Error("validator accepted invalid name")
}
// has only one Brief
tt4 := Trigger{"aaaa", Brief{""}, Brief{""}}
if e := tt4.Validate(); e == nil {
t.Error("validator accepted more than one Brief")
}
// has only one Short
tt5 := Trigger{"aaaa", Short{'a'}, Short{'a'}}
if e := tt5.Validate(); e == nil {
t.Error("validator allowed more than one Short")
}
// has only one Usage
tt6 := Trigger{"aaaa", Usage{""}, Usage{""}}
if e := tt6.Validate(); e == nil {
t.Error("validator allowed more than one Usage")
}
// has only one Help
tt7 := Trigger{"aaaa", Help{""}, Help{""}}
if e := tt7.Validate(); e == nil {
t.Error("validator allowed more than one Help")
}
// has only one handler
tt8 := Trigger{"name", Brief{""}, MakeTestHandler(), MakeTestHandler()}
if e := tt8.Validate(); e == nil {
t.Error("validator accepted more than one MakeTestHandler()")
}
// has only one DefaultOn
tt9 := Trigger{"aaaa", DefaultOn{}, DefaultOn{}}
if e := tt9.Validate(); e == nil {
t.Error("validator allowed more than one DefaultOn")
}
// has only one RunAfter
tt10 := Trigger{"aaaa", RunAfter{}, RunAfter{}}
if e := tt10.Validate(); e == nil {
t.Error("validator allowed more than one RunAfter")
}
// has only one Terminates
tt11 := Trigger{"aaaa", Terminates{}, Terminates{}}
if e := tt11.Validate(); e == nil {
t.Error("validator allowed more than one Terminates")
}
// has invalid Brief
tt12 := Trigger{"aaaa", Short{'a'}, Brief{1}}
if e := tt12.Validate(); e == nil {
t.Error("validator allowed invalid Brief")
}
// has invalid Short
tt13 := Trigger{"aaaa", Brief{"aaaa"}, Short{1}}
if e := tt13.Validate(); e == nil {
t.Error("validator allowed invalid Short")
}
// has invalid Usage
tt14 := Trigger{"aaaa", Brief{"aaaa"}, Usage{1}}
if e := tt14.Validate(); e == nil {
t.Error("validator allowed invalid Usage")
}
// has invalid Help
tt15 := Trigger{"aaaa", Brief{"aaaa"}, Help{1}}
if e := tt15.Validate(); e == nil {
t.Error("validator allowed invalid Help")
}
// has invalid MakeTestHandler()
handle := MakeTestHandler()
_ = handle
handle = nil
tt16 := Trigger{"aaaa", Brief{"aaaa"}, handle}
if e := tt16.Validate(); e == nil {
t.Error("validator allowed invalid MakeTestHandler()")
}
// has invalid DefaultOn
tt17 := Trigger{"aaaa", Brief{"aaaa"}, DefaultOn{1}}
if e := tt17.Validate(); e == nil {
t.Error("validator allowed invalid DefaultOn")
}
// has invalid RunAfter
tt18 := Trigger{"aaaa", Brief{"aaaa"}, RunAfter{1}}
if e := tt18.Validate(); e == nil {
t.Error("validator allowed invalid RunAfter")
}
// has invalid Terminates
tt19 := Trigger{"aaaa", Brief{"aaaa"}, Terminates{1}}
if e := tt19.Validate(); e == nil {
t.Error("validator allowed invalid Terminates")
}
// has one each of Brief and handler
tt20 := Trigger{"aaaa", Brief{"aaaa"}, Terminates{}}
if e := tt20.Validate(); e == nil {
t.Error("validator allowed Trigger without handler")
}
tt21 := Trigger{"aaaa", MakeTestHandler(), Terminates{}}
if e := tt21.Validate(); e == nil {
t.Error("validator allowed Trigger without Brief")
}
// has no other type than those foregoing
tt22 := Trigger{"aaaa", Brief{"aaaa"}, MakeTestHandler(), 3}
if e := tt22.Validate(); e == nil {
t.Error("validator allowed invalid element")
}
// has only one Group
tt23 := Trigger{"aaaa", Brief{"aaaa"}, MakeTestHandler(), Group{"aaaa"}, Group{"aaaa"}}
if e := tt23.Validate(); e == nil {
t.Error("validator allowed more than one DefaultOn")
}
// has invalid Group
tt24 := Trigger{"aaaa", Brief{"aaaa"}, MakeTestHandler(), Group{1}}
if e := tt24.Validate(); e == nil {
t.Error("validator allowed invalid DefaultOn")
}
// no error!
tt25 := Trigger{"aaaa", Brief{"aaaa"}, MakeTestHandler(), Terminates{}}
if e := tt25.Validate(); e != nil {
t.Error("validator rejected valid Trigger")
}
}
func TestUsage(t *testing.T) {
// only one element
tu1 := Usage{}
tu2 := Usage{1, 2}
e := tu1.Validate()
if e == nil {
t.Error("validator permitted empty Usage")
}
e = tu2.Validate()
if e == nil {
t.Error("validator permitted more than one element in Usage")
}
// element is string
tu3 := Usage{0.1}
e = tu3.Validate()
if e == nil {
t.Error("validator permitted element that is not a string")
}
// string is no more than 80 chars long
tu4 := Usage{
"123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890",
}
e = tu4.Validate()
if e == nil {
t.Error("validator permitted over 80 characters")
}
// string contains no control characters
tu5 := Usage{
"aaa\n",
}
e = tu5.Validate()
if e == nil {
t.Error("validator permitted control character in explainer")
}
// no error!
tu6 := Usage{
"aaaaaaa",
}
e = tu6.Validate()
if e != nil {
t.Error("validator rejected valid input")
}
}
func TestValidName(t *testing.T) {
// name is 3 or more characters long
tvn1 := "ab"
e := ValidName(tvn1)
if e == nil {
t.Error("validator accepted string under 3 characters length")
}
// name is only composed of letters
tvn2 := "ab3"
e = ValidName(tvn2)
if e == nil {
t.Error("validator accepted non-letter characters")
}
// no error!
tvn3 := "proper"
e = ValidName(tvn3)
if e != nil {
t.Error("validator rejected valid namme")
}
}
func TestVar(t *testing.T) {
// contains at least 3 elements
tv1 := Var{1, 1}
if e := tv1.Validate(); e == nil {
t.Error("Var must contain at least 3 elements")
}
// first is string
tv2 := Var{1, 1, 1}
if e := tv2.Validate(); e == nil {
t.Error("first element must be a string")
}
// name is ValidName
tv3 := Var{"a ", 1, 1}
if e := tv3.Validate(); e == nil {
t.Error("validator accepted invalid name")
}
// has only one Brief
tv4 := Var{"aaaa", Brief{""}, Brief{""}}
if e := tv4.Validate(); e == nil {
t.Error("validator accepted more than one Brief")
}
// has only one Short
tv5 := Var{"aaaa", Short{'a'}, Short{'a'}}
if e := tv5.Validate(); e == nil {
t.Error("validator allowed more than one Short")
}
// has only one Usage
tv6 := Var{"aaaa", Usage{""}, Usage{""}}
if e := tv6.Validate(); e == nil {
t.Error("validator allowed more than one Usage")
}
// has only one Help
tv7 := Var{"aaaa", Help{""}, Help{""}}
if e := tv7.Validate(); e == nil {
t.Error("validator allowed more than one Help")
}
// has only one Default
tv8 := Var{"aaaa", Default{"aaa"}, Default{"aaa"}}
if e := tv8.Validate(); e == nil {
t.Error("validator allowed more than one Default")
}
// has only one Slot
tstring := "valid string"
tv9 := Var{"aaaa", Slot{&tstring}, Slot{&tstring}}
if e := tv9.Validate(); e == nil {
t.Error("validator allowed more than one Slot")
}
// has invalid Brief
tv10 := Var{"aaaa", Brief{}, Short{""}}
if e := tv10.Validate(); e == nil {
t.Error("validator accepted invalid Brief")
}
// has invalid Short
tv11 := Var{"aaaa", Brief{"aaaa"}, Short{1}}
if e := tv11.Validate(); e == nil {
t.Error("validator allowed invalid Short")
}
// has invalid Usage
tv12 := Var{"aaaa", Brief{"aaaa"}, Usage{0.1}}
if e := tv12.Validate(); e == nil {
t.Error("validator allowed invalid Usage")
}
// has invalid Help
tv13 := Var{"aaaa", Brief{""}, Help{"aaa", 1}}
if e := tv13.Validate(); e == nil {
t.Error("validator allowed invalid Help")
}
// has invalid Default
tv14 := Var{"aaaa", Brief{"aaa"}, Default{1, 3}}
if e := tv14.Validate(); e == nil {
t.Error("validator allowed invalid Default")
}
// has invalid Slot
tv15 := Var{"aaaa", Brief{tstring}, Slot{tstring}}
if e := tv15.Validate(); e == nil {
t.Error("validator allowed invalid Slot")
}
// has one each of Brief and Slot
tv16 := Var{"aaaa", Brief{"aaa"}, Default{"aa"}}
if e := tv16.Validate(); e == nil {
t.Error("validator allowed absence of Brief or Slot")
}
// has no other type than those foregoing
tv17 := Var{"aaaa", Brief{tstring}, Slot{&tstring}, 1, MakeTestHandler()}
if e := tv17.Validate(); e == nil {
t.Error("validator rejected valid Var")
}
// has only one Group
tv18 := Var{"aaaa", Brief{tstring}, Slot{&tstring},
Group{"aaaa"}, Group{"aaaa"},
}
if e := tv18.Validate(); e == nil {
t.Error("validator accepted more than one Group")
}
// has invalid Group
tv19 := Var{"aaaa", Brief{"aaaa"}, Slot{&tstring}, Group{""}}
if e := tv19.Validate(); e == nil {
t.Error("validator allowed invalid Group")
}
// Default value is assignable to dereferenced Slot pointer
tv20 := Var{"aaaa", Brief{"aaaa"}, Slot{&tstring}, Default{5}}
if e := tv20.Validate(); e == nil {
t.Error("validator allowed default that can't be assigned to Slot")
}
var tint int
tv20 = Var{"aaaa", Brief{"aaaa"}, Slot{&tint}, Default{" "}}
if e := tv20.Validate(); e == nil {
t.Error("validator allowed default that can't be assigned to Slot")
}
var tuint32 uint32
tv20 = Var{"aaaa", Brief{"aaaa"}, Slot{&tuint32}, Default{0.1}}
if e := tv20.Validate(); e == nil {
t.Error("validator allowed default that can't be assigned to Slot")
}
var tfloat64 float64
tv20 = Var{"aaaa", Brief{"aaaa"}, Slot{&tfloat64}, Default{5}}
if e := tv20.Validate(); e == nil {
t.Error("validator allowed default that can't be assigned to Slot")
}
var tstringslice []string
tv20 = Var{"aaaa", Brief{"aaaa"}, Slot{&tstringslice}, Default{"aaa"}}
if e := tv20.Validate(); e == nil {
t.Error("validator allowed default that can't be assigned to Slot")
}
var tduration time.Duration
tv20 = Var{"aaaa", Brief{"aaaa"}, Slot{&tduration}, Default{5}}
if e := tv20.Validate(); e == nil {
t.Error("validator allowed default that can't be assigned to Slot")
}
// no error!}
tv21 := Var{"aaaa", Brief{tstring}, Slot{&tstring}}
if e := tv21.Validate(); e != nil {
t.Error("validator rejected valid Var")
}
}
func TestVersion(t *testing.T) {
// has no more than 4 fields
tv1 := Version{1, 2, 3, 4, 5}
e := tv1.Validate()
if e == nil {
t.Error("validator accepted more than three items")
}
// has at least 3 fields
tv2 := Version{4, 5}
e = tv2.Validate()
if e == nil {
t.Error("validator accepted less than three items")
}
// first three are integers
tv3 := Version{1.1, 2, 3, 4}
e = tv3.Validate()
if e == nil {
t.Error("validator accepted non-integer version numbers")
}
// integers are under 100
tv4 := Version{100, 2, 3, 4}
e = tv4.Validate()
if e == nil {
t.Error("validator accepted a version number over 100")
}
// 4th field is a string
tv5 := Version{10, 2, 3, 4}
e = tv5.Validate()
if e == nil {
t.Error("validator accepted a 4th field that is not a string")
}
// string contains only letters and numbers
tv6 := Version{10, 2, 3, "alpha3! "}
e = tv6.Validate()
if e == nil {
t.Error(
"validator accepted a 4th field that contains other than letters and numbers")
}
// no error!
tv7 := Version{10, 2, 3, "alpha3"}
e = tv7.Validate()
if e != nil {
t.Error(
"validator accepted a 4th field that contains other than letters and numbers")
}