Skip to content Search Apps Documentation Source Content Run Reset Eval Playground File Folder Download Copy Actions State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

Community realm. Read the code first. Community realm, deployed by its author. Read the code before you interact or send coins.

rule_test.gno

1.65 Kb · 55 lines
 1package rulecalc
 2
 3import "testing"
 4
 5func validRule() Rule {
 6	return Rule{
 7		Method:   WeightedSum,
 8		Criteria: []Criterion{{Name: "security", Weight: 3}},
 9		Quorum:   2,
10		Tie:      LowestID,
11	}
12}
13
14func TestValidateOK(t *testing.T) {
15	if err := validRule().Validate(3); err != nil {
16		t.Fatalf("valid rule rejected: %v", err)
17	}
18	r := validRule()
19	r.Method = MajorityFirst
20	r.Tie = Refund
21	r.Criteria = []Criterion{{"a", 1}, {"b", 10}, {"c", 5}, {"d", 2}}
22	r.Quorum = 3
23	if err := r.Validate(3); err != nil {
24		t.Fatalf("valid rule (4 criteria, quorum == evaluators) rejected: %v", err)
25	}
26}
27
28func TestValidateRejects(t *testing.T) {
29	cases := []struct {
30		name string
31		mod  func(r *Rule)
32	}{
33		{"no criteria", func(r *Rule) { r.Criteria = nil }},
34		{"five criteria", func(r *Rule) {
35			r.Criteria = []Criterion{{"a", 1}, {"b", 1}, {"c", 1}, {"d", 1}, {"e", 1}}
36		}},
37		{"weight 0", func(r *Rule) { r.Criteria = []Criterion{{"a", 0}} }},
38		{"weight 11", func(r *Rule) { r.Criteria = []Criterion{{"a", 11}} }},
39		{"empty name", func(r *Rule) { r.Criteria = []Criterion{{"", 1}} }},
40		{"duplicate name", func(r *Rule) { r.Criteria = []Criterion{{"a", 1}, {"a", 2}} }},
41		{"quorum 0", func(r *Rule) { r.Quorum = 0 }},
42		{"quorum above evaluators", func(r *Rule) { r.Quorum = 4 }},
43		{"unknown method", func(r *Rule) { r.Method = Method(0) }},
44		{"unknown method high", func(r *Rule) { r.Method = Method(3) }},
45		{"unknown tie", func(r *Rule) { r.Tie = TieBreak(0) }},
46		{"unknown tie high", func(r *Rule) { r.Tie = TieBreak(3) }},
47	}
48	for _, c := range cases {
49		r := validRule()
50		c.mod(&r)
51		if err := r.Validate(3); err == nil {
52			t.Errorf("%s: expected error, got nil", c.name)
53		}
54	}
55}