2009-12-01 18:49:57 +00:00
|
|
|
require 'test/test_helper'
|
2009-01-08 12:16:25 +00:00
|
|
|
|
|
|
|
class OrderedHashTest < Test::Unit::TestCase
|
|
|
|
|
|
|
|
def setup
|
2010-05-07 01:25:18 +00:00
|
|
|
@oh = BSON::OrderedHash.new
|
2009-01-08 12:16:25 +00:00
|
|
|
@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
|
|
|
|
|
2009-01-08 12:16:25 +00:00
|
|
|
def test_empty
|
2010-05-07 01:25:18 +00:00
|
|
|
assert_equal [], BSON::OrderedHash.new.keys
|
2009-01-08 12:16:25 +00:00
|
|
|
end
|
|
|
|
|
2009-12-14 17:56:29 +00:00
|
|
|
def test_uniq
|
|
|
|
list = []
|
2010-05-07 01:25:18 +00:00
|
|
|
doc = BSON::OrderedHash.new
|
2009-12-14 17:56:29 +00:00
|
|
|
doc['_id'] = 'ab12'
|
|
|
|
doc['name'] = 'test'
|
|
|
|
|
2010-05-07 01:25:18 +00:00
|
|
|
same_doc = BSON::OrderedHash.new
|
2009-12-14 17:56:29 +00:00
|
|
|
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
|
|
|
|
|
2009-01-08 12:16:25 +00:00
|
|
|
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
|
2010-02-19 23:00:03 +00:00
|
|
|
assert_equal @ordered_keys, @oh.to_a.map {|m| m.first}
|
2010-02-12 09:29:19 +00:00
|
|
|
end
|
|
|
|
|
2009-01-08 12:16:25 +00:00
|
|
|
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
|
2009-10-02 03:43:57 +00:00
|
|
|
|
|
|
|
assert_equal @oh, @oh.each {|k,v|}
|
2009-01-08 12:16:25 +00:00
|
|
|
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
|
2009-01-08 12:16:25 +00:00
|
|
|
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
|
2009-01-08 12:16:25 +00:00
|
|
|
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
|
2009-01-08 12:16:25 +00:00
|
|
|
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
|
|
|
|
|
2010-06-17 02:18:35 +00:00
|
|
|
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
|
|
|
|
|
2010-01-18 20:29:17 +00:00
|
|
|
def test_update
|
2010-05-07 01:25:18 +00:00
|
|
|
other = BSON::OrderedHash.new
|
2010-01-18 20:29:17 +00:00
|
|
|
other['f'] = 'foo'
|
|
|
|
noob = @oh.update(other)
|
|
|
|
assert_equal @ordered_keys + ['f'], noob.keys
|
|
|
|
assert_equal [1, 2, 3, 'foo'], noob.values
|
|
|
|
end
|
|
|
|
|
2009-01-09 18:54:12 +00:00
|
|
|
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
|
|
|
|
assert new.keys == @oh.keys
|
|
|
|
|
|
|
|
new = @oh.reject { |k, v| k == 'z' }
|
|
|
|
assert !new.keys.include?('z')
|
|
|
|
end
|
|
|
|
|
2009-01-08 12:16:25 +00:00
|
|
|
end
|