-
-
Notifications
You must be signed in to change notification settings - Fork 3k
/
directive_comment_spec.rb
201 lines (157 loc) · 5.43 KB
/
directive_comment_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# frozen_string_literal: true
RSpec.describe RuboCop::DirectiveComment do
subject(:directive_comment) { described_class.new(comment) }
let(:comment) { instance_double(Parser::Source::Comment, text: text) }
let(:comment_cop_names) { 'all' }
let(:text) { "#rubocop:enable #{comment_cop_names}" }
describe '.before_comment' do
subject { described_class.before_comment(text) }
[
['when line has code', 'def foo # rubocop:disable all', 'def foo '],
['when line has NO code', '# rubocop:disable all', '']
].each do |example|
context example[0] do
let(:text) { example[1] }
it { is_expected.to eq example[2] }
end
end
end
describe '#match?' do
subject(:match) { directive_comment.match?(cop_names) }
let(:comment_cop_names) { 'Metrics/AbcSize, Metrics/PerceivedComplexity, Style/Not' }
context 'no comment_cop_names' do
let(:cop_names) { [] }
it 'returns false' do
expect(match).to eq(false)
end
end
context 'same cop names as in the comment' do
let(:cop_names) { %w[Metrics/AbcSize Metrics/PerceivedComplexity Style/Not] }
it 'returns true' do
expect(match).to eq(true)
end
end
context 'same cop names as in the comment in a different order' do
let(:cop_names) { %w[Style/Not Metrics/AbcSize Metrics/PerceivedComplexity] }
it 'returns true' do
expect(match).to eq(true)
end
end
context 'subset of names' do
let(:cop_names) { %w[Metrics/AbcSize Style/Not] }
it 'returns false' do
expect(match).to eq(false)
end
end
context 'superset of names' do
let(:cop_names) { %w[Lint/Void Metrics/AbcSize Metrics/PerceivedComplexity Style/Not] }
it 'returns false' do
expect(match).to eq(false)
end
end
context 'duplicate names' do
let(:cop_names) { %w[Metrics/AbcSize Metrics/AbcSize Metrics/PerceivedComplexity Style/Not] }
it 'returns true' do
expect(match).to eq(true)
end
end
context 'all' do
let(:comment_cop_names) { 'all' }
let(:cop_names) { %w[all] }
it 'returns true' do
expect(match).to eq(true)
end
end
end
describe '#match_captures' do
subject { directive_comment.match_captures }
[
['when disable', '# rubocop:disable all', ['disable', 'all', nil, nil]],
['when enable', '# rubocop:enable Foo/Bar', ['enable', 'Foo/Bar', nil, 'Foo/']],
['when todo', '# rubocop:todo all', ['todo', 'all', nil, nil]],
['when typo', '# rudocop:todo Dig/ThisMine', nil]
].each do |example|
context example[0] do
let(:text) { example[1] }
it { is_expected.to eq example[2] }
end
end
end
describe '#single_line?' do
subject { directive_comment.single_line? }
[
['when relates to single line', 'def foo # rubocop:disable all', true],
['when does NOT relate to single line', '# rubocop:disable all', false]
].each do |example|
context example[0] do
let(:text) { example[1] }
it { is_expected.to eq example[2] }
end
end
end
describe '#disabled?' do
subject { directive_comment.disabled? }
[
['when disable', '# rubocop:disable all', true],
['when enable', '# rubocop:enable Foo/Bar', false],
['when todo', '# rubocop:todo all', true]
].each do |example|
context example[0] do
let(:text) { example[1] }
it { is_expected.to eq example[2] }
end
end
end
describe '#all_cops?' do
subject { directive_comment.all_cops? }
[
['when mentioned all', '# rubocop:disable all', true],
['when mentioned specific cops', '# rubocop:enable Foo/Bar', false]
].each do |example|
context example[0] do
let(:text) { example[1] }
it { is_expected.to eq example[2] }
end
end
end
describe '#cop_names' do
subject(:cop_names) { directive_comment.cop_names }
context 'when all cops mentioned' do
let(:comment_cop_names) { 'all' }
let(:global) { instance_double(RuboCop::Cop::Registry, names: names) }
let(:names) { %w[all_names Lint/RedundantCopDisableDirective] }
before { allow(RuboCop::Cop::Registry).to receive(:global).and_return(global) }
it 'returns all cop names except redundant' do
expect(cop_names).to eq(%w[all_names])
end
end
context 'when cop specified' do
let(:comment_cop_names) { 'Foo/Bar' }
it 'returns parsed cop names' do
expect(cop_names).to eq(%w[Foo/Bar])
end
end
end
describe '#line_number' do
let(:comment) { instance_double(Parser::Source::Comment, text: text, loc: loc) }
let(:loc) { instance_double(Parser::Source::Map, expression: expression) }
let(:expression) { instance_double(Parser::Source::Range, line: 1) }
it 'returns line number for directive' do
expect(directive_comment.line_number).to be 1
end
end
describe '#enabled_all?' do
subject { directive_comment.enabled_all? }
[
['when enabled all cops', 'def foo # rubocop:enable all', true],
['when enabled specific cops', '# rubocop:enable Foo/Bar', false],
['when disabled all cops', '# rubocop:disable all', false],
['when disabled specific cops', '# rubocop:disable Foo/Bar', false]
].each do |example|
context example[0] do
let(:text) { example[1] }
it { is_expected.to eq example[2] }
end
end
end
end