Sha256: df0fe66f7bc95f8474137b5ae2c6639f998aec2720498b9fa10f1a4a554f6c72

Contents?: true

Size: 1.34 KB

Versions: 39

Compression:

Stored size: 1.34 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module RSpec
      # Checks that the second argument to `describe` specifies a method.
      #
      # @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 < Base
        include TopLevelGroup

        MSG = 'The second argument to describe should be the method ' \
              "being tested. '#instance' or '.class'."

        # @!method second_string_literal_argument(node)
        def_node_matcher :second_string_literal_argument, <<~PATTERN
          (block
            (send #rspec? :describe _first_argument ${str dstr} ...)
          ...)
        PATTERN

        # @!method method_name?(node)
        def_node_matcher :method_name?, <<~PATTERN
          {(str #method_name_prefix?) (dstr (str #method_name_prefix?) ...)}
        PATTERN

        def on_top_level_group(node)
          second_string_literal_argument(node) do |argument|
            add_offense(argument) unless method_name?(argument)
          end
        end

        private

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

Version data entries

39 entries across 39 versions & 7 rubygems

Version Path
rubocop-rspec-2.27.0 lib/rubocop/cop/rspec/describe_method.rb
mlh-rubocop-config-1.0.3 vendor/bundle/ruby/3.2.0/gems/rubocop-rspec-2.26.1/lib/rubocop/cop/rspec/describe_method.rb
rubocop-rspec-2.26.1 lib/rubocop/cop/rspec/describe_method.rb
rubocop-rspec-2.26.0 lib/rubocop/cop/rspec/describe_method.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-rspec-2.22.0/lib/rubocop/cop/rspec/describe_method.rb
rubocop-rspec-2.25.0 lib/rubocop/cop/rspec/describe_method.rb
rubocop-rspec-2.24.1 lib/rubocop/cop/rspec/describe_method.rb
rubocop-rspec-2.24.0 lib/rubocop/cop/rspec/describe_method.rb
rubocop-rspec-2.23.2 lib/rubocop/cop/rspec/describe_method.rb
rubocop-rspec-2.23.1 lib/rubocop/cop/rspec/describe_method.rb
rubocop-rspec-2.23.0 lib/rubocop/cop/rspec/describe_method.rb
mlh-rubocop-config-1.0.2 vendor/bundle/ruby/3.2.0/gems/rubocop-rspec-2.22.0/lib/rubocop/cop/rspec/describe_method.rb
fablicop-1.10.3 vendor/bundle/ruby/3.2.0/gems/rubocop-rspec-2.22.0/lib/rubocop/cop/rspec/describe_method.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-rspec-2.22.0/lib/rubocop/cop/rspec/describe_method.rb
fablicop-1.10.2 vendor/bundle/ruby/3.2.0/gems/rubocop-rspec-2.22.0/lib/rubocop/cop/rspec/describe_method.rb
rubocop-rspec-2.22.0 lib/rubocop/cop/rspec/describe_method.rb
rubocop-rspec-2.21.0 lib/rubocop/cop/rspec/describe_method.rb
rubocop-rspec-2.20.0 lib/rubocop/cop/rspec/describe_method.rb
rubocop-rspec-2.19.0 lib/rubocop/cop/rspec/describe_method.rb