Debian Codegraph @

go/analysis/passes/inline/inline_test.go

raw symbols SCIP
1 // Copyright 2018 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 inline
6
7 import (
8 "fmt"
9 "go/ast"
10 "go/importer"
11 "go/parser"
12 "go/token"
13 "go/types"
14 "slices"
15 "testing"
16
17 gocmp "github.com/google/go-cmp/cmp"
18 "golang.org/x/tools/go/analysis/analysistest"
19 "golang.org/x/tools/internal/testenv"
20 "golang.org/x/tools/internal/testfiles"
21 )
22
23 func TestAnalyzer(t *testing.T) {
24 t.Run("a,b", func(t *testing.T) {
25 analysistest.RunWithSuggestedFixes(t, analysistest.TestData(), Analyzer, "a", "b")
26 })
27 t.Run("RemoveImport72035", func(t *testing.T) {
28 analysistest.RunWithSuggestedFixes(t, analysistest.TestData(), Analyzer, "rmimport")
29 })
30 t.Run("Issue76190", func(t *testing.T) {
31 dir1 := testfiles.ExtractTxtarFileToTmp(t, "testdata/src/issue76190.txtar")
32 analysistest.RunWithSuggestedFixes(t, dir1, Analyzer, "example.com/a", "example.com/b")
33 })
34 t.Run("Issue77610", func(t *testing.T) {
35 dir2 := testfiles.ExtractTxtarFileToTmp(t, "testdata/src/issue77610.txtar")
36 analysistest.RunWithSuggestedFixes(t, dir2, Analyzer,
37 "example.com/a",
38 "example.com/b",
39 "example.com/c",
40 "example.com/d")
41 })
42 t.Run("Issue78088", func(t *testing.T) {
43 dir3 := testfiles.ExtractTxtarFileToTmp(t, "testdata/src/issue78088.txtar")
44 patterns := []string{
45 "example.com/g",
46 "example.com/h",
47 }
48 // The following tests use errors.AsType, which was introduced in Go 1.26.
49 // This avoids the "build constraints exclude all Go files" testing error.
50 if testenv.Go1Point() >= 26 {
51 patterns = append(patterns, "example.com/e", "example.com/f")
52 }
53 analysistest.RunWithSuggestedFixes(t, dir3, Analyzer, patterns...)
54 })
55 t.Run("EmbeddedAlias78230", func(t *testing.T) {
56 dir := testfiles.ExtractTxtarFileToTmp(t, "testdata/src/embedded_alias.txtar")
57 analysistest.Run(t, dir, Analyzer, "example.com")
58 })
59 t.Run("Issue77844", func(t *testing.T) {
60 dir := testfiles.ExtractTxtarFileToTmp(t, "testdata/src/issue77844.txtar")
61 analysistest.Run(t, dir, Analyzer, "example.com/main")
62 })
63 t.Run("Issue78994", func(t *testing.T) {
64 dir := testfiles.ExtractTxtarFileToTmp(t, "testdata/src/issue78994.txtar")
65 analysistest.RunWithSuggestedFixes(t, dir, Analyzer, "example.com/a")
66 })
67 }
68
69 func TestAllowBindingDeclFlag(t *testing.T) {
70 saved := allowBindingDecl
71 defer func() { allowBindingDecl = saved }()
72
73 run := func(allow bool) {
74 name := fmt.Sprintf("binding_%v", allow)
75 t.Run(name, func(t *testing.T) {
76 allowBindingDecl = allow
77 analysistest.RunWithSuggestedFixes(t, analysistest.TestData(), Analyzer, name)
78 })
79 }
80 run(true) // testdata/src/binding_true
81 run(false) // testdata/src/binding_false
82 }
83
84 func TestTypesWithNames(t *testing.T) {
85 // Test setup inspired by internal/analysis/addimport_test.go.
86 testenv.NeedsDefaultImporter(t)
87
88 for _, test := range []struct {
89 typeExpr string
90 want []string
91 }{
92 {
93 "int",
94 []string{"int"},
95 },
96 {
97 "*int",
98 []string{"int"},
99 },
100 {
101 "[]*int",
102 []string{"int"},
103 },
104 {
105 "[2]int",
106 []string{"int"},
107 },
108 {
109 // go/types does not expose the length expression.
110 "[unsafe.Sizeof(uint(1))]int",
111 []string{"int"},
112 },
113 {
114 "map[string]int",
115 []string{"int", "string"},
116 },
117 {
118 "map[int]struct{x, y int}",
119 []string{"int"},
120 },
121 {
122 "T",
123 []string{"a.T"},
124 },
125 {
126 "iter.Seq[int]",
127 []string{"int", "iter.Seq"},
128 },
129 {
130 "io.Reader",
131 []string{"io.Reader"},
132 },
133 {
134 "map[*io.Writer]map[T]A",
135 []string{"a.A", "a.T", "io.Writer"},
136 },
137 {
138 "func(int, int) (bool, error)",
139 []string{"bool", "error", "int"},
140 },
141 {
142 "func(int, ...string) (T, *T, error)",
143 []string{"a.T", "error", "int", "string"},
144 },
145 {
146 "func(iter.Seq[int])",
147 []string{"int", "iter.Seq"},
148 },
149 {
150 "struct { a int; b bool}",
151 []string{"bool", "int"},
152 },
153 {
154 "struct { io.Reader; a int}",
155 []string{"int", "io.Reader"},
156 },
157 {
158 "map[*string]struct{x chan int; y [2]bool}",
159 []string{"bool", "int", "string"},
160 },
161 {
162 "interface {F(int) bool}",
163 []string{"bool", "int"},
164 },
165 {
166 "interface {io.Reader; F(int) bool}",
167 []string{"bool", "int", "io.Reader"},
168 },
169 {
170 "G", // a type parameter of the function
171 []string{"a.G"},
172 },
173 } {
174 src := `
175 package a
176 import ("io"; "iter"; "unsafe")
177 func _(io.Reader, iter.Seq[int]) uintptr {return unsafe.Sizeof(1)}
178 type T int
179 type A = T
180
181 func F[G any]() {
182 var V ` + test.typeExpr + `
183 _ = V
184 }`
185
186 // parse
187 fset := token.NewFileSet()
188 f, err := parser.ParseFile(fset, "a.go", src, 0)
189 if err != nil {
190 t.Errorf("%s: %v", test.typeExpr, err)
191 continue
192 }
193
194 // type-check
195 info := &types.Info{
196 Types: make(map[ast.Expr]types.TypeAndValue),
197 Scopes: make(map[ast.Node]*types.Scope),
198 Defs: make(map[*ast.Ident]types.Object),
199 Implicits: make(map[ast.Node]types.Object),
200 }
201 conf := &types.Config{
202 Error: func(err error) { t.Fatalf("%s: %v", test.typeExpr, err) },
203 Importer: importer.Default(),
204 }
205 pkg, err := conf.Check(f.Name.Name, fset, []*ast.File{f}, info)
206 if err != nil {
207 t.Errorf("%s: %v", test.typeExpr, err)
208 continue
209 }
210
211 // Look at V's type.
212 typ := pkg.Scope().Lookup("F").(*types.Func).
213 Scope().Lookup("V").(*types.Var).Type()
214 tns := typenames(typ)
215 // Sort names for comparison.
216 var got []string
217 for _, tn := range tns {
218 var prefix string
219 if p := tn.Pkg(); p != nil && p.Path() != "" {
220 prefix = p.Path() + "."
221 }
222 got = append(got, prefix+tn.Name())
223 }
224 slices.Sort(got)
225 got = slices.Compact(got)
226
227 if diff := gocmp.Diff(test.want, got); diff != "" {
228 t.Errorf("%s: mismatch (-want, +got):\n%s", test.typeExpr, diff)
229 }
230 }
231 }