Sha256: d6670738784072637ba1da7a52694958d4b6c5029226a5457e35a84c482ddc3b

Contents?: true

Size: 1.83 KB

Versions: 45

Compression:

Stored size: 1.83 KB

Contents

module Helpers
  module ValueMatchers
    ### Matchers
    def matches_value?(v1, v2, options = {})
      if v1 == :any
        # any value is acceptable
      elsif v1 == :not_nil
        return false if v2 == nil
      elsif v1.kind_of?(Hash)
        return false unless matches_hash?(v1, v2, options)
      elsif v1.kind_of?(Array)
        return false unless matches_array?(v1, v2, options)
      else
        return false unless v2 == v1
      end
      true
    end

    def matches_array?(array1, array2, options = {})
      return false unless array1.kind_of?(Array) && array2.kind_of?(Array)
      if options[:exact]
        return false unless array1.size == array2.size
      end

      # order of items shouldn't matter:
      #    ['a', 'b', 'c'], ['b', 'c', 'a'] -> true
      #
      # matched items should only be used once:
      #    ['a', 'b', 'c'], ['a', 'a', 'a'] -> false
      #    ['a', 'a', 'a'], ['a', 'b', 'c'] -> false
      matched = {}
      (0..(array1.size - 1)).each do |i|
        (0..(array2.size - 1)).each do |j|
          if !matched.has_value?(j.to_s) && matches_value?(array1[i], array2[j], options)
            matched[i.to_s] = j.to_s
            break
          end
        end
        return false unless matched.has_key?(i.to_s)
      end
      true
    end

    # options => {exact: true} # hashes must match exactly (i.e. have same number of key-value pairs that are all equal)
    def matches_hash?(hash1, hash2, options = {})
      return false unless hash1.kind_of?(Hash) && hash2.kind_of?(Hash)
      if options[:exact]
        return false unless hash1.size == hash2.size
      end

      hash1 = hash1.deep_symbolize_keys
      hash2 = hash2.deep_symbolize_keys

      hash1.each do |k1, v1|
        return false unless hash2.has_key?(k1) && matches_value?(v1, hash2[k1], options)
      end
      true
    end
  end
end

Version data entries

45 entries across 45 versions & 1 rubygems

Version Path
jsonapi-resources-0.7.1.beta1 test/helpers/value_matchers.rb
jsonapi-resources-0.7.0 test/helpers/value_matchers.rb
jsonapi-resources-0.6.2 test/helpers/value_matchers.rb
jsonapi-resources-0.6.1 test/helpers/value_matchers.rb
jsonapi-resources-0.6.0 test/helpers/value_matchers.rb
jsonapi-resources-0.5.9 test/helpers/value_matchers.rb
jsonapi-resources-0.5.8 test/helpers/value_matchers.rb
jsonapi-resources-0.5.7 test/helpers/value_matchers.rb
jsonapi-resources-0.5.6 test/helpers/value_matchers.rb
jsonapi-resources-0.5.5 test/helpers/value_matchers.rb
jsonapi-resources-0.5.4 test/helpers/value_matchers.rb
jsonapi-resources-0.5.3 test/helpers/value_matchers.rb
jsonapi-resources-0.5.2 test/helpers/value_matchers.rb
jsonapi-resources-0.5.1 test/helpers/value_matchers.rb
jsonapi-resources-0.5.0 test/helpers/value_matchers.rb
jsonapi-resources-0.4.4 test/helpers/value_matchers.rb
jsonapi-resources-0.4.3 test/helpers/value_matchers.rb
jsonapi-resources-0.4.2 test/helpers/value_matchers.rb
jsonapi-resources-0.4.1 test/helpers/value_matchers.rb
jsonapi-resources-0.4.0 test/helpers/value_matchers.rb