Sha256: a775c780c908fc05ffc0d7195abda8dadf32bb9cf0f81d85bc75ce1f1d037160

Contents?: true

Size: 1.73 KB

Versions: 16

Compression:

Stored size: 1.73 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module RSpec
      # Checks that around blocks actually run the test.
      #
      # @example
      #   # bad
      #   around do
      #     some_method
      #   end
      #
      #   around do |test|
      #     some_method
      #   end
      #
      #   # good
      #   around do |test|
      #     some_method
      #     test.call
      #   end
      #
      #   around do |test|
      #     some_method
      #     test.run
      #   end
      class AroundBlock < Base
        MSG_NO_ARG     = 'Test object should be passed to around block.'
        MSG_UNUSED_ARG = 'You should call `%<arg>s.call` ' \
                         'or `%<arg>s.run`.'

        # @!method hook(node)
        def_node_matcher :hook, <<-PATTERN
          (block (send nil? :around sym ?) (args $...) ...)
        PATTERN

        # @!method find_arg_usage(node)
        def_node_search :find_arg_usage, <<-PATTERN
          {(send $... {:call :run}) (send _ _ $...) (yield $...) (block-pass $...)}
        PATTERN

        def on_block(node)
          hook(node) do |(example_proxy)|
            if example_proxy.nil?
              add_no_arg_offense(node)
            else
              check_for_unused_proxy(node, example_proxy)
            end
          end
        end

        private

        def add_no_arg_offense(node)
          add_offense(node, message: MSG_NO_ARG)
        end

        def check_for_unused_proxy(block, proxy)
          name, = *proxy

          find_arg_usage(block) do |usage|
            return if usage.include?(s(:lvar, name))
          end

          add_offense(
            proxy,
            message: format(MSG_UNUSED_ARG, arg: name)
          )
        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/around_block.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-rspec-2.9.0/lib/rubocop/cop/rspec/around_block.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-rspec-2.12.1/lib/rubocop/cop/rspec/around_block.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-rspec-2.9.0/lib/rubocop/cop/rspec/around_block.rb
rubocop-rspec-2.12.1 lib/rubocop/cop/rspec/around_block.rb
rubocop-rspec-2.12.0 lib/rubocop/cop/rspec/around_block.rb
rubocop-rspec-2.11.1 lib/rubocop/cop/rspec/around_block.rb
rubocop-rspec-2.11.0 lib/rubocop/cop/rspec/around_block.rb
rubocop-rspec-2.10.0 lib/rubocop/cop/rspec/around_block.rb
rubocop-rspec-2.9.0 lib/rubocop/cop/rspec/around_block.rb
rubocop-rspec-2.8.0 lib/rubocop/cop/rspec/around_block.rb
rubocop-rspec-2.7.0 lib/rubocop/cop/rspec/around_block.rb
rubocop-rspec-2.6.0 lib/rubocop/cop/rspec/around_block.rb
rubocop-rspec-2.5.0 lib/rubocop/cop/rspec/around_block.rb
rubocop-rspec-2.4.0 lib/rubocop/cop/rspec/around_block.rb
rubocop-rspec-2.3.0 lib/rubocop/cop/rspec/around_block.rb