Sha256: 644dde2acf55773508fd642659625ee8fd01c96efc9d9c57df0073b202bf68e9

Contents?: true

Size: 1.41 KB

Versions: 8

Compression:

Stored size: 1.41 KB

Contents

module RSpec
  module Support
    # Provides a means to fuzzy-match between two arbitrary objects.
    # Understands array/hash nesting. Uses `===` or `==` to
    # perform the matching.
    module FuzzyMatcher
      # @api private
      def self.values_match?(expected, actual)
        if Array === expected && Enumerable === actual
          return arrays_match?(expected, actual.to_a)
        elsif Hash === expected && Hash === actual
          return hashes_match?(expected, actual)
        end

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

      # @private
      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

      # @private
      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

      private_class_method :arrays_match?, :hashes_match?
    end
  end
end

Version data entries

8 entries across 8 versions & 2 rubygems

Version Path
rspec-support-3.0.3 lib/rspec/support/fuzzy_matcher.rb
rspec-support-3.0.2 lib/rspec/support/fuzzy_matcher.rb
rspec-support-3.0.1 lib/rspec/support/fuzzy_matcher.rb
whos_dated_who-0.1.0 vendor/bundle/gems/rspec-support-3.0.0/lib/rspec/support/fuzzy_matcher.rb
whos_dated_who-0.0.1 vendor/bundle/gems/rspec-support-3.0.0/lib/rspec/support/fuzzy_matcher.rb
rspec-support-3.0.0 lib/rspec/support/fuzzy_matcher.rb
rspec-support-3.0.0.rc1 lib/rspec/support/fuzzy_matcher.rb
rspec-support-3.0.0.beta2 lib/rspec/support/fuzzy_matcher.rb