From 8627375c45688e0fe80903a478aa6f374268cd5e Mon Sep 17 00:00:00 2001 From: Alexander Momchilov Date: Wed, 10 Jan 2024 10:22:22 -0500 Subject: [PATCH] AssertKindOf: also replace `is_a?` ... likewise for RefuteKindOf. --- changelog/change_AssertKindOf_is_a.md | 1 + config/default.yml | 2 + lib/rubocop/cop/minitest/assert_kind_of.rb | 7 +++- lib/rubocop/cop/minitest/refute_kind_of.rb | 7 +++- .../cop/minitest/assert_kind_of_test.rb | 38 ++++++++++++++++++ .../cop/minitest/refute_kind_of_test.rb | 40 ++++++++++++++++++- 6 files changed, 92 insertions(+), 3 deletions(-) create mode 100644 changelog/change_AssertKindOf_is_a.md diff --git a/changelog/change_AssertKindOf_is_a.md b/changelog/change_AssertKindOf_is_a.md new file mode 100644 index 00000000..0fbcf137 --- /dev/null +++ b/changelog/change_AssertKindOf_is_a.md @@ -0,0 +1 @@ +* [#298](https://github.com/rubocop/rubocop-minitest/pull/298): Extend `Minitest/AssertKindOf` to also correct `assert(object.is_a?(Class))`. ([@amomchilov][]) diff --git a/config/default.yml b/config/default.yml index eeef7471..072de513 100644 --- a/config/default.yml +++ b/config/default.yml @@ -46,6 +46,7 @@ Minitest/AssertKindOf: StyleGuide: 'https://github.com/rubocop/minitest-style-guide#assert-kind-of' Enabled: 'pending' VersionAdded: '0.10' + VersionChanged: '<>' Minitest/AssertMatch: Description: 'This cop enforces the test to use `assert_match` instead of using `assert(matcher.match(object))`.' @@ -252,6 +253,7 @@ Minitest/RefuteKindOf: StyleGuide: 'https://github.com/rubocop/minitest-style-guide#refute-kind-of' Enabled: 'pending' VersionAdded: '0.10' + VersionChanged: '<>' Minitest/RefuteMatch: Description: 'This cop enforces the test to use `refute_match` instead of using `refute(matcher.match(object))`.' diff --git a/lib/rubocop/cop/minitest/assert_kind_of.rb b/lib/rubocop/cop/minitest/assert_kind_of.rb index a8f096b9..d1762557 100644 --- a/lib/rubocop/cop/minitest/assert_kind_of.rb +++ b/lib/rubocop/cop/minitest/assert_kind_of.rb @@ -11,6 +11,11 @@ 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') @@ -18,7 +23,7 @@ module Minitest 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 diff --git a/lib/rubocop/cop/minitest/refute_kind_of.rb b/lib/rubocop/cop/minitest/refute_kind_of.rb index e524bfe9..234e047c 100644 --- a/lib/rubocop/cop/minitest/refute_kind_of.rb +++ b/lib/rubocop/cop/minitest/refute_kind_of.rb @@ -11,6 +11,11 @@ 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') @@ -18,7 +23,7 @@ module Minitest 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 diff --git a/test/rubocop/cop/minitest/assert_kind_of_test.rb b/test/rubocop/cop/minitest/assert_kind_of_test.rb index ae89c200..93dd1b39 100644 --- a/test/rubocop/cop/minitest/assert_kind_of_test.rb +++ b/test/rubocop/cop/minitest/assert_kind_of_test.rb @@ -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 @@ -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 diff --git a/test/rubocop/cop/minitest/refute_kind_of_test.rb b/test/rubocop/cop/minitest/refute_kind_of_test.rb index 0014908a..fec7d3dc 100644 --- a/test/rubocop/cop/minitest/refute_kind_of_test.rb +++ b/test/rubocop/cop/minitest/refute_kind_of_test.rb @@ -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 @@ -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