Sha256: 8616296f63117f9c106a450bab8c272268250f87f9789fd650ba091a291b6b59

Contents?: true

Size: 1.24 KB

Versions: 6

Compression:

Stored size: 1.24 KB

Contents

# spec/support/deep_match.rb

def deep_match?(snapshot, result, path = [], errors = [])
  if snapshot.is_a?(Hash)
    snapshot.each do |key, value|
      current_path = path + [key]
      if result.is_a?(Hash) && result.key?(key)
        deep_match?(value, result[key], current_path, errors)
      elsif result.respond_to?(key)
        deep_match?(value, result.send(key), current_path, errors)
      else
        errors << "Key #{current_path.join('->')} not found in result"
      end
    end
  elsif snapshot.is_a?(Array)
    if snapshot.length != result.length
      errors << "Array length mismatch at #{path.join('->')}: snapshot #{snapshot.length}, result #{result.length}"
    else
      snapshot.each_with_index do |item, index|
        unless deep_match?(item, result[index], path + ["[#{index}]"], errors)
          errors << "Array item mismatch at #{path.join('->')}[#{index}]"
        end
      end
    end
  else
    unless snapshot == result
      errors << "Value mismatch at #{path.join('->')}: snapshot #{snapshot}, result #{result}"
    end
  end

  errors.empty?
end

def validate_deep_match(snapshot, result)
  errors = []
  if deep_match?(snapshot, result, [], errors)
    true
  else
    raise "Deep match failed:\n" + errors.join("\n")
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
voucherify-8.0.3 __tests__/spec/support/validate_deep_match.rb
voucherify-8.0.2 __tests__/spec/support/validate_deep_match.rb
voucherify-8.0.1 __tests__/spec/support/validate_deep_match.rb
voucherify-8.0.0 __tests__/spec/support/validate_deep_match.rb
voucherify-7.0.0 __tests__/spec/support/validate_deep_match.rb
voucherify-6.0.0 __tests__/spec/support/validate_deep_match.rb