Hash (In)equality

Burdette Lamar - Jan 23 '18 - - Dev Community

In my automated testing, I often want to test a pair of hashes for equality. If the pair is equal, well enough.

But if they're not equal, the simple way to record that is to log the failure, along with the two hashes. If the hashes are very small: I can visually compare them to determine the differences.

But for larger hashes, I can't easily determine the differences visually. That's where my class HashHelpler comes in.

It has method HashHelper.compare(expected, actual) that accepts the expected and actual hashes, and returns a hash having four keys and their corresponding values:

  • :ok: value is a hash containing the key/value pairs that are in both expected and actual.
  • :missing: value is a hash containing the key/value pairs that are in expected, but not in actual.
  • :unexpected: value is a hash containing the key/value pairs that are in actual, but not in expected.
  • :changed: value is a hash detailing the keys that are in both expected and actual, but whose values differ.

So: in my method Log#verdict_assert_equal?, a failed hash comparison gets and logs the detailed differences, making it easy to see what's what.

Links:

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .