1 package analysistest_test
11 "golang.org/x/tools/go/analysis/analysistest"
12 "golang.org/x/tools/go/analysis/passes/findcall"
13 "golang.org/x/tools/internal/testenv"
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 {
23 if err := os.Setenv("GOPROXY", "off"); err != nil {
28 // TestTheTest tests the analysistest testing infrastructure.
29 func TestTheTest(t *testing.T) {
30 testenv.NeedsTool(t, "go")
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")
36 filemap := map[string]string{
37 "a/b.go": `package main // want package:"found"
40 // The expectation is ill-formed:
41 print() // want: "diagnostic"
42 print() // want foo"fact"
44 print() // want "\xZZ scan error"
46 // A diagnostic is reported at this line, but the expectation doesn't match:
47 println("hello, world") // want "wrong expectation text"
49 // An unexpected diagnostic is reported at this line:
50 println() // trigger an unexpected diagnostic
52 // No diagnostic is reported at this line:
53 print() // want "unsatisfied expectation"
56 println("hello, world") // want "call of println"
59 println("안녕, 세계") /* want "call of println" */
61 // OK (nested comment)
62 println("Γειά σου, Κόσμε") // some comment // want "call of println"
64 // OK (nested comment in /**/)
65 println("你好,世界") /* some comment // want "call of println" */
67 // OK (multiple expectations on same line)
68 println(); println() // want "call of println(...)" "call of println(...)"
71 // OK (facts and diagnostics on same line)
72 func println(...interface{}) { println() } // want println:"found" "call of println(...)"
75 "a/b.go.golden": `package main // want package:"found"
78 // The expectation is ill-formed:
79 print() // want: "diagnostic"
80 print() // want foo"fact"
82 print() // want "\xZZ scan error"
84 // A diagnostic is reported at this line, but the expectation doesn't match:
85 println_TEST_("hello, world") // want "wrong expectation text"
87 // An unexpected diagnostic is reported at this line:
88 println_TEST_() // trigger an unexpected diagnostic
90 // No diagnostic is reported at this line:
91 print() // want "unsatisfied expectation"
94 println_TEST_("hello, world") // want "call of println"
97 println_TEST_("안녕, 세계") /* want "call of println" */
99 // OK (nested comment)
100 println_TEST_("Γειά σου, Κόσμε") // some comment // want "call of println"
102 // OK (nested comment in /**/)
103 println_TEST_("你好,世界") /* some comment // want "call of println" */
105 // OK (multiple expectations on same line)
107 println_TEST_() // want "call of println(...)" "call of println(...)"
110 // OK (facts and diagnostics on same line)
111 func println(...interface{}) { println_TEST_() } // want println:"found" "call of println(...)"
113 "a/b_test.go": `package main
115 // Test file shouldn't mess with things (issue #40574)
118 dir, cleanup, err := analysistest.WriteFiles(filemap)
125 t2 := errorfunc(func(s string) { got = append(got, s) }) // a fake *testing.T
126 analysistest.RunWithSuggestedFixes(t2, dir, findcall.Analyzer, "a")
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"`,
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"))
154 type errorfunc func(string)
156 func (f errorfunc) Errorf(format string, args ...interface{}) {
157 f(fmt.Sprintf(format, args...))