Sha256: 0b9bc35eaa747b32d65e0f9717aae6bd79ba57ef8dca1931e26f456bab15dae3

Contents?: true

Size: 1.47 KB

Versions: 13

Compression:

Stored size: 1.47 KB

Contents

module RuboCop
  module Cop
    module RSpec
      # Check for repeated description strings in example groups.
      #
      # @example
      #
      #     # bad
      #     RSpec.describe User do
      #       it 'is valid' do
      #         # ...
      #       end
      #
      #       it 'is valid' do
      #         # ...
      #       end
      #     end
      #
      #     # good
      #     RSpec.describe User do
      #       it 'is valid when first and last name are present' do
      #         # ...
      #       end
      #
      #       it 'is valid when last name only is present' do
      #         # ...
      #       end
      #     end
      #
      class RepeatedDescription < Cop
        MSG = "Don't repeat descriptions within an example group.".freeze

        def on_block(node)
          return unless example_group?(node)

          repeated_descriptions(node).each do |repeated_description|
            add_offense(repeated_description, :expression)
          end
        end

        private

        # Select examples in the current scope with repeated description strings
        def repeated_descriptions(node)
          grouped_examples =
            RuboCop::RSpec::ExampleGroup.new(node)
              .examples
              .group_by(&:doc_string)

          grouped_examples
            .select { |description, group| description && group.size > 1 }
            .values
            .flatten
            .map(&:definition)
        end
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
rubocop-rspec-1.18.0 lib/rubocop/cop/rspec/repeated_description.rb
rubocop-rspec-1.17.1 lib/rubocop/cop/rspec/repeated_description.rb
rubocop-rspec-1.17.0 lib/rubocop/cop/rspec/repeated_description.rb
rubocop-rspec-1.16.0 lib/rubocop/cop/rspec/repeated_description.rb
rubocop-rspec-1.15.1 lib/rubocop/cop/rspec/repeated_description.rb
rubocop-rspec-1.15.0 lib/rubocop/cop/rspec/repeated_description.rb
rubocop-rspec-1.14.0 lib/rubocop/cop/rspec/repeated_description.rb
rubocop-rspec-1.13.0 lib/rubocop/cop/rspec/repeated_description.rb
rubocop-rspec-1.12.0 lib/rubocop/cop/rspec/repeated_description.rb
rubocop-rspec-1.11.0 lib/rubocop/cop/rspec/repeated_description.rb
rubocop-rspec-1.10.0 lib/rubocop/cop/rspec/repeated_description.rb
rubocop-rspec-1.9.1 lib/rubocop/cop/rspec/repeated_description.rb
rubocop-rspec-1.9.0 lib/rubocop/cop/rspec/repeated_description.rb