Sha256: d6527f35b68050b1030979d9ab2ca04c20c6e9a7a09d9d9f560b8207e47b3589

Contents?: true

Size: 1.14 KB

Versions: 31

Compression:

Stored size: 1.14 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Minitest
      # Checks for `assert_raises` has an assertion method at
      # the bottom of block because the assertion will be never reached.
      #
      # @example
      #
      #   # bad
      #   assert_raises FooError do
      #     obj.occur_error
      #     assert_equal('foo', obj.bar) # Never asserted.
      #   end
      #
      #   # good
      #   assert_raises FooError do
      #     obj.occur_error
      #   end
      #   assert_equal('foo', obj.bar)
      #
      class UnreachableAssertion < Base
        include MinitestExplorationHelpers

        MSG = 'Unreachable `%<assertion_method>s` detected.'

        def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler
          return unless node.method?(:assert_raises) && (body = node.body)

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

          add_offense(last_node, message: format(MSG, assertion_method: last_node.method_name))
        end
      end
    end
  end
end

Version data entries

31 entries across 31 versions & 1 rubygems

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