golang.org/x/tools@v0.21.0/go/analysis/passes/findcall/findcall_test.go (about)

     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.
     4  
     5  package findcall_test
     6  
     7  import (
     8  	"testing"
     9  
    10  	"golang.org/x/tools/go/analysis/analysistest"
    11  	"golang.org/x/tools/go/analysis/passes/findcall"
    12  )
    13  
    14  func init() {
    15  	findcall.Analyzer.Flags.Set("name", "println")
    16  }
    17  
    18  // TestFromStringLiterals demonstrates how to test an analysis using
    19  // a table of string literals for each test case.
    20  //
    21  // Such tests are typically quite compact.
    22  func TestFromStringLiterals(t *testing.T) {
    23  
    24  	for _, test := range [...]struct {
    25  		desc    string
    26  		pkgpath string
    27  		files   map[string]string
    28  	}{
    29  		{
    30  			desc:    "SimpleTest",
    31  			pkgpath: "main",
    32  			files: map[string]string{"main/main.go": `package main // want package:"found"
    33  
    34  func main() {
    35  	println("hello") // want "call of println"
    36  	print("goodbye") // not a call of println
    37  }
    38  
    39  func println(s string) {} // want println:"found"`,
    40  			},
    41  		},
    42  	} {
    43  		t.Run(test.desc, func(t *testing.T) {
    44  			dir, cleanup, err := analysistest.WriteFiles(test.files)
    45  			if err != nil {
    46  				t.Fatal(err)
    47  			}
    48  			defer cleanup()
    49  			analysistest.Run(t, dir, findcall.Analyzer, test.pkgpath)
    50  		})
    51  	}
    52  }
    53  
    54  // TestFromFileSystem demonstrates how to test an analysis using input
    55  // files stored in the file system.
    56  //
    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.
    66  }