1 package tri
2 3 import (
4 "time"
5 "testing"
6 )
7 8 func MakeTestHandler() func(*Tri) int {
9 return func(*Tri) int { return 0 }
10 }
11 12 func TestBrief(t *testing.T) {
13 14 // one item only
15 tb1 := Brief{
16 "item1", "item2",
17 }
18 e := tb1.Validate()
19 if e == nil {
20 t.Error("validator allowed more than one")
21 }
22 23 // string typed item
24 tb2 := Brief{
25 1,
26 }
27 e = tb2.Validate()
28 if e == nil {
29 t.Error("validator permitted other than a string")
30 }
31 32 // string length < 80
33 tb3 := Brief{
34 "123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890",
35 }
36 e = tb3.Validate()
37 if e == nil {
38 t.Error("validator permitted over 80 characters")
39 }
40 41 // no control characters
42 tb4 := Brief{
43 "this should not have a cr at the end\n",
44 }
45 e = tb4.Validate()
46 if e == nil {
47 t.Error("validator permitted over 80 characters")
48 }
49 50 tb5 := Brief{
51 "this should not have a cr at the end\n",
52 }
53 e = tb5.Validate()
54 if e == nil {
55 t.Error("validator permitted over 80 characters")
56 }
57 58 tb6 := Brief{
59 "this is ok",
60 }
61 e = tb6.Validate()
62 if e != nil {
63 t.Error("validator rejected correct input")
64 }
65 66 }
67 68 func TestCommand(t *testing.T) {
69 // not empty
70 tc0 := Command{}
71 if e := tc0.Validate(); e == nil {
72 t.Error("validator accepted empty Command")
73 }
74 //string in index 0
75 tc1 := Command{1}
76 if e := tc1.Validate(); e == nil {
77 t.Error("validator accepted non-string name")
78 }
79 //string is valid name (letters only)
80 tc2 := Command{"!"}
81 if e := tc2.Validate(); e == nil {
82 t.Error("validator accepted invalid name")
83 }
84 //more than one brief not allowed
85 tc3 := Command{"name", Brief{""}, Brief{""}}
86 if e := tc3.Validate(); e == nil {
87 t.Error("validator accepted more than one Brief")
88 }
89 // brief is invalid
90 tc4 := Command{"name", Brief{}}
91 if e := tc4.Validate(); e == nil {
92 t.Error("validator accepted invalid Brief")
93 }
94 //more than one handler not allowed
95 tc5 := Command{"name", Brief{""}, MakeTestHandler(), MakeTestHandler()}
96 if e := tc5.Validate(); e == nil {
97 t.Error("validator accepted more than one handler")
98 }
99 // Handler not nil
100 isnil := MakeTestHandler()
101 _ = isnil
102 isnil = nil
103 tc6 := Command{"name", isnil}
104 if e := tc6.Validate(); e == nil {
105 t.Error("validator accepted nil MakeTestHandler()")
106 }
107 // no more than one Short
108 tc7 := Command{"name", Short{'a'}, Short{'b'}}
109 if e := tc7.Validate(); e == nil {
110 t.Error("validator accepted more than one Short")
111 }
112 // invalid Short
113 tc8 := Command{"name", Short{}}
114 if e := tc8.Validate(); e == nil {
115 t.Error("validator accepted invalid Short")
116 }
117 // no more than one Usage
118 tc9 := Command{"name", Usage{""}, Usage{""}}
119 if e := tc9.Validate(); e == nil {
120 t.Error("validator accepted more than one Short")
121 }
122 // invalid Usage
123 tc10 := Command{"name", Usage{}}
124 if e := tc10.Validate(); e == nil {
125 t.Error("validator invalid Usage")
126 }
127 // no more than one Help
128 tc11 := Command{"name", Help{""}, Help{""}}
129 if e := tc11.Validate(); e == nil {
130 t.Error("validator accepted more than one Short")
131 }
132 // invalid Help
133 tc12 := Command{"name", Help{}}
134 if e := tc12.Validate(); e == nil {
135 t.Error("validator invalid Usage")
136 }
137 // no more than one Examples
138 tc13 := Command{"name", Examples{"", ""}, Examples{"", ""}}
139 if e := tc13.Validate(); e == nil {
140 t.Error("validator accepted more than one Short")
141 }
142 // invalid Examples
143 tc14 := Command{"name", Examples{}}
144 if e := tc14.Validate(); e == nil {
145 t.Error("validator invalid Usage")
146 }
147 //invalid Var
148 tc15 := Command{"name", Var{}}
149 if e := tc15.Validate(); e == nil {
150 t.Error("validator accepted invalid Var")
151 }
152 //invalid Trigger
153 tc16 := Command{"name", Trigger{}}
154 if e := tc16.Validate(); e == nil {
155 t.Error("validator accepted invalid Trigger")
156 }
157 //Brief field present
158 tc17 := Command{"name", MakeTestHandler(), Help{"aaaaa"}}
159 // t.Log(spew.Sdump(tc17), tc17.Validate())
160 if e := tc17.Validate(); e == nil {
161 t.Error("validator accepted Command without a Brief")
162 }
163 // handler present
164 tc18 := Command{"name", Brief{""}, Help{"aaaa"}}
165 if e := tc18.Validate(); e == nil {
166 t.Error("validator accepted Command without a handler")
167 }
168 //invalid typed element
169 tc19 := Command{"name", Brief{""}, MakeTestHandler(), 1}
170 if e := tc19.Validate(); e == nil {
171 t.Error("validator accepted Command with a invalid typed eleement")
172 }
173 // no errors!
174 tc20 := Command{"name", Brief{""}, MakeTestHandler()}
175 if e := tc20.Validate(); e != nil {
176 t.Error("validator rejected valid Command")
177 }
178 }
179 func TestCommands(t *testing.T) {
180 tcc1 := Commands{Command{"name", Brief{""}, MakeTestHandler(), 1}}
181 if e := tcc1.Validate(); e == nil {
182 t.Error("validator accepted Commands with invalid element")
183 }
184 tcc2 := Commands{Command{"name", Brief{""}, MakeTestHandler()}}
185 if e := tcc2.Validate(); e != nil {
186 t.Error("validator rejected valid Commands")
187 }
188 }
189 190 func TestDefault(t *testing.T) {
191 192 // only one item
193 td1 := Default{
194 "item1", "item2",
195 }
196 e := td1.Validate()
197 if e == nil {
198 t.Error("validator allowed more than one")
199 }
200 // no error!
201 td2 := Default{1}
202 e = td2.Validate()
203 if e != nil {
204 t.Error("validator rejected valid Default")
205 }
206 207 }
208 209 func TestDefaultCommand(t *testing.T) {
210 211 // only one item
212 tdc1 := DefaultCommand{
213 "item1", "item2",
214 }
215 e := tdc1.Validate()
216 if e == nil {
217 t.Error("validator allowed more than one")
218 }
219 220 // item is string
221 tdc2 := DefaultCommand{
222 1,
223 }
224 e = tdc2.Validate()
225 if e == nil {
226 t.Error("validator permitted other than a string")
227 }
228 229 // item is a ValidName
230 tdc3 := DefaultCommand{
231 "abc123",
232 }
233 e = tdc3.Validate()
234 if e == nil {
235 t.Error("validator permitted an invalid name")
236 }
237 238 // item is a valid
239 tdc4 := DefaultCommand{
240 "abc",
241 }
242 e = tdc4.Validate()
243 if e != nil {
244 t.Error("validator rejected a valid name")
245 }
246 }
247 248 func TestDefaultOn(t *testing.T) {
249 250 // must be empty
251 tdo1 := DefaultOn{1}
252 e := tdo1.Validate()
253 if e == nil {
254 t.Error("validator permitted content in DefaultOn")
255 }
256 257 // is empty
258 tdo2 := DefaultOn{}
259 e = tdo2.Validate()
260 if e != nil {
261 t.Error("validator rejected valid empty DefaultOn")
262 }
263 264 }
265 266 func TestExamples(t *testing.T) {
267 268 // must not be empty
269 te1 := Examples{}
270 e := te1.Validate()
271 if e == nil {
272 t.Error("validator invalid empty Examples")
273 }
274 275 // must have pairs of elements
276 te2 := Examples{"1", 2, 3.0}
277 e = te2.Validate()
278 if e == nil {
279 t.Error("validator permitted odd number of items in Examples")
280 }
281 282 // elements must be strings
283 te3 := Examples{"1", 2}
284 e = te3.Validate()
285 if e == nil {
286 t.Error("validator permitted non-string in Examples")
287 }
288 289 // string length < 80
290 te4 := Examples{
291 "--max=0", "123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890",
292 }
293 e = te4.Validate()
294 if e == nil {
295 t.Error("validator permitted over 80 characters")
296 }
297 298 // string length < 80
299 te5 := Examples{
300 "a1234567890123456789012345678901234567890", "123",
301 }
302 e = te5.Validate()
303 if e == nil {
304 t.Error("validator permitted over 80 characters")
305 }
306 307 // even numbered (first in pair) elements have no control characters
308 te6 := Examples{
309 "--max=0", "aaa\n",
310 }
311 e = te6.Validate()
312 if e == nil {
313 t.Error("validator permitted control character in explainer")
314 }
315 316 // no error
317 te7 := Examples{
318 "--max=0", "aaaaaaaa",
319 }
320 e = te7.Validate()
321 if e != nil {
322 t.Error("validator rejected valid example")
323 }
324 325 }
326 327 func TestGroup(t *testing.T) {
328 329 // contains only one element
330 tg1 := Group{1, 1}
331 tg2 := Group{}
332 e := tg1.Validate()
333 if e == nil {
334 t.Error("validator accepted more than one")
335 }
336 e = tg2.Validate()
337 if e == nil {
338 t.Error("validator accepted no elements")
339 }
340 341 // element is a string
342 tg3 := Group{1}
343 e = tg3.Validate()
344 if e == nil {
345 t.Error("validator accepted non string elemeent")
346 }
347 348 // string is a ValidName
349 tg4 := Group{"abc123"}
350 e = tg4.Validate()
351 if e == nil {
352 t.Error("validator accepted invalid name")
353 }
354 355 // no error!
356 tg5 := Group{"abc"}
357 e = tg5.Validate()
358 if e != nil {
359 t.Error("validator rejected valid name")
360 }
361 362 }
363 364 func TestHelp(t *testing.T) {
365 366 // contains only one element
367 th1 := Help{1, 1}
368 e := th1.Validate()
369 if e == nil {
370 t.Error("validator accepted more than one")
371 }
372 th2 := Help{}
373 e = th2.Validate()
374 if e == nil {
375 t.Error("validator accepted no elements")
376 }
377 378 // element is a string
379 th3 := Help{1.0}
380 e = th3.Validate()
381 if e == nil {
382 t.Error("validator accepted non-string")
383 }
384 385 // no error!
386 th4 := Help{"this is a test with cr\nand other things"}
387 e = th4.Validate()
388 if e != nil {
389 t.Error("validator rejected valid element")
390 }
391 392 }
393 394 func TestRunAfter(t *testing.T) {
395 396 // may not contain anything
397 tra1 := RunAfter{
398 "",
399 }
400 e := tra1.Validate()
401 if e == nil {
402 t.Error("validator accepted content in RunAfter")
403 }
404 // no error
405 tra2 := RunAfter{}
406 e = tra2.Validate()
407 if e != nil {
408 t.Error("validator rejected valid RunAfter")
409 }
410 411 }
412 413 func TestShort(t *testing.T) {
414 415 // contains only one element
416 ts1 := Short{'1', 2}
417 e := ts1.Validate()
418 if e == nil {
419 t.Error("validator accepted more than one item")
420 }
421 422 // element is a rune (single character/unicode point)
423 ts2 := Short{1}
424 e = ts2.Validate()
425 if e == nil {
426 t.Error("validator accepted a non-rune element")
427 }
428 429 // element is a letter or number
430 ts3 := Short{'!'}
431 e = ts3.Validate()
432 if e == nil {
433 t.Error("validator accepted non alphanumeric element")
434 }
435 436 // no error!
437 ts4 := Short{'a'}
438 e = ts4.Validate()
439 if e != nil {
440 t.Error("validator rejected a valid short element")
441 }
442 443 }
444 445 func TestSlot(t *testing.T) {
446 447 // slots are all the same type (pointer to said type)
448 a := 1
449 b := "string"
450 ts1 := Slot{&a, &b}
451 e := ts1.Validate()
452 if e == nil {
453 t.Error("validator accepted heteregenous types")
454 }
455 456 // slots are all pointers
457 c := 2
458 ts2 := Slot{a, c}
459 e = ts2.Validate()
460 if e == nil {
461 t.Error("validator accepted heteregenous types")
462 }
463 464 // no error!
465 ts3 := Slot{&a, &c}
466 e = ts3.Validate()
467 if e != nil {
468 t.Error("validator rejected valid contents")
469 }
470 471 }
472 473 func TestTerminates(t *testing.T) {
474 // must be empty
475 tt1 := Terminates{1}
476 e := tt1.Validate()
477 if e == nil {
478 t.Error("validator permitted content")
479 }
480 481 // is empty
482 tt2 := Terminates{}
483 e = tt2.Validate()
484 if e != nil {
485 t.Error("validator rejected valid empty Terminates")
486 }
487 488 }
489 490 func TestTri(t *testing.T) {
491 // contains at least 3 elements
492 ttr1 := Tri{1, 1}
493 if e := ttr1.Validate(); e == nil {
494 t.Error("Trigger must contain at least 3 elements")
495 }
496 // first element is a string
497 ttr2 := Tri{1, 1, 1}
498 if e := ttr2.Validate(); e == nil {
499 t.Error("first element must be a string")
500 }
501 // string is a ValidName
502 ttr3 := Tri{"a ", 1, 1}
503 if e := ttr3.Validate(); e == nil {
504 t.Error("validator accepted invalid name")
505 }
506 // contains (only) one Brief
507 ttr4 := Tri{"aaaa", Brief{""}, Brief{""}}
508 if e := ttr4.Validate(); e == nil {
509 t.Error("validator accepted more than one Brief")
510 }
511 // contains (only) one Version
512 ttr5 := Tri{"aaaa", Version{1, 1, 1}, Version{1, 1, 1}, Brief{"aaaa"}}
513 if e := ttr5.Validate(); e == nil {
514 t.Error("validator accepted more than one Version")
515 }
516 // contains (only) one Commands
517 ttr6 := Tri{"aaaa", Commands{}, Commands{}, Brief{"aaaa"}}
518 if e := ttr6.Validate(); e == nil {
519 t.Error("validator accepted more than one Commands")
520 }
521 // contains no more than one DefaultCommand
522 ttr7 := Tri{"aaaa", DefaultCommand{"commname"}, DefaultCommand{"commname"}, Brief{"aaaa"}, Commands{Command{"commname"}}}
523 if e := ttr7.Validate(); e == nil {
524 t.Error("validator accepted more than one DefaultCommand")
525 }
526 // DefaultCommand with no Commands array
527 ttr8 := Tri{"aaaa", DefaultCommand{"aaaa"}, Brief{"aaaa"}}
528 if e := ttr8.Validate(); e == nil {
529 t.Error("validator accepted DefaultCommand with no Commands present")
530 }
531 // DefaultCommand's name appears in also present Commands array
532 ttr9 := Tri{"aaaa", DefaultCommand{"commname"}, Brief{"aaaa"}, Commands{Command{"NOTcommname"}}}
533 if e := ttr9.Validate(); e == nil {
534 t.Error("validator accepted more than one DefaultCommand")
535 }
536 // contains invalid Var
537 ttr10 := Tri{"aaaa", Version{1, 1, 1}, Brief{"aaaa"}, Var{}}
538 if e := ttr10.Validate(); e == nil {
539 t.Error("validator accepted invalid Var")
540 }
541 // contains invalid Trigger
542 ttr11 := Tri{"aaaa", Version{1, 1, 1}, Brief{"aaaa"}, Trigger{}}
543 if e := ttr11.Validate(); e == nil {
544 t.Error("validator accepted invalid Trigger")
545 }
546 // contains invalid DefaultCommand
547 ttr12 := Tri{"aaaa", Version{1, 1, 1}, DefaultCommand{}, Brief{"aaaa"}, Commands{Command{"commname"}}}
548 if e := ttr12.Validate(); e == nil {
549 t.Error("validator accepted invalid DefaultCommand")
550 }
551 // contains invalid Command in Commands
552 ttr13 := Tri{"aaaa", Version{1, 1, 1}, Brief{"aaaa"}, Commands{Command{}}}
553 if e := ttr13.Validate(); e == nil {
554 t.Error("validator accepted invalid Command element in Commands")
555 }
556 // only contains element from set of possible elements
557 ttr14 := Tri{"aaaa", Version{1, 1, 1}, Brief{"aaaa"}, 1}
558 if e := ttr14.Validate(); e == nil {
559 t.Error("validator accepted invalid element in Tri")
560 }
561 // contains invalid Brief
562 ttr15 := Tri{"aaaa", Version{1, 1, 1}, Brief{1}}
563 if e := ttr15.Validate(); e == nil {
564 t.Error("validator accepted invalid Brief")
565 }
566 // contains invalid Version
567 ttr16 := Tri{"aaaa", Version{1, 1, 1, 1}, Brief{"valid brief"}}
568 if e := ttr16.Validate(); e == nil {
569 t.Error("validator accepted invalid Version")
570 }
571 // Brief is missing
572 ttr17 := Tri{"aaaa", DefaultCommand{"commname"}, Version{1, 1, 1}, Commands{Command{"commname", Brief{"valid brief"}, MakeTestHandler()}}}
573 if e := ttr17.Validate(); e == nil {
574 t.Error("validator accepted missing Brief")
575 }
576 // Version is missing
577 ttr18 := Tri{"aaaa", DefaultCommand{"commname"}, Brief{"valid brief"},
578 Commands{
579 Command{
580 "commname",
581 Brief{"valid brief"},
582 MakeTestHandler(),
583 },
584 },
585 }
586 if e := ttr18.Validate(); e == nil {
587 t.Error("validator accepted missing Version")
588 }
589 // no error!
590 ttr21 := Tri{"aaaa", DefaultCommand{"commname"}, Brief{"valid brief"},
591 Commands{
592 Command{
593 "commname",
594 Brief{"valid brief"},
595 MakeTestHandler(),
596 },
597 },
598 Version{1, 1, 1},
599 }
600 if e := ttr21.Validate(); e != nil {
601 t.Error("validator rejected valid Tri")
602 }
603 604 }
605 606 func TestTrigger(t *testing.T) {
607 // contains at least 3 elements
608 tt1 := Trigger{1, 1}
609 if e := tt1.Validate(); e == nil {
610 t.Error("Trigger must contain at least 3 elements")
611 }
612 // first is string
613 tt2 := Trigger{1, 1, 1}
614 if e := tt2.Validate(); e == nil {
615 t.Error("first element must be a string")
616 }
617 // name is ValidName
618 tt3 := Trigger{"a ", 1, 1}
619 if e := tt3.Validate(); e == nil {
620 t.Error("validator accepted invalid name")
621 }
622 // has only one Brief
623 tt4 := Trigger{"aaaa", Brief{""}, Brief{""}}
624 if e := tt4.Validate(); e == nil {
625 t.Error("validator accepted more than one Brief")
626 }
627 // has only one Short
628 tt5 := Trigger{"aaaa", Short{'a'}, Short{'a'}}
629 if e := tt5.Validate(); e == nil {
630 t.Error("validator allowed more than one Short")
631 }
632 // has only one Usage
633 tt6 := Trigger{"aaaa", Usage{""}, Usage{""}}
634 if e := tt6.Validate(); e == nil {
635 t.Error("validator allowed more than one Usage")
636 }
637 // has only one Help
638 tt7 := Trigger{"aaaa", Help{""}, Help{""}}
639 if e := tt7.Validate(); e == nil {
640 t.Error("validator allowed more than one Help")
641 }
642 // has only one handler
643 tt8 := Trigger{"name", Brief{""}, MakeTestHandler(), MakeTestHandler()}
644 if e := tt8.Validate(); e == nil {
645 t.Error("validator accepted more than one MakeTestHandler()")
646 }
647 // has only one DefaultOn
648 tt9 := Trigger{"aaaa", DefaultOn{}, DefaultOn{}}
649 if e := tt9.Validate(); e == nil {
650 t.Error("validator allowed more than one DefaultOn")
651 }
652 // has only one RunAfter
653 tt10 := Trigger{"aaaa", RunAfter{}, RunAfter{}}
654 if e := tt10.Validate(); e == nil {
655 t.Error("validator allowed more than one RunAfter")
656 }
657 // has only one Terminates
658 tt11 := Trigger{"aaaa", Terminates{}, Terminates{}}
659 if e := tt11.Validate(); e == nil {
660 t.Error("validator allowed more than one Terminates")
661 }
662 // has invalid Brief
663 tt12 := Trigger{"aaaa", Short{'a'}, Brief{1}}
664 if e := tt12.Validate(); e == nil {
665 t.Error("validator allowed invalid Brief")
666 }
667 // has invalid Short
668 tt13 := Trigger{"aaaa", Brief{"aaaa"}, Short{1}}
669 if e := tt13.Validate(); e == nil {
670 t.Error("validator allowed invalid Short")
671 }
672 // has invalid Usage
673 tt14 := Trigger{"aaaa", Brief{"aaaa"}, Usage{1}}
674 if e := tt14.Validate(); e == nil {
675 t.Error("validator allowed invalid Usage")
676 }
677 // has invalid Help
678 tt15 := Trigger{"aaaa", Brief{"aaaa"}, Help{1}}
679 if e := tt15.Validate(); e == nil {
680 t.Error("validator allowed invalid Help")
681 }
682 // has invalid MakeTestHandler()
683 handle := MakeTestHandler()
684 _ = handle
685 handle = nil
686 tt16 := Trigger{"aaaa", Brief{"aaaa"}, handle}
687 if e := tt16.Validate(); e == nil {
688 t.Error("validator allowed invalid MakeTestHandler()")
689 }
690 // has invalid DefaultOn
691 tt17 := Trigger{"aaaa", Brief{"aaaa"}, DefaultOn{1}}
692 if e := tt17.Validate(); e == nil {
693 t.Error("validator allowed invalid DefaultOn")
694 }
695 // has invalid RunAfter
696 tt18 := Trigger{"aaaa", Brief{"aaaa"}, RunAfter{1}}
697 if e := tt18.Validate(); e == nil {
698 t.Error("validator allowed invalid RunAfter")
699 }
700 // has invalid Terminates
701 tt19 := Trigger{"aaaa", Brief{"aaaa"}, Terminates{1}}
702 if e := tt19.Validate(); e == nil {
703 t.Error("validator allowed invalid Terminates")
704 }
705 706 // has one each of Brief and handler
707 tt20 := Trigger{"aaaa", Brief{"aaaa"}, Terminates{}}
708 if e := tt20.Validate(); e == nil {
709 t.Error("validator allowed Trigger without handler")
710 }
711 tt21 := Trigger{"aaaa", MakeTestHandler(), Terminates{}}
712 if e := tt21.Validate(); e == nil {
713 t.Error("validator allowed Trigger without Brief")
714 }
715 // has no other type than those foregoing
716 tt22 := Trigger{"aaaa", Brief{"aaaa"}, MakeTestHandler(), 3}
717 if e := tt22.Validate(); e == nil {
718 t.Error("validator allowed invalid element")
719 }
720 // has only one Group
721 tt23 := Trigger{"aaaa", Brief{"aaaa"}, MakeTestHandler(), Group{"aaaa"}, Group{"aaaa"}}
722 if e := tt23.Validate(); e == nil {
723 t.Error("validator allowed more than one DefaultOn")
724 }
725 // has invalid Group
726 tt24 := Trigger{"aaaa", Brief{"aaaa"}, MakeTestHandler(), Group{1}}
727 if e := tt24.Validate(); e == nil {
728 t.Error("validator allowed invalid DefaultOn")
729 }
730 // no error!
731 tt25 := Trigger{"aaaa", Brief{"aaaa"}, MakeTestHandler(), Terminates{}}
732 if e := tt25.Validate(); e != nil {
733 t.Error("validator rejected valid Trigger")
734 }
735 }
736 737 func TestUsage(t *testing.T) {
738 739 // only one element
740 tu1 := Usage{}
741 tu2 := Usage{1, 2}
742 e := tu1.Validate()
743 if e == nil {
744 t.Error("validator permitted empty Usage")
745 }
746 e = tu2.Validate()
747 if e == nil {
748 t.Error("validator permitted more than one element in Usage")
749 }
750 751 // element is string
752 tu3 := Usage{0.1}
753 e = tu3.Validate()
754 if e == nil {
755 t.Error("validator permitted element that is not a string")
756 }
757 758 // string is no more than 80 chars long
759 tu4 := Usage{
760 "123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890",
761 }
762 e = tu4.Validate()
763 if e == nil {
764 t.Error("validator permitted over 80 characters")
765 }
766 767 // string contains no control characters
768 tu5 := Usage{
769 "aaa\n",
770 }
771 e = tu5.Validate()
772 if e == nil {
773 t.Error("validator permitted control character in explainer")
774 }
775 776 // no error!
777 tu6 := Usage{
778 "aaaaaaa",
779 }
780 e = tu6.Validate()
781 if e != nil {
782 t.Error("validator rejected valid input")
783 }
784 785 }
786 787 func TestValidName(t *testing.T) {
788 789 // name is 3 or more characters long
790 tvn1 := "ab"
791 e := ValidName(tvn1)
792 if e == nil {
793 t.Error("validator accepted string under 3 characters length")
794 }
795 796 // name is only composed of letters
797 tvn2 := "ab3"
798 e = ValidName(tvn2)
799 if e == nil {
800 t.Error("validator accepted non-letter characters")
801 }
802 803 // no error!
804 tvn3 := "proper"
805 e = ValidName(tvn3)
806 if e != nil {
807 t.Error("validator rejected valid namme")
808 }
809 810 }
811 812 func TestVar(t *testing.T) {
813 // contains at least 3 elements
814 tv1 := Var{1, 1}
815 if e := tv1.Validate(); e == nil {
816 t.Error("Var must contain at least 3 elements")
817 }
818 // first is string
819 tv2 := Var{1, 1, 1}
820 if e := tv2.Validate(); e == nil {
821 t.Error("first element must be a string")
822 }
823 // name is ValidName
824 tv3 := Var{"a ", 1, 1}
825 if e := tv3.Validate(); e == nil {
826 t.Error("validator accepted invalid name")
827 }
828 // has only one Brief
829 tv4 := Var{"aaaa", Brief{""}, Brief{""}}
830 if e := tv4.Validate(); e == nil {
831 t.Error("validator accepted more than one Brief")
832 }
833 // has only one Short
834 tv5 := Var{"aaaa", Short{'a'}, Short{'a'}}
835 if e := tv5.Validate(); e == nil {
836 t.Error("validator allowed more than one Short")
837 }
838 // has only one Usage
839 tv6 := Var{"aaaa", Usage{""}, Usage{""}}
840 if e := tv6.Validate(); e == nil {
841 t.Error("validator allowed more than one Usage")
842 }
843 // has only one Help
844 tv7 := Var{"aaaa", Help{""}, Help{""}}
845 if e := tv7.Validate(); e == nil {
846 t.Error("validator allowed more than one Help")
847 }
848 // has only one Default
849 tv8 := Var{"aaaa", Default{"aaa"}, Default{"aaa"}}
850 if e := tv8.Validate(); e == nil {
851 t.Error("validator allowed more than one Default")
852 }
853 // has only one Slot
854 tstring := "valid string"
855 tv9 := Var{"aaaa", Slot{&tstring}, Slot{&tstring}}
856 if e := tv9.Validate(); e == nil {
857 t.Error("validator allowed more than one Slot")
858 }
859 // has invalid Brief
860 tv10 := Var{"aaaa", Brief{}, Short{""}}
861 if e := tv10.Validate(); e == nil {
862 t.Error("validator accepted invalid Brief")
863 }
864 // has invalid Short
865 tv11 := Var{"aaaa", Brief{"aaaa"}, Short{1}}
866 if e := tv11.Validate(); e == nil {
867 t.Error("validator allowed invalid Short")
868 }
869 // has invalid Usage
870 tv12 := Var{"aaaa", Brief{"aaaa"}, Usage{0.1}}
871 if e := tv12.Validate(); e == nil {
872 t.Error("validator allowed invalid Usage")
873 }
874 // has invalid Help
875 tv13 := Var{"aaaa", Brief{""}, Help{"aaa", 1}}
876 if e := tv13.Validate(); e == nil {
877 t.Error("validator allowed invalid Help")
878 }
879 // has invalid Default
880 tv14 := Var{"aaaa", Brief{"aaa"}, Default{1, 3}}
881 if e := tv14.Validate(); e == nil {
882 t.Error("validator allowed invalid Default")
883 }
884 // has invalid Slot
885 tv15 := Var{"aaaa", Brief{tstring}, Slot{tstring}}
886 if e := tv15.Validate(); e == nil {
887 t.Error("validator allowed invalid Slot")
888 }
889 // has one each of Brief and Slot
890 tv16 := Var{"aaaa", Brief{"aaa"}, Default{"aa"}}
891 if e := tv16.Validate(); e == nil {
892 t.Error("validator allowed absence of Brief or Slot")
893 }
894 // has no other type than those foregoing
895 tv17 := Var{"aaaa", Brief{tstring}, Slot{&tstring}, 1, MakeTestHandler()}
896 if e := tv17.Validate(); e == nil {
897 t.Error("validator rejected valid Var")
898 }
899 // has only one Group
900 tv18 := Var{"aaaa", Brief{tstring}, Slot{&tstring},
901 Group{"aaaa"}, Group{"aaaa"},
902 }
903 if e := tv18.Validate(); e == nil {
904 t.Error("validator accepted more than one Group")
905 }
906 // has invalid Group
907 tv19 := Var{"aaaa", Brief{"aaaa"}, Slot{&tstring}, Group{""}}
908 if e := tv19.Validate(); e == nil {
909 t.Error("validator allowed invalid Group")
910 }
911 // Default value is assignable to dereferenced Slot pointer
912 tv20 := Var{"aaaa", Brief{"aaaa"}, Slot{&tstring}, Default{5}}
913 if e := tv20.Validate(); e == nil {
914 t.Error("validator allowed default that can't be assigned to Slot")
915 }
916 var tint int
917 tv20 = Var{"aaaa", Brief{"aaaa"}, Slot{&tint}, Default{" "}}
918 if e := tv20.Validate(); e == nil {
919 t.Error("validator allowed default that can't be assigned to Slot")
920 }
921 var tuint32 uint32
922 tv20 = Var{"aaaa", Brief{"aaaa"}, Slot{&tuint32}, Default{0.1}}
923 if e := tv20.Validate(); e == nil {
924 t.Error("validator allowed default that can't be assigned to Slot")
925 }
926 var tfloat64 float64
927 tv20 = Var{"aaaa", Brief{"aaaa"}, Slot{&tfloat64}, Default{5}}
928 if e := tv20.Validate(); e == nil {
929 t.Error("validator allowed default that can't be assigned to Slot")
930 }
931 var tstringslice []string
932 tv20 = Var{"aaaa", Brief{"aaaa"}, Slot{&tstringslice}, Default{"aaa"}}
933 if e := tv20.Validate(); e == nil {
934 t.Error("validator allowed default that can't be assigned to Slot")
935 }
936 var tduration time.Duration
937 tv20 = Var{"aaaa", Brief{"aaaa"}, Slot{&tduration}, Default{5}}
938 if e := tv20.Validate(); e == nil {
939 t.Error("validator allowed default that can't be assigned to Slot")
940 }
941 // no error!}
942 tv21 := Var{"aaaa", Brief{tstring}, Slot{&tstring}}
943 if e := tv21.Validate(); e != nil {
944 t.Error("validator rejected valid Var")
945 }
946 947 }
948 949 func TestVersion(t *testing.T) {
950 951 // has no more than 4 fields
952 tv1 := Version{1, 2, 3, 4, 5}
953 e := tv1.Validate()
954 if e == nil {
955 t.Error("validator accepted more than three items")
956 }
957 958 // has at least 3 fields
959 tv2 := Version{4, 5}
960 e = tv2.Validate()
961 if e == nil {
962 t.Error("validator accepted less than three items")
963 }
964 965 // first three are integers
966 tv3 := Version{1.1, 2, 3, 4}
967 e = tv3.Validate()
968 if e == nil {
969 t.Error("validator accepted non-integer version numbers")
970 }
971 972 // integers are under 100
973 tv4 := Version{100, 2, 3, 4}
974 e = tv4.Validate()
975 if e == nil {
976 t.Error("validator accepted a version number over 100")
977 }
978 979 // 4th field is a string
980 tv5 := Version{10, 2, 3, 4}
981 e = tv5.Validate()
982 if e == nil {
983 t.Error("validator accepted a 4th field that is not a string")
984 }
985 986 // string contains only letters and numbers
987 tv6 := Version{10, 2, 3, "alpha3! "}
988 e = tv6.Validate()
989 if e == nil {
990 t.Error(
991 "validator accepted a 4th field that contains other than letters and numbers")
992 }
993 994 // no error!
995 tv7 := Version{10, 2, 3, "alpha3"}
996 e = tv7.Validate()
997 if e != nil {
998 t.Error(
999 "validator accepted a 4th field that contains other than letters and numbers")
1000 }
1001 1002 }
1003