/
redundant_block_call_spec.rb
242 lines (213 loc) · 5.58 KB
/
redundant_block_call_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
# frozen_string_literal: true
RSpec.describe RuboCop::Cop::Performance::RedundantBlockCall, :config do
it 'registers and autocorrects an offense when `block.call` without arguments' do
expect_offense(<<~RUBY)
def method(&block)
block.call
^^^^^^^^^^ Use `yield` instead of `block.call`.
end
RUBY
expect_correction(<<~RUBY)
def method(&block)
yield
end
RUBY
end
it 'registers and autocorrects an offense when `block.call` with empty parentheses' do
expect_offense(<<~RUBY)
def method(&block)
block.call()
^^^^^^^^^^^^ Use `yield` instead of `block.call`.
end
RUBY
expect_correction(<<~RUBY)
def method(&block)
yield
end
RUBY
end
it 'registers and corrects an offense when using `block.call` in a class method' do
expect_offense(<<~RUBY)
def self.method(&block)
block.call
^^^^^^^^^^ Use `yield` instead of `block.call`.
end
RUBY
expect_correction(<<~RUBY)
def self.method(&block)
yield
end
RUBY
end
it 'registers and autocorrects an offense when `block.call` with arguments' do
expect_offense(<<~RUBY)
def method(&block)
block.call 1, 2
^^^^^^^^^^^^^^^ Use `yield` instead of `block.call`.
end
RUBY
expect_correction(<<~RUBY)
def method(&block)
yield 1, 2
end
RUBY
end
it 'registers and autocorrects an offense when multiple occurrences of `block.call` with arguments' do
expect_offense(<<~RUBY)
def method(&block)
block.call 1
^^^^^^^^^^^^ Use `yield` instead of `block.call`.
block.call 2
^^^^^^^^^^^^ Use `yield` instead of `block.call`.
end
RUBY
expect_correction(<<~RUBY)
def method(&block)
yield 1
yield 2
end
RUBY
end
it 'registers and autocorrects when even when block arg has a different name' do
expect_offense(<<~RUBY)
def method(&func)
func.call
^^^^^^^^^ Use `yield` instead of `func.call`.
end
RUBY
expect_correction(<<~RUBY)
def method(&func)
yield
end
RUBY
end
it 'accepts a block that is not `call`ed' do
expect_no_offenses(<<~RUBY)
def method(&block)
something.call
end
RUBY
end
it 'accepts an empty method body' do
expect_no_offenses(<<~RUBY)
def method(&block)
end
RUBY
end
it 'accepts another block being passed as the only arg' do
expect_no_offenses(<<~RUBY)
def method(&block)
block.call(&some_proc)
end
RUBY
end
it 'accepts when using `block.call` with block argument' do
expect_no_offenses(<<~RUBY)
def method(&block)
block.call { do_something }
end
RUBY
end
it 'accepts when using `block.call` with numbered block argument' do
expect_no_offenses(<<~RUBY)
def method(&block)
block.call { _1.do_something }
end
RUBY
end
it 'accepts another block being passed along with other args' do
expect_no_offenses(<<~RUBY)
def method(&block)
block.call(1, &some_proc)
end
RUBY
end
it 'accepts another block arg in at least one occurrence of block.call' do
expect_no_offenses(<<~RUBY)
def method(&block)
block.call(1, &some_proc)
block.call(2)
end
RUBY
end
it 'accepts an optional block that is defaulted' do
expect_no_offenses(<<~RUBY)
def method(&block)
block ||= ->(i) { puts i }
block.call(1)
end
RUBY
end
it 'accepts an optional block that is overridden by local variable' do
expect_no_offenses(<<~RUBY)
def method(&block)
block = ->(i) { puts i }
block.call(1)
end
RUBY
end
it 'accepts an optional block that is overridden by block variable' do
expect_no_offenses(<<~RUBY)
def method(&block)
->(i) { puts i }.then do |block|
block.call(1)
end
end
RUBY
end
it 'registers and corrects an offense when an optional block that is not overridden by block variable' do
expect_offense(<<~RUBY)
def method(&block)
->(i) { puts i }.then do |_block|
block.call(1)
^^^^^^^^^^^^^ Use `yield` instead of `block.call`.
end
end
RUBY
expect_correction(<<~RUBY)
def method(&block)
->(i) { puts i }.then do |_block|
yield(1)
end
end
RUBY
end
it 'formats the error message for func.call(1) correctly' do
expect_offense(<<~RUBY)
def method(&func)
func.call(1)
^^^^^^^^^^^^ Use `yield` instead of `func.call`.
end
RUBY
end
it 'registers and autocorrects an offense using parentheses when block.call uses parentheses' do
expect_offense(<<~RUBY)
def method(&block)
block.call(a, b)
^^^^^^^^^^^^^^^^ Use `yield` instead of `block.call`.
end
RUBY
expect_correction(<<~RUBY)
def method(&block)
yield(a, b)
end
RUBY
end
it 'registers and autocorrects an offense when the result of the call is used in a scope that requires parentheses' do
expect_offense(<<~RUBY)
def method(&block)
each_with_object({}) do |(key, value), acc|
acc.merge!(block.call(key) => rhs[value])
^^^^^^^^^^^^^^^ Use `yield` instead of `block.call`.
end
end
RUBY
expect_correction(<<~RUBY)
def method(&block)
each_with_object({}) do |(key, value), acc|
acc.merge!(yield(key) => rhs[value])
end
end
RUBY
end
end