...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package main
16
17 import (
18 "reflect"
19 "testing"
20 )
21
22 func TestParseNolint(t *testing.T) {
23 tests := []struct {
24 Name string
25 Comment string
26 Valid bool
27 Linters []string
28 }{
29 {
30 Name: "Invalid",
31 Comment: "not a comment",
32 },
33 {
34 Name: "No match",
35 Comment: "// comment",
36 },
37 {
38 Name: "All linters",
39 Comment: "//nolint",
40 Valid: true,
41 },
42 {
43 Name: "All linters (explicit)",
44 Comment: "//nolint:all",
45 Valid: true,
46 },
47 {
48 Name: "Single linter",
49 Comment: "// nolint:foo",
50 Valid: true,
51 Linters: []string{"foo"},
52 },
53 {
54 Name: "Multiple linters",
55 Comment: "// nolint:a,b,c",
56 Valid: true,
57 Linters: []string{"a", "b", "c"},
58 },
59 }
60
61 for _, tc := range tests {
62 t.Run(tc.Name, func(t *testing.T) {
63 result, ok := parseNolint(tc.Comment)
64 if tc.Valid != ok {
65 t.Fatalf("parseNolint expect %t got %t", tc.Valid, ok)
66 }
67 var linters map[string]bool
68 if len(tc.Linters) != 0 {
69 linters = make(map[string]bool)
70 for _, l := range tc.Linters {
71 linters[l] = true
72 }
73 }
74 if !reflect.DeepEqual(result, linters) {
75 t.Fatalf("parseNolint expect %v got %v", linters, result)
76 }
77 })
78 }
79 }
80
View as plain text