class RSpec::Matchers::ExpectedsForMultipleDiffs

@api private Handles list of expected values when there is a need to render multiple diffs. Also can handle one value.

Constants

DEFAULT_DIFF_LABEL

@private Default diff label when there is only one matcher in diff output

DESCRIPTION_MAX_LENGTH

@private Maximum readable matcher description length

Public Class Methods

for_many_matchers(matchers) click to toggle source

@api private Wraps provided matcher list in instance of ExpectedForMultipleDiffs. @param [Array<Any>] matchers list of matchers to wrap @return [RSpec::Matchers::ExpectedsForMultipleDiffs]

# File lib/rspec/matchers/expecteds_for_multiple_diffs.rb, line 36
def self.for_many_matchers(matchers)
  new(matchers.map { |m| [m.expected, diff_label_for(m)] })
end
from(expected) click to toggle source

@api private Wraps provided expected value in instance of ExpectedForMultipleDiffs. If provided value is already an ExpectedForMultipleDiffs then it just returns it. @param [Any] expected value to be wrapped @return [RSpec::Matchers::ExpectedsForMultipleDiffs]

# File lib/rspec/matchers/expecteds_for_multiple_diffs.rb, line 26
def self.from(expected)
  return expected if self === expected
  new([[expected, DEFAULT_DIFF_LABEL]])
end
new(expected_list) click to toggle source
# File lib/rspec/matchers/expecteds_for_multiple_diffs.rb, line 16
def initialize(expected_list)
  @expected_list = expected_list
end

Private Class Methods

diff_label_for(matcher) click to toggle source
# File lib/rspec/matchers/expecteds_for_multiple_diffs.rb, line 58
def diff_label_for(matcher)
  "Diff for (#{truncated(RSpec::Support::ObjectFormatter.format(matcher))}):"
end
truncated(description) click to toggle source
# File lib/rspec/matchers/expecteds_for_multiple_diffs.rb, line 62
def truncated(description)
  return description if description.length <= DESCRIPTION_MAX_LENGTH
  description[0...DESCRIPTION_MAX_LENGTH - 3] << "..."
end

Public Instance Methods

message_with_diff(message, differ, actual) click to toggle source

@api private Returns message with diff(s) appended for provided differ factory and actual value if there are any @param [String] message original failure message @param [Proc] differ @param [Any] actual value @return [String]

# File lib/rspec/matchers/expecteds_for_multiple_diffs.rb, line 47
def message_with_diff(message, differ, actual)
  diff = diffs(differ, actual)
  message = "#{message}\n#{diff}" unless diff.empty?
  message
end

Private Instance Methods

diffs(differ, actual) click to toggle source
# File lib/rspec/matchers/expecteds_for_multiple_diffs.rb, line 68
def diffs(differ, actual)
  @expected_list.map do |(expected, diff_label)|
    diff = differ.diff(actual, expected)
    next if diff.strip.empty?
    if diff == "\e[0m\n\e[0m"
      "#{diff_label}\n" \
        "  <The diff is empty, are your objects producing identical `#inspect` output?>"
    else
      "#{diff_label}#{diff}"
    end
  end.compact.join("\n")
end