Sha256: 549407a8b4607c1faef9cd566c38a85f09ea7beb72cb0405feaf21e59ed98529

Contents?: true

Size: 1.02 KB

Versions: 15

Compression:

Stored size: 1.02 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Minitest
      # Enforces the use of `skip` instead of `return` in test methods.
      #
      # @example
      #   # bad
      #   def test_something
      #     return if condition?
      #     assert_equal(42, something)
      #   end
      #
      #   # good
      #   def test_something
      #     skip if condition?
      #     assert_equal(42, something)
      #   end
      #
      class ReturnInTestMethod < Base
        include MinitestExplorationHelpers
        extend AutoCorrector

        MSG = 'Use `skip` instead of `return`.'

        def on_return(node)
          return unless node.ancestors.any? { |parent| test_case?(parent) }
          return if inside_block?(node)

          add_offense(node) do |corrector|
            corrector.replace(node, 'skip')
          end
        end

        private

        def inside_block?(node)
          node.ancestors.any?(&:block_type?) || node.ancestors.any?(&:numblock_type?)
        end
      end
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

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