diff options
Diffstat (limited to 'spec/ruby/core/hash')
-rw-r--r-- | spec/ruby/core/hash/constructor_spec.rb | 2 | ||||
-rw-r--r-- | spec/ruby/core/hash/hash_spec.rb | 8 | ||||
-rw-r--r-- | spec/ruby/core/hash/merge_spec.rb | 18 |
3 files changed, 27 insertions, 1 deletions
diff --git a/spec/ruby/core/hash/constructor_spec.rb b/spec/ruby/core/hash/constructor_spec.rb index d32117b5a1..14674a018b 100644 --- a/spec/ruby/core/hash/constructor_spec.rb +++ b/spec/ruby/core/hash/constructor_spec.rb @@ -54,7 +54,7 @@ describe "Hash.[]" do end ruby_version_is "2.7" do - it "ignores elements that are not arrays" do + it "raises for elements that are not arrays" do -> { Hash[[:a]].should == {} }.should raise_error(ArgumentError) diff --git a/spec/ruby/core/hash/hash_spec.rb b/spec/ruby/core/hash/hash_spec.rb index 7c26f02640..3649d4d8de 100644 --- a/spec/ruby/core/hash/hash_spec.rb +++ b/spec/ruby/core/hash/hash_spec.rb @@ -11,6 +11,14 @@ describe "Hash#hash" do { 0=>2, 11=>1 }.hash.should == { 11=>1, 0=>2 }.hash end + it "returns a value in which element values do not cancel each other out" do + { a: 2, b: 2 }.hash.should_not == { a: 7, b: 7 }.hash + end + + it "returns a value in which element keys and values do not cancel each other out" do + { :a => :a }.hash.should_not == { :b => :b }.hash + end + it "generates a hash for recursive hash structures" do h = {} h[:a] = h diff --git a/spec/ruby/core/hash/merge_spec.rb b/spec/ruby/core/hash/merge_spec.rb index e90beae87a..54abcb816d 100644 --- a/spec/ruby/core/hash/merge_spec.rb +++ b/spec/ruby/core/hash/merge_spec.rb @@ -63,6 +63,24 @@ 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 + ruby_version_is "2.6" do it "accepts multiple hashes" do result = { a: 1 }.merge({ b: 2 }, { c: 3 }, { d: 4 }) |