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

feat: support *_includes assertions in RSpec/Rails/MinitestAssertions #1779

Merged
merged 1 commit into from
Jan 12, 2024

Conversation

G-Rath
Copy link
Contributor

@G-Rath G-Rath commented Jan 11, 2024

Branched off #1778

This adds support for the includes assertions

related to rubocop/rubocop-rspec_rails#7


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.
  • Updated documentation.
  • Added an entry to the CHANGELOG.md 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).

If you have created a new cop:

  • Added the new cop to config/default.yml.
  • The cop is configured as Enabled: pending in config/default.yml.
  • The cop is configured as Enabled: true in .rubocop.yml.
  • The cop documents examples of good and bad code.
  • The tests assert both that bad code is reported and that good code is not reported.
  • Set VersionAdded: "<<next>>" in default/config.yml.

If you have modified an existing cop's configuration options:

  • Set VersionChanged: "<<next>>" in config/default.yml.

(send nil? {:assert_empty :assert_not_empty :refute_empty} $_ $_?)
PATTERN

def on_send(node) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've just disabled AbcSize for now because I don't want to go too far from #1778 until it's landed, but I think a good next step for improving this could be to move the node matchers / block into the assertion classes too, so it becomes something like:

EqualAssertion.check(node)
IncludesAssertion.check(node)
NilAssertion.check(node)
EmptyAssertion.check(node)
# ...

(which you could probably take further by having an ASSERTION_CHECKERS array, and then the rest of this rule just iterates over that in a few places to pull through node matchers, restrict sends, check, etc)

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, thank you!

CHANGELOG.md Outdated Show resolved Hide resolved
@@ -30,66 +30,146 @@ class MinitestAssertions < Base
RESTRICT_ON_SEND = %i[
assert_equal
assert_not_equal
refute_equal
assert_includes
assert_not_includes
Copy link
Member

Choose a reason for hiding this comment

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

Does it exist?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@@ -28,11 +30,14 @@ class MinitestAssertions < Base
RESTRICT_ON_SEND = %i[
assert_equal
assert_not_equal
assert_includes
assert_not_includes
assert_nil
assert_not_nil
Copy link
Member

Choose a reason for hiding this comment

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

There’s an “a-ha” moment, since all those assert_no_* are from test-unit, not minitest.

But I’d say we don’t care much if there’s a corresponding native RSpec expectation/matcher we can suggest using instead.

test-unit assertions https://www.rubydoc.info/gems/test-unit/2.3.2/Test/Unit/Assertions
Minitest assertions https://ruby-doc.org/stdlib-3.0.0/libdoc/minitest/rdoc/Minitest/Assertions.html

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yea though also they're defined by Rails which is actually why I was including them: https://github.com/rails/rails/blob/main/activesupport/lib/active_support/test_case.rb#L206

@G-Rath G-Rath force-pushed the support-assert-includes branch 3 times, most recently from a58d40c to 9a97e40 Compare January 12, 2024 01:54
@G-Rath G-Rath marked this pull request as ready for review January 12, 2024 01:54
@G-Rath G-Rath requested a review from a team as a code owner January 12, 2024 01:54
@G-Rath G-Rath force-pushed the support-assert-includes branch from 9a97e40 to 847e6a3 Compare January 12, 2024 01:55
Copy link
Member

@ydah ydah 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. Thank you!

@ydah ydah merged commit d94aca1 into rubocop:master Jan 12, 2024
24 checks passed
@G-Rath G-Rath deleted the support-assert-includes branch January 12, 2024 02:02
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