123456789_123456789_123456789_123456789_123456789_

Module: RSpec::Support::FuzzyMatcher

Relationships & Source Files
Defined in: rspec-support/lib/rspec/support/fuzzy_matcher.rb

Overview

Provides a means to fuzzy-match between two arbitrary objects. Understands array/hash nesting. Uses === or == to perform the matching.

Class Method Summary

Class Method Details

.arrays_match?(expected_list, actual_list) ⇒ Boolean (private)

This method is for internal use only.
[ GitHub ]

  
# File 'rspec-support/lib/rspec/support/fuzzy_matcher.rb', line 29

def self.arrays_match?(expected_list, actual_list)
  return false if expected_list.size != actual_list.size

  expected_list.zip(actual_list).all? do |expected, actual|
    values_match?(expected, actual)
  end
end

.hashes_match?(expected_hash, actual_hash) ⇒ Boolean (private)

This method is for internal use only.
[ GitHub ]

  
# File 'rspec-support/lib/rspec/support/fuzzy_matcher.rb', line 38

def self.hashes_match?(expected_hash, actual_hash)
  return false if expected_hash.size != actual_hash.size

  expected_hash.all? do |expected_key, expected_value|
    actual_value = actual_hash.fetch(expected_key) { return false }
    values_match?(expected_value, actual_value)
  end
end

.values_match?(expected, actual) ⇒ Boolean

This method is for internal use only.
[ GitHub ]

  
# File 'rspec-support/lib/rspec/support/fuzzy_matcher.rb', line 10

def self.values_match?(expected, actual)
  if Hash === actual
    return hashes_match?(expected, actual) if Hash === expected
  elsif Array === expected && Enumerable === actual && !(Struct === actual)
    return arrays_match?(expected, actual.to_a)
  end

  return true if expected == actual

  begin
    expected === actual
  rescue ArgumentError
    # Some objects, like 0-arg lambdas on 1.9+, raise
    # ArgumentError for `expected === actual`.
    false
  end
end