Browse Source

ah, forgot group for vars and triggers!

master
Loki Verloren 7 months ago
parent
commit
6daaa97296
9 changed files with 205 additions and 119 deletions
  1. 36
    22
      cmd/example/example.go
  2. 10
    0
      cmd/example/main.go
  3. 5
    1
      doc/checklist.md
  4. 0
    7
      parser.go
  5. 0
    7
      scanner.go
  6. 0
    9
      scanner_test.go
  7. 106
    46
      validators.go
  8. 48
    25
      validators_test.go
  9. 0
    2
      variables.go

example_test.go_ → cmd/example/example.go View File

@@ -1,72 +1,86 @@
1
-package tri
1
+package main
2 2
 
3 3
 import (
4
-	"fmt"
4
+	. "git.parallelcoin.io/tri"
5 5
 )
6 6
 
7
+type exampleConf struct {
8
+	datadir string
9
+
10
+}
11
+
12
+var cfg = exampleConf{
13
+	datadir: "/not/the/default/path",
14
+}
15
+
16
+// dot import used on tri as it should be for declarations, or every damned name has to have tri. in front of it. This is ok in other places, but not here, as you can obviously see:
7 17
 var exampleTri = Tri{
8 18
 	"appname",
9 19
 	Brief{"brief"},
10 20
 	Version{0, 1, 1, "alpha"},
11
-	DefaultCommand{"help"},
21
+	DefaultCommand{"ctl"},
12 22
 	Var{"datadir",
13
-		Short{"d"},
23
+		Short{'d'},
14 24
 		Brief{"brief"},
15 25
 		Usage{"usage"},
16 26
 		Help{"help"},
17 27
 		Default{"~/.pod"},
18
-		Slot{""},
28
+		Slot{&cfg.datadir},
29
+	},
30
+	Trigger{"init",
31
+		Short{'I'},
32
+		Brief{"brief"},
33
+		Usage{"usage"},
34
+		Help{"help"},
35
+		DefaultOn{},
36
+		RunAfter{},
37
+		func(*Tri) int {
38
+			return 0
39
+		},
19 40
 	},
20 41
 	Commands{
21 42
 		{"ctl",
22
-			Short{"c"},
43
+			Short{'c'},
23 44
 			Brief{"brief"},
24 45
 			Usage{"usage"},
25 46
 			Help{"help"},
26
-			Group{"groupname"},
27 47
 			Examples{
28 48
 				"example 1", "explaining text",
29 49
 				"example 2", "explaining text",
30 50
 			},
31 51
 			Var{"datadir",
32
-				Short{"d"},
52
+				Short{'d'},
33 53
 				Brief{"brief"},
34 54
 				Usage{"usage"},
35 55
 				Help{"help"},
36 56
 				Default{"~/.pod"},
37
-				Slot{""},
57
+				Group{"groupname"},
58
+				Slot{&cfg.datadir},
38 59
 			},
39
-			Trigger{"init",
40
-				Short{"I"},
60
+			Trigger{"wallet",
61
+				Short{'w'},
41 62
 				Brief{"brief"},
42 63
 				Usage{"usage"},
43 64
 				Help{"help"},
44 65
 				DefaultOn{},
45 66
 				Terminates{},
46
-				RunAfter{},
47
-				func(Tri) int {
67
+				func(*Tri) int {
48 68
 					return 0
49 69
 				},
50 70
 			},
51
-			func(Tri) int {
71
+			func(*Tri) int {
52 72
 				return 0
53 73
 			},
54 74
 		},
55 75
 		{"node",
56
-			Short{"n"},
76
+			Short{'n'},
57 77
 			Brief{"brief"},
58 78
 			Usage{"usage"},
59 79
 			Help{"help"},
60 80
 			Examples{"example1", "example2"},
61
-			func(Tri) int {
81
+			func(*Tri) int {
62 82
 				return 0
63 83
 			},
64 84
 		},
65 85
 	},
66 86
 }
67
-
68
-// selftest is just a self test to make golint not tell me about unused things
69
-func selftest() {
70
-	brief := exampleTri[1].(Brief)
71
-	fmt.Println(brief[0])
72
-}

+ 10
- 0
cmd/example/main.go View File

@@ -0,0 +1,10 @@
1
+package main
2
+
3
+import (
4
+	"fmt"
5
+)
6
+
7
+func main() {
8
+	e := exampleTri.Validate()
9
+	fmt.Println(e)
10
+}

+ 5
- 1
doc/checklist.md View File

@@ -175,6 +175,8 @@ Each validator has several error conditions, so each of them are elaborated unde
175 175
       - [x] has invalid RunAfter
176 176
       - [x] has one each of Brief and handler
177 177
       - [x] has no other type than those foregoing
178
+      - [x] has only one Group
179
+      - [x] has invalid Group
178 180
       - [x] no error!
179 181
 
180 182
    - [x] `Usage.Validate()`
@@ -204,7 +206,9 @@ Each validator has several error conditions, so each of them are elaborated unde
204 206
       - [x] has invalid Slot
205 207
       - [x] has one each of Brief and Slot
206 208
       - [x] has no other type than those foregoing
207
-      - [ ] Default value is assignable to dereferenced Slot pointer
209
+      - [x] Default value is assignable to dereferenced Slot pointer (NOTE: only one of the 6 available types is tested - write test later, it's all copypasta)
210
+      - [x] has only one Group
211
+      - [x] has invalid Group
208 212
       - [x] no error!
209 213
 
210 214
    - [x] `Version.Validate()`

+ 0
- 7
parser.go View File

@@ -1,7 +0,0 @@
1
-package tri
2
-
3
-// 
4
-
5
-func ParseVar(v *Var, in interface{}) error {
6
-	return nil
7
-}

+ 0
- 7
scanner.go View File

@@ -1,7 +0,0 @@
1
-package tri
2
-
3
-// Scanner takes a slice of strings, usually from os.Args, recognises the name, parses the value it defines (if any), loads it into the Tri and returns an error if values are not correct to type or not found in the Tri
4
-func (t *Tri) Scanner(args []string) error {
5
-
6
-	return nil
7
-}

+ 0
- 9
scanner_test.go View File

@@ -1,9 +0,0 @@
1
-package tri
2
-
3
-import (
4
-	"testing"
5
-)
6
-
7
-func TestScanner(t *testing.T) {
8
-
9
-}

+ 106
- 46
validators.go View File

@@ -1,6 +1,7 @@
1 1
 package tri
2 2
 
3 3
 import (
4
+	"time"
4 5
 	"reflect"
5 6
 	"errors"
6 7
 	"fmt"
@@ -35,7 +36,7 @@ func (r *Brief) Validate() error {
35 36
 func (r *Command) Validate() error {
36 37
 
37 38
 	R := *r
38
-	if len(R)<1 {
39
+	if len(R) < 1 {
39 40
 		return errors.New("empty Command")
40 41
 	}
41 42
 	s, ok := R[0].(string)
@@ -239,7 +240,7 @@ func (r *Group) Validate() error {
239 240
 		return errors.New("Group element must be a string")
240 241
 	}
241 242
 	if e := ValidName(s); e != nil {
242
-		return fmt.Errorf("error in name of Command: %v", e)
243
+		return fmt.Errorf("error in name of Group: %v", e)
243 244
 	}
244 245
 	return nil
245 246
 }
@@ -335,7 +336,7 @@ func (r *Tri) Validate() error {
335 336
 	// validSet is an array of 4 elements that represent the presence of the 4 mandatory parts.
336 337
 	var validSet [2]bool
337 338
 	brief, version := 0, 1
338
-	var singleSet [2]bool
339
+	var singleSet [3]bool
339 340
 	defcom, commands := 0, 1
340 341
 	n, ok := R[0].(string)
341 342
 	if !ok {
@@ -365,7 +366,7 @@ func (r *Tri) Validate() error {
365 366
 				return fmt.Errorf(
366 367
 					"Tri contains more than one Version, second found at index %d", i)
367 368
 			}
368
-			validSet[version]=true
369
+			validSet[version] = true
369 370
 			if e := y.Validate(); e != nil {
370 371
 				return fmt.Errorf("Tri field %d: %s", i, e)
371 372
 			}
@@ -390,38 +391,37 @@ func (r *Tri) Validate() error {
390 391
 			if e != nil {
391 392
 				return fmt.Errorf("error in Tri at index %d: %v", i, e)
392 393
 			}
393
-			
394 394
 		case DefaultCommand:
395 395
 			if singleSet[defcom] {
396 396
 				return fmt.Errorf(
397 397
 					"Tri contains more than one DefaultCommand, second found at index %d", i)
398
-				}
399
-				singleSet[defcom] = true
400
-				e := y.Validate()
401
-				if e != nil {
402
-					return fmt.Errorf("Tri field %d: %s", i, e)
403
-				}
404
-				commname := y[0].(string)
405
-				// DefaultCommand must match in its name one of the Command items in also present Commands array
406
-				foundComm := false
407
-				foundDefComm := false
408
-				for _, a := range R {
409
-					switch c := a.(type) {
410
-					case Commands:
411
-						foundComm = true
412
-						for _, b := range c {
413
-							if b[0].(string) == commname {
414
-								foundDefComm = true
415
-							}
398
+			}
399
+			singleSet[defcom] = true
400
+			e := y.Validate()
401
+			if e != nil {
402
+				return fmt.Errorf("Tri field %d: %s", i, e)
403
+			}
404
+			commname := y[0].(string)
405
+			// DefaultCommand must match in its name one of the Command items in also present Commands array
406
+			foundComm := false
407
+			foundDefComm := false
408
+			for _, a := range R {
409
+				switch c := a.(type) {
410
+				case Commands:
411
+					foundComm = true
412
+					for _, b := range c {
413
+						if b[0].(string) == commname {
414
+							foundDefComm = true
416 415
 						}
417
-					default:
418 416
 					}
417
+				default:
419 418
 				}
420
-				if !foundComm {
421
-					return errors.New("DefaultCommand with no Commands array present")
422
-				} else if !foundDefComm {
423
-						return errors.New("DefaultCommand found with no matching Command")
424
-				}
419
+			}
420
+			if !foundComm {
421
+				return errors.New("DefaultCommand with no Commands array present")
422
+			} else if !foundDefComm {
423
+				return errors.New("DefaultCommand found with no matching Command")
424
+			}
425 425
 
426 426
 		default:
427 427
 			return fmt.Errorf(
@@ -455,8 +455,8 @@ func (r *Trigger) Validate() error {
455 455
 	// validSet is an array that represent the presence of the mandatory parts.
456 456
 	var validSet [2]bool
457 457
 	brief, handler := 0, 1
458
-	var singleSet [6]bool
459
-	short, usage, help, defon, terminates, runafter := 0, 1, 2, 3, 4, 5
458
+	var singleSet [7]bool
459
+	short, usage, help, defon, terminates, runafter, group := 0, 1, 2, 3, 4, 5, 6
460 460
 	for i, x := range R[1:] {
461 461
 
462 462
 		switch y := x.(type) {
@@ -469,7 +469,7 @@ func (r *Trigger) Validate() error {
469 469
 			}
470 470
 			if e := y.Validate(); e != nil {
471 471
 				return fmt.Errorf(
472
-					"Trigger contains invalid element at %d :%s", i, e)
472
+					"Trigger contains invalid element at %d - %s", i, e)
473 473
 			}
474 474
 
475 475
 		case func(*Tri) int:
@@ -490,7 +490,7 @@ func (r *Trigger) Validate() error {
490 490
 			singleSet[short] = true
491 491
 			if e := y.Validate(); e != nil {
492 492
 				return fmt.Errorf(
493
-					"Trigger contains invalid element at %d :%s", i, e)
493
+					"Trigger contains invalid element at %d - %s", i, e)
494 494
 			}
495 495
 
496 496
 		case Usage:
@@ -500,7 +500,7 @@ func (r *Trigger) Validate() error {
500 500
 			singleSet[usage] = true
501 501
 			if e := y.Validate(); e != nil {
502 502
 				return fmt.Errorf(
503
-					"Trigger contains invalid element at %d :%s", i, e)
503
+					"Trigger contains invalid element at %d - %s", i, e)
504 504
 			}
505 505
 
506 506
 		case Help:
@@ -510,7 +510,7 @@ func (r *Trigger) Validate() error {
510 510
 			singleSet[help] = true
511 511
 			if e := y.Validate(); e != nil {
512 512
 				return fmt.Errorf(
513
-					"Trigger contains invalid element at %d :%s", i, e)
513
+					"Trigger contains invalid element at %d - %s", i, e)
514 514
 			}
515 515
 
516 516
 		case DefaultOn:
@@ -520,7 +520,7 @@ func (r *Trigger) Validate() error {
520 520
 			singleSet[defon] = true
521 521
 			if e := y.Validate(); e != nil {
522 522
 				return fmt.Errorf(
523
-					"Trigger contains invalid element at %d :%s", i, e)
523
+					"Trigger contains invalid element at %d - %s", i, e)
524 524
 			}
525 525
 
526 526
 		case Terminates:
@@ -530,7 +530,7 @@ func (r *Trigger) Validate() error {
530 530
 			singleSet[terminates] = true
531 531
 			if e := y.Validate(); e != nil {
532 532
 				return fmt.Errorf(
533
-					"Trigger contains invalid element at %d :%s", i, e)
533
+					"Trigger contains invalid element at %d - %s", i, e)
534 534
 			}
535 535
 
536 536
 		case RunAfter:
@@ -540,7 +540,18 @@ func (r *Trigger) Validate() error {
540 540
 			singleSet[runafter] = true
541 541
 			if e := y.Validate(); e != nil {
542 542
 				return fmt.Errorf(
543
-					"Trigger contains invalid element at %d :%s", i, e)
543
+					"Trigger contains invalid element at %d - %s", i, e)
544
+			}
545
+
546
+		case Group:
547
+			if singleSet[group] {
548
+				return fmt.Errorf(
549
+					"Trigger may only contain one Group, extra found at index %d", i)
550
+			}
551
+			singleSet[group] = true
552
+			if e := y.Validate(); e != nil {
553
+				return fmt.Errorf(
554
+					"Trigger contains invalid element at %d - %s", i, e)
544 555
 			}
545 556
 
546 557
 		default:
@@ -596,8 +607,8 @@ func (r *Var) Validate() error {
596 607
 	var validSet [2]bool
597 608
 	brief, slot := 0, 1
598 609
 	// singleSet is an array representing the optional elements that may not be more than one inside a Var
599
-	var singleSet [4]bool
600
-	short, usage, help, def := 0, 1, 2, 3
610
+	var singleSet [5]bool
611
+	short, usage, help, def, group := 0, 1, 2, 3, 4
601 612
 	for i, x := range R[1:] {
602 613
 
603 614
 		switch y := x.(type) {
@@ -610,7 +621,7 @@ func (r *Var) Validate() error {
610 621
 			}
611 622
 			if e := y.Validate(); e != nil {
612 623
 				return fmt.Errorf(
613
-					"Var contains invalid element at %d :%s", i, e)
624
+					"Var contains invalid element at %d - %s", i, e)
614 625
 			}
615 626
 
616 627
 		case Short:
@@ -620,7 +631,7 @@ func (r *Var) Validate() error {
620 631
 			singleSet[short] = true
621 632
 			if e := y.Validate(); e != nil {
622 633
 				return fmt.Errorf(
623
-					"Var contains invalid element at %d :%s", i, e)
634
+					"Var contains invalid element at %d - %s", i, e)
624 635
 			}
625 636
 
626 637
 		case Usage:
@@ -630,7 +641,7 @@ func (r *Var) Validate() error {
630 641
 			singleSet[usage] = true
631 642
 			if e := y.Validate(); e != nil {
632 643
 				return fmt.Errorf(
633
-					"Var contains invalid element at %d :%s", i, e)
644
+					"Var contains invalid element at %d - %s", i, e)
634 645
 			}
635 646
 
636 647
 		case Help:
@@ -640,7 +651,7 @@ func (r *Var) Validate() error {
640 651
 			singleSet[help] = true
641 652
 			if e := y.Validate(); e != nil {
642 653
 				return fmt.Errorf(
643
-					"Var contains invalid element at %d :%s", i, e)
654
+					"Var contains invalid element at %d - %s", i, e)
644 655
 			}
645 656
 
646 657
 		case Default:
@@ -650,7 +661,46 @@ func (r *Var) Validate() error {
650 661
 			singleSet[def] = true
651 662
 			if e := y.Validate(); e != nil {
652 663
 				return fmt.Errorf(
653
-					"Var contains invalid element at %d :%s", i, e)
664
+					"Var contains invalid element at %d - %s", i, e)
665
+			}
666
+			for _, z := range R {
667
+				s, ok := z.(Slot)
668
+				if ok {
669
+					switch S := s[0].(type) {
670
+					case *string:
671
+						ss, ok := y[0].(string)
672
+						if !ok {
673
+							return errors.New("slot is not same type as default")
674
+						}
675
+						*S = ss
676
+					case *int:
677
+						_, ok := y[0].(int)
678
+						if !ok {
679
+							return errors.New("slot is not same type as default")
680
+						}
681
+					case *uint32:
682
+						_, ok := y[0].(uint32)
683
+						if !ok {
684
+							return errors.New("slot is not same type as default")
685
+						}
686
+					case *float64:
687
+						_, ok := y[0].(float64)
688
+						if !ok {
689
+							return errors.New("slot is not same type as default")
690
+						}
691
+					case *[]string:
692
+						_, ok := y[0].([]string)
693
+						if !ok {
694
+							return errors.New("slot is not same type as default")
695
+						}
696
+					case *time.Duration:
697
+						_, ok := y[0].(time.Duration)
698
+						if !ok {
699
+							return errors.New("slot is not same type as default")
700
+						}
701
+					}
702
+					// *s[0] = *y[0]
703
+				}
654 704
 			}
655 705
 
656 706
 		case Slot:
@@ -660,9 +710,19 @@ func (r *Var) Validate() error {
660 710
 			validSet[slot] = true
661 711
 			if e := y.Validate(); e != nil {
662 712
 				return fmt.Errorf(
663
-					"Var contains invalid element at %d :%s", i, e)
713
+					"Var contains invalid element at %d - %s", i, e)
664 714
 			}
665 715
 
716
+		case Group:
717
+			if singleSet[group] {
718
+				return fmt.Errorf(
719
+					"Var may only contain one Group, extra found at index %d", i)
720
+			}
721
+			singleSet[group] = true
722
+			if e := y.Validate(); e != nil {
723
+				return fmt.Errorf(
724
+					"Var contains invalid element at %d - %s", i, e)
725
+			}
666 726
 		default:
667 727
 			return fmt.Errorf(
668 728
 				"found invalid item type at element %d in a Var", i)

+ 48
- 25
validators_test.go View File

@@ -175,10 +175,8 @@ func TestCommand(t *testing.T) {
175 175
 		t.Error("validator rejected valid Command")
176 176
 	}
177 177
 }
178
-
179
-
180 178
 func TestCommands(t *testing.T) {
181
-	tcc1 := Commands{		Command{"name", Brief{""}, MakeTestHandler(), 1}	}
179
+	tcc1 := Commands{Command{"name", Brief{""}, MakeTestHandler(), 1}}
182 180
 	if e := tcc1.Validate(); e == nil {
183 181
 		t.Error("validator accepted Commands with invalid element")
184 182
 	}
@@ -510,7 +508,7 @@ func TestTri(t *testing.T) {
510 508
 		t.Error("validator accepted more than one Brief")
511 509
 	}
512 510
 	// contains (only) one Version
513
-	ttr5 := Tri{"aaaa", Version{1,1,1}, Version{1,1,1}, Brief{"aaaa"}}
511
+	ttr5 := Tri{"aaaa", Version{1, 1, 1}, Version{1, 1, 1}, Brief{"aaaa"}}
514 512
 	if e := ttr5.Validate(); e == nil {
515 513
 		t.Error("validator accepted more than one Version")
516 514
 	}
@@ -525,7 +523,7 @@ func TestTri(t *testing.T) {
525 523
 		t.Error("validator accepted more than one DefaultCommand")
526 524
 	}
527 525
 	// DefaultCommand with no Commands array
528
-	ttr8 := Tri{"aaaa", DefaultCommand{"aaaa"}, Brief{"aaaa"} }
526
+	ttr8 := Tri{"aaaa", DefaultCommand{"aaaa"}, Brief{"aaaa"}}
529 527
 	if e := ttr8.Validate(); e == nil {
530 528
 		t.Error("validator accepted DefaultCommand with no Commands present")
531 529
 	}
@@ -535,42 +533,42 @@ func TestTri(t *testing.T) {
535 533
 		t.Error("validator accepted more than one DefaultCommand")
536 534
 	}
537 535
 	// contains invalid Var
538
-	ttr10 := Tri{"aaaa", Version{1,1,1}, Brief{"aaaa"}, Var{}}
536
+	ttr10 := Tri{"aaaa", Version{1, 1, 1}, Brief{"aaaa"}, Var{}}
539 537
 	if e := ttr10.Validate(); e == nil {
540 538
 		t.Error("validator accepted invalid Var")
541 539
 	}
542 540
 	// contains invalid Trigger
543
-	ttr11 := Tri{"aaaa", Version{1,1,1}, Brief{"aaaa"}, Trigger{}}
541
+	ttr11 := Tri{"aaaa", Version{1, 1, 1}, Brief{"aaaa"}, Trigger{}}
544 542
 	if e := ttr11.Validate(); e == nil {
545 543
 		t.Error("validator accepted invalid Trigger")
546 544
 	}
547 545
 	// contains invalid DefaultCommand
548
-	ttr12 := Tri{"aaaa", Version{1,1,1}, DefaultCommand{}, Brief{"aaaa"}, Commands{Command{"commname"}}}
546
+	ttr12 := Tri{"aaaa", Version{1, 1, 1}, DefaultCommand{}, Brief{"aaaa"}, Commands{Command{"commname"}}}
549 547
 	if e := ttr12.Validate(); e == nil {
550 548
 		t.Error("validator accepted invalid DefaultCommand")
551 549
 	}
552 550
 	// contains invalid Command in Commands
553
-	ttr13 := Tri{"aaaa", Version{1,1,1}, Brief{"aaaa"}, Commands{Command{}}}
551
+	ttr13 := Tri{"aaaa", Version{1, 1, 1}, Brief{"aaaa"}, Commands{Command{}}}
554 552
 	if e := ttr13.Validate(); e == nil {
555 553
 		t.Error("validator accepted invalid Command element in Commands")
556 554
 	}
557 555
 	// only contains element from set of possible elements
558
-	ttr14 := Tri{"aaaa", Version{1,1,1}, Brief{"aaaa"},1}
556
+	ttr14 := Tri{"aaaa", Version{1, 1, 1}, Brief{"aaaa"}, 1}
559 557
 	if e := ttr14.Validate(); e == nil {
560 558
 		t.Error("validator accepted invalid element in Tri")
561 559
 	}
562 560
 	// contains invalid Brief
563
-	ttr15 := Tri{"aaaa", Version{1,1,1}, Brief{1}}
561
+	ttr15 := Tri{"aaaa", Version{1, 1, 1}, Brief{1}}
564 562
 	if e := ttr15.Validate(); e == nil {
565 563
 		t.Error("validator accepted invalid Brief")
566 564
 	}
567 565
 	// contains invalid Version
568
-	ttr16 := Tri{"aaaa", Version{1,1,1,1}, Brief{"valid brief"}}
566
+	ttr16 := Tri{"aaaa", Version{1, 1, 1, 1}, Brief{"valid brief"}}
569 567
 	if e := ttr16.Validate(); e == nil {
570 568
 		t.Error("validator accepted invalid Version")
571 569
 	}
572 570
 	// Brief is missing
573
-	ttr17 := Tri{"aaaa", DefaultCommand{"commname"}, Version{1,1,1}, Commands{Command{"commname",	Brief{"valid brief"},MakeTestHandler()}}}
571
+	ttr17 := Tri{"aaaa", DefaultCommand{"commname"}, Version{1, 1, 1}, Commands{Command{"commname", Brief{"valid brief"}, MakeTestHandler()}}}
574 572
 	if e := ttr17.Validate(); e == nil {
575 573
 		t.Error("validator accepted missing Brief")
576 574
 	}
@@ -588,7 +586,7 @@ func TestTri(t *testing.T) {
588 586
 		t.Error("validator accepted missing Version")
589 587
 	}
590 588
 	// no error!
591
-	ttr19 := Tri{"aaaa", DefaultCommand{"commname"}, Brief{"valid brief"},
589
+	ttr21 := Tri{"aaaa", DefaultCommand{"commname"}, Brief{"valid brief"},
592 590
 		Commands{
593 591
 			Command{
594 592
 				"commname",
@@ -596,9 +594,9 @@ func TestTri(t *testing.T) {
596 594
 				MakeTestHandler(),
597 595
 			},
598 596
 		},
599
-		Version{1,1,1},
597
+		Version{1, 1, 1},
600 598
 	}
601
-	if e := ttr19.Validate(); e != nil {
599
+	if e := ttr21.Validate(); e != nil {
602 600
 		t.Error("validator rejected valid Tri")
603 601
 	}
604 602
 
@@ -682,8 +680,8 @@ func TestTrigger(t *testing.T) {
682 680
 	}
683 681
 	// has invalid MakeTestHandler()
684 682
 	handle := MakeTestHandler()
685
-	_=handle
686
-	handle=nil
683
+	_ = handle
684
+	handle = nil
687 685
 	tt16 := Trigger{"aaaa", Brief{"aaaa"}, handle}
688 686
 	if e := tt16.Validate(); e == nil {
689 687
 		t.Error("validator allowed invalid MakeTestHandler()")
@@ -718,9 +716,19 @@ func TestTrigger(t *testing.T) {
718 716
 	if e := tt22.Validate(); e == nil {
719 717
 		t.Error("validator allowed invalid element")
720 718
 	}
719
+	// has only one Group
720
+	tt23 := Trigger{"aaaa", Brief{"aaaa"}, MakeTestHandler(), Group{"aaaa"}, Group{"aaaa"}}
721
+	if e := tt23.Validate(); e == nil {
722
+		t.Error("validator allowed more than one DefaultOn")
723
+	}
724
+	// has invalid Group
725
+	tt24 := Trigger{"aaaa", Brief{"aaaa"}, MakeTestHandler(), Group{1}}
726
+	if e := tt24.Validate(); e == nil {
727
+		t.Error("validator allowed invalid DefaultOn")
728
+	}
721 729
 	// no error!
722
-	tt23 := Trigger{"aaaa", Brief{"aaaa"}, MakeTestHandler(), Terminates{}}
723
-	if e := tt23.Validate(); e != nil {
730
+	tt25 := Trigger{"aaaa", Brief{"aaaa"}, MakeTestHandler(), Terminates{}}
731
+	if e := tt25.Validate(); e != nil {
724 732
 		t.Error("validator rejected valid Trigger")
725 733
 	}
726 734
 }
@@ -882,16 +890,31 @@ func TestVar(t *testing.T) {
882 890
 	if e := tv16.Validate(); e == nil {
883 891
 		t.Error("validator allowed absence of Brief or Slot")
884 892
 	}
885
-
886 893
 	// has no other type than those foregoing
887
-	tv17 := Var{"aaaa", Brief{tstring}, Slot{&tstring}, 1}
894
+	tv17 := Var{"aaaa", Brief{tstring}, Slot{&tstring}, 1, MakeTestHandler()}
888 895
 	if e := tv17.Validate(); e == nil {
889 896
 		t.Error("validator rejected valid Var")
890 897
 	}
891
-
898
+	// has only one Group
899
+	tv18 := Var{"aaaa", Brief{tstring}, Slot{&tstring},
900
+		Group{"aaaa"}, Group{"aaaa"},
901
+	}
902
+	if e := tv18.Validate(); e == nil {
903
+		t.Error("validator accepted more than one Group")
904
+	}
905
+	// has invalid Group
906
+	tv19 := Var{"aaaa", Brief{"aaaa"}, Slot{&tstring}, Group{""}}
907
+	if e := tv19.Validate(); e == nil {
908
+		t.Error("validator allowed invalid Group")
909
+	}
910
+	// Default value is assignable to dereferenced Slot pointer
911
+	tv20 := Var{"aaaa", Brief{"aaaa"}, Slot{&tstring}, Default{5}}
912
+	if e := tv20.Validate(); e == nil {
913
+		t.Error("validator allowed default that can't be assigned to Slot")
914
+	}
892 915
 	// no error!}
893
-	tv18 := Var{"aaaa", Brief{tstring}, Slot{&tstring}}
894
-	if e := tv18.Validate(); e != nil {
916
+	tv21 := Var{"aaaa", Brief{tstring}, Slot{&tstring}}
917
+	if e := tv21.Validate(); e != nil {
895 918
 		t.Error("validator rejected valid Var")
896 919
 	}
897 920
 

+ 0
- 2
variables.go View File

@@ -1,2 +0,0 @@
1
-package tri
2
-

Loading…
Cancel
Save