Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance AssertSame/RefuteSame to check for object_id comparison #276

Merged
merged 1 commit into from Nov 28, 2023

Conversation

fatkodima
Copy link
Contributor

It is a very popular pattern:

# bad
assert_equal(expected.object_id, actual.object_id)

# good
assert_same(expected, actual)

Tested on rails/rails codebase and got 11 offenses.

# Provide a method to define offense rule for Minitest cops.
module AssertRefuteSameHelper
def define_rule(assertion_method)
class_eval(<<~RUBY, __FILE__, __LINE__ + 1)
Copy link
Member

Choose a reason for hiding this comment

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

Since it's specialized for Minitest/AssertSame and Minitest/RefuteSame, there's likely no need for metaprogramming with eval.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

But we define different things depending on where we use this method.

Copy link
Member

@koic koic Nov 27, 2023

Choose a reason for hiding this comment

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

If abstraction is needed, MinitestCopRule module should be extended. In this AssertRefuteSameHelper module implementation approach, please write a concrete implementation instead of using eval.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated.

@koic koic merged commit d0c87f2 into rubocop:master Nov 28, 2023
11 checks passed
@koic
Copy link
Member

koic commented Nov 28, 2023

Thanks!

@fatkodima fatkodima deleted the assert_same-with-object-id branch November 28, 2023 06:24
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.

None yet

2 participants