Giant blob of minor changes
[dotfiles/.git] / .config / coc / extensions / coc-go-data / tools / pkg / mod / golang.org / x / tools@v0.0.0-20201105173854-bc9fc8d8c4bc / go / analysis / internal / checker / checker_test.go
1 package checker_test
2
3 import (
4         "fmt"
5         "go/ast"
6         "io/ioutil"
7         "path/filepath"
8         "testing"
9
10         "golang.org/x/tools/go/analysis"
11         "golang.org/x/tools/go/analysis/analysistest"
12         "golang.org/x/tools/go/analysis/internal/checker"
13         "golang.org/x/tools/go/analysis/passes/inspect"
14         "golang.org/x/tools/go/ast/inspector"
15         "golang.org/x/tools/internal/testenv"
16 )
17
18 var from, to string
19
20 func TestApplyFixes(t *testing.T) {
21         testenv.NeedsGoPackages(t)
22
23         from = "bar"
24         to = "baz"
25
26         files := map[string]string{
27                 "rename/test.go": `package rename
28
29 func Foo() {
30         bar := 12
31         _ = bar
32 }
33
34 // the end
35 `}
36         want := `package rename
37
38 func Foo() {
39         baz := 12
40         _ = baz
41 }
42
43 // the end
44 `
45
46         testdata, cleanup, err := analysistest.WriteFiles(files)
47         if err != nil {
48                 t.Fatal(err)
49         }
50         path := filepath.Join(testdata, "src/rename/test.go")
51         checker.Fix = true
52         checker.Run([]string{"file=" + path}, []*analysis.Analyzer{analyzer})
53
54         contents, err := ioutil.ReadFile(path)
55         if err != nil {
56                 t.Fatal(err)
57         }
58
59         got := string(contents)
60         if got != want {
61                 t.Errorf("contents of rewritten file\ngot: %s\nwant: %s", got, want)
62         }
63
64         defer cleanup()
65 }
66
67 var analyzer = &analysis.Analyzer{
68         Name:     "rename",
69         Requires: []*analysis.Analyzer{inspect.Analyzer},
70         Run:      run,
71 }
72
73 func run(pass *analysis.Pass) (interface{}, error) {
74         inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)
75         nodeFilter := []ast.Node{(*ast.Ident)(nil)}
76         inspect.Preorder(nodeFilter, func(n ast.Node) {
77                 ident := n.(*ast.Ident)
78                 if ident.Name == from {
79                         msg := fmt.Sprintf("renaming %q to %q", from, to)
80                         pass.Report(analysis.Diagnostic{
81                                 Pos:     ident.Pos(),
82                                 End:     ident.End(),
83                                 Message: msg,
84                                 SuggestedFixes: []analysis.SuggestedFix{{
85                                         Message: msg,
86                                         TextEdits: []analysis.TextEdit{{
87                                                 Pos:     ident.Pos(),
88                                                 End:     ident.End(),
89                                                 NewText: []byte(to),
90                                         }},
91                                 }},
92                         })
93                 }
94         })
95
96         return nil, nil
97 }