1
2
3
4
5 package gotestchecker_test
6
7 import (
8 "go/analysis"
9 "slices"
10 "testing"
11
12 "internal/analysis/gotestchecker"
13 )
14
15
16
17
18
19
20 func TestAnalyzers(t *testing.T) {
21
22 d := newAnalyzer("d")
23 b := newAnalyzer("b", d)
24 c := newAnalyzer("c", d)
25 a := newAnalyzer("a", b, c)
26
27
28
29 p := newAnalyzer("p")
30 noRun := newAnalyzer("norun", p)
31 noRun.Run = nil
32 badName := newAnalyzer("bad-name", p)
33 cycle1 := newAnalyzer("cycle1")
34 cycle2 := newAnalyzer("cycle2", cycle1)
35 cycle1.Requires = []*analysis.Analyzer{cycle2}
36 nilReq := newAnalyzer("nilreq", nil)
37 valueFact := newAnalyzer("valuefact")
38 valueFact.FactTypes = []analysis.Fact{nonPointerFact{}}
39 badReq := newAnalyzer("badreq", noRun)
40
41 for _, test := range []struct {
42 name string
43 vars []any
44 want []string
45 }{
46 {"single", {d}, {"d"}},
47 {"closure", {a}, {"d", "b", "c", "a"}},
48 {"shared prerequisite", {b, c}, {"d", "b", "c"}},
49 {"var order", {c, a}, {"d", "c", "b", "a"}},
50 {"repeated var", {a, a}, {"d", "b", "c", "a"}},
51 {"suite", {[]*analysis.Analyzer{c, b}}, {"d", "c", "b"}},
52 {"suite with repeated element", {[]*analysis.Analyzer{b, b, d}}, {"d", "b"}},
53 {"suite and analyzer", {[]*analysis.Analyzer{b}, a}, {"d", "b", "c", "a"}},
54 {"empty suite", {[]*analysis.Analyzer{}}, nil},
55
56
57
58 {"nil Run", {noRun, b}, {"d", "b"}},
59 {"invalid name", {badName, b}, {"d", "b"}},
60 {"cycle", {cycle1, b}, {"d", "b"}},
61 {"nil prerequisite", {nilReq, b}, {"d", "b"}},
62 {"non-pointer fact type", {valueFact, b}, {"d", "b"}},
63 {"invalid prerequisite", {badReq, b}, {"d", "b"}},
64 {"invalid suite member", {[]*analysis.Analyzer{noRun, b}}, {"d", "b"}},
65 {"valid use of invalid analyzer's prerequisite", {noRun, p}, {"p"}},
66
67
68 {"nil analyzer", {(*analysis.Analyzer)(nil), b}, {"d", "b"}},
69 {"nil suite element", {[]*analysis.Analyzer{b, nil}, c}, {"d", "c"}},
70 {"suite with name clash", {[]*analysis.Analyzer{b, newAnalyzer("b")}, c}, {"d", "c"}},
71 {"wrong type", {42, b}, {"d", "b"}},
72 } {
73 t.Run(test.name, func(t *testing.T) {
74 var vars []gotestchecker.Var
75 for _, v := range test.vars {
76 vars = append(vars, gotestchecker.Var{Symbol: "example.com/p.V", Value: v})
77 }
78 var got []string
79 for _, a := range gotestchecker.Analyzers(vars) {
80 got = append(got, a.Name)
81 }
82 if !slices.Equal(got, test.want) {
83 t.Errorf("got %q, want %q", got, test.want)
84 }
85 })
86 }
87 }
88
89
90
91 func newAnalyzer(name string, requires ...*analysis.Analyzer) *analysis.Analyzer {
92 return &analysis.Analyzer{
93 Name: name,
94 Doc: "doc",
95 Requires: requires,
96 Run: func(*analysis.Pass) (any, error) { return nil, nil },
97 }
98 }
99
100
101 type nonPointerFact struct{}
102
103 func (nonPointerFact) AFact() {}
104
View as plain text