Sha256: 6d10f68a58e92190d0237b8d62a962eac5db63b30fc1025628f04f0b178eca1d

Contents?: true

Size: 1.1 KB

Versions: 8

Compression:

Stored size: 1.1 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module RSpec
      # `context` should not be used for specifying methods.
      #
      # @example
      #   # bad
      #   context '#foo_bar' do
      #     # ...
      #   end
      #
      #   context '.foo_bar' do
      #     # ...
      #   end
      #
      #   # good
      #   describe '#foo_bar' do
      #     # ...
      #   end
      #
      #   describe '.foo_bar' do
      #     # ...
      #   end
      class ContextMethod < Cop
        MSG = 'Use `describe` for testing methods.'

        def_node_matcher :context_method, <<-PATTERN
          (block (send #{RSPEC} :context $(str #method_name?) ...) ...)
        PATTERN

        def on_block(node)
          context_method(node) do |context|
            add_offense(context)
          end
        end

        def autocorrect(node)
          lambda do |corrector|
            corrector.replace(node.parent.loc.selector, 'describe')
          end
        end

        private

        def method_name?(description)
          description.start_with?('.', '#')
        end
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
rubocop-rspec-1.41.0 lib/rubocop/cop/rspec/context_method.rb
rubocop-rspec-1.40.0 lib/rubocop/cop/rspec/context_method.rb
rubocop-rspec-1.39.0 lib/rubocop/cop/rspec/context_method.rb
rubocop-rspec-1.38.1 lib/rubocop/cop/rspec/context_method.rb
rubocop-rspec-1.38.0 lib/rubocop/cop/rspec/context_method.rb
rubocop-rspec-1.37.1 lib/rubocop/cop/rspec/context_method.rb
rubocop-rspec-1.37.0 lib/rubocop/cop/rspec/context_method.rb
rubocop-rspec-1.36.0 lib/rubocop/cop/rspec/context_method.rb