Skip to content

Conversation

@dduugg
Copy link
Contributor

@dduugg dduugg commented Jan 6, 2020

Adds an autocorrect to RSpec/ExpectActual, swapping the arguments to expect and eq when possible.


Before submitting the PR make sure the following are checked:

  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Added an entry to the changelog if the new code introduces user-observable changes.
  • The build (bundle exec rake) passes (be sure to run this locally, since it may produce updated documentation that you will need to commit).

Copy link
Member

@pirj pirj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me.
Thanks for the contribution!

Copy link
Member

@pirj pirj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Few minor notes.

expectation = node.parent.parent
rhs = expectation.children.last
return unless rhs.is_a?(RuboCop::AST::MethodDispatchNode)
return if rhs.method_name != :eq
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe also :==, :eql?, :equal?, and :be?
A culprit with :be is that it might not have an argument:

expect(3).to be

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @pirj, thanks for the suggestions. Could I trouble you to provide examples using the first three methods? I interpreted your suggestion as applying like so:

expect(123).to ==(bar)
expect(123).to eql?(bar)
expect(123).to equal?(bar)

…but that doesn't appear to be valid:

     Failure/Error: expect(123).to equal?(123)

     ArgumentError:
       The expect syntax does not support operator matchers, so you must pass a matcher to `#to`.

Copy link
Member

@pirj pirj Jan 8, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My bad. Should of course be:

expect(123).to be
expect(123).to be == 123
expect(123).to eql(123)
expect(123).to equal(123)

Thanks for the extra care.

@pirj
Copy link
Member

pirj commented Jan 10, 2020

Do you need a hand with this @dduugg ? Please let me know if you get stuck with something.

@dduugg
Copy link
Contributor Author

dduugg commented Jan 10, 2020

Hi @pirj I'll close this until I have more bandwidth to cover the various examples. It seemed much simpler when I was able to autocorrect the cop's spec and my code with ease 😸

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants