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.
10 "golang.org/x/tools/go/analysis/analysistest"
11 "golang.org/x/tools/go/analysis/passes/findcall"
15 findcall.Analyzer.Flags.Set("name", "println")
18 // TestFromStringLiterals demonstrates how to test an analysis using
19 // a table of string literals for each test case.
21 // Such tests are typically quite compact.
22 func TestFromStringLiterals(t *testing.T) {
24 for _, test := range [...]struct {
27 files map[string]string
32 files: map[string]string{"main/main.go": `package main // want package:"found"
35 println("hello") // want "call of println"
36 print("goodbye") // not a call of println
39 func println(s string) {} // want println:"found"`,
43 t.Run(test.desc, func(t *testing.T) {
44 dir, cleanup, err := analysistest.WriteFiles(test.files)
49 analysistest.Run(t, dir, findcall.Analyzer, test.pkgpath)
54 // TestFromFileSystem demonstrates how to test an analysis using input
55 // files stored in the file system.
57 // These tests have the advantages that test data can be edited
58 // directly, and that files named in error messages can be opened.
59 // However, they tend to spread a small number of lines of text across a
60 // rather deep directory hierarchy, and obscure similarities among
61 // related tests, especially when tests involve multiple packages, or
62 // multiple variants of a single scenario.
63 func TestFromFileSystem(t *testing.T) {
64 testdata := analysistest.TestData()
65 analysistest.RunWithSuggestedFixes(t, testdata, findcall.Analyzer, "a") // loads testdata/src/a/a.go.