Skip to content

Commit

Permalink
Merge pull request #298 from amomchilov/AssertKindOf-is_a
Browse files Browse the repository at this point in the history
`AssertKindOf`: also replace `is_a?` as well
  • Loading branch information
koic authored Jan 12, 2024
2 parents 3fa043a + 8627375 commit 4218ced
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 3 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/pull/298): Extend `Minitest/AssertKindOf` to also correct `assert(object.is_a?(Class))`. ([@amomchilov][])
2 changes: 2 additions & 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: '<<next>>'

Minitest/AssertMatch:
Description: 'This cop enforces the test to use `assert_match` instead of using `assert(matcher.match(object))`.'
Expand Down Expand Up @@ -252,6 +253,7 @@ Minitest/RefuteKindOf:
StyleGuide: 'https://github.com/rubocop/minitest-style-guide#refute-kind-of'
Enabled: 'pending'
VersionAdded: '0.10'
VersionChanged: '<<next>>'

Minitest/RefuteMatch:
Description: 'This cop enforces the test to use `refute_match` instead of using `refute(matcher.match(object))`.'
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
7 changes: 6 additions & 1 deletion lib/rubocop/cop/minitest/refute_kind_of.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,19 @@ module Minitest
# refute(object.kind_of?(Class))
# refute(object.kind_of?(Class), 'message')
#
# # bad
# # `is_a?` is an alias for `kind_of?`
# refute(object.is_of?(Class))
# refute(object.is_of?(Class), 'message')
#
# # good
# refute_kind_of(Class, object)
# refute_kind_of(Class, object, 'message')
#
class RefuteKindOf < Base
extend MinitestCopRule

define_rule :refute, target_method: :kind_of?, inverse: true
define_rule :refute, target_method: %i[kind_of? is_a?], preferred_method: :refute_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
40 changes: 39 additions & 1 deletion test/rubocop/cop/minitest/refute_kind_of_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,26 @@ def test_do_something
RUBY
end

def test_registers_offense_when_using_refute_with_kind_of_and_message
def test_registers_offense_when_using_refute_with_is_a
assert_offense(<<~RUBY)
class FooTest < Minitest::Test
def test_do_something
refute(object.is_a?(SomeClass))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `refute_kind_of(SomeClass, object)`.
end
end
RUBY

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

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

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

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

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

0 comments on commit 4218ced

Please sign in to comment.