github.com/chenfeining/golangci-lint@v1.0.2-0.20230730162517-14c6c67868df/test/testshared/analysis_test.go (about)

     1  package testshared
     2  
     3  import (
     4  	"regexp"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  	"github.com/stretchr/testify/require"
     9  )
    10  
    11  func Test_parseComments(t *testing.T) {
    12  	testCases := []struct {
    13  		filename string
    14  		data     string
    15  		expected map[key][]expectation
    16  	}{
    17  		{
    18  			filename: "a/b.go",
    19  			data: `package main // want package:"found"
    20  
    21  `,
    22  			expected: map[key][]expectation{
    23  				{file: "a/b.go", line: 1}: {
    24  					{kind: "diagnostic", name: "package", rx: regexp.MustCompile(`found`)},
    25  				},
    26  			},
    27  		},
    28  		{
    29  			filename: "a/c.go",
    30  			data: `package main
    31  
    32  func main() {
    33  	println("hello, world") // want "call of println"
    34  }
    35  `,
    36  			expected: map[key][]expectation{
    37  				{file: "a/c.go", line: 4}: {
    38  					{kind: "diagnostic", name: "", rx: regexp.MustCompile(`call of println`)},
    39  				},
    40  			},
    41  		},
    42  		{
    43  			filename: "a/d.go",
    44  			data: `package main
    45  
    46  func main() {
    47  	// OK /* */-form.
    48  	println("hello") /* want "call of println" */
    49  }
    50  `,
    51  			expected: map[key][]expectation{
    52  				{file: "a/d.go", line: 5}: {
    53  					{kind: "diagnostic", name: "", rx: regexp.MustCompile(`call of println`)},
    54  				},
    55  			},
    56  		},
    57  		{
    58  			filename: "a/e.go",
    59  			data: `package main
    60  
    61  func main() {
    62  	// OK  (nested comment)
    63  	println("hello") // some comment // want "call of println"
    64  }
    65  `,
    66  			expected: map[key][]expectation{
    67  				{file: "a/e.go", line: 5}: {
    68  					{kind: "diagnostic", name: "", rx: regexp.MustCompile(`call of println`)},
    69  				},
    70  			},
    71  		},
    72  		{
    73  			filename: "a/f.go",
    74  			data: `package main
    75  
    76  func main() {
    77  	// OK (nested comment in /**/)
    78  	println("hello") /* some comment // want "call of println" */
    79  }
    80  `,
    81  			expected: map[key][]expectation{
    82  				{file: "a/f.go", line: 5}: {
    83  					{kind: "diagnostic", name: "", rx: regexp.MustCompile(`call of println`)},
    84  				},
    85  			},
    86  		},
    87  		{
    88  			filename: "a/g.go",
    89  			data: `package main
    90  
    91  func main() {
    92  	// OK (multiple expectations on same line)
    93  	println(); println() // want "call of println(...)" "call of println(...)"
    94  }
    95  `,
    96  			expected: map[key][]expectation{
    97  				{file: "a/g.go", line: 5}: {
    98  					{kind: "diagnostic", name: "", rx: regexp.MustCompile(`call of println(...)`)},
    99  					{kind: "diagnostic", name: "", rx: regexp.MustCompile(`call of println(...)`)},
   100  				},
   101  			},
   102  		},
   103  		{
   104  			filename: "a/h.go",
   105  			data: `package main
   106  
   107  func println(...interface{}) { println() } // want println:"found" "call of println(...)"
   108  `,
   109  			expected: map[key][]expectation{
   110  				{file: "a/h.go", line: 3}: {
   111  					{kind: "diagnostic", name: "println", rx: regexp.MustCompile(`found`)},
   112  					{kind: "diagnostic", name: "", rx: regexp.MustCompile(`call of println(...)`)},
   113  				},
   114  			},
   115  		},
   116  		{
   117  			filename: "a/b_test.go",
   118  			data: `package main
   119  
   120  // Test file shouldn't mess with things
   121  `,
   122  			expected: map[key][]expectation{},
   123  		},
   124  	}
   125  
   126  	for _, test := range testCases {
   127  		test := test
   128  		t.Run(test.filename, func(t *testing.T) {
   129  			t.Parallel()
   130  
   131  			expectations, err := parseComments(test.filename, []byte(test.data))
   132  			require.NoError(t, err)
   133  
   134  			assert.Equal(t, test.expected, expectations)
   135  		})
   136  	}
   137  }