Sha256: 978017d6e5c717ea54a0de4dc1b86f9ff257685ff89efb8e4492dc7ab6827569

Contents?: true

Size: 1.66 KB

Versions: 37

Compression:

Stored size: 1.66 KB

Contents

module RSpec
  module Matchers
    module Pretty
      def split_words(sym)
        sym.to_s.gsub(/_/,' ')
      end

      def to_sentence(words)
        return "" unless words
        words = Array(words).map { |w| to_word(w) }
        case words.length
          when 0
            ""
          when 1
            " #{words[0]}"
          when 2
            " #{words[0]} and #{words[1]}"
          else
            " #{words[0...-1].join(', ')}, and #{words[-1]}"
        end
      end

      def _pretty_print(array)
        result = ""
        array.each_with_index do |item, index|
          if index < (array.length - 2)
            result << "#{item.inspect}, "
          elsif index < (array.length - 1)
            result << "#{item.inspect} and "
          else
            result << "#{item.inspect}"
          end
        end
        result
      end

      def to_word(item)
        is_matcher_with_description?(item) ? item.description : item.inspect
      end

      def name_to_sentence
        split_words(name)
      end

      def expected_to_sentence
        to_sentence(@expected) if defined?(@expected)
      end

      def name
        defined?(@name) ? @name : underscore(self.class.name.split("::").last)
      end

      # Borrowed from ActiveSupport
      def underscore(camel_cased_word)
        word = camel_cased_word.to_s.dup
        word.gsub!(/([A-Z]+)([A-Z][a-z])/,'\1_\2')
        word.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
        word.tr!("-", "_")
        word.downcase!
        word
      end

      private

      def is_matcher_with_description?(object)
        RSpec::Matchers.is_a_matcher?(object) && object.respond_to?(:description)
      end
    end
  end
end

Version data entries

37 entries across 37 versions & 10 rubygems

Version Path
vagrant-unbundled-2.2.7.0 vendor/bundle/ruby/2.4.0/gems/rspec-expectations-2.14.5/lib/rspec/matchers/pretty.rb
vagrant-unbundled-1.9.5.1 vendor/bundle/ruby/2.4.0/gems/rspec-expectations-2.14.5/lib/rspec/matchers/pretty.rb
vagrant-unbundled-1.9.1.1 vendor/bundle/ruby/2.4.0/gems/rspec-expectations-2.14.5/lib/rspec/matchers/pretty.rb
vagrant-unbundled-1.8.5.2 vendor/bundle/ruby/2.3.0/gems/rspec-expectations-2.14.5/lib/rspec/matchers/pretty.rb
vagrant-unbundled-1.8.5.1 vendor/bundle/ruby/2.3.0/gems/rspec-expectations-2.14.5/lib/rspec/matchers/pretty.rb
vagrant-unbundled-1.8.4.2 vendor/bundle/ruby/2.3.0/gems/rspec-expectations-2.14.5/lib/rspec/matchers/pretty.rb
vagrant-unbundled-1.8.4.1 vendor/bundle/ruby/2.3.0/gems/rspec-expectations-2.14.5/lib/rspec/matchers/pretty.rb
vagrant-unbundled-1.8.1.1 vendor/bundle/ruby/2.3.0/gems/rspec-expectations-2.14.5/lib/rspec/matchers/pretty.rb
opal-rspec-cj-0.4.4 vendor_lib/rspec/matchers/pretty.rb
opal-rspec-0.4.3 vendor_lib/rspec/matchers/pretty.rb
vagrant-cloudstack-1.1.0 vendor/bundle/gems/rspec-expectations-2.14.5/lib/rspec/matchers/pretty.rb
opal-rspec-0.4.2 vendor_lib/rspec/matchers/pretty.rb
opal-rspec-0.4.1 vendor_lib/rspec/matchers/pretty.rb
opal-rspec-0.4.0 vendor_lib/rspec/matchers/pretty.rb
opal-rspec-0.4.0.beta4 vendor_lib/rspec/matchers/pretty.rb
dxruby_rp5-0.0.4 spec/vendor/rspec-expectations-2.14.4/lib/rspec/matchers/pretty.rb
rspec-expectations-2.99.0.beta2 lib/rspec/matchers/pretty.rb
rspec-expectations-2.14.5 lib/rspec/matchers/pretty.rb
mango-0.8.0 vendor/bundler/ruby/2.1.0/gems/rspec-expectations-2.14.4/lib/rspec/matchers/pretty.rb
mango-0.7.1 vendor/bundler/ruby/2.0.0/gems/rspec-expectations-2.14.4/lib/rspec/matchers/pretty.rb