Skip to content

Commit

Permalink
Extend matcher support in ExpectActual auto-correct
Browse files Browse the repository at this point in the history
  • Loading branch information
pirj committed Jan 15, 2020
1 parent 412e5c5 commit f152aaa
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 6 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

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

## 1.37.1 (2019-12-16)

Expand Down
13 changes: 9 additions & 4 deletions lib/rubocop/cop/rspec/expect_actual.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,16 @@ class ExpectActual < Cop
regexp
].freeze

SUPPORTED_MATCHERS = %i[eq eql equal be].freeze

def_node_matcher :expect_literal, <<~PATTERN
(send
(send nil? :expect $#literal?)
#{Runners::ALL.node_pattern_union}
$(send nil? ...)
{
(send (send nil? $:be) :== $_)
(send nil? $_ $_ ...)
}
)
PATTERN

Expand All @@ -56,11 +61,11 @@ def on_send(node)
end

def autocorrect(node)
argument, matcher = expect_literal(node)
actual, matcher, expected = expect_literal(node)
lambda do |corrector|
return if matcher.method_name != :eq
return unless SUPPORTED_MATCHERS.include?(matcher)

swap(corrector, argument, matcher.arguments.first)
swap(corrector, actual, expected)
end
end

Expand Down
88 changes: 87 additions & 1 deletion spec/rubocop/cop/rspec/expect_actual_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,93 @@
RUBY
end

it 'flags but does not autocorrect violations without eq' do
it 'ignores `be` with no argument' do
expect_no_offenses(<<~RUBY)
describe Foo do
it 'uses expect legitimately' do
expect(1).to be
end
end
RUBY
end

it 'flags `be` with an argument' do
expect_offense(<<~RUBY)
describe Foo do
it 'uses expect incorrectly' do
expect(true).to be(a)
^^^^ Provide the actual you are testing to `expect(...)`.
end
end
RUBY

expect_correction(<<~RUBY)
describe Foo do
it 'uses expect incorrectly' do
expect(a).to be(true)
end
end
RUBY
end

it 'flags `be ==`' do
expect_offense(<<~RUBY)
describe Foo do
it 'uses expect incorrectly' do
expect(1).to be == a
^ Provide the actual you are testing to `expect(...)`.
end
end
RUBY

expect_correction(<<~RUBY)
describe Foo do
it 'uses expect incorrectly' do
expect(a).to be == 1
end
end
RUBY
end

it 'flags with `eql` matcher' do
expect_offense(<<-RUBY)
describe Foo do
it 'uses expect incorrectly' do
expect(1).to eql(bar)
^ Provide the actual you are testing to `expect(...)`.
end
end
RUBY

expect_correction(<<-RUBY)
describe Foo do
it 'uses expect incorrectly' do
expect(bar).to eql(1)
end
end
RUBY
end

it 'flags with `equal` matcher' do
expect_offense(<<-RUBY)
describe Foo do
it 'uses expect incorrectly' do
expect(1).to equal(bar)
^ Provide the actual you are testing to `expect(...)`.
end
end
RUBY

expect_correction(<<-RUBY)
describe Foo do
it 'uses expect incorrectly' do
expect(bar).to equal(1)
end
end
RUBY
end

it 'flags but does not autocorrect violations for other matchers' do
expect_offense(<<-RUBY)
describe Foo do
it 'uses expect incorrectly' do
Expand Down

0 comments on commit f152aaa

Please sign in to comment.