123456789_123456789_123456789_123456789_123456789_

Class: RSpec::Matchers::BuiltIn::All Private

Do not use. This class is for internal use only.
Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, BaseMatcher
Instance Chain:
Inherits: RSpec::Matchers::BuiltIn::BaseMatcher
Defined in: rspec-expectations/lib/rspec/matchers/built_in/all.rb

Overview

Provides the implementation for RSpec::Matchers#all. Not intended to be instantiated directly.

Constant Summary

BaseMatcher - Inherited

UNDEFINED

Class Method Summary

BaseMatcher - Inherited

.matcher_name, .new,
.underscore

Borrowed from ActiveSupport.

Instance Attribute Summary

BaseMatcher - Inherited

#actual,
#diffable?

::RSpec::Matchers are not diffable by default.

#expected, #expects_call_stack_jump?, #matcher_name, #matcher_name=, #rescued_exception,
#supports_block_expectations?

Most matchers are value matchers (i.e. meant to work with ‘expect(value)`) rather than block matchers (i.e. meant to work with `expect { }`), so this defaults to false.

#supports_value_expectations?

BaseMatcher::StringEncodingFormatting - Included

Instance Method Summary

BaseMatcher - Inherited

#actual_formatted,
#description

Generates a description using ::RSpec::Matchers::EnglishPhrasing.

#expected_formatted,
#match_unless_raises

Used to wrap a block of code that will indicate failure by raising one of the named exceptions.

#matches?

Indicates if the match is successful.

BaseMatcher::DefaultFailureMessages - Included

#failure_message

Provides a good generic failure message.

#failure_message_when_negated

Provides a good generic negative failure message.

BaseMatcher::StringEncodingFormatting - Included

#format_encoding

Formats a String’s encoding as a human readable string :nocov:

BaseMatcher::HashFormatting - Included

#improve_hash_formatting

‘{ :a => 5, :b => 2 }.inspect` produces:

::RSpec::Matchers::Composable - Included

#&

Alias for Composable#and.

#===

Delegates to #matches?.

#and

Creates a compound and expectation.

#description_of

Returns the description of the given object in a way that is aware of composed matchers.

#or

Creates a compound or expectation.

#values_match?

This provides a generic way to fuzzy-match an expected value against an actual value.

#|

Alias for Composable#or.

#should_enumerate?

We should enumerate arrays as long as they are not recursive.

#surface_descriptions_in

Transforms the given data structure (typically a hash or array) into a new data structure that, when #inspect is called on it, will provide descriptions of any contained matchers rather than the normal #inspect output.

#unreadable_io?,
#with_matchers_cloned

Historically, a single matcher instance was only checked against a single value.

Instance Attribute Details

#failed_objects (readonly)

[ GitHub ]

  
# File 'rspec-expectations/lib/rspec/matchers/built_in/all.rb', line 9

attr_reader :matcher, :failed_objects

#iterable?Boolean (readonly, private)

[ GitHub ]

  
# File 'rspec-expectations/lib/rspec/matchers/built_in/all.rb', line 80

def iterable?
  @actual.respond_to?(:each_with_index)
end

#matcher (readonly)

[ GitHub ]

  
# File 'rspec-expectations/lib/rspec/matchers/built_in/all.rb', line 9

attr_reader :matcher, :failed_objects

Instance Method Details

#add_new_line_if_needed(message) (private)

[ GitHub ]

  
# File 'rspec-expectations/lib/rspec/matchers/built_in/all.rb', line 63

def add_new_line_if_needed(message)
  message.start_with?("\n") ? message : "\n#{message}"
end

#descriptionString

[ GitHub ]

  
# File 'rspec-expectations/lib/rspec/matchers/built_in/all.rb', line 37

def description
  improve_hash_formatting "all #{description_of matcher}"
end

#does_not_match?(_actual) ⇒ Boolean

Raises:

  • (NotImplementedError)
[ GitHub ]

  
# File 'rspec-expectations/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_messageString

[ GitHub ]

  
# File 'rspec-expectations/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

#failure_message_for_item(index, failure_message) (private)

[ GitHub ]

  
# File 'rspec-expectations/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) (private)

[ GitHub ]

  
# File 'rspec-expectations/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 (private)

[ GitHub ]

  
# File 'rspec-expectations/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) (private)

[ GitHub ]

  
# File 'rspec-expectations/lib/rspec/matchers/built_in/all.rb', line 74

def initialize_copy(other)
  @matcher = @matcher.clone
  @failed_objects = @failed_objects.clone
  super
end

#match(_expected, _actual) (private)

[ GitHub ]

  
# File 'rspec-expectations/lib/rspec/matchers/built_in/all.rb', line 43

def match(_expected, _actual)
  return false unless iterable?

  index_failed_objects
  failed_objects.empty?
end