module RSpec::SleepingKingStudios::Matchers::Description

Reusable logic for building a matcher description across different versions of the base RSpec library.

@since 2.2.0

Constants

DEFAULT_EXPECTED_ITEMS

@api private

Public Instance Methods

description() click to toggle source

A short string that describes the purpose of the matcher.

@return [String] the matcher description

# File lib/rspec/sleeping_king_studios/matchers/description.rb, line 20
def description
  desc = matcher_name.to_s

  desc << format_expected_items

  desc
end

Private Instance Methods

expected_items_for_description() click to toggle source
# File lib/rspec/sleeping_king_studios/matchers/description.rb, line 30
def expected_items_for_description
  defined?(@expected) ? @expected : DEFAULT_EXPECTED_ITEMS
end
format_expected_items() click to toggle source
# File lib/rspec/sleeping_king_studios/matchers/description.rb, line 34
def format_expected_items
  expected_items = expected_items_for_description

  return '' if expected_items == DEFAULT_EXPECTED_ITEMS

  if defined?(RSpec::Matchers::EnglishPhrasing)
    # RSpec 3.4+
    RSpec::Matchers::EnglishPhrasing.list(expected_items)
  elsif defined?(to_sentence)
    # RSpec 3.0-3.3
    to_sentence(expected_items)
  else
    array_tools = ::SleepingKingStudios::Tools::ArrayTools
    processed   = [expected_items].flatten.map(&:inspect)

    ' ' << array_tools.humanize_list(processed)
  end # if-elsif-else
end
matcher_name() click to toggle source
# File lib/rspec/sleeping_king_studios/matchers/description.rb, line 53
def matcher_name
  return @matcher_name if @matcher_name

  string_tools = ::SleepingKingStudios::Tools::StringTools
  name         = string_tools.underscore(self.class.name.split('::').last)

  @matcher_name = name.tr('_', ' ').sub(/ matcher\z/, '')
end