Sha256: 811c7f60c3bdb409037a33c0766b6953a4fc365fbd20749fab3faf325763f717

Contents?: true

Size: 985 Bytes

Versions: 1

Compression:

Stored size: 985 Bytes

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Minitest
      # Enforces the block body of `assert_raises { ... }` to be reduced to only the raising code.
      #
      # @example
      #   # bad
      #   assert_raises(MyError) do
      #     foo
      #     bar
      #   end
      #
      #   # good
      #   assert_raises(MyError) do
      #     foo
      #   end
      #
      #   # good
      #   assert_raises(MyError) do
      #     foo do
      #       bar
      #       baz
      #     end
      #   end
      #
      class AssertRaisesCompoundBody < Base
        MSG = 'Reduce `assert_raises` block body to contain only the raising code.'

        def on_block(node)
          return unless node.method?(:assert_raises) && multi_statement_begin?(node.body)

          add_offense(node)
        end

        private

        def multi_statement_begin?(node)
          node.begin_type? && node.children.size > 1
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rubocop-minitest-0.21.0 lib/rubocop/cop/minitest/assert_raises_compound_body.rb