Skip to content

Commit

Permalink
Make Minitest/UnreachableAssertion aware of assert and refute p…
Browse files Browse the repository at this point in the history
…refix methods

This PR makes `Minitest/UnreachableAssertion` aware of `assert` and `refute` prefix methods
and removes `MinitestExplorationHelpers#assertion?` method.
It makes the detection to assertion methods consistent.

NOTE: The `MinitestExplorationHelpers#assertion?` method is redundant with
`MinitestExplorationHelpers#assertion_method?` OTOH, the implementation uses
`MinitestExplorationHelpers#assertion` instead of `MinitestExplorationHelpers#assertion_method?`.
Maybe it's enough to verify that it starts with `assert` or `refute` instead of
exact match of the known assertion method names.
  • Loading branch information
koic committed Jan 24, 2022
1 parent 4b4aa33 commit bfdadd2
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 28 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#158](https://github.com/rubocop/rubocop-minitest/pull/158): Make `Minitest/UnreachableAssertion` aware of `assert` and `refute` prefix methods. ([@koic][])
2 changes: 1 addition & 1 deletion lib/rubocop/cop/minitest/assertion_in_lifecycle_hook.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def on_class(class_node)

lifecycle_hooks(class_node).each do |hook_node|
hook_node.each_descendant(:send) do |node|
if assertion?(node)
if assertion_method?(node)
message = format(MSG, assertion: node.method_name, hook: hook_node.method_name)
add_offense(node, message: message)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/minitest/multiple_assertions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def on_class(class_node)
private

def assertions_count(node)
base = assertion?(node) ? 1 : 0
base = assertion_method?(node) ? 1 : 0
base + node.each_child_node.sum { |c| assertions_count(c) }
end

Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/minitest/no_assertions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def on_class(class_node)
private

def assertions_count(node)
base = assertion?(node) ? 1 : 0
base = assertion_method?(node) ? 1 : 0
base + node.each_child_node.sum { |c| assertions_count(c) }
end
end
Expand Down
6 changes: 2 additions & 4 deletions lib/rubocop/cop/minitest/unreachable_assertion.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,9 @@ def on_block(node)

last_node = body.begin_type? ? body.children.last : body
return unless last_node.send_type?
return unless assertion_method?(last_node)

method_name = last_node.method_name
return unless assertion_method?(method_name)

add_offense(last_node, message: format(MSG, assertion_method: method_name))
add_offense(last_node, message: format(MSG, assertion_method: last_node.method_name))
end
end
end
Expand Down
30 changes: 9 additions & 21 deletions lib/rubocop/cop/mixin/minitest_exploration_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,6 @@ module MinitestExplorationHelpers

ASSERTION_PREFIXES = %w[assert refute].freeze

ASSERTION_METHODS = %i[
assert assert_empty assert_equal assert_in_delta assert_in_epsilon assert_includes assert_instance_of
assert_kind_of assert_match assert_nil assert_operator assert_output assert_path_exists assert_predicate
assert_raises assert_respond_to assert_same assert_send assert_silent assert_throws
refute refute_empty refute_equal refute_in_delta refute_in_epsilon refute_includes refute_instance_of
refute_kind_of refute_match refute_nil refute_operator refute_path_exists refute_predicate
refute_respond_to refute_same
].freeze

FLUNK = 'flunk'

LIFECYCLE_HOOK_METHODS = %i[
before_setup
setup
Expand Down Expand Up @@ -84,19 +73,18 @@ def assertions(def_node)
method_def.each_child_node(:send)
end

send_nodes.select { |send_node| assertion?(send_node) }
send_nodes.select { |send_node| assertion_method?(send_node) }
end

def assertion?(node)
node.send_type? &&
ASSERTION_PREFIXES.any? do |prefix|
method_name = node.method_name.to_s
method_name == FLUNK || method_name.start_with?(prefix)
end
end
def assertion_method?(node)
return false unless node.send_type?

ASSERTION_PREFIXES.any? do |prefix|
method_name = node.method_name

def assertion_method?(method_name)
method_name == FLUNK || ASSERTION_METHODS.include?(method_name)
# TODO: Remove the fllowing `to_s` since Ruby 2.7 that supports `Symbol#start_with?`.
method_name.to_s.start_with?(prefix) || node.method?(:flunk)
end
end

def lifecycle_hook_method?(node)
Expand Down

0 comments on commit bfdadd2

Please sign in to comment.