Sha256: 05fa0f20e3f68f7b6f36c1bac08b78c4382027fe26bc3aee703a0e8440fc44b3

Contents?: true

Size: 1.01 KB

Versions: 4

Compression:

Stored size: 1.01 KB

Contents

# encoding: utf-8
# 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

        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, args)
          second_arg = args[1]
          return unless second_arg && second_arg.type == :str
          return if METHOD_STRING_MATCHER =~ second_arg.children.first

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

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
rubocop-rspec-1.5.1 lib/rubocop/cop/rspec/describe_method.rb
rubocop-rspec-1.5.0 lib/rubocop/cop/rspec/describe_method.rb
rubocop-rspec-1.4.1 lib/rubocop/cop/rspec/describe_method.rb
rubocop-rspec-1.4.0 lib/rubocop/cop/rspec/describe_method.rb