mongo-ruby-driver/test/mongo_bson/ordered_hash_test.rb

198 lines
3.9 KiB
Ruby
Raw Normal View History

2010-09-09 19:58:51 +00:00
require './test/test_helper'
class OrderedHashTest < Test::Unit::TestCase
def setup
2010-05-07 01:25:18 +00:00
@oh = BSON::OrderedHash.new
@oh['c'] = 1
@oh['a'] = 2
@oh['z'] = 3
@ordered_keys = %w(c a z)
end
2009-08-14 15:38:25 +00:00
def test_initialize
2010-05-07 01:25:18 +00:00
a = BSON::OrderedHash.new
2009-08-14 15:38:25 +00:00
a['x'] = 1
a['y'] = 2
2010-05-07 01:25:18 +00:00
b = BSON::OrderedHash['x' => 1, 'y' => 2]
2009-08-14 15:38:25 +00:00
assert_equal a, b
end
2010-01-21 22:35:53 +00:00
def test_hash_code
2010-05-07 01:25:18 +00:00
o = BSON::OrderedHash.new
2010-01-21 22:35:53 +00:00
o['number'] = 50
assert o.hash
end
def test_empty
2010-05-07 01:25:18 +00:00
assert_equal [], BSON::OrderedHash.new.keys
end
def test_uniq
list = []
2010-05-07 01:25:18 +00:00
doc = BSON::OrderedHash.new
doc['_id'] = 'ab12'
doc['name'] = 'test'
2010-05-07 01:25:18 +00:00
same_doc = BSON::OrderedHash.new
same_doc['_id'] = 'ab12'
same_doc['name'] = 'test'
list << doc
list << same_doc
assert_equal 2, list.size
assert_equal 1, list.uniq.size
end
2009-03-05 18:48:15 +00:00
def test_equality
2010-05-07 01:25:18 +00:00
a = BSON::OrderedHash.new
2009-03-05 18:48:15 +00:00
a['x'] = 1
a['y'] = 2
2010-05-07 01:25:18 +00:00
b = BSON::OrderedHash.new
2009-03-05 18:48:15 +00:00
b['y'] = 2
b['x'] = 1
2010-05-07 01:25:18 +00:00
c = BSON::OrderedHash.new
2009-03-05 18:48:15 +00:00
c['x'] = 1
c['y'] = 2
2010-05-07 01:25:18 +00:00
d = BSON::OrderedHash.new
2009-03-05 18:48:15 +00:00
d['x'] = 2
d['y'] = 3
2010-05-07 01:25:18 +00:00
e = BSON::OrderedHash.new
2009-03-05 18:48:15 +00:00
e['z'] = 1
e['y'] = 2
assert_equal a, c
assert_not_equal a, b
assert_not_equal a, d
assert_not_equal a, e
end
def test_order_preserved
assert_equal @ordered_keys, @oh.keys
end
2010-02-12 09:29:19 +00:00
def test_to_a_order_preserved
assert_equal @ordered_keys, @oh.to_a.map {|m| m.first}
2010-02-12 09:29:19 +00:00
end
def test_order_preserved_after_replace
@oh['a'] = 42
assert_equal @ordered_keys, @oh.keys
@oh['c'] = 'foobar'
assert_equal @ordered_keys, @oh.keys
@oh['z'] = /huh?/
assert_equal @ordered_keys, @oh.keys
end
def test_each
keys = []
@oh.each { |k, v| keys << k }
assert_equal keys, @oh.keys
@oh['z'] = 42
assert_equal keys, @oh.keys
assert_equal @oh, @oh.each {|k,v|}
end
def test_values
assert_equal [1, 2, 3], @oh.values
end
def test_merge
2010-05-07 01:25:18 +00:00
other = BSON::OrderedHash.new
other['f'] = 'foo'
noob = @oh.merge(other)
assert_equal @ordered_keys + ['f'], noob.keys
assert_equal [1, 2, 3, 'foo'], noob.values
end
def test_merge_bang
2010-05-07 01:25:18 +00:00
other = BSON::OrderedHash.new
other['f'] = 'foo'
@oh.merge!(other)
assert_equal @ordered_keys + ['f'], @oh.keys
assert_equal [1, 2, 3, 'foo'], @oh.values
end
def test_merge_bang_with_overlap
2010-05-07 01:25:18 +00:00
other = BSON::OrderedHash.new
other['a'] = 'apple'
other['c'] = 'crab'
other['f'] = 'foo'
@oh.merge!(other)
assert_equal @ordered_keys + ['f'], @oh.keys
assert_equal ['crab', 'apple', 3, 'foo'], @oh.values
end
def test_merge_bang_with_hash_with_overlap
other = Hash.new
other['a'] = 'apple'
other['c'] = 'crab'
other['f'] = 'foo'
@oh.merge!(other)
assert_equal @ordered_keys + ['f'], @oh.keys
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
2010-05-07 01:25:18 +00:00
other = BSON::OrderedHash.new
other['f'] = 'foo'
noob = @oh.update(other)
assert_equal @ordered_keys + ['f'], noob.keys
assert_equal [1, 2, 3, 'foo'], noob.values
end
def test_inspect_retains_order
assert_equal '{"c"=>1, "a"=>2, "z"=>3}', @oh.inspect
end
2009-01-21 15:53:26 +00:00
def test_clear
@oh.clear
assert @oh.keys.empty?
end
def test_delete
assert @oh.keys.include?('z')
@oh.delete('z')
assert !@oh.keys.include?('z')
end
def test_delete_if
assert @oh.keys.include?('z')
@oh.delete_if { |k,v| k == 'z' }
assert !@oh.keys.include?('z')
end
2010-08-23 18:19:32 +00:00
def test_reject
new = @oh.reject { |k, v| k == 'foo' }
2010-08-23 18:19:32 +00:00
assert new.keys == @oh.keys
new = @oh.reject { |k, v| k == 'z' }
assert !new.keys.include?('z')
end
2010-08-27 19:50:42 +00:00
def test_clone
copy = @oh.clone
assert copy.keys == @oh.keys
copy[:foo] = 1
assert copy.keys != @oh.keys
end
end