Sha256: c9b24c2ae43c73d847b436c83c4b64a5b22525783c8d29f4ed0298e8b0f46223

Contents?: true

Size: 1.7 KB

Versions: 19

Compression:

Stored size: 1.7 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module RSpec
      # Checks for a specified error in checking raised errors.
      #
      # Enforces one of an Exception type, a string, or a regular
      # expression to match against the exception message as a parameter
      # to `raise_error`
      #
      # @example
      #   # bad
      #   expect {
      #     raise StandardError.new('error')
      #   }.to raise_error
      #
      #   # good
      #   expect {
      #     raise StandardError.new('error')
      #   }.to raise_error(StandardError)
      #
      #   expect {
      #     raise StandardError.new('error')
      #   }.to raise_error('error')
      #
      #   expect {
      #     raise StandardError.new('error')
      #   }.to raise_error(/err/)
      #
      #   expect { do_something }.not_to raise_error
      #
      class UnspecifiedException < Base
        MSG = 'Specify the exception being captured'
        RESTRICT_ON_SEND = %i[to].freeze

        # @!method empty_raise_error_or_exception(node)
        def_node_matcher :empty_raise_error_or_exception, <<~PATTERN
          (send
            (block
                (send nil? :expect) ...)
            :to
            (send nil? {:raise_error :raise_exception})
          )
        PATTERN

        def on_send(node)
          return unless empty_exception_matcher?(node)

          add_offense(node.children.last)
        end

        private

        def empty_exception_matcher?(node)
          empty_raise_error_or_exception(node) && !block_with_args?(node.parent)
        end

        def block_with_args?(node)
          return false unless node&.block_type?

          node.arguments?
        end
      end
    end
  end
end

Version data entries

19 entries across 19 versions & 5 rubygems

Version Path
minato_ruby_api_client-0.2.2 vendor/bundle/ruby/3.2.0/gems/rubocop-rspec-3.0.3/lib/rubocop/cop/rspec/unspecified_exception.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/rubocop-rspec-3.0.1/lib/rubocop/cop/rspec/unspecified_exception.rb
rubocop-rspec-3.0.3 lib/rubocop/cop/rspec/unspecified_exception.rb
rubocop-rspec-3.0.2 lib/rubocop/cop/rspec/unspecified_exception.rb
katalyst-govuk-formbuilder-1.9.2 vendor/bundle/ruby/3.3.0/gems/rubocop-rspec-3.0.1/lib/rubocop/cop/rspec/unspecified_exception.rb
rubocop-rspec-3.0.1 lib/rubocop/cop/rspec/unspecified_exception.rb
rubocop-rspec-3.0.0 lib/rubocop/cop/rspec/unspecified_exception.rb
rubocop-rspec-3.0.0.pre lib/rubocop/cop/rspec/unspecified_exception.rb
rubocop-rspec-2.31.0 lib/rubocop/cop/rspec/unspecified_exception.rb
rubocop-rspec-2.30.0 lib/rubocop/cop/rspec/unspecified_exception.rb
rubocop-rspec-2.29.2 lib/rubocop/cop/rspec/unspecified_exception.rb
rubocop-rspec-2.29.1 lib/rubocop/cop/rspec/unspecified_exception.rb
rubocop-rspec-2.29.0 lib/rubocop/cop/rspec/unspecified_exception.rb
rubocop-rspec-2.28.0 lib/rubocop/cop/rspec/unspecified_exception.rb
rubocop-rspec-2.27.1 lib/rubocop/cop/rspec/unspecified_exception.rb
rubocop-rspec-2.27.0 lib/rubocop/cop/rspec/unspecified_exception.rb
mlh-rubocop-config-1.0.3 vendor/bundle/ruby/3.2.0/gems/rubocop-rspec-2.26.1/lib/rubocop/cop/rspec/unspecified_exception.rb
rubocop-rspec-2.26.1 lib/rubocop/cop/rspec/unspecified_exception.rb
rubocop-rspec-2.26.0 lib/rubocop/cop/rspec/unspecified_exception.rb