Sha256: 40646c09c473fbb941fc7c7b4a18e940935a1f2001f36129527aad4da289489a

Contents?: true

Size: 1.45 KB

Versions: 4

Compression:

Stored size: 1.45 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Minitest
      # Checks for the use of test methods outside of a test class.
      #
      # Test methods should be defined within a test class to ensure their execution.
      #
      # NOTE: This cop assumes that classes whose superclass name includes the word
      # "`Test`" are test classes, in order to prevent false positives.
      #
      # @example
      #
      #   # bad
      #   class FooTest < Minitest::Test
      #   end
      #   def test_method_should_be_inside_test_class
      #   end
      #
      #   # good
      #   class FooTest < Minitest::Test
      #     def test_method_should_be_inside_test_class
      #     end
      #   end
      #
      class NonExecutableTestMethod < Base
        include MinitestExplorationHelpers

        MSG = 'Test method should be defined inside a test class to ensure execution.'

        def on_def(node)
          return if !test_method?(node) || !use_test_class?
          return if node.left_siblings.none? { |sibling| possible_test_class?(sibling) }

          add_offense(node)
        end

        def use_test_class?
          root_node = processed_source.ast

          root_node.each_descendant(:class).any? { |class_node| test_class?(class_node) }
        end

        def possible_test_class?(node)
          node.is_a?(AST::ClassNode) && test_class?(node) && node.parent_class.source.include?('Test')
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
rubocop-minitest-0.34.3 lib/rubocop/cop/minitest/non_executable_test_method.rb
rubocop-minitest-0.34.2 lib/rubocop/cop/minitest/non_executable_test_method.rb
rubocop-minitest-0.34.1 lib/rubocop/cop/minitest/non_executable_test_method.rb
rubocop-minitest-0.34.0 lib/rubocop/cop/minitest/non_executable_test_method.rb