-
-
Notifications
You must be signed in to change notification settings - Fork 3k
/
redundant_line_break_spec.rb
519 lines (456 loc) · 15.2 KB
/
redundant_line_break_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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
# frozen_string_literal: true
RSpec.describe RuboCop::Cop::Layout::RedundantLineBreak, :config do
let(:config) do
RuboCop::Config.new('Layout/LineLength' => { 'Max' => max_line_length },
'Layout/RedundantLineBreak' => { 'InspectBlocks' => inspect_blocks },
'Layout/SingleLineBlockChain' => {
'Enabled' => single_line_block_chain_enabled
})
end
let(:max_line_length) { 31 }
let(:single_line_block_chain_enabled) { true }
shared_examples 'common behavior' do
context 'when Layout/SingleLineBlockChain is disabled' do
let(:single_line_block_chain_enabled) { false }
let(:max_line_length) { 90 }
it 'reports an offense for a method call chained onto a single line block' do
expect_offense(<<~RUBY)
e.select { |i| i.cond? }
^^^^^^^^^^^^^^^^^^^^^^^^ Redundant line break detected.
.join
a = e.select { |i| i.cond? }
^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Redundant line break detected.
.join
e.select { |i| i.cond? }
^^^^^^^^^^^^^^^^^^^^^^^^ Redundant line break detected.
.join + []
RUBY
end
end
context 'when Layout/SingleLineBlockChain is enabled' do
let(:single_line_block_chain_enabled) { true }
let(:max_line_length) { 90 }
it 'accepts a method call chained onto a single line block' do
expect_no_offenses(<<~RUBY)
e.select { |i| i.cond? }
.join
a = e.select { |i| i.cond? }
.join
e.select { |i| i.cond? }
.join + []
RUBY
end
end
context 'for an expression that fits on a single line' do
it 'accepts an assignment containing an if expression' do
expect_no_offenses(<<~RUBY)
a =
if x
1
else
2
end
RUBY
end
it 'accepts an assignment containing a case expression' do
expect_no_offenses(<<~RUBY)
a =
case x
when :a
1
else
2
end
RUBY
end
it 'accepts a binary expression containing an if expression' do
expect_no_offenses(<<~RUBY)
a +
if x
1
else
2
end
RUBY
end
it 'accepts a method call with a block' do
expect_no_offenses(<<~RUBY)
a do
x
y
end
RUBY
end
it 'accepts an assignment containing a begin-end expression' do
expect_no_offenses(<<~RUBY)
a ||= begin
x
y
end
RUBY
end
it 'accepts a method call on a single line' do
expect_no_offenses(<<~RUBY)
my_method(1, 2, "x")
RUBY
end
it 'registers an offense for a method call on multiple lines with backslash' do
expect_offense(<<~RUBY)
my_method(1) \\
^^^^^^^^^^^^^^ Redundant line break detected.
[:a]
RUBY
expect_correction(<<~RUBY)
my_method(1) [:a]
RUBY
end
context 'with LineLength Max 100' do
let(:max_line_length) { 100 }
it 'registers an offense for a method without parentheses on multiple lines' do
expect_offense(<<~RUBY)
def resolve_inheritance_from_gems(hash)
gems = hash.delete('inherit_gem')
(gems || {}).each_pair do |gem_name, config_path|
if gem_name == 'rubocop'
raise ArgumentError,
^^^^^^^^^^^^^^^^^^^^ Redundant line break detected.
"can't inherit configuration from the rubocop gem"
end
#{' '}
hash['inherit_from'] = Array(hash['inherit_from'])
Array(config_path).reverse_each do |path|
# Put gem configuration first so local configuration overrides it.
hash['inherit_from'].unshift gem_config_path(gem_name, path)
end
end
end
RUBY
expect_correction(<<~RUBY)
def resolve_inheritance_from_gems(hash)
gems = hash.delete('inherit_gem')
(gems || {}).each_pair do |gem_name, config_path|
if gem_name == 'rubocop'
raise ArgumentError, "can't inherit configuration from the rubocop gem"
end
#{' '}
hash['inherit_from'] = Array(hash['inherit_from'])
Array(config_path).reverse_each do |path|
# Put gem configuration first so local configuration overrides it.
hash['inherit_from'].unshift gem_config_path(gem_name, path)
end
end
end
RUBY
end
end
it 'registers an offense for a method call on multiple lines' do
expect_offense(<<~RUBY)
my_method(1,
^^^^^^^^^^^^ Redundant line break detected.
2,
"x")
RUBY
expect_correction(<<~RUBY)
my_method(1, 2, "x")
RUBY
end
it 'accepts a method call on multiple lines if there are comments on them' do
expect_no_offenses(<<~RUBY)
my_method(1,
2,
"x") # X
RUBY
end
it 'registers an offense for a method call with a double quoted split string in parentheses' do
expect_offense(<<~RUBY)
my_method("a" \\
^^^^^^^^^^^^^^^ Redundant line break detected.
"b")
RUBY
expect_correction(<<~RUBY)
my_method("ab")
RUBY
end
it 'registers an offense for a method call with a double quoted split string without parentheses' do
expect_offense(<<~RUBY)
puts "(\#{pl(i)}, " \\
^^^^^^^^^^^^^^^^^^^^ Redundant line break detected.
"\#{pl(f)})"
RUBY
expect_correction(<<~RUBY)
puts "(\#{pl(i)}, \#{pl(f)})"
RUBY
end
it 'registers an offense for a method call with a single quoted split string' do
expect_offense(<<~RUBY)
my_method('a'\\
^^^^^^^^^^^^^^ Redundant line break detected.
'b')
RUBY
expect_correction(<<~RUBY)
my_method('ab')
RUBY
end
it 'registers an offense for a method call with a double and single quoted split string' do
expect_offense(<<~RUBY)
my_method("a" \\
^^^^^^^^^^^^^^^ Redundant line break detected.
'b')
my_method('a' \\
^^^^^^^^^^^^^^^ Redundant line break detected.
"b")
RUBY
expect_correction(<<~RUBY)
my_method("a" + 'b')
my_method('a' + "b")
RUBY
end
it 'registers an offense for a method call with a split operation' do
expect_offense(<<~RUBY)
my_method(1 +
^^^^^^^^^^^^^ Redundant line break detected.
2 +
3)
RUBY
expect_correction(<<~RUBY)
my_method(1 + 2 + 3)
RUBY
end
it 'registers an offense for a method call as right hand side of an assignment' do
expect_offense(<<~RUBY)
a =
^^^ Redundant line break detected.
m(1 +
2 +
3)
b = m(4 +
^^^^^^^^^ Redundant line break detected.
5 +
6)
long_variable_name =
m(7 +
^^^^^ Redundant line break detected.
8 +
9)
RUBY
expect_correction(<<~RUBY)
a = m(1 + 2 + 3)
b = m(4 + 5 + 6)
long_variable_name =
m(7 + 8 + 9)
RUBY
end
it 'properly corrects a method chain on multiple lines' do
expect_offense(<<~RUBY)
foo
^^^ Redundant line break detected.
.bar
.baz
RUBY
expect_correction(<<~RUBY)
foo.bar.baz
RUBY
end
end
context 'for an expression that does not fit on a single line' do
it 'accepts a method call on a multiple lines' do
expect_no_offenses(<<~RUBY)
my_method(11111,
22222,
"abcxyz")
my_method(111111 +
222222 +
333333)
RUBY
end
context 'with a longer max line length' do
let(:max_line_length) { 82 }
it 'accepts an assignment containing a method definition' do
expect_no_offenses(<<~RUBY)
VariableReference = Struct.new(:name) do
def assignment?
false
end
end
RUBY
end
it 'accepts a method call followed by binary operations that are too long taken together' do
expect_no_offenses(<<~RUBY)
File.fnmatch?(
pattern, path,
File::FNM_PATHNAME | File::FNM_EXTGLOB
) && a && File.basename(path).start_with?('.') && !hidden_dir?(path)
RUBY
expect_no_offenses(<<~RUBY)
File.fnmatch?(
pattern, path,
File::FNM_PATHNAME | File::FNM_EXTGLOB
) + a + File.basename(path).start_with?('.') + !hidden_dir?(path)
RUBY
end
it 'accepts an assignment containing a heredoc' do
expect_no_offenses(<<~RUBY)
correct = lambda do
expect_no_offenses(<<~EOT1)
<<-EOT2
foo
EOT2
EOT1
end
RUBY
end
it 'accepts a complex method call on a multiple lines' do
expect_no_offenses(<<~RUBY)
node.each_node(:dstr)
.select(&:heredoc?)
.map { |n| n.loc.heredoc_body }
.flat_map { |b| (b.line...b.last_line).to_a }
RUBY
end
it 'accepts method call with a do keyword that would just surpass the max line length' do
expect_no_offenses(<<~RUBY)
context 'when the configuration includes ' \\
'an unsafe cop that is 123456789012345678' do
end
RUBY
end
it 'registers an offense for a method call with a do keyword that is just under the max line length' do
expect_offense(<<~RUBY)
context 'when the configuration includes ' \\
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Redundant line break detected.
'an unsafe cop that is 123456789012345' do
end
RUBY
expect_correction(<<~RUBY)
context 'when the configuration includes an unsafe cop that is 123456789012345' do
end
RUBY
end
context 'for a block' do
it 'accepts when it is difficult to convert to single line' do
expect_no_offenses(<<~RUBY)
RSpec.shared_context 'ruby 2.4', :ruby24 do
let(:ruby_version) { 2.4 }
end
RUBY
end
end
end
end
end
context 'when InspectBlocks is true' do
let(:inspect_blocks) { true }
include_examples 'common behavior'
context 'for a block' do
let(:max_line_length) { 82 }
it 'registers an offense when the method call has parentheses' do
expect_offense(<<~RUBY)
RSpec.shared_context('ruby 2.4', :ruby24) do
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Redundant line break detected.
let(:ruby_version) { 2.4 }
end
RUBY
end
it 'registers an offense when the method call has no argumnets' do
expect_offense(<<~RUBY)
RSpec.shared_context do
^^^^^^^^^^^^^^^^^^^^^^^ Redundant line break detected.
let(:ruby_version) { 2.4 }
end
RUBY
end
context 'when Layout/SingleLineBlockChain is enabled' do
let(:single_line_block_chain_enabled) { true }
it 'reports an offense for a multiline block without a chained method call' do
expect_offense(<<~RUBY)
f do
^^^^ Redundant line break detected.
end
RUBY
end
end
context 'when Layout/SingleLineBlockChain is disabled' do
let(:single_line_block_chain_enabled) { false }
it 'reports an offense for a multiline block without a chained method call' do
expect_offense(<<~RUBY)
f do
^^^^ Redundant line break detected.
end
RUBY
end
it 'reports an offense for a method call chained onto a multiline block' do
expect_offense(<<~RUBY)
e.select do |i|
^^^^^^^^^^^^^^^ Redundant line break detected.
i.cond?
end.join
RUBY
expect_offense(<<~RUBY)
a = e.select do |i|
^^^^^^^^^^^^^^^^^^^ Redundant line break detected.
i.cond?
end.join
RUBY
expect_offense(<<~RUBY)
e.select do |i|
^^^^^^^^^^^^^^^ Redundant line break detected.
i.cond?
end.join + []
RUBY
end
end
end
end
context 'when InspectBlocks is false' do
let(:inspect_blocks) { false }
include_examples 'common behavior'
context 'for a block' do
let(:max_line_length) { 100 }
it 'accepts when the method call has parentheses' do
expect_no_offenses(<<~RUBY)
a = RSpec.shared_context('ruby 2.4', :ruby24) do
let(:ruby_version) { 2.4 }
end
RUBY
end
it 'accepts when the method call has no argumnets' do
expect_no_offenses(<<~RUBY)
RSpec.shared_context do
let(:ruby_version) { 2.4 }
end
RUBY
end
context 'when Layout/SingleLineBlockChain is enabled' do
let(:single_line_block_chain_enabled) { true }
it 'accepts a multiline block without a chained method call' do
expect_no_offenses(<<~RUBY)
f do
end
RUBY
end
end
context 'when Layout/SingleLineBlockChain is disabled' do
let(:single_line_block_chain_enabled) { false }
it 'accepts a multiline block without a chained method call' do
expect_no_offenses(<<~RUBY)
f do
end
RUBY
end
it 'accepts a method call chained onto a multiline block' do
expect_no_offenses(<<~RUBY)
e.select do |i|
i.cond?
end.join
a = e.select do |i|
i.cond?
end.join
e.select do |i|
i.cond?
end.join + []
RUBY
end
end
end
end
end