forked from rubocop/rubocop
/
arguments_forwarding_spec.rb
253 lines (223 loc) · 6.57 KB
/
arguments_forwarding_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
# frozen_string_literal: true
RSpec.describe RuboCop::Cop::Style::ArgumentsForwarding, :config do
context 'TargetRubyVersion <= 2.6', :ruby26 do
it 'does not registers an offense when using restarg with block arg' do
expect_no_offenses(<<~RUBY)
def foo(*args, &block)
bar(*args, &block)
end
RUBY
end
end
context 'TargetRubyVersion >= 2.7', :ruby27 do
it 'registers an offense when using restarg and block arg' do
expect_offense(<<~RUBY)
def foo(*args, &block)
^^^^^^^^^^^^^ Use arguments forwarding.
bar(*args, &block)
^^^^^^^^^^^^^ Use arguments forwarding.
end
RUBY
expect_correction(<<~RUBY)
def foo(...)
bar(...)
end
RUBY
end
it 'registers an offense when using restarg, kwargs and block arg' do
expect_offense(<<~RUBY)
def foo(*args, **kwargs, &block)
^^^^^^^^^^^^^^^^^^^^^^^ Use arguments forwarding.
bar(*args, **kwargs, &block)
^^^^^^^^^^^^^^^^^^^^^^^ Use arguments forwarding.
end
RUBY
expect_correction(<<~RUBY)
def foo(...)
bar(...)
end
RUBY
end
it 'registers an offense when passing restarg and block arg in defs' do
expect_offense(<<~RUBY)
def self.foo(*args, &block)
^^^^^^^^^^^^^ Use arguments forwarding.
bar(*args, &block)
^^^^^^^^^^^^^ Use arguments forwarding.
end
RUBY
expect_correction(<<~RUBY)
def self.foo(...)
bar(...)
end
RUBY
end
it 'registers an offense when the parentheses of arguments are omitted' do
expect_offense(<<~RUBY)
def foo *args, &block
^^^^^^^^^^^^^ Use arguments forwarding.
bar *args, &block
^^^^^^^^^^^^^ Use arguments forwarding.
end
RUBY
# A method definition that uses forwarding arguments without parentheses
# is a syntax error. e.g. `def do_something ...`
# Therefore it enforces parentheses with auto-correction.
expect_correction(<<~RUBY)
def foo(...)
bar(...)
end
RUBY
end
it 'registers an offense when forwarding to a method in block' do
expect_offense(<<~RUBY)
def foo(*args, &block)
^^^^^^^^^^^^^ Use arguments forwarding.
do_something do
bar(*args, &block)
^^^^^^^^^^^^^ Use arguments forwarding.
end
end
RUBY
expect_correction(<<~RUBY)
def foo(...)
do_something do
bar(...)
end
end
RUBY
end
it 'registers an offense when delegating' do
expect_offense(<<~RUBY)
def foo(*args, &block)
^^^^^^^^^^^^^ Use arguments forwarding.
obj.bar(*args, &block)
^^^^^^^^^^^^^ Use arguments forwarding.
end
RUBY
end
it 'does not register an offense when using arguments forwarding' do
expect_no_offenses(<<~RUBY)
def foo(...)
bar(...)
end
RUBY
end
it 'does not register an offense when different arguments are used' do
expect_no_offenses(<<~RUBY)
def foo(*args, &block)
bar(*args)
end
RUBY
end
it 'does not register an offense when different argument names are used' do
expect_no_offenses(<<~RUBY)
def foo(*args, &block)
bar(*arguments, &block)
end
RUBY
end
it 'does not register an offense when the restarg is overwritten' do
expect_no_offenses(<<~RUBY)
def foo(*args, **kwargs, &block)
args = new_args
bar(*args, **kwargs, &block)
end
RUBY
end
it 'does not register an offense when the kwarg is overwritten' do
expect_no_offenses(<<~RUBY)
def foo(*args, **kwargs, &block)
kwargs = new_kwargs
bar(*args, **kwargs, &block)
end
RUBY
end
it 'does not register an offense when the block arg is overwritten' do
expect_no_offenses(<<~RUBY)
def foo(*args, **kwargs, &block)
block = new_block
bar(*args, **kwargs, &block)
end
RUBY
end
it 'does not register an offense when using the restarg outside forwarding method arguments' do
expect_no_offenses(<<~RUBY)
def foo(*args, **kwargs, &block)
args.do_something
bar(*args, **kwargs, &block)
end
RUBY
end
it 'does not register an offense when assigning the restarg outside forwarding method arguments' do
expect_no_offenses(<<~'RUBY')
def foo(*args, &block)
var = args
foo(*args, &block)
end
RUBY
end
it 'does not register an offense when referencing the restarg outside forwarding method arguments' do
expect_no_offenses(<<~RUBY)
def foo(*args, &block)
args
foo(*args, &block)
end
RUBY
end
it 'does not register an offense when body of method definition is empty' do
expect_no_offenses(<<~'RUBY')
def foo(*args, &block)
end
RUBY
end
context 'AllowOnlyRestArgument: true' do
let(:cop_config) { { 'AllowOnlyRestArgument' => true } }
it 'does not register an offense when using only rest arg' do
expect_no_offenses(<<~RUBY)
def foo(*args)
bar(*args)
end
RUBY
end
it 'does not register an offense when using only kwrest arg' do
expect_no_offenses(<<~RUBY)
def foo(**kwargs)
bar(**kwargs)
end
RUBY
end
end
context 'AllowOnlyRestArgument: false' do
let(:cop_config) { { 'AllowOnlyRestArgument' => false } }
it 'registers an offense when using only rest arg' do
expect_offense(<<~RUBY)
def foo(*args)
^^^^^ Use arguments forwarding.
bar(*args)
^^^^^ Use arguments forwarding.
end
RUBY
expect_correction(<<~RUBY)
def foo(...)
bar(...)
end
RUBY
end
it 'registers an offense when using only kwrest arg' do
expect_offense(<<~RUBY)
def foo(**kwargs)
^^^^^^^^ Use arguments forwarding.
bar(**kwargs)
^^^^^^^^ Use arguments forwarding.
end
RUBY
expect_correction(<<~RUBY)
def foo(...)
bar(...)
end
RUBY
end
end
end
end