Sha256: 0890ff9cb6adfd6cac120ad002619e2c99639a2fc66e8ef9cae1f571d37118f6

Contents?: true

Size: 1.46 KB

Versions: 29

Compression:

Stored size: 1.46 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 && !(Struct === actual)
          return arrays_match?(expected, actual.to_a)
        elsif Hash === expected && Hash === actual
          return hashes_match?(expected, actual)
        elsif actual == expected
          return true
        end

        begin
          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

29 entries across 26 versions & 8 rubygems

Version Path
opal-rspec-0.8.0 rspec-support/upstream/lib/rspec/support/fuzzy_matcher.rb
opal-rspec-0.8.0.alpha3 rspec-support/upstream/lib/rspec/support/fuzzy_matcher.rb
opal-rspec-0.8.0.alpha2 rspec-support/upstream/lib/rspec/support/fuzzy_matcher.rb
opal-rspec-0.8.0.alpha1 rspec-support/upstream/lib/rspec/support/fuzzy_matcher.rb
opal-rspec-0.7.1 rspec-support/upstream/lib/rspec/support/fuzzy_matcher.rb
opal-rspec-0.7.0 rspec-support/upstream/lib/rspec/support/fuzzy_matcher.rb
opal-rspec-0.6.2 rspec-support/lib/rspec/support/fuzzy_matcher.rb
opal-rspec-0.7.0.rc.2 rspec-support/upstream/lib/rspec/support/fuzzy_matcher.rb
opal-rspec-0.6.1 rspec-support/lib/rspec/support/fuzzy_matcher.rb
opal-rspec-0.6.0 rspec-support/lib/rspec/support/fuzzy_matcher.rb
opal-rspec-0.6.0.beta1 rspec-support/lib/rspec/support/fuzzy_matcher.rb
opal-connect-rspec-0.5.0 rspec-support/lib/rspec/support/fuzzy_matcher.rb
able-neo4j-1.0.0 vendor/bundle/jruby/1.9/gems/rspec-support-3.1.2/lib/rspec/support/fuzzy_matcher.rb
honeybadger-2.4.0 vendor/gems/ruby/2.1.0/gems/rspec-support-3.1.0/lib/rspec/support/fuzzy_matcher.rb
honeybadger-2.4.0 vendor/gems/ruby/1.9.1/gems/rspec-support-3.1.0/lib/rspec/support/fuzzy_matcher.rb
honeybadger-2.4.0 vendor/gems/ruby/2.2.0/gems/rspec-support-3.1.0/lib/rspec/support/fuzzy_matcher.rb
opal-rspec-0.5.0 rspec-support/lib/rspec/support/fuzzy_matcher.rb
logstash-input-beats-2.0.2 vendor/jruby/1.9/gems/rspec-support-3.1.2/lib/rspec/support/fuzzy_matcher.rb
logstash-input-beats-2.0.2 vendor/jruby/1.9/gems/logstash-codec-json-2.0.3/vendor/gems/rspec-support-3.1.2/lib/rspec/support/fuzzy_matcher.rb
logstash-codec-json-2.0.3 vendor/gems/rspec-support-3.1.2/lib/rspec/support/fuzzy_matcher.rb