require 'test/unit' require 'facet/hash/%3C%3C' require 'facet/hash/alias%21' require 'facet/hash/collate' require 'facet/hash/collate%21' require 'facet/hash/has_keys' require 'facet/hash/keys_to_s' require 'facet/hash/keys_to_sym' require 'facet/hash/swap%21' require 'facet/hash/swapkey%21' require 'facet/hash/to_h' require 'facet/hash/to_ostruct' require 'facet/hash/to_ostruct_recurse' require 'facet/hash/traverse' require 'facet/hash/traverse%21' require 'facet/hash/update_each' require 'facet/hash/weave' require 'facet/hash/zipnew' class TC_Hash < Test::Unit::TestCase # << def test_shift_update a = { :a => 1, :b => 2, :c => 3 } b = { :a => 0, :d => 4 } e = { :a => 0, :b => 2, :c => 3, :d => 4 } assert_equal( e, a << b ) end # alias! def test_alias! foo = { 'a'=>1, 'b'=>2 } assert_equal( { 'a'=>1, 'b'=>2, 'c'=>2 }, foo.alias!('c','b') ) foo = { 'a'=>1, 'b'=>2 } assert_equal( { :a=>1, 'a'=>1, 'b'=>2 }, foo.alias!(:a,'a') ) foo = { :a=>1, :b=>2 } assert_equal( { :a=>1, :b=>2 }, foo.alias!('bar','foo') ) end # collate def test_collate a = { :a => 1, :b => 2, :c => 3 } e = { :a => 2, :b => 3, :c => 4 } assert_equal( e, a.collate{ |k,v| { k => v+1 } } ) end def test_collate! a = { :a => 1, :b => 2, :c => 3 } e = { :a => 2, :b => 3, :c => 4 } a.collate!{ |k,v| { k => v+1 } } assert_equal( e, a ) end # has_keys? def test_has_keys? assert( { :a=>1,:b=>2,:c=>3}.has_keys?(:a,:b) ) end # keys_to_s def test_keys_to_s foo = { :a=>1, :b=>2 } assert_equal( { "a"=>1, "b"=>2 }, foo.keys_to_s ) assert_equal( { :a =>1, :b=>2 }, foo ) end def test_keys_to_s! foo = { :a=>1, :b=>2 } assert_equal( { "a"=>1, "b"=>2 }, foo.keys_to_s! ) assert_equal( { "a"=>1, "b"=>2 }, foo ) end # keys_to_sym def test_keys_to_sym foo = { 'a'=>1, 'b'=>2 } assert_equal( { :a=>1, :b=>2 }, foo.keys_to_sym ) assert_equal( { "a" =>1, "b"=>2 }, foo ) end def test_keys_to_sym! foo = { 'a'=>1, 'b'=>2 } assert_equal( { :a=>1, :b=>2 }, foo.keys_to_sym! ) assert_equal( { :a=>1, :b=>2 }, foo ) end # swap! def test_swap! h = { :a=>1, :b=>2 } assert_equal( { :a=>2, :b=>1 }, h.swap!(:a, :b) ) end # swapkeys! def test_swapkeys! foo = { :a=>1, :b=>2 } assert_equal( { 'a'=>1, :b=>2 }, foo.swapkey!('a', :a) ) assert_equal( { 'a'=>1, 'b'=>2 }, foo.swapkey!('b', :b) ) assert_equal( { 'a'=>1, 'b'=>2 }, foo.swapkey!('foo','bar') ) end # to_h def test_to_h a = { :a => 1, :b => 2, :c => 3 } assert_equal( a, a.to_h ) end # to_ostruct def test_to_ostruct a = { :a => 1, :b => 2, :c => 3 } ao = a.to_ostruct assert_equal( a[:a], ao.a ) assert_equal( a[:b], ao.b ) assert_equal( a[:c], ao.c ) end def test_to_ostruct_recurse a = { :a => 1, :b => 2, :c => { :x => 4 } } ao = a.to_ostruct_recurse assert_equal( a[:a], ao.a ) assert_equal( a[:b], ao.b ) assert_equal( a[:c][:x], ao.c.x ) end # traverse def test_traverse h = { "A" => "x", "B" => "y" } h2 = h.traverse { |k,v| [k.downcase, v.upcase] } e = { "a" => "X", "b" => "Y" } assert_not_equal( h, h2 ) assert_equal( e, h2 ) end def test_traverse! h = { "A" => "x", "B" => "y" } h.traverse! { |k,v| [k.downcase, v.upcase] } e = { "a" => "X", "b" => "Y" } assert_equal( e, h ) end # update_each def test_update_each a = { :a => 1, :b => 2, :c => 3 } e = { :a => 2, :b => 3, :c => 4 } a.update_each{ |k,v| { k => v+1 } } assert_equal( e, a ) end def test_replace_each a = { :a => 1, :b => 2, :c => 3 } e = { :a => 2, :b => 3, :c => 4 } a.replace_each{ |k,v| { k => v+1 } } assert_equal( e, a ) end # weave def test_weave b = { :a => 1, :b => [1,2,3], :c => { :x => 'X' } } c = { :a => 2, :b => [4,5,6], :c => { :x => 'A', :y => 'B' } } r = { :a => 2, :b => [1,2,3,4,5,6], :c => { :x => 'A', :y => 'B' } } assert_equal( r, b.weave(c) ) end # zipnew def test_weave a = [1,2,3] b = [4,5,6] assert_equal( {1=>4,2=>5,3=>6}, Hash.zipnew(a,b) ) end end