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 / analysistest / analysistest_test.go
1 package analysistest_test
2
3 import (
4         "fmt"
5         "log"
6         "os"
7         "reflect"
8         "strings"
9         "testing"
10
11         "golang.org/x/tools/go/analysis/analysistest"
12         "golang.org/x/tools/go/analysis/passes/findcall"
13         "golang.org/x/tools/internal/testenv"
14 )
15
16 func init() {
17         // This test currently requires GOPATH mode.
18         // Explicitly disabling module mode should suffice, but
19         // we'll also turn off GOPROXY just for good measure.
20         if err := os.Setenv("GO111MODULE", "off"); err != nil {
21                 log.Fatal(err)
22         }
23         if err := os.Setenv("GOPROXY", "off"); err != nil {
24                 log.Fatal(err)
25         }
26 }
27
28 // TestTheTest tests the analysistest testing infrastructure.
29 func TestTheTest(t *testing.T) {
30         testenv.NeedsTool(t, "go")
31
32         // We'll simulate a partly failing test of the findcall analysis,
33         // which (by default) reports calls to functions named 'println'.
34         findcall.Analyzer.Flags.Set("name", "println")
35
36         filemap := map[string]string{
37                 "a/b.go": `package main // want package:"found"
38
39 func main() {
40         // The expectation is ill-formed:
41         print() // want: "diagnostic"
42         print() // want foo"fact"
43         print() // want foo:
44         print() // want "\xZZ scan error"
45
46         // A diagnostic is reported at this line, but the expectation doesn't match:
47         println("hello, world") // want "wrong expectation text"
48
49         // An unexpected diagnostic is reported at this line:
50         println() // trigger an unexpected diagnostic
51
52         // No diagnostic is reported at this line:
53         print() // want "unsatisfied expectation"
54
55         // OK
56         println("hello, world") // want "call of println"
57
58         // OK /* */-form.
59         println("안녕, 세계") /* want "call of println" */
60
61         // OK  (nested comment)
62         println("Γειά σου, Κόσμε") // some comment // want "call of println"
63
64         // OK (nested comment in /**/)
65         println("你好,世界") /* some comment // want "call of println" */
66
67         // OK (multiple expectations on same line)
68         println(); println() // want "call of println(...)" "call of println(...)"
69 }
70
71 // OK (facts and diagnostics on same line)
72 func println(...interface{}) { println() } // want println:"found" "call of println(...)"
73
74 `,
75                 "a/b.go.golden": `package main // want package:"found"
76
77 func main() {
78         // The expectation is ill-formed:
79         print() // want: "diagnostic"
80         print() // want foo"fact"
81         print() // want foo:
82         print() // want "\xZZ scan error"
83
84         // A diagnostic is reported at this line, but the expectation doesn't match:
85         println_TEST_("hello, world") // want "wrong expectation text"
86
87         // An unexpected diagnostic is reported at this line:
88         println_TEST_() // trigger an unexpected diagnostic
89
90         // No diagnostic is reported at this line:
91         print() // want "unsatisfied expectation"
92
93         // OK
94         println_TEST_("hello, world") // want "call of println"
95
96         // OK /* */-form.
97         println_TEST_("안녕, 세계") /* want "call of println" */
98
99         // OK  (nested comment)
100         println_TEST_("Γειά σου, Κόσμε") // some comment // want "call of println"
101
102         // OK (nested comment in /**/)
103         println_TEST_("你好,世界") /* some comment // want "call of println" */
104
105         // OK (multiple expectations on same line)
106         println_TEST_()
107         println_TEST_() // want "call of println(...)" "call of println(...)"
108 }
109
110 // OK (facts and diagnostics on same line)
111 func println(...interface{}) { println_TEST_() } // want println:"found" "call of println(...)"
112 `,
113                 "a/b_test.go": `package main
114
115 // Test file shouldn't mess with things (issue #40574)
116 `,
117         }
118         dir, cleanup, err := analysistest.WriteFiles(filemap)
119         if err != nil {
120                 t.Fatal(err)
121         }
122         defer cleanup()
123
124         var got []string
125         t2 := errorfunc(func(s string) { got = append(got, s) }) // a fake *testing.T
126         analysistest.RunWithSuggestedFixes(t2, dir, findcall.Analyzer, "a")
127
128         want := []string{
129                 `a/b.go:5: in 'want' comment: unexpected ":"`,
130                 `a/b.go:6: in 'want' comment: got String after foo, want ':'`,
131                 `a/b.go:7: in 'want' comment: got EOF, want regular expression`,
132                 `a/b.go:8: in 'want' comment: invalid char escape`,
133                 `a/b.go:11:9: diagnostic "call of println(...)" does not match pattern "wrong expectation text"`,
134                 `a/b.go:14:9: unexpected diagnostic: call of println(...)`,
135                 `a/b.go:11: no diagnostic was reported matching "wrong expectation text"`,
136                 `a/b.go:17: no diagnostic was reported matching "unsatisfied expectation"`,
137                 // duplicate copies of each message from the test package (see issue #40574)
138                 `a/b.go:5: in 'want' comment: unexpected ":"`,
139                 `a/b.go:6: in 'want' comment: got String after foo, want ':'`,
140                 `a/b.go:7: in 'want' comment: got EOF, want regular expression`,
141                 `a/b.go:8: in 'want' comment: invalid char escape`,
142                 `a/b.go:11:9: diagnostic "call of println(...)" does not match pattern "wrong expectation text"`,
143                 `a/b.go:14:9: unexpected diagnostic: call of println(...)`,
144                 `a/b.go:11: no diagnostic was reported matching "wrong expectation text"`,
145                 `a/b.go:17: no diagnostic was reported matching "unsatisfied expectation"`,
146         }
147         if !reflect.DeepEqual(got, want) {
148                 t.Errorf("got:\n%s\nwant:\n%s",
149                         strings.Join(got, "\n"),
150                         strings.Join(want, "\n"))
151         }
152 }
153
154 type errorfunc func(string)
155
156 func (f errorfunc) Errorf(format string, args ...interface{}) {
157         f(fmt.Sprintf(format, args...))
158 }