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