Sha256: 195c7c9dd6e1d143c738aef0a7ef544f5952441f1cb697286e008e7b73129265

Contents?: true

Size: 1.01 KB

Versions: 25

Compression:

Stored size: 1.01 KB

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) # rubocop:disable InternalAffairs/NumblockHandler
          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

25 entries across 25 versions & 1 rubygems

Version Path
rubocop-minitest-0.34.2 lib/rubocop/cop/minitest/assert_raises_compound_body.rb
rubocop-minitest-0.34.1 lib/rubocop/cop/minitest/assert_raises_compound_body.rb
rubocop-minitest-0.34.0 lib/rubocop/cop/minitest/assert_raises_compound_body.rb
rubocop-minitest-0.33.0 lib/rubocop/cop/minitest/assert_raises_compound_body.rb
rubocop-minitest-0.32.2 lib/rubocop/cop/minitest/assert_raises_compound_body.rb
rubocop-minitest-0.32.1 lib/rubocop/cop/minitest/assert_raises_compound_body.rb
rubocop-minitest-0.32.0 lib/rubocop/cop/minitest/assert_raises_compound_body.rb
rubocop-minitest-0.31.1 lib/rubocop/cop/minitest/assert_raises_compound_body.rb
rubocop-minitest-0.31.0 lib/rubocop/cop/minitest/assert_raises_compound_body.rb
rubocop-minitest-0.30.0 lib/rubocop/cop/minitest/assert_raises_compound_body.rb
rubocop-minitest-0.29.0 lib/rubocop/cop/minitest/assert_raises_compound_body.rb
rubocop-minitest-0.28.0 lib/rubocop/cop/minitest/assert_raises_compound_body.rb
rubocop-minitest-0.27.0 lib/rubocop/cop/minitest/assert_raises_compound_body.rb
rubocop-minitest-0.26.1 lib/rubocop/cop/minitest/assert_raises_compound_body.rb
rubocop-minitest-0.26.0 lib/rubocop/cop/minitest/assert_raises_compound_body.rb
rubocop-minitest-0.25.1 lib/rubocop/cop/minitest/assert_raises_compound_body.rb
rubocop-minitest-0.25.0 lib/rubocop/cop/minitest/assert_raises_compound_body.rb
rubocop-minitest-0.24.0 lib/rubocop/cop/minitest/assert_raises_compound_body.rb
rubocop-minitest-0.23.2 lib/rubocop/cop/minitest/assert_raises_compound_body.rb
rubocop-minitest-0.23.1 lib/rubocop/cop/minitest/assert_raises_compound_body.rb