diff options
Diffstat (limited to 'spec/ruby/core/hash/merge_spec.rb')
| -rw-r--r-- | spec/ruby/core/hash/merge_spec.rb | 76 |
1 files changed, 61 insertions, 15 deletions
diff --git a/spec/ruby/core/hash/merge_spec.rb b/spec/ruby/core/hash/merge_spec.rb index 21401ffd08..9e566fcee9 100644 --- a/spec/ruby/core/hash/merge_spec.rb +++ b/spec/ruby/core/hash/merge_spec.rb @@ -1,7 +1,5 @@ -require File.expand_path('../../../spec_helper', __FILE__) -require File.expand_path('../fixtures/classes', __FILE__) -require File.expand_path('../shared/iteration', __FILE__) -require File.expand_path('../shared/update', __FILE__) +require_relative '../../spec_helper' +require_relative 'fixtures/classes' describe "Hash#merge" do it "returns a new hash by combining self with the contents of other" do @@ -28,9 +26,9 @@ describe "Hash#merge" do r = h1.merge(h2) { |k,x,y| "#{k}:#{x+2*y}" } r.should == { a: "a:-2", b: "b:9", c: -3, d: 5 } - lambda { + -> { h1.merge(h2) { |k, x, y| raise(IndexError) } - }.should raise_error(IndexError) + }.should.raise(IndexError) r = h1.merge(h1) { |k,x,y| :x } r.should == { a: :x, b: :x, d: :x } @@ -47,8 +45,8 @@ describe "Hash#merge" do end it "returns subclass instance for subclasses" do - HashSpecs::MyHash[1 => 2, 3 => 4].merge({ 1 => 2 }).should be_an_instance_of(HashSpecs::MyHash) - HashSpecs::MyHash[].merge({ 1 => 2 }).should be_an_instance_of(HashSpecs::MyHash) + HashSpecs::MyHash[1 => 2, 3 => 4].merge({ 1 => 2 }).should.instance_of?(HashSpecs::MyHash) + HashSpecs::MyHash[].merge({ 1 => 2 }).should.instance_of?(HashSpecs::MyHash) { 1 => 2, 3 => 4 }.merge(HashSpecs::MyHash[1 => 2]).class.should == Hash {}.merge(HashSpecs::MyHash[1 => 2]).class.should == Hash @@ -63,15 +61,63 @@ describe "Hash#merge" do merge_pairs.should == each_pairs end + it "preserves the order of merged elements" do + h1 = { 1 => 2, 3 => 4, 5 => 6 } + h2 = { 1 => 7 } + merge_pairs = [] + h1.merge(h2).each_pair { |k, v| merge_pairs << [k, v] } + merge_pairs.should == [[1,7], [3, 4], [5, 6]] + end + + it "preserves the order of merged elements for large hashes" do + h1 = {} + h2 = {} + merge_pairs = [] + expected_pairs = [] + (1..100).each { |x| h1[x] = x; h2[101 - x] = x; expected_pairs << [x, 101 - x] } + h1.merge(h2).each_pair { |k, v| merge_pairs << [k, v] } + merge_pairs.should == expected_pairs + end + + it "accepts multiple hashes" do + result = { a: 1 }.merge({ b: 2 }, { c: 3 }, { d: 4 }) + result.should == { a: 1, b: 2, c: 3, d: 4 } + end + + it "accepts zero arguments and returns a copy of self" do + hash = { a: 1 } + merged = hash.merge + + merged.should.eql?(hash) + merged.should_not.equal?(hash) + end + + it "retains the default value" do + h = Hash.new(1) + h.merge(b: 1, d: 2).default.should == 1 + end + + it "retains the default_proc" do + pr = proc { |h, k| h[k] = [] } + h = Hash.new(&pr) + h.merge(b: 1, d: 2).default_proc.should == pr + end + + it "retains compare_by_identity flag" do + h = { a: 9, c: 4 }.compare_by_identity + h2 = h.merge(b: 1, d: 2) + h2.compare_by_identity?.should == true + end + + it "ignores compare_by_identity flag of an argument" do + h = { a: 9, c: 4 }.compare_by_identity + h2 = { b: 1, d: 2 }.merge(h) + h2.compare_by_identity?.should == false + end end describe "Hash#merge!" do - it_behaves_like(:hash_update, :merge!) - - it "does not raise an exception if changing the value of an existing key during iteration" do - hash = {1 => 2, 3 => 4, 5 => 6} - hash2 = {1 => :foo, 3 => :bar} - hash.each { hash.merge!(hash2) } - hash.should == {1 => :foo, 3 => :bar, 5 => 6} + it "is an alias of Hash#update" do + Hash.instance_method(:merge!).should == Hash.instance_method(:update) end end |
