Skip to content

Commit f152aaa

Browse files
committed
Extend matcher support in ExpectActual auto-correct
1 parent 412e5c5 commit f152aaa

File tree

3 files changed

+97
-6
lines changed

3 files changed

+97
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
* Fix `RSpec/InstanceVariable` detection inside custom matchers. ([@pirj][])
66
* Fix `RSpec/ScatteredSetup` to distinguish hooks with different metadata. ([@pirj][])
7-
* Add autocorrect support for `RSpec/ExpectActual` cop. ([@dduugg][])
7+
* Add autocorrect support for `RSpec/ExpectActual` cop. ([@dduugg][], [@pirj][])
88

99
## 1.37.1 (2019-12-16)
1010

lib/rubocop/cop/rspec/expect_actual.rb

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,16 @@ class ExpectActual < Cop
4141
regexp
4242
].freeze
4343

44+
SUPPORTED_MATCHERS = %i[eq eql equal be].freeze
45+
4446
def_node_matcher :expect_literal, <<~PATTERN
4547
(send
4648
(send nil? :expect $#literal?)
4749
#{Runners::ALL.node_pattern_union}
48-
$(send nil? ...)
50+
{
51+
(send (send nil? $:be) :== $_)
52+
(send nil? $_ $_ ...)
53+
}
4954
)
5055
PATTERN
5156

@@ -56,11 +61,11 @@ def on_send(node)
5661
end
5762

5863
def autocorrect(node)
59-
argument, matcher = expect_literal(node)
64+
actual, matcher, expected = expect_literal(node)
6065
lambda do |corrector|
61-
return if matcher.method_name != :eq
66+
return unless SUPPORTED_MATCHERS.include?(matcher)
6267

63-
swap(corrector, argument, matcher.arguments.first)
68+
swap(corrector, actual, expected)
6469
end
6570
end
6671

spec/rubocop/cop/rspec/expect_actual_spec.rb

Lines changed: 87 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,93 @@
207207
RUBY
208208
end
209209

210-
it 'flags but does not autocorrect violations without eq' do
210+
it 'ignores `be` with no argument' do
211+
expect_no_offenses(<<~RUBY)
212+
describe Foo do
213+
it 'uses expect legitimately' do
214+
expect(1).to be
215+
end
216+
end
217+
RUBY
218+
end
219+
220+
it 'flags `be` with an argument' do
221+
expect_offense(<<~RUBY)
222+
describe Foo do
223+
it 'uses expect incorrectly' do
224+
expect(true).to be(a)
225+
^^^^ Provide the actual you are testing to `expect(...)`.
226+
end
227+
end
228+
RUBY
229+
230+
expect_correction(<<~RUBY)
231+
describe Foo do
232+
it 'uses expect incorrectly' do
233+
expect(a).to be(true)
234+
end
235+
end
236+
RUBY
237+
end
238+
239+
it 'flags `be ==`' do
240+
expect_offense(<<~RUBY)
241+
describe Foo do
242+
it 'uses expect incorrectly' do
243+
expect(1).to be == a
244+
^ Provide the actual you are testing to `expect(...)`.
245+
end
246+
end
247+
RUBY
248+
249+
expect_correction(<<~RUBY)
250+
describe Foo do
251+
it 'uses expect incorrectly' do
252+
expect(a).to be == 1
253+
end
254+
end
255+
RUBY
256+
end
257+
258+
it 'flags with `eql` matcher' do
259+
expect_offense(<<-RUBY)
260+
describe Foo do
261+
it 'uses expect incorrectly' do
262+
expect(1).to eql(bar)
263+
^ Provide the actual you are testing to `expect(...)`.
264+
end
265+
end
266+
RUBY
267+
268+
expect_correction(<<-RUBY)
269+
describe Foo do
270+
it 'uses expect incorrectly' do
271+
expect(bar).to eql(1)
272+
end
273+
end
274+
RUBY
275+
end
276+
277+
it 'flags with `equal` matcher' do
278+
expect_offense(<<-RUBY)
279+
describe Foo do
280+
it 'uses expect incorrectly' do
281+
expect(1).to equal(bar)
282+
^ Provide the actual you are testing to `expect(...)`.
283+
end
284+
end
285+
RUBY
286+
287+
expect_correction(<<-RUBY)
288+
describe Foo do
289+
it 'uses expect incorrectly' do
290+
expect(bar).to equal(1)
291+
end
292+
end
293+
RUBY
294+
end
295+
296+
it 'flags but does not autocorrect violations for other matchers' do
211297
expect_offense(<<-RUBY)
212298
describe Foo do
213299
it 'uses expect incorrectly' do

0 commit comments

Comments
 (0)