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

AssertKindOf: also replace is_a? as well #298

Merged
merged 1 commit into from
Jan 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
amomchilov marked this conversation as resolved.
Show resolved Hide resolved
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