Sha256: e1c993f9a6a74e9413ea8ef6062f0c51d4a48311a31ef0b1d7d7d92b81086032

Contents?: true

Size: 923 Bytes

Versions: 4

Compression:

Stored size: 923 Bytes

Contents

class HashHelper

  # Compare two hashes.
  # Returns a hash with keys +:ok+, +:missing+, +:unexpected+, +:changed+.
  def self.compare(expected, actual)
    result = {
        :missing => {},
        :unexpected => {},
        :changed => {},
        :ok => {},
    }
    expected.each_pair do |key_expected, value_expected|
      if actual.include?(key_expected)
        value_actual = actual[key_expected]
        if value_actual == value_expected
          result[:ok][key_expected] = value_expected
        else
          result[:changed][key_expected] = {:expected => value_expected, :actual => value_actual}
        end
      else
        result[:missing][key_expected] = value_expected
      end
    end
    actual.each_pair do |key_actual, value_actual|
      next if expected.include?(key_actual)
      result[:unexpected][key_actual] = value_actual
    end
    result
  end

end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
minitest_log-1.0.1 lib/helpers/hash_helper.rb
minitest_log-1.0.0 lib/helpers/hash_helper.rb
minitest_log-0.2.0 lib/helpers/hash_helper.rb
minitest_log-0.1.0 lib/helpers/hash_helper.rb