class RSpec::Matchers::BuiltIn::All

@api private Provides the implementation for ‘all`. Not intended to be instantiated directly.

Attributes

failed_objects[R]

@private

matcher[R]

@private

Public Class Methods

new(matcher) click to toggle source
# File lib/rspec/matchers/built_in/all.rb, line 11
def initialize(matcher)
  @matcher = matcher
  @failed_objects = {}
end

Public Instance Methods

description() click to toggle source

@api private @return [String]

# File lib/rspec/matchers/built_in/all.rb, line 37
def description
  improve_hash_formatting "all #{description_of matcher}"
end
does_not_match?(_actual) click to toggle source

@private

# File lib/rspec/matchers/built_in/all.rb, line 17
def does_not_match?(_actual)
  raise NotImplementedError, '`expect().not_to all( matcher )` is not supported.'
end
failure_message() click to toggle source

@api private @return [String]

Calls superclass method
# File lib/rspec/matchers/built_in/all.rb, line 23
def failure_message
  unless iterable?
    return "#{improve_hash_formatting(super)}, but was not iterable"
  end

  all_messages = [improve_hash_formatting(super)]
  failed_objects.each do |index, matcher_failure_message|
    all_messages << failure_message_for_item(index, matcher_failure_message)
  end
  all_messages.join("\n\n")
end

Private Instance Methods

add_new_line_if_needed(message) click to toggle source
# File lib/rspec/matchers/built_in/all.rb, line 63
def add_new_line_if_needed(message)
  message.start_with?("\n") ? message : "\n#{message}"
end
failure_message_for_item(index, failure_message) click to toggle source
# File lib/rspec/matchers/built_in/all.rb, line 58
def failure_message_for_item(index, failure_message)
  failure_message = indent_multiline_message(add_new_line_if_needed(failure_message))
  indent_multiline_message("object at index #{index} failed to match:#{failure_message}")
end
indent_multiline_message(message) click to toggle source
# File lib/rspec/matchers/built_in/all.rb, line 67
def indent_multiline_message(message)
  message = message.sub(/\n+\z/, '')
  message.lines.map do |line|
    line =~ /\S/ ? '   ' + line : line
  end.join
end
index_failed_objects() click to toggle source
# File lib/rspec/matchers/built_in/all.rb, line 50
def index_failed_objects
  actual.each_with_index do |actual_item, index|
    cloned_matcher = matcher.clone
    matches = cloned_matcher.matches?(actual_item)
    failed_objects[index] = cloned_matcher.failure_message unless matches
  end
end
initialize_copy(other) click to toggle source
Calls superclass method
# File lib/rspec/matchers/built_in/all.rb, line 74
def initialize_copy(other)
  @matcher = @matcher.clone
  @failed_objects = @failed_objects.clone
  super
end
iterable?() click to toggle source
# File lib/rspec/matchers/built_in/all.rb, line 80
def iterable?
  @actual.respond_to?(:each_with_index)
end
match(_expected, _actual) click to toggle source
# File lib/rspec/matchers/built_in/all.rb, line 43
def match(_expected, _actual)
  return false unless iterable?

  index_failed_objects
  failed_objects.empty?
end