module RuboCop::Cop::MinitestExplorationHelpers

Helper methods for different explorations against test files and test cases.

Constants

ASSERTION_METHODS
ASSERTION_PREFIXES
FLUNK
LIFECYCLE_HOOK_METHODS

Private Instance Methods

assertion?(node) click to toggle source
# File lib/rubocop/cop/mixin/minitest_exploration_helpers.rb, line 90
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
assertion_method?(method_name) click to toggle source
# File lib/rubocop/cop/mixin/minitest_exploration_helpers.rb, line 98
def assertion_method?(method_name)
  method_name == FLUNK || ASSERTION_METHODS.include?(method_name)
end
assertions(def_node) click to toggle source
# File lib/rubocop/cop/mixin/minitest_exploration_helpers.rb, line 76
def assertions(def_node)
  method_def = def_node.body
  return [] unless method_def

  send_nodes =
    if method_def.send_type?
      [method_def]
    else
      method_def.each_child_node(:send)
    end

  send_nodes.select { |send_node| assertion?(send_node) }
end
class_def_nodes(class_node) click to toggle source
# File lib/rubocop/cop/mixin/minitest_exploration_helpers.rb, line 61
def class_def_nodes(class_node)
  class_def = class_node.body
  return [] unless class_def

  if class_def.def_type?
    [class_def]
  else
    class_def.each_child_node(:def).to_a
  end
end
lifecycle_hook_method?(node) click to toggle source
# File lib/rubocop/cop/mixin/minitest_exploration_helpers.rb, line 102
def lifecycle_hook_method?(node)
  node.def_type? && LIFECYCLE_HOOK_METHODS.include?(node.method_name)
end
lifecycle_hooks(class_node) click to toggle source
# File lib/rubocop/cop/mixin/minitest_exploration_helpers.rb, line 56
def lifecycle_hooks(class_node)
  class_def_nodes(class_node)
    .select { |def_node| lifecycle_hook_method?(def_node) }
end
test_case?(node) click to toggle source
# File lib/rubocop/cop/mixin/minitest_exploration_helpers.rb, line 39
def test_case?(node)
  return false unless node&.def_type? && test_case_name?(node.method_name)

  class_ancestor = node.each_ancestor(:class).first
  test_class?(class_ancestor)
end
test_case_name?(name) click to toggle source
# File lib/rubocop/cop/mixin/minitest_exploration_helpers.rb, line 72
def test_case_name?(name)
  name.to_s.start_with?('test_')
end
test_cases(class_node) click to toggle source
# File lib/rubocop/cop/mixin/minitest_exploration_helpers.rb, line 46
def test_cases(class_node)
  test_cases = class_def_nodes(class_node).select { |def_node| test_case_name?(def_node.method_name) }

  # Support Active Support's `test 'example' { ... }` method.
  # https://api.rubyonrails.org/classes/ActiveSupport/Testing/Declarative.html
  test_blocks = class_node.each_descendant(:block).select { |block_node| block_node.method?(:test) }

  test_cases + test_blocks
end
test_class?(class_node) click to toggle source
# File lib/rubocop/cop/mixin/minitest_exploration_helpers.rb, line 35
def test_class?(class_node)
  class_node.parent_class && class_node.identifier.source.end_with?('Test')
end