-
-
Notifications
You must be signed in to change notification settings - Fork 3k
/
method_call_without_args_parentheses_spec.rb
161 lines (133 loc) · 4.02 KB
/
method_call_without_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
# frozen_string_literal: true
RSpec.describe RuboCop::Cop::Style::MethodCallWithoutArgsParentheses, :config do
it 'registers an offense for parens in method call without args' do
expect_offense(<<~RUBY)
top.test()
^^ Do not use parentheses for method calls with no arguments.
RUBY
expect_correction(<<~RUBY)
top.test
RUBY
end
it 'accepts parentheses for methods starting with an upcase letter' do
expect_no_offenses('Test()')
end
it 'accepts parens in method call with args' do
expect_no_offenses('top.test(a)')
end
it 'accepts special lambda call syntax' do
# Style/LambdaCall checks for this syntax
expect_no_offenses('thing.()')
end
it 'accepts parens after not' do
expect_no_offenses('not(something)')
end
context 'with IgnoredMethods' do
context 'with a string' do
let(:cop_config) { { 'IgnoredMethods' => %w[s] } }
it 'ignores method listed in IgnoredMethods' do
expect_no_offenses('s()')
end
end
context 'with a regex' do
let(:cop_config) { { 'IgnoredMethods' => [/test/] } }
it 'ignores method listed in IgnoredMethods' do
expect_no_offenses('my_test()')
end
end
end
context 'assignment to a variable with the same name' do
it 'accepts parens in local variable assignment' do
expect_no_offenses('test = test()')
end
it 'accepts parens in default argument assignment' do
expect_no_offenses(<<~RUBY)
def foo(test = test())
end
RUBY
end
it 'accepts parens in shorthand assignment' do
expect_no_offenses('test ||= test()')
end
it 'accepts parens in parallel assignment' do
expect_no_offenses('one, test = 1, test()')
end
it 'accepts parens in complex assignment' do
expect_no_offenses(<<~RUBY)
test = begin
case a
when b
c = test() if d
end
end
RUBY
end
end
it 'registers an offense for `obj.method ||= func()`' do
expect_offense(<<~RUBY)
obj.method ||= func()
^^ Do not use parentheses for method calls with no arguments.
RUBY
expect_correction(<<~RUBY)
obj.method ||= func
RUBY
end
it 'registers an offense for `obj.method &&= func()`' do
expect_offense(<<~RUBY)
obj.method &&= func()
^^ Do not use parentheses for method calls with no arguments.
RUBY
expect_correction(<<~RUBY)
obj.method &&= func
RUBY
end
it 'registers an offense for `obj.method += func()`' do
expect_offense(<<~RUBY)
obj.method += func()
^^ Do not use parentheses for method calls with no arguments.
RUBY
expect_correction(<<~RUBY)
obj.method += func
RUBY
end
# These will be offenses for the EmptyLiteral cop. The autocorrect loop will
# handle that.
it 'auto-corrects calls that could be empty literals' do
expect_offense(<<~RUBY)
Hash.new()
^^ Do not use parentheses for method calls with no arguments.
Array.new()
^^ Do not use parentheses for method calls with no arguments.
String.new()
^^ Do not use parentheses for method calls with no arguments.
RUBY
expect_correction(<<~RUBY)
Hash.new
Array.new
String.new
RUBY
end
context 'method call as argument' do
it 'accepts without parens' do
expect_no_offenses('_a = c(d.e)')
end
it 'registers an offense with empty parens' do
expect_offense(<<~RUBY)
_a = c(d())
^^ Do not use parentheses for method calls with no arguments.
RUBY
expect_correction(<<~RUBY)
_a = c(d)
RUBY
end
it 'registers an empty parens offense for multiple assignment' do
expect_offense(<<~RUBY)
_a, _b, _c = d(e())
^^ Do not use parentheses for method calls with no arguments.
RUBY
expect_correction(<<~RUBY)
_a, _b, _c = d(e)
RUBY
end
end
end