Sha256: 61de41ef21593eceaf52ae808f82c0eda979482828927f77fa857b0cf185b1cc

Contents?: true

Size: 1.8 KB

Versions: 46

Compression:

Stored size: 1.8 KB

Contents

module RSpec
  module Matchers
    # Facilitates converting ruby objects to English phrases.
    module EnglishPhrasing
      # Converts a symbol into an English expression.
      #
      #     split_words(:banana_creme_pie) #=> "banana creme pie"
      #
      def self.split_words(sym)
        sym.to_s.tr('_', ' ')
      end

      # @note The returned string has a leading space except
      # when given an empty list.
      #
      # Converts an object (often a collection of objects)
      # into an English list.
      #
      #     list(['banana', 'kiwi', 'mango'])
      #     #=> " \"banana\", \"kiwi\", and \"mango\""
      #
      # Given an empty collection, returns the empty string.
      #
      #     list([]) #=> ""
      #
      def self.list(obj)
        return " #{RSpec::Support::ObjectFormatter.format(obj)}" if !obj || Struct === obj
        items = Array(obj).map { |w| RSpec::Support::ObjectFormatter.format(w) }
        case items.length
        when 0
          ""
        when 1
          " #{items[0]}"
        when 2
          " #{items[0]} and #{items[1]}"
        else
          " #{items[0...-1].join(', ')}, and #{items[-1]}"
        end
      end

      if RUBY_VERSION == '1.8.7'
        # Not sure why, but on travis on 1.8.7 we have gotten these warnings:
        # lib/rspec/matchers/english_phrasing.rb:28: warning: default `to_a' will be obsolete
        # So it appears that `Array` can trigger that (e.g. by calling `to_a` on the passed object?)
        # So here we replace `Kernel#Array` with our own warning-free implementation for 1.8.7.
        # @private
        # rubocop:disable Style/MethodName
        def self.Array(obj)
          case obj
          when Array then obj
          else [obj]
          end
        end
        # rubocop:enable Style/MethodName
      end
    end
  end
end

Version data entries

46 entries across 41 versions & 19 rubygems

Version Path
dirwatch-0.0.9 vendor/bundle/ruby/2.5.0/gems/rspec-expectations-3.7.0/lib/rspec/matchers/english_phrasing.rb
dirwatch-0.0.8 vendor/bundle/ruby/2.5.0/gems/rspec-expectations-3.7.0/lib/rspec/matchers/english_phrasing.rb
cloudsmith-api-0.30.7 vendor/bundle/ruby/2.3.0/gems/rspec-expectations-3.7.0/lib/rspec/matchers/english_phrasing.rb
logstash-filter-device_detection-1.0.7-java vendor/bundle/jruby/1.9/gems/rspec-expectations-3.7.0/lib/rspec/matchers/english_phrasing.rb
vagrant-packet-0.1.1 vendor/bundle/ruby/2.3.0/gems/rspec-expectations-3.7.0/lib/rspec/matchers/english_phrasing.rb
vagrant-packet-0.1.1 vendor/bundle/ruby/2.5.0/gems/rspec-expectations-3.7.0/lib/rspec/matchers/english_phrasing.rb
vagrant-packet-0.1.1 vendor/bundle/ruby/2.4.0/gems/rspec-expectations-3.6.0/lib/rspec/matchers/english_phrasing.rb
tdiary-5.0.9 vendor/bundle/gems/rspec-expectations-3.7.0/lib/rspec/matchers/english_phrasing.rb
color_me_shop-1.0.0 vendor/bundle/ruby/2.5.0/gems/rspec-expectations-3.7.0/lib/rspec/matchers/english_phrasing.rb
mrcooper-logstash-output-azuresearch-0.2.2 vendor/jruby/2.5.0/gems/rspec-expectations-3.7.0/lib/rspec/matchers/english_phrasing.rb
monero_wallet_gen-0.1.0 vendor/bundle/ruby/2.3.0/gems/rspec-expectations-3.7.0/lib/rspec/matchers/english_phrasing.rb
sb_prime_table-0.1.1 vendor/bundle/ruby/2.4.0/gems/rspec-expectations-3.7.0/lib/rspec/matchers/english_phrasing.rb
sb_prime_table-0.1.0 vendor/bundle/ruby/2.4.0/gems/rspec-expectations-3.7.0/lib/rspec/matchers/english_phrasing.rb
tdiary-5.0.8 vendor/bundle/ruby/2.5.0/gems/rspec-expectations-3.7.0/lib/rspec/matchers/english_phrasing.rb
tdiary-5.0.8 vendor/bundle/gems/tdiary-5.0.7/vendor/bundle/gems/rspec-expectations-3.7.0/lib/rspec/matchers/english_phrasing.rb
tdiary-5.0.8 vendor/bundle/gems/rspec-expectations-3.7.0/lib/rspec/matchers/english_phrasing.rb
tdiary-5.0.7 vendor/bundle/gems/rspec-expectations-3.7.0/lib/rspec/matchers/english_phrasing.rb
comiditaULL-0.1.1 vendor/bundle/ruby/2.3.0/gems/rspec-expectations-3.7.0/lib/rspec/matchers/english_phrasing.rb
comidita_ull-0.1.1 vendor/bundle/ruby/2.3.0/gems/rspec-expectations-3.7.0/lib/rspec/matchers/english_phrasing.rb
comidita_ull-0.1.0 vendor/bundle/ruby/2.3.0/gems/rspec-expectations-3.7.0/lib/rspec/matchers/english_phrasing.rb