/
cop_spec.rb
373 lines (285 loc) · 11 KB
/
cop_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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
# frozen_string_literal: true
RSpec.describe RuboCop::Cop::Cop, :config do
let(:source) { 'code = {some: :ruby}' }
let(:location) { source_range(0...1) }
before { cop.send(:begin_investigation, processed_source) }
it 'initially has 0 offenses' do
expect(cop.offenses.empty?).to be(true)
end
describe '.qualified_cop_name' do
before do
$stderr = StringIO.new
end
after do
$stderr = STDERR
end
it 'adds namespace if the cop name is found in exactly one namespace' do
expect(described_class.qualified_cop_name('LineLength', '--only'))
.to eq('Layout/LineLength')
end
it 'returns the given cop name if it is not found in any namespace' do
expect(described_class.qualified_cop_name('UnknownCop', '--only'))
.to eq('UnknownCop')
end
it 'returns the given cop name if it already has a namespace' do
expect(described_class.qualified_cop_name('Layout/LineLength', '--only'))
.to eq('Layout/LineLength')
end
it 'returns the cop name in a different namespace if the provided ' \
'namespace is incorrect' do
expect(described_class.qualified_cop_name('Style/LineLength', '--only'))
.to eq('Layout/LineLength')
end
# `Rails/SafeNavigation` was extracted to rubocop-rails gem,
# there were no cop whose names overlapped.
xit 'raises an error if the cop name is in more than one namespace' do
expect { described_class.qualified_cop_name('SafeNavigation', '--only') }
.to raise_error(RuboCop::Cop::AmbiguousCopName)
end
it 'returns the given cop name if it already has a namespace even when ' \
'the cop exists in multiple namespaces' do
qualified_cop_name =
described_class.qualified_cop_name('Style/SafeNavigation', '--only')
expect(qualified_cop_name).to eq('Style/SafeNavigation')
end
end
describe '.documentation_url' do
subject(:url) { cop_class.documentation_url }
describe 'for a builtin cop class' do
let(:cop_class) { RuboCop::Cop::Layout::BlockEndNewline }
it { is_expected.to eq 'https://docs.rubocop.org/rubocop/cops_layout.html#layoutblockendnewline' } # rubocop:disable Layout/LineLength
end
describe 'for a custom cop class', :restore_registry do
let(:cop_class) { stub_cop_class('Some::Cop') { def foo; end } }
it { is_expected.to eq nil }
end
end
it 'keeps track of offenses' do
cop.add_offense(nil, location: location, message: 'message')
expect(cop.offenses.size).to eq(1)
end
it 'will report registered offenses' do
cop.add_offense(nil, location: location, message: 'message')
expect(cop.offenses.empty?).to be(false)
end
it 'will set default severity' do
cop.add_offense(nil, location: location, message: 'message')
expect(cop.offenses.first.severity).to eq(:convention)
end
it 'will set custom severity if present' do
cop.config[cop.name] = { 'Severity' => 'warning' }
cop.add_offense(nil, location: location, message: 'message')
expect(cop.offenses.first.severity).to eq(:warning)
end
it 'will warn if custom severity is invalid' do
cop.config[cop.name] = { 'Severity' => 'superbad' }
expect { cop.add_offense(nil, location: location, message: 'message') }
.to output(/Warning: Invalid severity 'superbad'./).to_stderr
end
context 'when disabled by a comment' do
subject(:offense_status) do
cop.add_offense(nil, location: location, message: 'message')
cop.offenses.first.status
end
before do
allow(processed_source.comment_config).to receive(:cop_enabled_at_line?)
.and_return(false)
end
context 'ignore_disable_comments is false' do
let(:cop_options) { { ignore_disable_comments: false } }
it 'will set offense as disabled' do
expect(offense_status).to eq :disabled
end
end
context 'ignore_disable_comments is true' do
let(:cop_options) { { ignore_disable_comments: true } }
it 'will not set offense as disabled' do
expect(offense_status).not_to eq :disabled
end
end
end
describe 'for a cop with a name' do
let(:cop_class) { RuboCop::Cop::Style::For }
it 'registers offense with its name' do
offenses = cop.add_offense(location, message: 'message')
expect(offenses.first.cop_name).to eq('Style/For')
end
end
describe 'setting of Offense#corrected attribute' do
context 'when cop does not support autocorrection' do
before do
allow(cop).to receive(:support_autocorrect?).and_return(false)
end
it 'is not specified (set to nil)' do
cop.add_offense(nil, location: location, message: 'message')
expect(cop.offenses.first.corrected?).to be(false)
end
context 'when autocorrect is requested' do
before do
allow(cop).to receive(:autocorrect_requested?).and_return(true)
end
it 'is not specified (set to nil)' do
cop.add_offense(nil, location: location, message: 'message')
expect(cop.offenses.first.corrected?).to be(false)
end
context 'when disable_uncorrectable is enabled' do
before do
allow(cop).to receive(:disable_uncorrectable?).and_return(true)
end
let(:node) do
instance_double(RuboCop::AST::Node,
location: instance_double(Parser::Source::Map,
expression: location,
line: 1))
end
it 'is set to true' do
cop.add_offense(node, location: location, message: 'message')
expect(cop.offenses.first.corrected?).to be(true)
expect(cop.offenses.first.status).to be(:corrected_with_todo)
end
end
end
end
context 'when cop supports autocorrection', :restore_registry do
let(:cop_class) do
stub_cop_class('RuboCop::Cop::Test::StubCop', inherit: described_class) do
def autocorrect(node); end
end
end
context 'when offense was corrected' do
before do
allow(cop).to receive(:autocorrect?).and_return(true)
allow(cop).to receive(:autocorrect).and_return(lambda do |corrector|
corrector.insert_before(location, 'hi!')
end)
end
it 'is set to true' do
cop.add_offense(nil, location: location, message: 'message')
expect(cop.offenses.first.corrected?).to eq(true)
end
end
context 'when autocorrection is not needed' do
before do
allow(cop).to receive(:autocorrect?).and_return(false)
end
it 'is set to false' do
cop.add_offense(nil, location: location, message: 'message')
expect(cop.offenses.first.corrected?).to eq(false)
end
end
context 'when offense was not corrected because of an error' do
before do
allow(cop).to receive(:autocorrect?).and_return(true)
allow(cop).to receive(:autocorrect).and_return(false)
end
it 'is set to false' do
cop.add_offense(nil, location: location, message: 'message')
expect(cop.offenses.first.corrected?).to eq(false)
end
end
end
end
context 'with no submodule' do
it('has right name') { expect(cop_class.cop_name).to eq('Cop/Cop') }
it('has right department') { expect(cop_class.department).to eq(:Cop) }
end
context 'with style cops' do
let(:cop_class) { RuboCop::Cop::Style::For }
it('has right name') { expect(cop_class.cop_name).to eq('Style/For') }
it('has right department') { expect(cop_class.department).to eq(:Style) }
end
context 'with lint cops' do
let(:cop_class) { RuboCop::Cop::Lint::Loop }
it('has right name') { expect(cop_class.cop_name).to eq('Lint/Loop') }
it('has right department') { expect(cop_class.department).to eq(:Lint) }
end
describe 'Registry' do
describe '#departments' do
subject(:departments) { described_class.registry.departments }
it('has departments') { expect(departments.length).not_to eq(0) }
it { is_expected.to include(:Lint) }
it { is_expected.to include(:Style) }
it 'contains every value only once' do
expect(departments.length).to eq(departments.uniq.length)
end
end
describe '#with_department' do
let(:departments) { described_class.registry.departments }
it 'has at least one cop per department' do
departments.each do |c|
expect(described_class.registry.with_department(c).length).to be > 0
end
end
it 'has each cop in exactly one type' do
sum = 0
departments.each do |c|
sum += described_class.registry.with_department(c).length
end
expect(sum).to be described_class.registry.length
end
it 'returns 0 for an invalid type' do
expect(described_class.registry.with_department('x').length).to be 0
end
end
end
describe '#autocorrect?' do
subject { cop.autocorrect? }
let(:support_autocorrect) { true }
let(:disable_uncorrectable) { false }
before do
allow(cop.class).to receive(:support_autocorrect?) { support_autocorrect }
allow(cop).to receive(:disable_uncorrectable?) { disable_uncorrectable }
end
context 'when the option is not given' do
let(:options) { {} }
it { is_expected.to be(false) }
end
context 'when the option is given' do
let(:cop_options) { { auto_correct: true } }
it { is_expected.to be(true) }
context 'when cop does not support autocorrection' do
let(:support_autocorrect) { false }
it { is_expected.to be(false) }
context 'when disable_uncorrectable is enabled' do
let(:disable_uncorrectable) { true }
it { is_expected.to be(true) }
end
end
context 'when the cop is set to not autocorrect' do
let(:cop_options) { { 'AutoCorrect' => false } }
it { is_expected.to be(false) }
end
end
end
describe '#relevant_file?' do
subject { cop.relevant_file?(file) }
let(:cop_config) { { 'Include' => ['foo.rb'] } }
context 'when the file matches the Include configuration' do
let(:file) { 'foo.rb' }
it { is_expected.to be(true) }
end
context 'when the file doesn\'t match the Include configuration' do
let(:file) { 'bar.rb' }
it { is_expected.to be(false) }
end
context 'when the file is an anonymous source' do
let(:file) { '(string)' }
it { is_expected.to be(true) }
end
end
describe '#safe_autocorrect?' do
subject { cop.safe_autocorrect? }
context 'when cop is declared unsafe' do
let(:cop_config) { { 'Safe' => false } }
it { is_expected.to be(false) }
end
context 'when auto-correction of the cop is declared unsafe' do
let(:cop_config) { { 'SafeAutoCorrect' => false } }
it { is_expected.to be(false) }
end
context 'when safety is undeclared' do
let(:cop_config) { {} }
it { is_expected.to be(true) }
end
end
end