Sha256: 1b664025991e9c93ec6cb94d010d5a4a1ccb3310e9ee4f47ee86f85408ca40a6

Contents?: true

Size: 1.73 KB

Versions: 19

Compression:

Stored size: 1.73 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Minitest
      # Enforces that test method names start with `test_` prefix.
      # It aims to prevent tests that aren't executed by forgetting to start test method name with `test_`.
      #
      # @example
      #   # bad
      #   class FooTest < Minitest::Test
      #     def does_something
      #       assert_equal 42, do_something
      #     end
      #   end
      #
      #   # good
      #   class FooTest < Minitest::Test
      #     def test_does_something
      #       assert_equal 42, do_something
      #     end
      #   end
      #
      #   # good
      #   class FooTest < Minitest::Test
      #     def helper_method(argument)
      #     end
      #   end
      #
      class TestMethodName < Base
        include MinitestExplorationHelpers
        include DefNode
        extend AutoCorrector

        MSG = 'Test method name should start with `test_` prefix.'

        def on_class(class_node)
          return unless test_class?(class_node)

          class_def_nodes(class_node).each do |node|
            next unless offense?(node)

            test_method_name = node.loc.name

            add_offense(test_method_name) do |corrector|
              corrector.replace(test_method_name, "test_#{node.method_name}")
            end
          end
        end

        private

        def offense?(node)
          return false if assertions(node).none?

          public?(node) && node.arguments.empty? && !test_method_name?(node) && !lifecycle_hook_method?(node)
        end

        def public?(node)
          !non_public?(node)
        end

        def test_method_name?(node)
          node.method_name.to_s.start_with?('test_')
        end
      end
    end
  end
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
rubocop-minitest-0.36.0 lib/rubocop/cop/minitest/test_method_name.rb
rubocop-minitest-0.35.1 lib/rubocop/cop/minitest/test_method_name.rb
rubocop-minitest-0.35.0 lib/rubocop/cop/minitest/test_method_name.rb
rubocop-minitest-0.34.5 lib/rubocop/cop/minitest/test_method_name.rb
rubocop-minitest-0.34.4 lib/rubocop/cop/minitest/test_method_name.rb
rubocop-minitest-0.34.3 lib/rubocop/cop/minitest/test_method_name.rb
rubocop-minitest-0.34.2 lib/rubocop/cop/minitest/test_method_name.rb
rubocop-minitest-0.34.1 lib/rubocop/cop/minitest/test_method_name.rb
rubocop-minitest-0.34.0 lib/rubocop/cop/minitest/test_method_name.rb
rubocop-minitest-0.33.0 lib/rubocop/cop/minitest/test_method_name.rb
rubocop-minitest-0.32.2 lib/rubocop/cop/minitest/test_method_name.rb
rubocop-minitest-0.32.1 lib/rubocop/cop/minitest/test_method_name.rb
rubocop-minitest-0.32.0 lib/rubocop/cop/minitest/test_method_name.rb
rubocop-minitest-0.31.1 lib/rubocop/cop/minitest/test_method_name.rb
rubocop-minitest-0.31.0 lib/rubocop/cop/minitest/test_method_name.rb
rubocop-minitest-0.30.0 lib/rubocop/cop/minitest/test_method_name.rb
rubocop-minitest-0.29.0 lib/rubocop/cop/minitest/test_method_name.rb
rubocop-minitest-0.28.0 lib/rubocop/cop/minitest/test_method_name.rb
rubocop-minitest-0.27.0 lib/rubocop/cop/minitest/test_method_name.rb