Skip to content

Commit

Permalink
AssertKindOf: also replace is_a? as well
Browse files Browse the repository at this point in the history
  • Loading branch information
amomchilov committed Jan 10, 2024
1 parent 3fa043a commit 6b91ee1
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 2 deletions.
1 change: 1 addition & 0 deletions changelog/change_AssertKindOf_is_a.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#298](https://github.com/rubocop/rubocop-minitest/issues/298): Extend `Minitest/AssertKindOf` to also correct `assert(object.is_a?(Class))`. ([@amomchilov][])
1 change: 1 addition & 0 deletions config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ Minitest/AssertKindOf:
StyleGuide: 'https://github.com/rubocop/minitest-style-guide#assert-kind-of'
Enabled: 'pending'
VersionAdded: '0.10'
VersionChanged: '0.35.0'

Minitest/AssertMatch:
Description: 'This cop enforces the test to use `assert_match` instead of using `assert(matcher.match(object))`.'
Expand Down
7 changes: 6 additions & 1 deletion docs/modules/ROOT/pages/cops_minitest.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ assert_instance_of(Class, object, 'message')
| Yes
| Yes
| 0.10
| -
| 0.35.0
|===
Enforces the test to use `assert_kind_of(Class, object)`
Expand All @@ -218,6 +218,11 @@ over `assert(object.kind_of?(Class))`.
assert(object.kind_of?(Class))
assert(object.kind_of?(Class), 'message')
# bad
# `#is_a?` is an alias for `#kind_of?
assert(object.is_a?(Class))
assert(object.is_a?(Class), 'message')
# good
assert_kind_of(Class, object)
assert_kind_of(Class, object, 'message')
Expand Down
7 changes: 6 additions & 1 deletion lib/rubocop/cop/minitest/assert_kind_of.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,19 @@ module Minitest
# assert(object.kind_of?(Class))
# assert(object.kind_of?(Class), 'message')
#
# # bad
# # `#is_a?` is an alias for `#kind_of?
# assert(object.is_a?(Class))
# assert(object.is_a?(Class), 'message')
#
# # good
# assert_kind_of(Class, object)
# assert_kind_of(Class, object, 'message')
#
class AssertKindOf < Base
extend MinitestCopRule

define_rule :assert, target_method: :kind_of?, inverse: true
define_rule :assert, target_method: %i[kind_of? is_a?], preferred_method: :assert_kind_of, inverse: true
end
end
end
Expand Down
38 changes: 38 additions & 0 deletions test/rubocop/cop/minitest/assert_kind_of_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,25 @@ def test_do_something
RUBY
end

def test_registers_offense_when_using_assert_with_is_a
assert_offense(<<~RUBY)
class FooTest < Minitest::Test
def test_do_something
assert(object.is_a?(SomeClass))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `assert_kind_of(SomeClass, object)`.
end
end
RUBY

assert_correction(<<~RUBY)
class FooTest < Minitest::Test
def test_do_something
assert_kind_of(SomeClass, object)
end
end
RUBY
end

def test_registers_offense_when_using_assert_with_kind_of_and_message
assert_offense(<<~RUBY)
class FooTest < Minitest::Test
Expand All @@ -41,6 +60,25 @@ def test_do_something
RUBY
end

def test_registers_offense_when_using_assert_with_is_a_and_message
assert_offense(<<~RUBY)
class FooTest < Minitest::Test
def test_do_something
assert(object.is_a?(SomeClass), 'message')
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `assert_kind_of(SomeClass, object, 'message')`.
end
end
RUBY

assert_correction(<<~RUBY)
class FooTest < Minitest::Test
def test_do_something
assert_kind_of(SomeClass, object, 'message')
end
end
RUBY
end

def test_does_not_register_offense_when_using_assert_kind_of_method
assert_no_offenses(<<~RUBY)
class FooTest < Minitest::Test
Expand Down

0 comments on commit 6b91ee1

Please sign in to comment.