Sha256: af87e25be19e08b0a83439309df216e84f205c1c6ca306b959d1e3e009e3c5dd

Contents?: true

Size: 1.7 KB

Versions: 16

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

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

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

          node.arguments?
        end
      end
    end
  end
end

Version data entries

16 entries across 14 versions & 2 rubygems

Version Path
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-rspec-2.12.1/lib/rubocop/cop/rspec/unspecified_exception.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-rspec-2.9.0/lib/rubocop/cop/rspec/unspecified_exception.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-rspec-2.12.1/lib/rubocop/cop/rspec/unspecified_exception.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-rspec-2.9.0/lib/rubocop/cop/rspec/unspecified_exception.rb
rubocop-rspec-2.12.1 lib/rubocop/cop/rspec/unspecified_exception.rb
rubocop-rspec-2.12.0 lib/rubocop/cop/rspec/unspecified_exception.rb
rubocop-rspec-2.11.1 lib/rubocop/cop/rspec/unspecified_exception.rb
rubocop-rspec-2.11.0 lib/rubocop/cop/rspec/unspecified_exception.rb
rubocop-rspec-2.10.0 lib/rubocop/cop/rspec/unspecified_exception.rb
rubocop-rspec-2.9.0 lib/rubocop/cop/rspec/unspecified_exception.rb
rubocop-rspec-2.8.0 lib/rubocop/cop/rspec/unspecified_exception.rb
rubocop-rspec-2.7.0 lib/rubocop/cop/rspec/unspecified_exception.rb
rubocop-rspec-2.6.0 lib/rubocop/cop/rspec/unspecified_exception.rb
rubocop-rspec-2.5.0 lib/rubocop/cop/rspec/unspecified_exception.rb
rubocop-rspec-2.4.0 lib/rubocop/cop/rspec/unspecified_exception.rb
rubocop-rspec-2.3.0 lib/rubocop/cop/rspec/unspecified_exception.rb