Sha256: 73e619a45ca554dbb8859c03253696f22d734904d6efba1304263f161d97d41b

Contents?: true

Size: 1.1 KB

Versions: 13

Compression:

Stored size: 1.1 KB

Contents

# frozen_string_literal: true

module RuboCop
  module RSpec
    # RSpec example wording rewriter
    class Wording
      def initialize(text, ignore:, replace:)
        @text         = text
        @ignores      = ignore
        @replacements = replace
      end

      def rewrite
        text.split.tap do |words|
          first_word = words.shift
          words.unshift('not') if first_word.eql?("shouldn't")

          words.each_with_index do |value, key|
            next if ignores.include?(value)
            words[key] = simple_present(words.fetch(key))
            break
          end
        end.join(' ')
      end

      private

      attr_reader :text, :ignores, :replacements

      def simple_present(word)
        return replacements.fetch(word) if replacements.key?(word)

        # ends with o s x ch sh or ss
        if %w(o s x ch sh).any?(&word.public_method(:end_with?))
          return "#{word}es"
        end

        # ends with y
        if word.end_with?('y') && !%w(a u o e).include?(word[-2])
          return "#{word[0..-2]}ies"
        end

        "#{word}s"
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
rubocop-rspec-1.15.0 lib/rubocop/rspec/wording.rb
rubocop-rspec-1.14.0 lib/rubocop/rspec/wording.rb
rubocop-rspec-1.13.0 lib/rubocop/rspec/wording.rb
rubocop-rspec-1.12.0 lib/rubocop/rspec/wording.rb
rubocop-rspec-1.11.0 lib/rubocop/rspec/wording.rb
rubocop-rspec-1.10.0 lib/rubocop/rspec/wording.rb
rubocop-rspec-1.9.1 lib/rubocop/rspec/wording.rb
rubocop-rspec-1.9.0 lib/rubocop/rspec/wording.rb
rubocop-rspec-1.8.0 lib/rubocop/rspec/wording.rb
rubocop-rspec-1.7.0 lib/rubocop/rspec/wording.rb
rubocop-rspec-1.6.0 lib/rubocop/rspec/wording.rb
rubocop-rspec-1.5.3 lib/rubocop/rspec/wording.rb
rubocop-rspec-1.5.2 lib/rubocop/rspec/wording.rb