forked from rubocop/rubocop
/
method_call_with_args_parentheses_spec.rb
574 lines (478 loc) · 14.5 KB
/
method_call_with_args_parentheses_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
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
# frozen_string_literal: true
RSpec.describe RuboCop::Cop::Style::MethodCallWithArgsParentheses, :config do
subject(:cop) { described_class.new(config) }
context 'when EnforcedStyle is require_parentheses (default)' do
let(:cop_config) do
{ 'IgnoredMethods' => %w[puts] }
end
it 'accepts no parens in method call without args' do
expect_no_offenses('top.test')
end
it 'accepts parens in method call with args' do
expect_no_offenses('top.test(a, b)')
end
it 'accepts parens in method call with do-end blocks' do
expect_no_offenses(<<-RUBY.strip_indent)
foo(:arg) do
bar
end
RUBY
end
it 'register an offense for method call without parens' do
expect_offense(<<-RUBY.strip_indent)
top.test a, b
^^^^^^^^^^^^^ Use parentheses for method calls with arguments.
RUBY
end
it 'register an offense for non-receiver method call without parens' do
expect_offense(<<-RUBY.strip_indent)
def foo
test a, b
^^^^^^^^^ Use parentheses for method calls with arguments.
end
RUBY
end
it 'register an offense for methods starting with capital without parens' do
expect_offense(<<-RUBY.strip_indent)
def foo
Test a, b
^^^^^^^^^ Use parentheses for method calls with arguments.
end
RUBY
end
it 'register an offense for superclass call without parens' do
expect_offense(<<-RUBY.strip_indent)
def foo
super a
^^^^^^^ Use parentheses for method calls with arguments.
end
RUBY
end
it 'register no offense for superclass call without args' do
expect_no_offenses('super')
end
it 'register no offense for yield without args' do
expect_no_offenses('yield')
end
it 'register no offense for superclass call with parens' do
expect_no_offenses('super(a)')
end
it 'register an offense for yield without parens' do
expect_offense(<<-RUBY.strip_indent)
def foo
yield a
^^^^^^^ Use parentheses for method calls with arguments.
end
RUBY
end
it 'accepts no parens for operators' do
expect_no_offenses('top.test + a')
end
it 'accepts no parens for setter methods' do
expect_no_offenses('top.test = a')
end
it 'accepts no parens for unary operators' do
expect_no_offenses('!test')
end
it 'auto-corrects call by adding needed braces' do
new_source = autocorrect_source('top.test a')
expect(new_source).to eq('top.test(a)')
end
it 'auto-corrects superclass call by adding needed braces' do
new_source = autocorrect_source(<<-RUBY.strip_indent)
def foo
super a
end
RUBY
expect(new_source).to eq(<<-RUBY.strip_indent)
def foo
super(a)
end
RUBY
end
it 'auto-corrects yield by adding needed braces' do
new_source = autocorrect_source(<<-RUBY.strip_indent)
def foo
yield a
end
RUBY
expect(new_source).to eq(<<-RUBY.strip_indent)
def foo
yield(a)
end
RUBY
end
it 'auto-corrects fully parenthesized args by removing space' do
new_source = autocorrect_source(<<-RUBY.strip_indent)
top.eq (1 + 2)
RUBY
expect(new_source).to eq(<<-RUBY.strip_indent)
top.eq(1 + 2)
RUBY
end
it 'auto-corrects parenthesized args for local methods by removing space' do
new_source = autocorrect_source(<<-RUBY.strip_indent)
def foo
eq (1 + 2)
end
RUBY
expect(new_source).to eq(<<-RUBY.strip_indent)
def foo
eq(1 + 2)
end
RUBY
end
it 'auto-corrects call with multiple args by adding braces' do
new_source = autocorrect_source(<<-RUBY.strip_indent)
def foo
eq 1, (2 + 3)
eq 1, 2, 3
end
RUBY
expect(new_source).to eq(<<-RUBY.strip_indent)
def foo
eq(1, (2 + 3))
eq(1, 2, 3)
end
RUBY
end
it 'auto-corrects partially parenthesized args by adding needed braces' do
new_source = autocorrect_source(<<-RUBY.strip_indent)
top.eq (1 + 2) + 3
RUBY
expect(new_source).to eq(<<-RUBY.strip_indent)
top.eq((1 + 2) + 3)
RUBY
end
it 'auto-corrects calls with multiple args by adding needed braces' do
new_source = autocorrect_source(<<-RUBY.strip_indent)
top.eq (1 + 2), 3
RUBY
expect(new_source).to eq(<<-RUBY.strip_indent)
top.eq((1 + 2), 3)
RUBY
end
it 'auto-corrects calls where arg is method call' do
new_source = autocorrect_source(<<-RUBY.strip_indent)
def my_method
foo bar.baz(abc, xyz)
end
RUBY
expect(new_source).to eq(<<-RUBY.strip_indent)
def my_method
foo(bar.baz(abc, xyz))
end
RUBY
end
it 'auto-corrects calls where multiple args are method calls' do
new_source = autocorrect_source(<<-RUBY.strip_indent)
def my_method
foo bar.baz(abc, xyz), foo(baz)
end
RUBY
expect(new_source).to eq(<<-RUBY.strip_indent)
def my_method
foo(bar.baz(abc, xyz), foo(baz))
end
RUBY
end
it 'auto-corrects calls where the argument node is a constant' do
new_source = autocorrect_source(<<-RUBY.strip_indent)
def my_method
raise NotImplementedError
end
RUBY
expect(new_source).to eq(<<-RUBY.strip_indent)
def my_method
raise(NotImplementedError)
end
RUBY
end
it 'auto-corrects calls where the argument node is a number' do
new_source = autocorrect_source(<<-RUBY.strip_indent)
def my_method
sleep 1
end
RUBY
expect(new_source).to eq(<<-RUBY.strip_indent)
def my_method
sleep(1)
end
RUBY
end
it 'ignores method listed in IgnoredMethods' do
expect_no_offenses('puts :test')
end
context 'when inspecting macro methods' do
let(:cop_config) do
{ 'IgnoreMacros' => 'true' }
end
context 'in a class body' do
it 'does not register an offense' do
expect_no_offenses(<<-RUBY.strip_indent)
class Foo
bar :baz
end
RUBY
end
end
context 'in a module body' do
it 'does not register an offense' do
expect_no_offenses(<<-RUBY.strip_indent)
module Foo
bar :baz
end
RUBY
end
end
end
end
context 'when EnforcedStyle is omit_parentheses' do
let(:cop_config) do
{ 'EnforcedStyle' => 'omit_parentheses' }
end
it 'register an offense for parens in method call without args' do
expect_offense(<<-RUBY.strip_indent)
top.test()
^^ Omit parentheses for method calls with arguments.
RUBY
end
it 'register an offense for multi-line method calls' do
expect_offense(<<-RUBY.strip_indent)
test(
^ Omit parentheses for method calls with arguments.
foo: bar
)
RUBY
end
it 'register an offense for superclass call with parens' do
expect_offense(<<-RUBY.strip_indent)
def foo
super(a)
^^^ Omit parentheses for method calls with arguments.
end
RUBY
end
it 'register an offense for yield call with parens' do
expect_offense(<<-RUBY.strip_indent)
def foo
yield(a)
^^^ Omit parentheses for method calls with arguments.
end
RUBY
end
it 'register an offense for parens in the last chain' do
expect_offense(<<-RUBY.strip_indent)
foo().bar(3).wait(4)
^^^ Omit parentheses for method calls with arguments.
RUBY
end
it 'register an offense for parens in do-end blocks' do
expect_offense(<<-RUBY.strip_indent)
foo(:arg) do
^^^^^^ Omit parentheses for method calls with arguments.
bar
end
RUBY
end
it 'register an offense for hashes in keyword values' do
expect_offense(<<-RUBY.strip_indent)
method_call(hash: {foo: :bar})
^^^^^^^^^^^^^^^^^^^ Omit parentheses for method calls with arguments.
RUBY
end
it 'accepts no parens in method call without args' do
expect_no_offenses('top.test')
end
it 'accepts no parens in method call with args' do
expect_no_offenses('top.test 1, 2, foo: bar')
end
it 'accepts parens in method args' do
expect_no_offenses('top.test 1, 2, foo: bar(3)')
end
it 'accepts parens in nested method args' do
expect_no_offenses('top.test 1, 2, foo: [bar(3)]')
end
it 'accepts parens in calls with hash as arg' do
expect_no_offenses('top.test({foo: :bar})')
expect_no_offenses('top.test({foo: :bar}.merge(baz: :maz))')
expect_no_offenses('top.test(:first, {foo: :bar}.merge(baz: :maz))')
end
it 'accepts special lambda call syntax' do
expect_no_offenses('thing.()')
end
it 'accepts parens in chained method calls' do
expect_no_offenses('foo().bar(3).wait(4).it')
end
it 'accepts parens in chaining with operators' do
expect_no_offenses('foo().bar(3).wait(4) + 4')
end
it 'accepts parens in blocks with braces' do
expect_no_offenses('foo(1) { 2 }')
end
it 'accepts parens in calls with logical operators' do
expect_no_offenses('foo(a) && bar(b)')
expect_no_offenses('foo(a) || bar(b)')
end
it 'accepts parens in calls with args with logical operators' do
expect_no_offenses('foo(a, b || c)')
expect_no_offenses('foo a, b || c')
expect_no_offenses('foo a, b(1) || c(2, d(3))')
end
it 'accepts parens in args splat' do
expect_no_offenses('foo(*args)')
expect_no_offenses('foo *args')
expect_no_offenses('foo(**kwargs)')
expect_no_offenses('foo **kwargs')
end
it 'accepts parens in implicit #to_proc' do
expect_no_offenses('foo(&block)')
expect_no_offenses('foo &block')
end
it 'accepts parens in super without args' do
expect_no_offenses('super()')
end
it 'accepts parens in camel case method without args' do
expect_no_offenses('Array()')
end
it 'accepts parens in ternary condition calls' do
expect_no_offenses(<<-RUBY)
foo.include?(bar) ? bar : quux
RUBY
end
it 'accepts parens in args with ternary conditions' do
expect_no_offenses(<<-RUBY)
foo.include?(bar ? baz : quux)
RUBY
end
it 'accepts parens in splat calls' do
expect_no_offenses(<<-RUBY)
foo(*bar(args))
foo(**quux(args))
RUBY
end
it 'accepts parens in block passing calls' do
expect_no_offenses(<<-RUBY)
foo(&method(:args))
RUBY
end
it 'auto-corrects single-line calls' do
original = <<-RUBY.strip_indent
top.test(1, 2, foo: bar(3))
RUBY
expect(autocorrect_source(original)).to eq(<<-RUBY.strip_indent)
top.test 1, 2, foo: bar(3)
RUBY
end
it 'auto-corrects multi-line calls' do
original = <<-RUBY.strip_indent
foo(
bar: 3
)
RUBY
expect(autocorrect_source(original)).to eq(<<-RUBY.strip_indent)
foo \\
bar: 3
RUBY
end
it 'auto-corrects multi-line calls with trailing whitespace' do
original = <<-RUBY.strip_indent
foo(
bar: 3
)
RUBY
expect(autocorrect_source(original)).to eq(<<-RUBY.strip_indent)
foo \\
bar: 3
RUBY
end
it 'auto-corrects complex multi-line calls' do
original = <<-RUBY.strip_indent
foo(arg,
option: true
)
RUBY
expect(autocorrect_source(original)).to eq(<<-RUBY.strip_indent)
foo arg,
option: true
RUBY
end
it 'auto-corrects chained calls' do
original = <<-RUBY.strip_indent
foo().bar(3).wait(4)
RUBY
expect(autocorrect_source(original)).to eq(<<-RUBY.strip_indent)
foo().bar(3).wait 4
RUBY
end
context 'TargetRubyVersion >= 2.3', :ruby23 do
it 'accepts parens in chaining with safe operators' do
expect_no_offenses('Something.find(criteria: given)&.field')
end
end
context 'allowing parenthesis in chaining' do
let(:cop_config) do
{
'EnforcedStyle' => 'omit_parentheses',
'AllowParenthesesInChaining' => true
}
end
it 'register offense for single-line chaining without previous parens' do
expect_offense(<<-RUBY.strip_indent)
Rails.convoluted.example.logger.error("something")
^^^^^^^^^^^^^ Omit parentheses for method calls with arguments.
RUBY
end
it 'register offense for multi-line chaining without previous parens' do
expect_offense(<<-RUBY.strip_indent)
Rails
.convoluted
.example
.logger
.error("something")
^^^^^^^^^^^^^ Omit parentheses for method calls with arguments.
RUBY
end
it 'accepts parens in the last call if previous calls with parens' do
expect_no_offenses('foo().bar(3).wait 4')
end
it 'does not auto-correct if any previous call have parentheses' do
original = <<-RUBY.strip_indent
foo().bar(3).quux.wait(4)
RUBY
expect(autocorrect_source(original)).to eq(original)
end
it 'auto-correct if previous does calls have parentheses' do
original = <<-RUBY.strip_indent
foo.bar.wait(4)
RUBY
expect(autocorrect_source(original)).to eq(<<-RUBY.strip_indent)
foo.bar.wait 4
RUBY
end
end
context 'allowing parens in multi-line calls' do
let(:cop_config) do
{
'EnforcedStyle' => 'omit_parentheses',
'AllowParenthesesInMultilineCall' => true
}
end
it 'accepts parens for multi-line calls ' do
expect_no_offenses(<<-RUBY.strip_indent)
test(
foo: bar
)
RUBY
end
it 'does not auto-correct' do
original = <<-RUBY.strip_indent
foo(
bar: 3
)
RUBY
expect(autocorrect_source(original)).to eq(original)
end
end
end
end