From a7de4f2eedddd342e96ce2ef5bdc730243342ae3 Mon Sep 17 00:00:00 2001 From: Ryan Angilly Date: Wed, 16 Jun 2010 22:18:35 -0400 Subject: [PATCH] make BSON::OrderedHash equal to other types of hashes in both directions --- lib/bson/ordered_hash.rb | 9 ++++++--- test/mongo_bson/ordered_hash_test.rb | 10 ++++++++++ 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/lib/bson/ordered_hash.rb b/lib/bson/ordered_hash.rb index 009fbc1..8045732 100644 --- a/lib/bson/ordered_hash.rb +++ b/lib/bson/ordered_hash.rb @@ -27,9 +27,12 @@ module BSON def ==(other) begin - !other.nil? && - keys == other.keys && - values == other.values + case other + when BSON::OrderedHash + keys == other.keys && values == other.values + else + !other.nil? && keys.size == other.keys.size && keys.all? {|x| self[x] == other[x] } + end rescue false end diff --git a/test/mongo_bson/ordered_hash_test.rb b/test/mongo_bson/ordered_hash_test.rb index 5481357..9df639c 100644 --- a/test/mongo_bson/ordered_hash_test.rb +++ b/test/mongo_bson/ordered_hash_test.rb @@ -140,6 +140,16 @@ class OrderedHashTest < Test::Unit::TestCase assert_equal ['crab', 'apple', 3, 'foo'], @oh.values end + def test_equality_with_hash + o = BSON::OrderedHash.new + o[:a] = 1 + o[:b] = 2 + o[:c] = 3 + r = {:a => 1, :b => 2, :c => 3} + assert r == o + assert o == r + end + def test_update other = BSON::OrderedHash.new other['f'] = 'foo'