Class: RSpec::Matchers::BuiltIn::ContainExactly::PairingsMaximizer Private

Inherits:
Object
  • Object
show all
Defined in:
lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-expectations-3.12.2/lib/rspec/matchers/built_in/contain_exactly.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Once we started supporting composing matchers, the algorithm for this matcher got much more complicated. Consider this expression:

expect(["fool", "food"]).to contain_exactly(/foo/, /fool/)

This should pass (because we can pair /fool/ with “fool” and /foo/ with “food”), but the original algorithm used by this matcher would pair the first elements it could (/foo/ with “fool”), which would leave /fool/ and “food” unmatched. When we have an expected element which is a matcher that matches a superset of actual items compared to another expected element matcher, we need to consider every possible pairing.

This class is designed to maximize the number of actual/expected pairings – or, conversely, to minimize the number of unpaired items. It’s essentially a brute force solution, but with a few heuristics applied to reduce the size of the problem space:

* Any items which match none of the items in the other list are immediately
  placed into the `unmatched_expected_indexes` or `unmatched_actual_indexes` array.
  The extra items and missing items in the matcher failure message are derived
  from these arrays.
* Any items which reciprocally match only each other are paired up and not
  considered further.

What’s left is only the items which match multiple items from the other list (or vice versa). From here, it performs a brute-force depth-first search, looking for a solution which pairs all elements in both lists, or, barring that, that produces the fewest unmatched items.

Defined Under Namespace

Classes: Solution

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(expected_to_actual_matched_indexes, actual_to_expected_matched_indexes) ⇒ PairingsMaximizer

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of PairingsMaximizer.



223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-expectations-3.12.2/lib/rspec/matchers/built_in/contain_exactly.rb', line 223

def initialize(expected_to_actual_matched_indexes, actual_to_expected_matched_indexes)
  @expected_to_actual_matched_indexes = expected_to_actual_matched_indexes
  @actual_to_expected_matched_indexes = actual_to_expected_matched_indexes

  unmatched_expected_indexes, indeterminate_expected_indexes =
    categorize_indexes(expected_to_actual_matched_indexes, actual_to_expected_matched_indexes)

  unmatched_actual_indexes, indeterminate_actual_indexes =
    categorize_indexes(actual_to_expected_matched_indexes, expected_to_actual_matched_indexes)

  @solution = Solution.new(unmatched_expected_indexes,     unmatched_actual_indexes,
                           indeterminate_expected_indexes, indeterminate_actual_indexes)
end

Instance Attribute Details

#actual_to_expected_matched_indexesObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



221
222
223
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-expectations-3.12.2/lib/rspec/matchers/built_in/contain_exactly.rb', line 221

def actual_to_expected_matched_indexes
  @actual_to_expected_matched_indexes
end

#expected_to_actual_matched_indexesObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



221
222
223
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-expectations-3.12.2/lib/rspec/matchers/built_in/contain_exactly.rb', line 221

def expected_to_actual_matched_indexes
  @expected_to_actual_matched_indexes
end

#solutionObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



221
222
223
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-expectations-3.12.2/lib/rspec/matchers/built_in/contain_exactly.rb', line 221

def solution
  @solution
end

Instance Method Details

#find_best_solutionObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-expectations-3.12.2/lib/rspec/matchers/built_in/contain_exactly.rb', line 237

def find_best_solution
  return solution if solution.candidate?
  best_solution_so_far = NullSolution

  expected_index = solution.indeterminate_expected_indexes.first
  actuals = expected_to_actual_matched_indexes[expected_index]

  actuals.each do |actual_index|
    solution = best_solution_for_pairing(expected_index, actual_index)
    return solution if solution.ideal?
    best_solution_so_far = solution if best_solution_so_far.worse_than?(solution)
  end

  best_solution_so_far
end