Sha256: 4d5266532b7deb442c503ee5b4e1d90d19967cde521705cf1b62600efbf54676

Contents?: true

Size: 1.28 KB

Versions: 17

Compression:

Stored size: 1.28 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module RSpec
      # Checks for long examples.
      #
      # A long example is usually more difficult to understand. Consider
      # extracting out some behaviour, e.g. with a `let` block, or a helper
      # method.
      #
      # @example
      #   # bad
      #   it do
      #     service = described_class.new
      #     more_setup
      #     more_setup
      #     result = service.call
      #     expect(result).to be(true)
      #   end
      #
      #   # good
      #   it do
      #     service = described_class.new
      #     result = service.call
      #     expect(result).to be(true)
      #   end
      class ExampleLength < Cop
        include CodeLength

        MSG = 'Example has too many lines [%<total>d/%<max>d].'.freeze

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

          length = code_length(node)

          return unless length > max_length

          add_offense(node, location: :expression, message: message(length))
        end

        private

        def code_length(node)
          node.source.lines[1..-2].count { |line| !irrelevant_line(line) }
        end

        def message(length)
          format(MSG, total: length, max: max_length)
        end
      end
    end
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
rubocop-rspec-1.32.0 lib/rubocop/cop/rspec/example_length.rb
rubocop-rspec-1.31.0 lib/rubocop/cop/rspec/example_length.rb
rubocop-rspec-1.30.1 lib/rubocop/cop/rspec/example_length.rb
rubocop-rspec-1.30.0 lib/rubocop/cop/rspec/example_length.rb
rubocop-rspec-1.29.1 lib/rubocop/cop/rspec/example_length.rb
rubocop-rspec-1.29.0 lib/rubocop/cop/rspec/example_length.rb
rubocop-rspec-1.28.0 lib/rubocop/cop/rspec/example_length.rb
rubocop-rspec-1.27.0 lib/rubocop/cop/rspec/example_length.rb
rubocop-rspec-1.26.0 lib/rubocop/cop/rspec/example_length.rb
rubocop-rspec-1.25.1 lib/rubocop/cop/rspec/example_length.rb
rubocop-rspec-1.25.0 lib/rubocop/cop/rspec/example_length.rb
rubocop-rspec-1.24.0 lib/rubocop/cop/rspec/example_length.rb
rubocop-rspec-1.23.0 lib/rubocop/cop/rspec/example_length.rb
rubocop-rspec-1.22.2 lib/rubocop/cop/rspec/example_length.rb
rubocop-rspec-1.22.1 lib/rubocop/cop/rspec/example_length.rb
rubocop-rspec-1.22.0 lib/rubocop/cop/rspec/example_length.rb
rubocop-rspec-1.21.0 lib/rubocop/cop/rspec/example_length.rb