|
| 1 | +package lint |
| 2 | + |
| 3 | +import ( |
| 4 | + "go/ast" |
| 5 | + "go/token" |
| 6 | + "testing" |
| 7 | +) |
| 8 | + |
| 9 | +func TestFile_disabledIntervals(t *testing.T) { |
| 10 | + buildCommentGroups := func(comments ...string) []*ast.CommentGroup { |
| 11 | + commentGroups := make([]*ast.CommentGroup, 0, len(comments)) |
| 12 | + for _, c := range comments { |
| 13 | + commentGroups = append(commentGroups, &ast.CommentGroup{ |
| 14 | + List: []*ast.Comment{ |
| 15 | + {Text: c}, |
| 16 | + }, |
| 17 | + }) |
| 18 | + } |
| 19 | + return commentGroups |
| 20 | + } |
| 21 | + |
| 22 | + tests := []struct { |
| 23 | + name string |
| 24 | + comments []*ast.CommentGroup |
| 25 | + expected disabledIntervalsMap |
| 26 | + }{ |
| 27 | + { |
| 28 | + name: "no directives", |
| 29 | + comments: buildCommentGroups("// some comment"), |
| 30 | + expected: disabledIntervalsMap{}, |
| 31 | + }, |
| 32 | + { |
| 33 | + name: "disable rule", |
| 34 | + comments: buildCommentGroups("//revive:disable:rule1"), |
| 35 | + expected: disabledIntervalsMap{ |
| 36 | + "rule1": { |
| 37 | + { |
| 38 | + RuleName: "rule1", |
| 39 | + From: token.Position{ |
| 40 | + Filename: "test.go", |
| 41 | + }, |
| 42 | + To: token.Position{ |
| 43 | + Filename: "test.go", |
| 44 | + Line: 2147483647, |
| 45 | + }, |
| 46 | + }, |
| 47 | + }, |
| 48 | + }, |
| 49 | + }, |
| 50 | + { |
| 51 | + name: "enable rule", |
| 52 | + comments: buildCommentGroups("//revive:enable:rule1"), |
| 53 | + expected: disabledIntervalsMap{ |
| 54 | + "rule1": {}, |
| 55 | + }, |
| 56 | + }, |
| 57 | + { |
| 58 | + name: "disable and enable rule", |
| 59 | + comments: buildCommentGroups("//revive:disable:rule1", "//revive:enable:rule1"), |
| 60 | + expected: disabledIntervalsMap{ |
| 61 | + "rule1": { |
| 62 | + { |
| 63 | + RuleName: "rule1", |
| 64 | + From: token.Position{ |
| 65 | + Filename: "test.go", |
| 66 | + }, |
| 67 | + To: token.Position{ |
| 68 | + Filename: "test.go", |
| 69 | + }, |
| 70 | + }, |
| 71 | + }, |
| 72 | + }, |
| 73 | + }, |
| 74 | + { |
| 75 | + name: "disable-line rule", |
| 76 | + comments: buildCommentGroups("//revive:disable-line:rule1"), |
| 77 | + expected: disabledIntervalsMap{ |
| 78 | + "rule1": { |
| 79 | + { |
| 80 | + RuleName: "rule1", |
| 81 | + From: token.Position{ |
| 82 | + Filename: "test.go", |
| 83 | + }, |
| 84 | + To: token.Position{ |
| 85 | + Filename: "test.go", |
| 86 | + }, |
| 87 | + }, |
| 88 | + }, |
| 89 | + }, |
| 90 | + }, |
| 91 | + { |
| 92 | + name: "enable-line rule", |
| 93 | + comments: buildCommentGroups("//revive:enable-line:rule1"), |
| 94 | + expected: disabledIntervalsMap{ |
| 95 | + "rule1": { |
| 96 | + { |
| 97 | + RuleName: "rule1", |
| 98 | + From: token.Position{ |
| 99 | + Filename: "test.go", |
| 100 | + }, |
| 101 | + To: token.Position{ |
| 102 | + Filename: "test.go", |
| 103 | + Line: 2147483647, |
| 104 | + }, |
| 105 | + }, |
| 106 | + }, |
| 107 | + }, |
| 108 | + }, |
| 109 | + { |
| 110 | + name: "disable-next-line rule", |
| 111 | + comments: buildCommentGroups("//revive:disable-next-line:rule1"), |
| 112 | + expected: disabledIntervalsMap{ |
| 113 | + "rule1": { |
| 114 | + { |
| 115 | + RuleName: "rule1", |
| 116 | + From: token.Position{ |
| 117 | + Filename: "test.go", |
| 118 | + Line: 1, |
| 119 | + }, |
| 120 | + To: token.Position{ |
| 121 | + Filename: "test.go", |
| 122 | + Line: 1, |
| 123 | + }, |
| 124 | + }, |
| 125 | + }, |
| 126 | + }, |
| 127 | + }, |
| 128 | + { |
| 129 | + name: "enable-next-line rule", |
| 130 | + comments: buildCommentGroups("//revive:enable-next-line:rule1"), |
| 131 | + expected: disabledIntervalsMap{ |
| 132 | + "rule1": { |
| 133 | + { |
| 134 | + RuleName: "rule1", |
| 135 | + From: token.Position{ |
| 136 | + Filename: "test.go", |
| 137 | + Line: 1, |
| 138 | + }, |
| 139 | + To: token.Position{ |
| 140 | + Filename: "test.go", |
| 141 | + Line: 2147483647, |
| 142 | + }, |
| 143 | + }, |
| 144 | + }, |
| 145 | + }, |
| 146 | + }, |
| 147 | + } |
| 148 | + |
| 149 | + for _, tt := range tests { |
| 150 | + t.Run(tt.name, func(t *testing.T) { |
| 151 | + f := &File{ |
| 152 | + Name: "test.go", |
| 153 | + Pkg: &Package{ |
| 154 | + fset: token.NewFileSet(), |
| 155 | + }, |
| 156 | + AST: &ast.File{ |
| 157 | + Comments: tt.comments, |
| 158 | + }, |
| 159 | + } |
| 160 | + got := f.disabledIntervals(nil, false, make(chan Failure, 10)) |
| 161 | + if len(got) != len(tt.expected) { |
| 162 | + t.Errorf("disabledIntervals() = got %v, want %v", got, tt.expected) |
| 163 | + } |
| 164 | + for rule, intervals := range got { |
| 165 | + expectedIntervals, ok := tt.expected[rule] |
| 166 | + if !ok { |
| 167 | + t.Errorf("unexpected rule %q", rule) |
| 168 | + continue |
| 169 | + } |
| 170 | + if len(intervals) != len(expectedIntervals) { |
| 171 | + t.Errorf("intervals for rule %q = got %+v, want %+v", rule, intervals, expectedIntervals) |
| 172 | + continue |
| 173 | + } |
| 174 | + for i, interval := range intervals { |
| 175 | + if interval != expectedIntervals[i] { |
| 176 | + t.Errorf("interval %d for rule %q = got %+v, want %+v", i, rule, interval, expectedIntervals[i]) |
| 177 | + } |
| 178 | + } |
| 179 | + } |
| 180 | + }) |
| 181 | + } |
| 182 | +} |
0 commit comments