Source file src/internal/analysis/gotestchecker/describe_test.go

     1  // Copyright 2026 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package gotestchecker_test
     6  
     7  import (
     8  	"go/analysis"
     9  	"slices"
    10  	"testing"
    11  
    12  	"internal/analysis/gotestchecker"
    13  )
    14  
    15  // TestAnalyzers checks the graph of analyzers reachable from the
    16  // variables passed to Main: its canonical order (each analyzer after
    17  // its prerequisites, and each appearing once), and the exclusion of
    18  // invalid variables and invalid members, along with the Requires
    19  // closure of each, without affecting their valid siblings.
    20  func TestAnalyzers(t *testing.T) {
    21  	// Requires: a -> {b, c}, b -> d, c -> d.
    22  	d := newAnalyzer("d")
    23  	b := newAnalyzer("b", d)
    24  	c := newAnalyzer("c", d)
    25  	a := newAnalyzer("a", b, c)
    26  
    27  	// Invalid analyzers. Each is invalid only because of its own
    28  	// properties or those of its Requires closure.
    29  	p := newAnalyzer("p") // a valid prerequisite of invalid analyzers
    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) // invalid by way of its prerequisite
    40  
    41  	for _, test := range []struct {
    42  		name string
    43  		vars []any // values of the variables
    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  		// Invalid members are dropped, with their closures,
    57  		// but not their valid siblings.
    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  		// Invalid variables contribute nothing.
    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  // newAnalyzer returns a new valid analyzer with the specified
    90  // name and prerequisites.
    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  // A nonPointerFact is a fact type that is (invalidly) not a pointer.
   101  type nonPointerFact struct{}
   102  
   103  func (nonPointerFact) AFact() {}
   104  

View as plain text