package rules import ( "testing" "gitea.oblak.solutions/dimitar/gitFlow/pkg/status" ) func TestRuleValidate(t *testing.T) { good := Rule{Name: "stale", Field: "behind", Op: ">=", Value: 10, Label: "stale"} if err := good.Validate(); err != nil { t.Errorf("Validate(good): %v", err) } bad := []Rule{ {Name: "a", Field: "nope", Op: ">=", Label: "x"}, {Name: "b", Field: "behind", Op: "~", Label: "x"}, {Name: "c", Field: "behind", Op: ">=", Label: ""}, } for _, r := range bad { if err := r.Validate(); err == nil { t.Errorf("Validate(%+v) succeeded, want error", r) } } } func TestRuleMatches(t *testing.T) { repo := status.RepoInfo{BehindBy: 12, StashCount: 1, ModifiedFiles: []string{"a"}} cases := []struct { rule Rule want bool }{ {Rule{Field: "behind", Op: ">=", Value: 10}, true}, {Rule{Field: "behind", Op: ">=", Value: 13}, false}, {Rule{Field: "behind", Op: "==", Value: 12}, true}, {Rule{Field: "stash", Op: ">", Value: 0}, true}, {Rule{Field: "stash", Op: "==", Value: 0}, false}, {Rule{Field: "changes", Op: ">=", Value: 1}, true}, {Rule{Field: "ahead", Op: "==", Value: 0}, true}, {Rule{Field: "changes", Op: "!=", Value: 3}, true}, } for _, tc := range cases { got, err := tc.rule.Matches(repo) if err != nil { t.Errorf("Matches(%+v): %v", tc.rule, err) continue } if got != tc.want { t.Errorf("Matches(%+v) = %v, want %v", tc.rule, got, tc.want) } } } func TestEval(t *testing.T) { rules := []Rule{ {Name: "stale", Field: "behind", Op: ">=", Value: 10, Label: "stale"}, {Name: "dirty", Field: "changes", Op: ">=", Value: 1, Label: "dirty"}, } result := status.ScanResult{Repos: []status.RepoInfo{ {Path: "/a", BehindBy: 15}, {Path: "/b", ModifiedFiles: []string{"x"}}, {Path: "/c"}, }} flags, err := Eval(rules, result) if err != nil { t.Fatalf("Eval: %v", err) } want := []Flag{{RepoPath: "/a", Label: "stale"}, {RepoPath: "/b", Label: "dirty"}} if len(flags) != len(want) { t.Fatalf("Eval returned %d flags, want %d: %+v", len(flags), len(want), flags) } for i, f := range flags { if f != want[i] { t.Errorf("flag[%d] = %+v, want %+v", i, f, want[i]) } } } func TestEvalInvalidRule(t *testing.T) { rules := []Rule{{Name: "bad", Field: "nope", Op: ">=", Label: "x"}} if _, err := Eval(rules, status.ScanResult{}); err == nil { t.Error("Eval(invalid rule) succeeded, want error") } }