Sha256: dab75822233947b6e9e6e0be824b5198284776c87454c40b7d6f587e3e349a69

Contents?: true

Size: 1 KB

Versions: 3

Compression:

Stored size: 1 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module RSpec
      # Checks that the second argument to the top level describe is the tested
      # method name.
      #
      # @example
      #   # bad
      #   describe MyClass, 'do something' do
      #   end
      #
      #   # good
      #   describe MyClass, '#my_instance_method' do
      #   end
      #
      #   describe MyClass, '.my_class_method' do
      #   end
      class DescribeMethod < Cop
        include RuboCop::RSpec::TopLevelDescribe, RuboCop::RSpec::Util

        MESSAGE = 'The second argument to describe should be the method ' \
                  "being tested. '#instance' or '.class'".freeze
        METHOD_STRING_MATCHER = /^[\#\.].+/

        def on_top_level_describe(_node, (_, second_arg))
          return unless second_arg && second_arg.type.equal?(:str)
          return if METHOD_STRING_MATCHER =~ one(second_arg.children)

          add_offense(second_arg, :expression, MESSAGE)
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rubocop-rspec-1.6.0 lib/rubocop/cop/rspec/describe_method.rb
rubocop-rspec-1.5.3 lib/rubocop/cop/rspec/describe_method.rb
rubocop-rspec-1.5.2 lib/rubocop/cop/rspec/describe_method.rb