rubocop-minitest icon indicating copy to clipboard operation
rubocop-minitest copied to clipboard

Adding new checks for assert_in and refute_in to mirror the assert_includes and refute_includes checks

Open tayjohno opened this issue 3 years ago • 1 comments

So there's already a assert_includes and refute_includes which each respectively verify that you use assert_includes over assert(collection.includes?(object)), but there's another method we can check for here, which I think should be helpful. (I've been using a similar check to this at my organization and decided it would be nice to contribute it upstream).

# bad
assert(object.in?(collection))
assert(object.in?(collection), 'message')

# good
assert_includes(collection, object)
assert_includes(collection, object, 'message')

I recognize that Object#in? is implemented in Rails, so this cop won't apply to those not using the Rails framework, but it's certainly PRIMARILY a Minitest linter, so I figured this was still the place to implement it.

The implementation of Object#in? in Rails is to simply flip the arguments and call .include? so these auto-corrects should be safe to change, so long as the in? method being matched is truly the Rails Object#in?

From apidoc link above:

# File activesupport/lib/active_support/core_ext/object/inclusion.rb, line 12
  def in?(another_object)
    another_object.include?(self)
  rescue NoMethodError
    raise ArgumentError.new("The parameter passed to #in? must respond to #include?")
  end

Before submitting the PR make sure the following are checked:

  • [x] Wrote good commit messages.
  • [x] Commit message starts with [Fix #issue-number] (if the related issue exists).
  • [x] Feature branch is up-to-date with master (if not - rebase it).
  • [x] Squashed related commits together.
  • [x] Added tests.
  • [x] Added an entry to the Changelog if the new code introduces user-observable changes. See changelog entry format.
  • [x] The PR relates to only one subject with a clear title and description in grammatically correct, complete sentences.
  • [x] Run bundle exec rake default. It executes all tests and RuboCop for itself, and generates the documentation.

tayjohno avatar Jun 25 '21 16:06 tayjohno

It may be better to extend Minitest/AssertIncludes and Minitest/RefuteIncludes instead of adding new cops.

  • https://github.com/rubocop/rubocop-minitest/blob/master/lib/rubocop/cop/minitest/assert_includes.rb
  • https://github.com/rubocop/rubocop-minitest/blob/master/lib/rubocop/cop/minitest/refute_includes.rb

These are suitable as rolls.

koic avatar Jul 05 '21 18:07 koic

I'm going to close this PR because no activity seen. Anyway it can restart with the following approach: https://github.com/rubocop/rubocop-minitest/pull/138#issuecomment-874270105

koic avatar Jan 06 '23 05:01 koic