Class: RSpec::Matchers::BuiltIn::ContainExactly Private
Relationships & Source Files | |
Namespace Children | |
Classes:
| |
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/contain_exactly.rb |
Overview
Provides the implementation for RSpec::Matchers#contain_exactly and RSpec::Matchers#match_array. Not intended to be instantiated directly.
Constant Summary
BaseMatcher
- Inherited
Class Method Summary
BaseMatcher
- Inherited
.matcher_name, .new, | |
.underscore | Borrowed from ActiveSupport. |
Instance Attribute Summary
-
#match_when_sorted? ⇒ Boolean
readonly
private
Internal use only
This cannot always work (e.g.
BaseMatcher
- Inherited
#actual, | |
#diffable? |
|
#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
#string_encoding_differs? | :nocov: |
Instance Method Summary
- #description ⇒ String Internal use only
- #failure_message ⇒ String Internal use only
- #failure_message_when_negated ⇒ String Internal use only
- #matches?(actual) ⇒ Boolean Internal use only
- #actual_collection_line private Internal use only
- #best_solution private Internal use only
- #convert_actual_to_an_array private Internal use only
- #describe_collection(collection, surface_descriptions = false) private Internal use only
- #expected_collection_line private Internal use only
- #extra_elements_line private Internal use only
- #extra_items private Internal use only
- #generate_failure_message private Internal use only
- #match(_expected, _actual) private Internal use only
- #message_line(prefix, collection, surface_descriptions = false) private Internal use only
- #missing_elements_line private Internal use only
- #missing_items private Internal use only
- #pairings_maximizer private Internal use only
- #safe_sort(array) private Internal use only
-
#to_a_disallowed?(object) ⇒ Boolean
private
Internal use only
:nocov:
BaseMatcher
- Inherited
#actual_formatted, | |
#description | Generates a description using |
#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 | ‘{ |
::RSpec::Matchers::Composable
- Included
#& | Alias for Composable#and. |
#=== | Delegates to #matches?. |
#and | Creates a compound |
#description_of | Returns the description of the given object in a way that is aware of composed matchers. |
#or | Creates a compound |
#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 |
#unreadable_io?, | |
#with_matchers_cloned | Historically, a single matcher instance was only checked against a single value. |
Instance Attribute Details
#match_when_sorted? ⇒ Boolean
(readonly, private)
This cannot always work (e.g. when dealing with unsortable items, or matchers as expected items), but it’s practically free compared to the slowness of the full matching algorithm, and in common cases this works, so it’s worth a try.
Instance Method Details
#actual_collection_line (private)
[ GitHub ]# File 'rspec-expectations/lib/rspec/matchers/built_in/contain_exactly.rb', line 56
def actual_collection_line ('actual collection contained', actual) end
#best_solution (private)
[ GitHub ]# File 'rspec-expectations/lib/rspec/matchers/built_in/contain_exactly.rb', line 137
def best_solution @best_solution ||= pairings_maximizer.find_best_solution end
#convert_actual_to_an_array (private)
[ GitHub ]# File 'rspec-expectations/lib/rspec/matchers/built_in/contain_exactly.rb', line 94
def convert_actual_to_an_array if actual.respond_to?(:to_ary) @actual = actual.to_ary elsif actual.respond_to?(:to_a) && !to_a_disallowed?(actual) @actual = actual.to_a else false end end
#describe_collection(collection, surface_descriptions = false) (private)
[ GitHub ]
#description ⇒ String
# File 'rspec-expectations/lib/rspec/matchers/built_in/contain_exactly.rb', line 29
def description list = EnglishPhrasing.list(surface_descriptions_in(expected)) "contain exactly#{list}" end
#expected_collection_line (private)
[ GitHub ]# File 'rspec-expectations/lib/rspec/matchers/built_in/contain_exactly.rb', line 52
def expected_collection_line ('expected collection contained', expected, true) end
#extra_elements_line (private)
[ GitHub ]# File 'rspec-expectations/lib/rspec/matchers/built_in/contain_exactly.rb', line 64
def extra_elements_line ('the extra elements were', extra_items) end
#extra_items (private)
[ GitHub ]# File 'rspec-expectations/lib/rspec/matchers/built_in/contain_exactly.rb', line 131
def extra_items @extra_items ||= best_solution.unmatched_actual_indexes.map do |index| actual[index] end end
#failure_message ⇒ String
# File 'rspec-expectations/lib/rspec/matchers/built_in/contain_exactly.rb', line 11
def if Array === actual else "expected a collection that can be converted to an array with " \ "`#to_ary` or `#to_a`, but got #{actual_formatted}" end end
#failure_message_when_negated ⇒ String
# File 'rspec-expectations/lib/rspec/matchers/built_in/contain_exactly.rb', line 22
def list = EnglishPhrasing.list(surface_descriptions_in(expected)) "expected #{actual_formatted} not to contain exactly#{list}" end
#generate_failure_message (private)
[ GitHub ]# File 'rspec-expectations/lib/rspec/matchers/built_in/contain_exactly.rb', line 44
def = expected_collection_line += actual_collection_line += missing_elements_line unless missing_items.empty? += extra_elements_line unless extra_items.empty? end
#match(_expected, _actual) (private)
[ GitHub ]# File 'rspec-expectations/lib/rspec/matchers/built_in/contain_exactly.rb', line 81
def match(_expected, _actual) return false unless convert_actual_to_an_array match_when_sorted? || (extra_items.empty? && missing_items.empty?) end
#matches?(actual) ⇒ Boolean
# File 'rspec-expectations/lib/rspec/matchers/built_in/contain_exactly.rb', line 34
def matches?(actual) @pairings_maximizer = nil @best_solution = nil @extra_items = nil @missing_items = nil super(actual) end
#message_line(prefix, collection, surface_descriptions = false) (private)
[ GitHub ]# File 'rspec-expectations/lib/rspec/matchers/built_in/contain_exactly.rb', line 76
def (prefix, collection, surface_descriptions=false) "%-32s%s" % [prefix + ':', describe_collection(collection, surface_descriptions)] end
#missing_elements_line (private)
[ GitHub ]# File 'rspec-expectations/lib/rspec/matchers/built_in/contain_exactly.rb', line 60
def missing_elements_line ('the missing elements were', missing_items, true) end
#missing_items (private)
[ GitHub ]# File 'rspec-expectations/lib/rspec/matchers/built_in/contain_exactly.rb', line 125
def missing_items @missing_items ||= best_solution.unmatched_expected_indexes.map do |index| expected[index] end end
#pairings_maximizer (private)
[ GitHub ]# File 'rspec-expectations/lib/rspec/matchers/built_in/contain_exactly.rb', line 141
def pairings_maximizer @pairings_maximizer ||= begin expected_matches = Hash[Array.new(expected.size) { |i| [i, []] }] actual_matches = Hash[Array.new(actual.size) { |i| [i, []] }] expected.each_with_index do |e, ei| actual.each_with_index do |a, ai| next unless values_match?(e, a) expected_matches[ei] << ai actual_matches[ai] << ei end end PairingsMaximizer.new(expected_matches, actual_matches) end end
#safe_sort(array) (private)
[ GitHub ]# File 'rspec-expectations/lib/rspec/matchers/built_in/contain_exactly.rb', line 104
def safe_sort(array) array.sort rescue Support::AllExceptionsExceptOnesWeMustNotRescue array end
#to_a_disallowed?(object) ⇒ Boolean
(private)
:nocov:
See additional method definition at line 112.
# File 'rspec-expectations/lib/rspec/matchers/built_in/contain_exactly.rb', line 120
def to_a_disallowed?(object) case object when NilClass, String then true else Kernel == RSpec::Support.method_handle_for(object, :to_a).owner end end