This commit is contained in:
@@ -0,0 +1,25 @@
|
||||
covers 'facets/hash/alias'
|
||||
|
||||
test_case Hash do
|
||||
|
||||
method :alias! do
|
||||
|
||||
test do
|
||||
foo = { 'a'=>1, 'b'=>2 }
|
||||
foo.alias!('c','b').assert == { 'a'=>1, 'b'=>2, 'c'=>2 }
|
||||
end
|
||||
|
||||
test do
|
||||
foo = { 'a'=>1, 'b'=>2 }
|
||||
foo.alias!(:a,'a').assert == { :a=>1, 'a'=>1, 'b'=>2 }
|
||||
end
|
||||
|
||||
test do
|
||||
foo = { :a=>1, :b=>2 }
|
||||
foo.alias!('bar','foo').assert == { :a=>1, :b=>2 }
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
covers 'facets/hash/argumentize'
|
||||
|
||||
test_case Hash do
|
||||
|
||||
method :argumentize do
|
||||
|
||||
setup do
|
||||
@h = { :list => [1,2], :base => "HI" }
|
||||
end
|
||||
|
||||
test "without an argument field" do
|
||||
@h.argumentize.assert == [ { :list => [1,2], :base => "HI" } ]
|
||||
end
|
||||
|
||||
test "with an argument field" do
|
||||
@h.argumentize(:list).assert == [ 1, 2, { :base => "HI" } ]
|
||||
@h.argumentize(:base).assert == [ "HI", { :list => [1,2] } ]
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
covers 'facets/hash/at'
|
||||
|
||||
test_case Hash do
|
||||
|
||||
method :at do
|
||||
|
||||
test do
|
||||
h = { :a=>1, :b=>2 }
|
||||
h.at(:a).assert == 1
|
||||
h.at(:b).assert == 2
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
covers 'facets/hash/autonew'
|
||||
|
||||
test_case Hash do
|
||||
|
||||
class_method :autonew do
|
||||
|
||||
test do
|
||||
h = Hash.autonew
|
||||
h['s1']['p1'] = 4
|
||||
h['s1']['p2'] = 5
|
||||
h['s1']['p3'] = 2
|
||||
h['s1']['p4'] = 3
|
||||
|
||||
h.assert == {"s1"=>{"p1"=>4, "p4"=>3, "p3"=>2, "p2"=>5}}
|
||||
|
||||
h['s1'].keys.sort.assert == ["p1", "p2", "p3", "p4"]
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
covers 'facets/hash/collate'
|
||||
|
||||
test_case Hash do
|
||||
|
||||
setup do
|
||||
@a = { :a=>1, :b=>2, :z=>26, :all=>%w|a b z|, :stuff1=>%w|foo bar|, :whee=>%w|a b| }
|
||||
@b = { :a=>1, :b=>4, :c=>9, :all=>%w|a b c|, :stuff2=>%w|jim jam|, :whee=>%w|a b| }
|
||||
@c = { :a=>1, :b=>8, :c=>27 }
|
||||
end
|
||||
|
||||
method :collate do
|
||||
|
||||
test do
|
||||
collated = @a.collate(@b)
|
||||
|
||||
collated.keys.length.assert == 8
|
||||
|
||||
collated[:a].assert == [1,1]
|
||||
collated[:b].assert == [2,4]
|
||||
collated[:c].assert == [9]
|
||||
collated[:z].assert == [26]
|
||||
|
||||
collated[:all].assert == %w|a b z a b c|
|
||||
collated[:stuff1].assert == %w|foo bar|
|
||||
collated[:stuff2].assert == %w|jim jam|
|
||||
collated[:whee].assert == %w|a b a b|
|
||||
end
|
||||
|
||||
test "multiple times" do
|
||||
collated = @a.collate(@b).collate(@c)
|
||||
|
||||
collated[:a].assert == [1,1,1]
|
||||
collated[:b].assert == [2,4,8]
|
||||
collated[:c].assert == [9,27]
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
method :collate! do
|
||||
|
||||
test "in place variant of #collate" do
|
||||
collated = @a.dup
|
||||
collated.collate!(@b)
|
||||
|
||||
collated.keys.length.assert == 8
|
||||
|
||||
collated[:a].assert == [1,1]
|
||||
collated[:b].assert == [2,4]
|
||||
collated[:c].assert == [9]
|
||||
collated[:z].assert == [26]
|
||||
|
||||
collated[:all].assert == %w|a b z a b c|
|
||||
collated[:stuff1].assert == %w|foo bar|
|
||||
collated[:stuff2].assert == %w|jim jam|
|
||||
collated[:whee].assert == %w|a b a b|
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
covers 'facets/hash/count'
|
||||
|
||||
test_case Hash do
|
||||
|
||||
method :count do
|
||||
|
||||
test do
|
||||
{:A=>1, :B=>1, :C=>2}.count(1).assert == 2
|
||||
{:A=>1, :B=>1, :C=>2}.count(2).assert == 1
|
||||
end
|
||||
|
||||
test "no matches" do
|
||||
{:A=>1, :B=>1, :C=>2}.count(3).assert == 0
|
||||
end
|
||||
|
||||
test "still works with Enumerable#count" do
|
||||
{:A=>1, :B=>1, :C=>2}.count.assert == 3
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
covers 'facets/hash/data'
|
||||
|
||||
test_case Hash do
|
||||
|
||||
method :data do
|
||||
|
||||
test do
|
||||
h = {:a=>1, :b=>2}
|
||||
|
||||
h.data.a.assert == 1
|
||||
h.data.b.assert == 2
|
||||
h.data.c.assert == nil
|
||||
end
|
||||
|
||||
test do
|
||||
h = {:a=>1, :b=>2}
|
||||
|
||||
h.data.c = 3
|
||||
h.data.c.assert == 3
|
||||
end
|
||||
|
||||
test do
|
||||
h = {:a=>1, :b=>2}
|
||||
|
||||
# Note, we can't apply a functor to a functor.
|
||||
#h.data.assert.a?
|
||||
#h.data.refute.d?
|
||||
|
||||
h.data.a?.assert == true
|
||||
h.data.d?.assert == false
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
@@ -0,0 +1,26 @@
|
||||
covers 'facets/hash/dearray_values'
|
||||
|
||||
test_case Hash do
|
||||
|
||||
method :dearray_values do
|
||||
|
||||
test do
|
||||
h = { :a=>[1], :b=>[1,2], :c=>3, :d=>[] }
|
||||
x = { :a=>1, :b=>1, :c=>3, :d=>nil }
|
||||
h.dearray_values.assert == x
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
method :dearray_singular_values do
|
||||
|
||||
test do
|
||||
h = { :a=>[1], :b=>[1,2], :c=>3, :d=>[] }
|
||||
x = { :a=>1, :b=>[1,2], :c=>3, :d=>nil }
|
||||
h.dearray_singular_values.assert == x
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
covers 'facets/hash/deep_merge'
|
||||
|
||||
test_case Hash do
|
||||
|
||||
method :deep_merge do
|
||||
|
||||
test do
|
||||
h1 = { :a=>1, :c=>{:x=>10} }
|
||||
h2 = { :b=>2, :c=>{:y=>11} }
|
||||
|
||||
h = h1.deep_merge(h2)
|
||||
|
||||
h.assert == {:a=>1, :b=>2, :c=>{:x=>10, :y=>11}}
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
method :deep_merge! do
|
||||
|
||||
test do
|
||||
h1 = { :a=>1, :c=>{:x=>10} }
|
||||
h2 = { :b=>2, :c=>{:y=>11} }
|
||||
|
||||
h1.deep_merge!(h2)
|
||||
|
||||
h1.assert == {:a=>1, :b=>2, :c=>{:x=>10, :y=>11}}
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
@@ -0,0 +1,131 @@
|
||||
covers 'facets/hash/deep_rekey'
|
||||
|
||||
test_case Hash do
|
||||
|
||||
method :deep_rekey do
|
||||
|
||||
test "default converts all keys to symbols" do
|
||||
foo = { "c"=>{ "a"=>1, "b"=>2 } }
|
||||
foo.deep_rekey.assert == { :c=>{ :a=>1, :b=>2 } }
|
||||
foo.assert == { "c"=>{ "a"=>1, "b"=>2 } }
|
||||
end
|
||||
|
||||
test "specific key" do
|
||||
bar = { :d=>{ :a=>1, :b=>2 } }
|
||||
foo = bar.deep_rekey(:a=>:c)
|
||||
foo[:d][:c].assert == 1
|
||||
foo[:d][:b].assert == 2
|
||||
foo[:d][:a].assert == nil
|
||||
end
|
||||
|
||||
test "multiple keys" do
|
||||
bar = { :z=>{ :a=>1, :b=>2, :c=>3 } }
|
||||
foo = bar.deep_rekey(:a=>:d,:b=>:e)
|
||||
foo[:z][:d].assert == 1
|
||||
foo[:z][:e].assert == 2
|
||||
foo[:z][:c].assert == 3
|
||||
foo[:z][:a].assert == nil
|
||||
foo[:z][:b].assert == nil
|
||||
end
|
||||
|
||||
test "multiple keys, some on same level as other hash" do
|
||||
bar = { :z=>{ :a=>1, :b=>2, :c=>3 }, :y=>4 }
|
||||
foo = bar.deep_rekey(:a=>:d,:b=>:e)
|
||||
foo[:z][:d].assert == 1
|
||||
foo[:z][:e].assert == 2
|
||||
foo[:z][:c].assert == 3
|
||||
foo[:y].assert == 4
|
||||
foo[:z][:a].assert == nil
|
||||
foo[:z][:b].assert == nil
|
||||
end
|
||||
|
||||
# TODO: Is using Hash#deep_rekey to exhange keys possible?
|
||||
|
||||
#test "exchange keys via dummy key" do
|
||||
# bar = { :a=>1, :b=>2, :c=>3 }
|
||||
# foo = bar.deep_rekey(:a=>:d,:c=>:a,:d=>:c)
|
||||
# foo[:a].assert == 3
|
||||
# foo[:b].assert == 2
|
||||
# foo[:c].assert == 1
|
||||
# foo[:d].assert == nil
|
||||
#end
|
||||
|
||||
test "with block" do
|
||||
bar = { :c=>{ :a=>1, :b=>2 } }
|
||||
foo = bar.deep_rekey{ |k| k.to_s }
|
||||
foo['c']['a'].assert == 1
|
||||
foo['c']['b'].assert == 2
|
||||
foo['c'][:a].assert == nil
|
||||
foo['c'][:b].assert == nil
|
||||
foo.assert == { 'c'=>{ 'a'=>1, 'b'=>2 } }
|
||||
end
|
||||
|
||||
test "with block of key, value" do
|
||||
bar = { :c=>{ :a=>1, :b=>2 } }
|
||||
foo = bar.deep_rekey{ |k, v| v.to_s }
|
||||
k = foo.keys.first
|
||||
foo[k]['1'].assert == 1
|
||||
foo[k]['2'].assert == 2
|
||||
foo[k][:a].assert == nil
|
||||
foo[k][:b].assert == nil
|
||||
foo[k]['a'].assert == nil
|
||||
foo[k]['b'].assert == nil
|
||||
foo.assert == { k=>{ '1'=>1, '2'=>2 } }
|
||||
end
|
||||
|
||||
test "with keys and block" do
|
||||
expect ArgumentError do
|
||||
bar = { :c=>{ :a=>1, :b=>2 } }
|
||||
bar.deep_rekey(:a=>:c){ |k| k.to_s }
|
||||
end
|
||||
end
|
||||
|
||||
test "symbol proc" do
|
||||
foo = { :c=>{ :a=>1, :b=>2 } }
|
||||
foo.deep_rekey(&:to_s).assert == { "c"=>{ "a"=>1, "b"=>2 } }
|
||||
foo.assert == { :c=>{ :a =>1, :b=>2 } }
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
method :deep_rekey! do
|
||||
|
||||
test "default" do
|
||||
foo = { "c"=>{ "a"=>1, "b"=>2 } }
|
||||
foo.deep_rekey!.assert == { :c=>{ :a=>1, :b=>2 } }
|
||||
foo.assert == { :c=>{ :a=>1, :b=>2 } }
|
||||
end
|
||||
|
||||
test "specific key" do
|
||||
foo = { :d=>{ :a=>1, :b=>2 } }
|
||||
foo.deep_rekey!(:a=>:c)
|
||||
foo[:d][:c].assert == 1
|
||||
foo[:d][:b].assert == 2
|
||||
foo[:d][:a].assert == nil
|
||||
end
|
||||
|
||||
test "with block" do
|
||||
foo = { :c=>{ :a=>1, :b=>2 } }
|
||||
foo.deep_rekey!{ |k| k.to_s }
|
||||
foo['c']['a'].assert == 1
|
||||
foo['c']['b'].assert == 2
|
||||
foo['c'][:a].assert == nil
|
||||
foo['c'][:b].assert == nil
|
||||
foo.assert == { 'c'=>{ 'a'=>1, 'b'=>2 } }
|
||||
end
|
||||
|
||||
test "symbol proc" do
|
||||
foo = { :c=>{ :a=>1, :b=>2 } }
|
||||
foo.deep_rekey!(&:to_s).assert == { "c"=>{ "a"=>1, "b"=>2 } }
|
||||
foo.assert == { "c"=>{ "a"=>1, "b"=>2 } }
|
||||
end
|
||||
|
||||
test "no conflict between keys" do
|
||||
r = { 0 => {1 => :a, 2 => :b} }.deep_rekey!{ |k| k + 1 }
|
||||
r[1].refute = {3 => :a}
|
||||
r[1].assert = {2 => :a, 3 => :b}
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
@@ -0,0 +1,18 @@
|
||||
covers 'facets/hash/delete_at'
|
||||
|
||||
test_case Hash do
|
||||
|
||||
method :delete_at do
|
||||
|
||||
test do
|
||||
a = { :a => 1, :b => 2, :c => 3 }
|
||||
x = { :b => 2, :c => 3 }
|
||||
v = a.delete_at(:a)
|
||||
v.assert == 1
|
||||
a.assert == x
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
covers 'facets/hash/delete_unless'
|
||||
|
||||
test_case Hash do
|
||||
|
||||
method :delete_unless do
|
||||
|
||||
test do
|
||||
a = { :a => 1, :b => 2, :c => 3 }
|
||||
x = { :a => 1 }
|
||||
r = a.delete_unless{|k,v| v == 1}
|
||||
r.assert == x
|
||||
a.assert == x
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
covers 'facets/hash/delete_values'
|
||||
|
||||
test_case Hash do
|
||||
|
||||
method :delete_values do
|
||||
|
||||
test do
|
||||
a = { :a => 1, :b => 2, :c => 3 }
|
||||
x = { :b => 2, :c => 3 }
|
||||
r = a.delete_values(1)
|
||||
#r.assert == x
|
||||
a.assert == x
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
method :delete_values_at do
|
||||
|
||||
test do
|
||||
a = { :a => 1, :b => 2, :c => 3 }
|
||||
x = { :b => 2, :c => 3 }
|
||||
r = a.delete_values_at(:a)
|
||||
r.assert == [1]
|
||||
a.assert == x
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
covers "facets/hash/diff"
|
||||
|
||||
test_case Hash do
|
||||
|
||||
method :diff do
|
||||
|
||||
test do
|
||||
h1 = {:a=>1,:b=>2}
|
||||
h2 = {:a=>1,:b=>3}
|
||||
h1.diff(h2).assert == {:b=>2}
|
||||
h2.diff(h1).assert == {:b=>3}
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
@@ -0,0 +1,16 @@
|
||||
covers 'facets/hash/each_with_key'
|
||||
|
||||
test_case Hash do
|
||||
|
||||
method :each_with_key do
|
||||
|
||||
test do
|
||||
h1 = {:a=>1, :b=>2}
|
||||
h2 = {}
|
||||
h1.each_with_key { |v,k| h2[v] = k }
|
||||
h2.assert == {1=>:a, 2=>:b}
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
@@ -0,0 +1,34 @@
|
||||
covers 'facets/hash/except'
|
||||
|
||||
test_case Hash do
|
||||
|
||||
method :except do
|
||||
|
||||
test do
|
||||
h = {:a=>1, :b=>2, :c=>3}
|
||||
h.except(:a).assert == {:b=>2,:c=>3}
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
method :except! do
|
||||
|
||||
test do
|
||||
h = {:a=>1, :b=>2, :c=>3}
|
||||
h.except!(:a).assert == {:b=>2,:c=>3}
|
||||
h.assert == {:b=>2,:c=>3}
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
method :remove! do
|
||||
|
||||
test do
|
||||
h = {:a=>1, :b=>2, :c=>3}
|
||||
h.remove!(:a).assert == {:a=>1}
|
||||
h.assert == {:b=>2,:c=>3}
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
@@ -0,0 +1,24 @@
|
||||
covers 'facets/hash/fetch_nested'
|
||||
|
||||
test_case Hash do
|
||||
|
||||
setup do
|
||||
@a = {'hello'=>{'world'=>42}}
|
||||
@b = {}
|
||||
end
|
||||
|
||||
method :fetch_nested do
|
||||
|
||||
test 'safe case' do
|
||||
@a['hello']['world'].assert == 42
|
||||
@a.fetch_nested(*['hello','world']).assert == 42
|
||||
end
|
||||
|
||||
test 'dangerous case' do
|
||||
expect(NoMethodError){@b['hello']['world']}
|
||||
@b.fetch_nested(*['hello','world']).assert == nil
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
@@ -0,0 +1,40 @@
|
||||
covers 'facets/hash/graph'
|
||||
|
||||
test_case Hash do
|
||||
|
||||
method :graph! do
|
||||
|
||||
test "using a hash" do
|
||||
a = { :a => 1, :b => 2, :c => 3 }
|
||||
e = { :a => 2, :b => 3, :c => 4 }
|
||||
a.graph!{ |k,v| { k => v+1 } }
|
||||
a.assert == e
|
||||
end
|
||||
|
||||
test "using an associative array" do
|
||||
h = {:a=>1,:b=>2,:c=>3}
|
||||
h.graph!{ |k,v| [v, v*v] }
|
||||
h.assert == {1=>1, 2=>4, 3=>9}
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
method :mash! do
|
||||
|
||||
test "using a hash" do
|
||||
a = { :a => 1, :b => 2, :c => 3 }
|
||||
e = { :a => 2, :b => 3, :c => 4 }
|
||||
a.mash!{ |k,v| { k => v+1 } }
|
||||
a.assert == e
|
||||
end
|
||||
|
||||
test "using an associative array" do
|
||||
h = {:a=>1,:b=>2,:c=>3}
|
||||
h.mash!{ |k,v| [v, v*v] }
|
||||
h.assert == {1=>1, 2=>4, 3=>9}
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
covers 'facets/hash/insert'
|
||||
|
||||
test_case Hash do
|
||||
|
||||
method :insert do
|
||||
|
||||
test do
|
||||
h = {:a=>1, :b=>2}
|
||||
|
||||
h.assert.insert(:c,3)
|
||||
h.assert == {:a=>1, :b=>2, :c=>3}
|
||||
|
||||
h.refute.insert(:a,0)
|
||||
h.assert == {:a=>1, :b=>2, :c=>3}
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
covers 'facets/hash/inverse'
|
||||
|
||||
test_case Hash do
|
||||
|
||||
method :inverse do
|
||||
|
||||
test do
|
||||
h1 = { :a=>1, :b=>2, :c=>2 }
|
||||
h2 = h1.inverse
|
||||
|
||||
h2[1].assert == :a
|
||||
|
||||
h2[2].assert.include?(:b)
|
||||
h2[2].assert.include?(:c)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
covers 'facets/hash/join'
|
||||
|
||||
test_case Hash do
|
||||
|
||||
method :join do
|
||||
|
||||
setup do
|
||||
@h = {:a=>1, :b=>2}
|
||||
end
|
||||
|
||||
test do
|
||||
s = @h.join
|
||||
s.assert.include?('b2')
|
||||
s.assert.include?('a1')
|
||||
end
|
||||
|
||||
test do
|
||||
s = @h.join(',')
|
||||
s.assert.include?('a,1')
|
||||
s.assert.include?('b,2')
|
||||
end
|
||||
|
||||
test do
|
||||
s = @h.join(',', ':')
|
||||
s.assert.include?('a,1')
|
||||
s.assert.include?('b,2')
|
||||
s.assert.include?(':')
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
@@ -0,0 +1,41 @@
|
||||
covers 'facets/hash/keys'
|
||||
|
||||
test_case Hash do
|
||||
|
||||
method :keys? do
|
||||
|
||||
test do
|
||||
{:a=>1,:b=>2,:c=>3}.assert.keys?(:a,:b)
|
||||
{:a=>1,:b=>2,:c=>3 }.refute.keys?(:a,:b,:d)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
method :only_keys? do
|
||||
|
||||
test do
|
||||
{:a=>1,:b=>2,:c=>3}.assert.only_keys?(:a,:b,:c)
|
||||
{:a=>1,:b=>2,:c=>3}.refute.only_keys?(:a,:b)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
method :has_keys? do
|
||||
|
||||
test do
|
||||
{:a=>1,:b=>2,:c=>3}.assert.has_keys?(:a,:b)
|
||||
{:a=>1,:b=>2,:c=>3 }.refute.has_keys?(:a,:b,:d)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
method :has_only_keys? do
|
||||
|
||||
test do
|
||||
{:a=>1,:b=>2,:c=>3}.assert.has_only_keys?(:a,:b,:c)
|
||||
{:a=>1,:b=>2,:c=>3}.refute.has_only_keys?(:a,:b)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
@@ -0,0 +1,17 @@
|
||||
covers 'facets/hash/new_with'
|
||||
|
||||
test_case Hash do
|
||||
|
||||
class_method :new_with do
|
||||
|
||||
test do
|
||||
h = Hash.new_with{0}
|
||||
|
||||
h[:ab].assert == 0
|
||||
h[100].assert == 0
|
||||
h["a"].assert == 0
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
@@ -0,0 +1,17 @@
|
||||
covers 'facets/hash/op_add'
|
||||
|
||||
test_case Hash do
|
||||
|
||||
method :+ do
|
||||
|
||||
test do
|
||||
a = { :a => 1, :b => 2 }
|
||||
b = { :b => 3, :c => 4 }
|
||||
x = { :a => 1, :b => 3, :c => 4 }
|
||||
r = a + b
|
||||
r.assert == x
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
@@ -0,0 +1,31 @@
|
||||
covers 'facets/hash/op_and'
|
||||
|
||||
test_case Hash do
|
||||
|
||||
method :& do
|
||||
|
||||
test "hash and hash" do
|
||||
a = { :a => 1, :b => 2 }
|
||||
b = { :a => 1 }
|
||||
x = { :a => 1 }
|
||||
(a & b).assert == x
|
||||
end
|
||||
|
||||
test "hashes with sub-array" do
|
||||
a = { :a => [1], :b => [2] }
|
||||
b = { :a => [1] }
|
||||
x = { :a => [1] }
|
||||
(a & b).assert == x
|
||||
end
|
||||
|
||||
test "hash and array" do
|
||||
a = { :a => 1, :b => 2 }
|
||||
b = [ :a ]
|
||||
x = { :a => 1 }
|
||||
(a & b).assert == x
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
covers 'facets/hash/op_mul'
|
||||
|
||||
test_case Hash do
|
||||
|
||||
method :* do
|
||||
|
||||
test do
|
||||
a = { :a => 1, :b => 2 }
|
||||
b = { :b => 3 }
|
||||
x = { :a => 1, :b => 2 }
|
||||
(a * b).assert == x
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
covers 'facets/hash/op_or'
|
||||
|
||||
test_case Hash do
|
||||
|
||||
method :| do
|
||||
|
||||
test do
|
||||
a = { :a => 1, :b => 2 }
|
||||
b = { :b => 3 }
|
||||
x = { :a => 1, :b => 2 }
|
||||
(a | b).assert == x
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
covers 'facets/hash/op_push'
|
||||
|
||||
test_case Hash do
|
||||
|
||||
method :<< do
|
||||
|
||||
test "two element array" do
|
||||
h = {}
|
||||
a = [:b, 2]
|
||||
x = {:b => 2}
|
||||
(h << a).assert == h
|
||||
h.assert == x
|
||||
end
|
||||
|
||||
test "update new entries" do
|
||||
a = {:a => 1}
|
||||
b = {:b => 2, :c => 3 }
|
||||
x = {:a => 1, :b => 2, :c => 3}
|
||||
(a << b).assert == x
|
||||
a.assert == x
|
||||
end
|
||||
|
||||
test "update overwrite" do
|
||||
a = {:a => 1, :b => 2, :c => 3}
|
||||
b = {:a => 0, :d => 4}
|
||||
x = {:a => 0, :b => 2, :c => 3, :d => 4}
|
||||
(a << b).assert == x
|
||||
a.assert == x
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
covers 'facets/hash/op_sub'
|
||||
|
||||
test_case Hash do
|
||||
|
||||
method :- do
|
||||
|
||||
test "hash minus array" do
|
||||
a = { :a => 1, :b => 2, :c => 3 }
|
||||
b = [ :a ]
|
||||
x = { :b => 2, :c => 3 }
|
||||
(a - b).assert == x
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
method :- do
|
||||
|
||||
test "hash minus hash" do
|
||||
a = { :a => 1, :b => 2, :c => 3 }
|
||||
b = { :a => 1, :d => 4 }
|
||||
x = { :b => 2, :c => 3 }
|
||||
(a - b).assert == x
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
covers 'facets/hash/recurse'
|
||||
|
||||
test_case Hash do
|
||||
|
||||
method :recurse do
|
||||
|
||||
test do
|
||||
h = {:a=>1, :b=>{:b1=>1, :b2=>2}}
|
||||
g = h.recurse{|h| h.inject({}){|h,(k,v)| h[k.to_s] = v; h} }
|
||||
g.assert == {"a"=>1, "b"=>{"b1"=>1, "b2"=>2}}
|
||||
end
|
||||
|
||||
test "with Arrays" do
|
||||
require 'facets/array/recurse'
|
||||
objects = []
|
||||
struct = {:a => 3, :b => [4, 5, {:c=>6}]}
|
||||
struct.recurse(Array, Hash){|o| objects << o; o }
|
||||
|
||||
objects.assert.include?( {:c=>6} )
|
||||
objects.assert.include?( [4, 5, {:c=>6}] )
|
||||
objects.assert.include?( struct )
|
||||
|
||||
objects.length.assert == 3
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
method :recurse! do
|
||||
|
||||
test do
|
||||
h = {:a=>1, :b=>{:b1=>1, :b2=>2}}
|
||||
h.recurse!{|h| h.inject({}){|h,(k,v)| h[k.to_s] = v; h} }
|
||||
h.assert == {"a"=>1, "b"=>{"b1"=>1, "b2"=>2}}
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -0,0 +1,71 @@
|
||||
covers 'facets/hash/recursively'
|
||||
covers 'facets/hash/graph'
|
||||
|
||||
test_case Hash do
|
||||
|
||||
method :recursively do
|
||||
|
||||
test "each" do
|
||||
h = {:a=>1,:b=>{:c=>3}}
|
||||
r = []
|
||||
h.recursively.each{ |k,v| r << [k,v] }
|
||||
r.assert.include? [:a,1]
|
||||
r.assert.include? [:b,{:c=>3}]
|
||||
r.assert.include? [:c,3]
|
||||
end
|
||||
|
||||
test "map" do
|
||||
h = {:a=>1,:b=>{:c=>3}}
|
||||
r = h.recursively{ |k,v| [k,v] }.map{ |k,v| [k, v.succ] }
|
||||
r.assert.include? [:a,2]
|
||||
r.assert.include? [:b,[[:c,4]]]
|
||||
end
|
||||
|
||||
test "map (moot)" do
|
||||
h = {:a=>1,:b=>{:c=>3}}
|
||||
a = h.recursively.map{ |k,v| [k, v] }
|
||||
a.assert.include? [:a,1]
|
||||
a.assert.include? [:b,[[:c,3]]]
|
||||
end
|
||||
|
||||
test 'graph' do
|
||||
h = {:a=>1,:b=>{:c=>3}}
|
||||
r = h.recursively{ |k,v| [k.to_s, v] }.graph{ |k,v| [k.to_s, v.to_s] }
|
||||
r.assert == {'a'=>'1','b'=>{'c'=>'3'}}
|
||||
end
|
||||
|
||||
test 'graph!' do
|
||||
h = {:a=>1,:b=>{:c=>3}}
|
||||
h.recursively{ |k,v| [k.to_s, v] }.graph!{ |k,v| [k.to_s, v.to_s] }
|
||||
h.assert == {'a'=>'1','b'=>{'c'=>'3'}}
|
||||
end
|
||||
|
||||
test 'mash' do
|
||||
h = {:a=>1,:b=>{:c=>3}}
|
||||
r = h.recursively{ |k,v| [k.to_s, v] }.mash{ |k,v| [k.to_s, v.to_s] }
|
||||
r.assert == {'a'=>'1','b'=>{'c'=>'3'}}
|
||||
end
|
||||
|
||||
test 'mash!' do
|
||||
h = {:a=>1,:b=>{:c=>3}}
|
||||
h.recursively{ |k,v| [k.to_s, v] }.mash!{ |k,v| [k.to_s, v.to_s] }
|
||||
h.assert == {'a'=>'1','b'=>{'c'=>'3'}}
|
||||
end
|
||||
|
||||
#test 'merge' do
|
||||
# h1 = {:a=>1,:b=>{:c=>3}}
|
||||
# h2 = {:b=>{:d=>4}}
|
||||
# r = h1.recursively.merge(h2)
|
||||
# r.assert == {:a=>1,:b=>{:c=>3, :d=>4}}
|
||||
#end
|
||||
|
||||
#test 'merge!' do
|
||||
# h1 = {:a=>1,:b=>{:c=>3}}
|
||||
# h2 = {:b=>{:d=>4}}
|
||||
# h1.recursively.merge!(h2)
|
||||
# h1.assert == {:a=>1,:b=>{:c=>3, :d=>4}}
|
||||
#end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
@@ -0,0 +1,119 @@
|
||||
covers 'facets/hash/rekey'
|
||||
|
||||
test_case Hash do
|
||||
|
||||
method :rekey do
|
||||
|
||||
test "default converts all keys to symbols" do
|
||||
foo = { "a"=>1, "b"=>2 }
|
||||
foo.rekey.assert == { :a=>1, :b=>2 }
|
||||
foo.assert == { "a"=>1, "b"=>2 }
|
||||
end
|
||||
|
||||
test "specific key" do
|
||||
bar = { :a=>1, :b=>2 }
|
||||
foo = bar.rekey(:a=>:c)
|
||||
foo[:c].assert == 1
|
||||
foo[:b].assert == 2
|
||||
foo[:a].assert == nil
|
||||
end
|
||||
|
||||
test "multiple keys" do
|
||||
bar = { :a=>1, :b=>2, :c=>3 }
|
||||
foo = bar.rekey(:a=>:d,:b=>:e)
|
||||
foo[:d].assert == 1
|
||||
foo[:e].assert == 2
|
||||
foo[:c].assert == 3
|
||||
foo[:a].assert == nil
|
||||
foo[:b].assert == nil
|
||||
end
|
||||
|
||||
# TODO: Is using Hash#rekey to exhange keys possible?
|
||||
|
||||
#test "exchange keys via dummy key" do
|
||||
# bar = { :a=>1, :b=>2, :c=>3 }
|
||||
# foo = bar.rekey(:a=>:d,:c=>:a,:d=>:c)
|
||||
# foo[:a].assert == 3
|
||||
# foo[:b].assert == 2
|
||||
# foo[:c].assert == 1
|
||||
# foo[:d].assert == nil
|
||||
#end
|
||||
|
||||
test "with block" do
|
||||
bar = { :a=>1, :b=>2 }
|
||||
foo = bar.rekey{ |k| k.to_s }
|
||||
foo['a'].assert == 1
|
||||
foo['b'].assert == 2
|
||||
foo[:a].assert == nil
|
||||
foo[:b].assert == nil
|
||||
foo.assert == { 'a'=>1, 'b'=>2 }
|
||||
end
|
||||
|
||||
test "with block of key, value" do
|
||||
bar = { :a=>1, :b=>2 }
|
||||
foo = bar.rekey{ |k, v| v.to_s }
|
||||
foo['1'].assert == 1
|
||||
foo['2'].assert == 2
|
||||
foo[:a].assert == nil
|
||||
foo[:b].assert == nil
|
||||
foo['a'].assert == nil
|
||||
foo['b'].assert == nil
|
||||
foo.assert == { '1'=>1, '2'=>2 }
|
||||
end
|
||||
|
||||
test "with keys and block" do
|
||||
expect ArgumentError do
|
||||
bar = { :a=>1, :b=>2 }
|
||||
bar.rekey(:a=>:c){ |k| k.to_s }
|
||||
end
|
||||
end
|
||||
|
||||
test "symbol proc" do
|
||||
foo = { :a=>1, :b=>2 }
|
||||
foo.rekey(&:to_s).assert == { "a"=>1, "b"=>2 }
|
||||
foo.assert == { :a =>1, :b=>2 }
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
method :rekey! do
|
||||
|
||||
test "default" do
|
||||
foo = { "a"=>1, "b"=>2 }
|
||||
foo.rekey!.assert == { :a=>1, :b=>2 }
|
||||
foo.assert == { :a=>1, :b=>2 }
|
||||
end
|
||||
|
||||
test "specific key" do
|
||||
foo = { :a=>1, :b=>2 }
|
||||
foo.rekey!(:a=>:c)
|
||||
foo[:c].assert == 1
|
||||
foo[:b].assert == 2
|
||||
foo[:a].assert == nil
|
||||
end
|
||||
|
||||
test "with block" do
|
||||
foo = { :a=>1, :b=>2 }
|
||||
foo.rekey!{ |k| k.to_s }
|
||||
foo['a'].assert == 1
|
||||
foo['b'].assert == 2
|
||||
foo[:a].assert == nil
|
||||
foo[:b].assert == nil
|
||||
foo.assert == { 'a'=>1, 'b'=>2 }
|
||||
end
|
||||
|
||||
test "symbol proc" do
|
||||
foo = { :a=>1, :b=>2 }
|
||||
foo.rekey!(&:to_s).assert == { "a"=>1, "b"=>2 }
|
||||
foo.assert == { "a"=>1, "b"=>2 }
|
||||
end
|
||||
|
||||
test "no conflict between keys" do
|
||||
r = {1 => :a, 2 => :b}.rekey!{ |k| k + 1 }
|
||||
r.refute = {3 => :a}
|
||||
r.assert = {2 => :a, 3 => :b}
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
@@ -0,0 +1,17 @@
|
||||
covers 'facets/hash/replace_each'
|
||||
|
||||
test_case Hash do
|
||||
|
||||
method :replace_each do
|
||||
|
||||
test do
|
||||
a = { :a => 1, :b => 2, :c => 3 }
|
||||
x = { :a => 2, :b => 3, :c => 4 }
|
||||
a.replace_each{ |k,v| { k => v+1 } }
|
||||
a.assert == x
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -0,0 +1,98 @@
|
||||
covers 'facets/hash/revalue'
|
||||
|
||||
test_case Hash do
|
||||
|
||||
method :revalue do
|
||||
|
||||
#test "default converts all keys to symbols" do
|
||||
# foo = { "a"=>1, "b"=>2 }
|
||||
# foo.rekey.assert == { :a=>1, :b=>2 }
|
||||
# foo.assert == { "a"=>1, "b"=>2 }
|
||||
#end
|
||||
|
||||
test "specific value" do
|
||||
bar = { :a=>1, :b=>2 }
|
||||
foo = bar.revalue(2=>3)
|
||||
foo[:a].assert == 1
|
||||
foo[:b].assert == 3
|
||||
foo.size.assert == 2
|
||||
end
|
||||
|
||||
test "multiple values" do
|
||||
bar = { :a=>1, :b=>2, :c=>3 }
|
||||
foo = bar.revalue(1=>4,3=>5)
|
||||
foo[:a].assert == 4
|
||||
foo[:b].assert == 2
|
||||
foo[:c].assert == 5
|
||||
foo.size.assert == 3
|
||||
end
|
||||
|
||||
test "with block" do
|
||||
bar = { :a=>1, :b=>2 }
|
||||
foo = bar.revalue{ |v| v.to_s }
|
||||
foo[:a].assert == "1"
|
||||
foo[:b].assert == "2"
|
||||
foo.assert == { :a=>"1", :b=>"2" }
|
||||
end
|
||||
|
||||
test "with block of key, value" do
|
||||
bar = { :a=>1, :b=>2 }
|
||||
foo = bar.revalue{ |k, v| [k,v] }
|
||||
foo[:a].assert == [:a,1]
|
||||
foo[:b].assert == [:b,2]
|
||||
foo.assert == { :a=>[:a,1], :b=>[:b,2] }
|
||||
end
|
||||
|
||||
test "with values and block" do
|
||||
expect ArgumentError do
|
||||
bar = { :a=>1, :b=>2 }
|
||||
bar.revalue(1=>3){ |v| v.to_s }
|
||||
end
|
||||
end
|
||||
|
||||
test "symbol proc" do
|
||||
foo = { :a=>1, :b=>2 }
|
||||
foo.revalue(&:to_s).assert == { :a=>"1", :b=>"2" }
|
||||
foo.assert == { :a =>1, :b=>2 }
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
method :revalue! do
|
||||
|
||||
#test "default" do
|
||||
# foo = { "a"=>1, "b"=>2 }
|
||||
# foo.revalue!.assert == { :a=>1, :b=>2 }
|
||||
# foo.assert == { :a=>1, :b=>2 }
|
||||
#end
|
||||
|
||||
test "specific value" do
|
||||
foo = { :a=>1, :b=>2 }
|
||||
foo.revalue!(1=>3)
|
||||
foo[:a].assert == 3
|
||||
foo[:b].assert == 2
|
||||
end
|
||||
|
||||
test "with block" do
|
||||
foo = { :a=>1, :b=>2 }
|
||||
foo.revalue!{ |v| v.to_s }
|
||||
foo[:a].assert == "1"
|
||||
foo[:b].assert == "2"
|
||||
foo.assert == { :a=>"1", :b=>"2" }
|
||||
end
|
||||
|
||||
test "symbol proc" do
|
||||
foo = { :a=>1, :b=>2 }
|
||||
foo.revalue!(&:to_s).assert == { :a=>"1", :b=>"2" }
|
||||
foo.assert == { :a=>"1", :b=>"2" }
|
||||
end
|
||||
|
||||
test "no conflict between keys" do
|
||||
r = {:a => 1, :b => 2}.revalue!{ |v| v + 1 }
|
||||
r.refute = {:a => 3}
|
||||
r.assert = {:a => 2, :b => 3}
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
@@ -0,0 +1,39 @@
|
||||
covers 'facets/hash/reverse_merge'
|
||||
|
||||
test_case Hash do
|
||||
|
||||
method :reverse_merge do
|
||||
|
||||
test do
|
||||
h1 = { :a=>1, :b=>2, :c=>2 }
|
||||
h2 = { :c=>3 }
|
||||
h3 = h1.reverse_merge(h2)
|
||||
h3[:c].assert == 2
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
method :reverse_merge! do
|
||||
|
||||
test do
|
||||
h1 = { :a=>1, :b=>2, :c=>2 }
|
||||
h2 = { :c=>3 }
|
||||
h1.reverse_merge!(h2)
|
||||
h1[:c].assert == 2
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
method :reverse_update do
|
||||
|
||||
test do
|
||||
h1 = { :a=>1, :b=>2, :c=>2 }
|
||||
h2 = { :c=>3 }
|
||||
h1.reverse_update(h2)
|
||||
h1[:c].assert == 2
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
covers 'facets/hash/slice'
|
||||
|
||||
test_case Hash do
|
||||
|
||||
method :slice do
|
||||
|
||||
test 'only existing keys' do
|
||||
h = {:a=>1,:b=>2,:c=>3}
|
||||
h.slice(:a, :b).assert == {:b=>2, :a=>1}
|
||||
end
|
||||
|
||||
test 'with non-existent keys' do
|
||||
h = {:a=>1,:b=>2,:c=>3}
|
||||
h.slice(:a, :d).assert == {:a=>1}
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
method :slice! do
|
||||
|
||||
test do
|
||||
h = {:a=>1,:b=>2,:c=>3}
|
||||
h.slice!(:a, :b).assert == {:c=>3}
|
||||
h.assert == {:a=>1,:b=>2}
|
||||
end
|
||||
|
||||
test do
|
||||
h = {:a=>1,:b=>2,:c=>3}
|
||||
h.slice!(:a, :d).assert == {:c=>3, :b=>2}
|
||||
h.assert == {:a=>1}
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
covers 'facets/hash/stringify_keys'
|
||||
|
||||
test_case Hash do
|
||||
|
||||
method :stringify_keys do
|
||||
|
||||
test do
|
||||
foo = { :a=>1, :b=>2 }
|
||||
foo.stringify_keys.assert == { "a"=>1, "b"=>2 }
|
||||
foo.assert == { :a =>1, :b=>2 }
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
method :stringify_keys! do
|
||||
|
||||
test do
|
||||
foo = { :a=>1, :b=>2 }
|
||||
foo.stringify_keys!.assert == { "a"=>1, "b"=>2 }
|
||||
foo.assert == { "a"=>1, "b"=>2 }
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
covers 'facets/hash/swap'
|
||||
|
||||
test_case Hash do
|
||||
|
||||
method :swap! do
|
||||
|
||||
test do
|
||||
h = { :a=>1, :b=>2 }
|
||||
h.swap!(:a, :b).assert == { :a=>2, :b=>1 }
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
covers 'facets/hash/symbolize_keys'
|
||||
|
||||
test_case Hash do
|
||||
|
||||
method :symbolize_keys do
|
||||
|
||||
test do
|
||||
foo = { 'a'=>1, 'b'=>2 }
|
||||
foo.symbolize_keys == { :a=>1, :b=>2 }
|
||||
foo.assert == { "a" =>1, "b"=>2 }
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
method :symbolize_keys! do
|
||||
|
||||
test do
|
||||
foo = { 'a'=>1, 'b'=>2 }
|
||||
foo.symbolize_keys!.assert == { :a=>1, :b=>2 }
|
||||
foo.assert == { :a=>1, :b=>2 }
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
covers 'facets/hash/to_mod'
|
||||
|
||||
test_case Hash do
|
||||
|
||||
method :to_mod do
|
||||
|
||||
test do
|
||||
h = { :a=>1, :b=>2 }
|
||||
m = h.to_mod
|
||||
Module.assert === m
|
||||
end
|
||||
|
||||
test "check methods" do
|
||||
h = { :a=>1, :b=>2 }
|
||||
m = h.to_mod
|
||||
x = Class.new{ include m }.new
|
||||
x.a.assert == 1
|
||||
x.b.assert == 2
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
covers 'facets/hash/to_proc'
|
||||
|
||||
test_case Hash do
|
||||
|
||||
method :to_proc do
|
||||
|
||||
test do
|
||||
c = Class.new do
|
||||
attr_accessor :a
|
||||
end
|
||||
|
||||
h = {:a => 1}
|
||||
o = c.new
|
||||
|
||||
h.to_proc.call(o)
|
||||
|
||||
o.a.assert == 1
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
covers 'facets/hash/to_struct'
|
||||
|
||||
test_case Hash do
|
||||
|
||||
method :to_struct do
|
||||
|
||||
test do
|
||||
h = {:name=>"Dan","age"=>33,"rank"=>"SrA","grade"=>"E4"}
|
||||
s = h.to_struct("Foo")
|
||||
|
||||
s.name.assert = "Dan"
|
||||
s.age.assert = 33
|
||||
s.rank.assert = "SrA"
|
||||
s.grade.assert = "E4"
|
||||
|
||||
#Foo.assert.is_a?(Struct) ?
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
covers 'facets/hash/traverse'
|
||||
|
||||
test_case Hash do
|
||||
|
||||
method :traverse do
|
||||
|
||||
test do
|
||||
h = { "A" => "x", "B" => "y" }
|
||||
j = h.traverse{ |k,v| [k.downcase, v.upcase] }
|
||||
x = { "a" => "X", "b" => "Y" }
|
||||
j.refute == h
|
||||
j.assert == x
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
method :traverse! do
|
||||
|
||||
test do
|
||||
h = { "A" => "x", "B" => "y" }
|
||||
h.traverse!{ |k,v| [k.downcase, v.upcase] }
|
||||
x = { "a" => "X", "b" => "Y" }
|
||||
h.assert == x
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
covers 'facets/hash/update_each'
|
||||
|
||||
test_case Hash do
|
||||
|
||||
method :update_each do
|
||||
|
||||
test do
|
||||
a = { :a => 1, :b => 2, :c => 3 }
|
||||
x = { :a => 2, :b => 3, :c => 4 }
|
||||
a.update_each{ |k,v| { k => v+1 } }
|
||||
a.assert == x
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
@@ -0,0 +1,21 @@
|
||||
covers 'facets/hash/update_keys'
|
||||
|
||||
test_case Hash do
|
||||
|
||||
method :update_keys do
|
||||
|
||||
test do
|
||||
h = { 'A' => 1, 'B' => 2 }
|
||||
h.update_keys{ |k| k.downcase }
|
||||
h.assert == {'a' => 1, 'b' => 2}
|
||||
end
|
||||
|
||||
test do
|
||||
h = { :a => 1, :b => 2 }
|
||||
h.update_keys{ |k| "#{k}!" }
|
||||
h.assert == {'a!' => 1, 'b!' => 2}
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
@@ -0,0 +1,15 @@
|
||||
covers 'facets/hash/update_values'
|
||||
|
||||
test_case Hash do
|
||||
|
||||
method :update_values do
|
||||
|
||||
test do
|
||||
h = {1 => 'A', 2 => 'B'}
|
||||
h.update_values{ |v| v.downcase }
|
||||
h.assert == {1 => 'a', 2 => 'b'}
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
@@ -0,0 +1,18 @@
|
||||
covers 'facets/hash/weave'
|
||||
|
||||
test_case Hash do
|
||||
|
||||
method :weave do
|
||||
|
||||
test do
|
||||
b = { :a=>1, :b=>[1,2,3], :c=>{ :x=>'X' } }
|
||||
c = { :a=>2, :b=>[4,5,6], :c=>{ :x=>'A', :y => 'B' } }
|
||||
x = { :a=>2, :b=>[1,2,3,4,5,6], :c=>{ :x => 'A', :y => 'B' } }
|
||||
b.weave(c).assert == x
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
covers 'facets/hash/zip'
|
||||
|
||||
test_case Hash do
|
||||
|
||||
class_method :zip do
|
||||
|
||||
test do
|
||||
a = [1,2,3]
|
||||
b = [4,5,6]
|
||||
Hash.zip(a,b).assert == {1=>4,2=>5,3=>6}
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user