diff options
Diffstat (limited to 'spec/ruby/core/hash')
26 files changed, 92 insertions, 55 deletions
diff --git a/spec/ruby/core/hash/delete_if_spec.rb b/spec/ruby/core/hash/delete_if_spec.rb index 2edc88a205..93dab192b5 100644 --- a/spec/ruby/core/hash/delete_if_spec.rb +++ b/spec/ruby/core/hash/delete_if_spec.rb @@ -39,6 +39,6 @@ describe "Hash#delete_if" do lambda { HashSpecs.empty_frozen_hash.delete_if { true } }.should raise_error(frozen_error_class) end - it_behaves_like(:hash_iteration_no_block, :delete_if) - it_behaves_like(:enumeratorized_with_origin_size, :delete_if, { 1 => 2, 3 => 4, 5 => 6 }) + it_behaves_like :hash_iteration_no_block, :delete_if + it_behaves_like :enumeratorized_with_origin_size, :delete_if, { 1 => 2, 3 => 4, 5 => 6 } end diff --git a/spec/ruby/core/hash/each_key_spec.rb b/spec/ruby/core/hash/each_key_spec.rb index 4a4078a594..9cc31d803c 100644 --- a/spec/ruby/core/hash/each_key_spec.rb +++ b/spec/ruby/core/hash/each_key_spec.rb @@ -18,6 +18,6 @@ describe "Hash#each_key" do keys.should == h.keys end - it_behaves_like(:hash_iteration_no_block, :each_key) - it_behaves_like(:enumeratorized_with_origin_size, :each_key, { 1 => 2, 3 => 4, 5 => 6 }) + it_behaves_like :hash_iteration_no_block, :each_key + it_behaves_like :enumeratorized_with_origin_size, :each_key, { 1 => 2, 3 => 4, 5 => 6 } end diff --git a/spec/ruby/core/hash/each_pair_spec.rb b/spec/ruby/core/hash/each_pair_spec.rb index 285ca01b26..3fa07c8b67 100644 --- a/spec/ruby/core/hash/each_pair_spec.rb +++ b/spec/ruby/core/hash/each_pair_spec.rb @@ -5,7 +5,7 @@ require File.expand_path('../shared/each', __FILE__) require File.expand_path('../../enumerable/shared/enumeratorized', __FILE__) describe "Hash#each_pair" do - it_behaves_like(:hash_each, :each_pair) - it_behaves_like(:hash_iteration_no_block, :each_pair) - it_behaves_like(:enumeratorized_with_origin_size, :each_pair, { 1 => 2, 3 => 4, 5 => 6 }) + it_behaves_like :hash_each, :each_pair + it_behaves_like :hash_iteration_no_block, :each_pair + it_behaves_like :enumeratorized_with_origin_size, :each_pair, { 1 => 2, 3 => 4, 5 => 6 } end diff --git a/spec/ruby/core/hash/each_spec.rb b/spec/ruby/core/hash/each_spec.rb index 676fd284b9..261d46c5a4 100644 --- a/spec/ruby/core/hash/each_spec.rb +++ b/spec/ruby/core/hash/each_spec.rb @@ -5,7 +5,7 @@ require File.expand_path('../shared/each', __FILE__) require File.expand_path('../../enumerable/shared/enumeratorized', __FILE__) describe "Hash#each" do - it_behaves_like(:hash_each, :each) - it_behaves_like(:hash_iteration_no_block, :each) - it_behaves_like(:enumeratorized_with_origin_size, :each, { 1 => 2, 3 => 4, 5 => 6 }) + it_behaves_like :hash_each, :each + it_behaves_like :hash_iteration_no_block, :each + it_behaves_like :enumeratorized_with_origin_size, :each, { 1 => 2, 3 => 4, 5 => 6 } end diff --git a/spec/ruby/core/hash/each_value_spec.rb b/spec/ruby/core/hash/each_value_spec.rb index d3b2b8692e..f203675386 100644 --- a/spec/ruby/core/hash/each_value_spec.rb +++ b/spec/ruby/core/hash/each_value_spec.rb @@ -18,6 +18,6 @@ describe "Hash#each_value" do values.should == h.values end - it_behaves_like(:hash_iteration_no_block, :each_value) - it_behaves_like(:enumeratorized_with_origin_size, :each_value, { 1 => 2, 3 => 4, 5 => 6 }) + it_behaves_like :hash_iteration_no_block, :each_value + it_behaves_like :enumeratorized_with_origin_size, :each_value, { 1 => 2, 3 => 4, 5 => 6 } end diff --git a/spec/ruby/core/hash/element_set_spec.rb b/spec/ruby/core/hash/element_set_spec.rb index a2d67c7f22..e12356a1e5 100644 --- a/spec/ruby/core/hash/element_set_spec.rb +++ b/spec/ruby/core/hash/element_set_spec.rb @@ -3,5 +3,5 @@ require File.expand_path('../fixtures/classes', __FILE__) require File.expand_path('../shared/store', __FILE__) describe "Hash#[]=" do - it_behaves_like(:hash_store, :[]=) + it_behaves_like :hash_store, :[]= end diff --git a/spec/ruby/core/hash/fetch_spec.rb b/spec/ruby/core/hash/fetch_spec.rb index 5e701b1162..24662c2b0e 100644 --- a/spec/ruby/core/hash/fetch_spec.rb +++ b/spec/ruby/core/hash/fetch_spec.rb @@ -1,15 +1,17 @@ require File.expand_path('../../../spec_helper', __FILE__) require File.expand_path('../fixtures/classes', __FILE__) +require File.expand_path('../../../shared/hash/key_error', __FILE__) describe "Hash#fetch" do - it "returns the value for key" do - { a: 1, b: -1 }.fetch(:b).should == -1 + context "when the key is not found" do + it_behaves_like :key_error, ->(obj, key) { obj.fetch(key) }, Hash.new(a: 5) + it_behaves_like :key_error, ->(obj, key) { obj.fetch(key) }, {} + it_behaves_like :key_error, ->(obj, key) { obj.fetch(key) }, Hash.new { 5 } + it_behaves_like :key_error, ->(obj, key) { obj.fetch(key) }, Hash.new(5) end - it "raises a KeyError if key is not found" do - lambda { {}.fetch(:a) }.should raise_error(KeyError) - lambda { Hash.new(5).fetch(:a) }.should raise_error(KeyError) - lambda { Hash.new { 5 }.fetch(:a) }.should raise_error(KeyError) + it "returns the value for key" do + { a: 1, b: -1 }.fetch(:b).should == -1 end it "returns default if key is not found when passed a default" do diff --git a/spec/ruby/core/hash/fetch_values_spec.rb b/spec/ruby/core/hash/fetch_values_spec.rb index d6e47d5885..0d7c9599e7 100644 --- a/spec/ruby/core/hash/fetch_values_spec.rb +++ b/spec/ruby/core/hash/fetch_values_spec.rb @@ -1,5 +1,6 @@ require File.expand_path('../../../spec_helper', __FILE__) require File.expand_path('../fixtures/classes', __FILE__) +require File.expand_path('../../../shared/hash/key_error', __FILE__) ruby_version_is "2.3" do describe "Hash#fetch_values" do @@ -15,10 +16,7 @@ ruby_version_is "2.3" do end describe "with unmatched keys" do - it "raises a KeyError" do - ->{ @hash.fetch_values :z }.should raise_error(KeyError) - ->{ @hash.fetch_values :a, :z }.should raise_error(KeyError) - end + it_behaves_like :key_error, ->(obj, key) { obj.fetch_values(key) }, Hash.new(a: 5) it "returns the default value from block" do @hash.fetch_values(:z) { |key| "`#{key}' is not found" }.should == ["`z' is not found"] diff --git a/spec/ruby/core/hash/has_key_spec.rb b/spec/ruby/core/hash/has_key_spec.rb index 1d2aa279f1..90762ec4f3 100644 --- a/spec/ruby/core/hash/has_key_spec.rb +++ b/spec/ruby/core/hash/has_key_spec.rb @@ -3,6 +3,6 @@ require File.expand_path('../fixtures/classes', __FILE__) require File.expand_path('../shared/key', __FILE__) describe "Hash#has_key?" do - it_behaves_like(:hash_key_p, :has_key?) + it_behaves_like :hash_key_p, :has_key? end diff --git a/spec/ruby/core/hash/has_value_spec.rb b/spec/ruby/core/hash/has_value_spec.rb index dc8fdf9b69..c5fa12c8c3 100644 --- a/spec/ruby/core/hash/has_value_spec.rb +++ b/spec/ruby/core/hash/has_value_spec.rb @@ -3,6 +3,6 @@ require File.expand_path('../fixtures/classes', __FILE__) require File.expand_path('../shared/value', __FILE__) describe "Hash#has_value?" do - it_behaves_like(:hash_value_p, :has_value?) + it_behaves_like :hash_value_p, :has_value? end diff --git a/spec/ruby/core/hash/include_spec.rb b/spec/ruby/core/hash/include_spec.rb index 8731673c19..118dff2798 100644 --- a/spec/ruby/core/hash/include_spec.rb +++ b/spec/ruby/core/hash/include_spec.rb @@ -3,5 +3,5 @@ require File.expand_path('../fixtures/classes', __FILE__) require File.expand_path('../shared/key', __FILE__) describe "Hash#include?" do - it_behaves_like(:hash_key_p, :include?) + it_behaves_like :hash_key_p, :include? end diff --git a/spec/ruby/core/hash/keep_if_spec.rb b/spec/ruby/core/hash/keep_if_spec.rb index 7a9db092e2..0879d415b8 100644 --- a/spec/ruby/core/hash/keep_if_spec.rb +++ b/spec/ruby/core/hash/keep_if_spec.rb @@ -32,6 +32,6 @@ describe "Hash#keep_if" do lambda { HashSpecs.empty_frozen_hash.keep_if { false } }.should raise_error(frozen_error_class) end - it_behaves_like(:hash_iteration_no_block, :keep_if) - it_behaves_like(:enumeratorized_with_origin_size, :keep_if, { 1 => 2, 3 => 4, 5 => 6 }) + it_behaves_like :hash_iteration_no_block, :keep_if + it_behaves_like :enumeratorized_with_origin_size, :keep_if, { 1 => 2, 3 => 4, 5 => 6 } end diff --git a/spec/ruby/core/hash/key_spec.rb b/spec/ruby/core/hash/key_spec.rb index dc78174641..da3c5de972 100644 --- a/spec/ruby/core/hash/key_spec.rb +++ b/spec/ruby/core/hash/key_spec.rb @@ -4,9 +4,9 @@ require File.expand_path('../shared/key', __FILE__) require File.expand_path('../shared/index', __FILE__) describe "Hash#key?" do - it_behaves_like(:hash_key_p, :key?) + it_behaves_like :hash_key_p, :key? end describe "Hash#key" do - it_behaves_like(:hash_index, :key) + it_behaves_like :hash_index, :key end diff --git a/spec/ruby/core/hash/length_spec.rb b/spec/ruby/core/hash/length_spec.rb index 90751402eb..da8d0cb811 100644 --- a/spec/ruby/core/hash/length_spec.rb +++ b/spec/ruby/core/hash/length_spec.rb @@ -3,5 +3,5 @@ require File.expand_path('../fixtures/classes', __FILE__) require File.expand_path('../shared/length', __FILE__) describe "Hash#length" do - it_behaves_like(:hash_length, :length) + it_behaves_like :hash_length, :length end diff --git a/spec/ruby/core/hash/member_spec.rb b/spec/ruby/core/hash/member_spec.rb index 376bb4c006..154a042010 100644 --- a/spec/ruby/core/hash/member_spec.rb +++ b/spec/ruby/core/hash/member_spec.rb @@ -3,5 +3,5 @@ require File.expand_path('../fixtures/classes', __FILE__) require File.expand_path('../shared/key', __FILE__) describe "Hash#member?" do - it_behaves_like(:hash_key_p, :member?) + it_behaves_like :hash_key_p, :member? end diff --git a/spec/ruby/core/hash/merge_spec.rb b/spec/ruby/core/hash/merge_spec.rb index 21401ffd08..24852df303 100644 --- a/spec/ruby/core/hash/merge_spec.rb +++ b/spec/ruby/core/hash/merge_spec.rb @@ -66,7 +66,7 @@ describe "Hash#merge" do end describe "Hash#merge!" do - it_behaves_like(:hash_update, :merge!) + 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} diff --git a/spec/ruby/core/hash/rehash_spec.rb b/spec/ruby/core/hash/rehash_spec.rb index 5b15af923a..f44b42eecf 100644 --- a/spec/ruby/core/hash/rehash_spec.rb +++ b/spec/ruby/core/hash/rehash_spec.rb @@ -35,6 +35,21 @@ describe "Hash#rehash" do h[k2].should == v2 end + it "removes duplicate keys" do + a = [1,2] + b = [1] + + h = {} + h[a] = true + h[b] = true + b << 2 + h.size.should == 2 + h.keys.should == [a, b] + h.rehash + h.size.should == 1 + h.keys.should == [a] + end + it "raises a #{frozen_error_class} if called on a frozen instance" do lambda { HashSpecs.frozen_hash.rehash }.should raise_error(frozen_error_class) lambda { HashSpecs.empty_frozen_hash.rehash }.should raise_error(frozen_error_class) diff --git a/spec/ruby/core/hash/reject_spec.rb b/spec/ruby/core/hash/reject_spec.rb index 557435a019..fe2bec42d7 100644 --- a/spec/ruby/core/hash/reject_spec.rb +++ b/spec/ruby/core/hash/reject_spec.rb @@ -49,8 +49,8 @@ describe "Hash#reject" do reject_pairs.should == reject_bang_pairs end - it_behaves_like(:hash_iteration_no_block, :reject) - it_behaves_like(:enumeratorized_with_origin_size, :reject, { 1 => 2, 3 => 4, 5 => 6 }) + it_behaves_like :hash_iteration_no_block, :reject + it_behaves_like :enumeratorized_with_origin_size, :reject, { 1 => 2, 3 => 4, 5 => 6 } end describe "Hash#reject!" do @@ -95,6 +95,6 @@ describe "Hash#reject!" do lambda { HashSpecs.frozen_hash.reject! { false } }.should raise_error(frozen_error_class) end - it_behaves_like(:hash_iteration_no_block, :reject!) - it_behaves_like(:enumeratorized_with_origin_size, :reject!, { 1 => 2, 3 => 4, 5 => 6 }) + it_behaves_like :hash_iteration_no_block, :reject! + it_behaves_like :enumeratorized_with_origin_size, :reject!, { 1 => 2, 3 => 4, 5 => 6 } end diff --git a/spec/ruby/core/hash/replace_spec.rb b/spec/ruby/core/hash/replace_spec.rb index 61b3164355..642c78d63c 100644 --- a/spec/ruby/core/hash/replace_spec.rb +++ b/spec/ruby/core/hash/replace_spec.rb @@ -3,5 +3,5 @@ require File.expand_path('../fixtures/classes', __FILE__) require File.expand_path('../shared/replace', __FILE__) describe "Hash#replace" do - it_behaves_like(:hash_replace, :replace) + it_behaves_like :hash_replace, :replace end diff --git a/spec/ruby/core/hash/select_spec.rb b/spec/ruby/core/hash/select_spec.rb index 8a73116bc1..9348cc4c39 100644 --- a/spec/ruby/core/hash/select_spec.rb +++ b/spec/ruby/core/hash/select_spec.rb @@ -40,8 +40,8 @@ describe "Hash#select" do @empty.select.should be_an_instance_of(Enumerator) end - it_behaves_like(:hash_iteration_no_block, :select) - it_behaves_like(:enumeratorized_with_origin_size, :select, { 1 => 2, 3 => 4, 5 => 6 }) + it_behaves_like :hash_iteration_no_block, :select + it_behaves_like :enumeratorized_with_origin_size, :select, { 1 => 2, 3 => 4, 5 => 6 } end describe "Hash#select!" do @@ -78,6 +78,6 @@ describe "Hash#select!" do lambda { HashSpecs.frozen_hash.select! { true } }.should raise_error(frozen_error_class) end - it_behaves_like(:hash_iteration_no_block, :select!) - it_behaves_like(:enumeratorized_with_origin_size, :select!, { 1 => 2, 3 => 4, 5 => 6 }) + it_behaves_like :hash_iteration_no_block, :select! + it_behaves_like :enumeratorized_with_origin_size, :select!, { 1 => 2, 3 => 4, 5 => 6 } end diff --git a/spec/ruby/core/hash/size_spec.rb b/spec/ruby/core/hash/size_spec.rb index 71660af038..7143a1cb82 100644 --- a/spec/ruby/core/hash/size_spec.rb +++ b/spec/ruby/core/hash/size_spec.rb @@ -3,5 +3,5 @@ require File.expand_path('../fixtures/classes', __FILE__) require File.expand_path('../shared/length', __FILE__) describe "Hash#size" do - it_behaves_like(:hash_length, :size) + it_behaves_like :hash_length, :size end diff --git a/spec/ruby/core/hash/store_spec.rb b/spec/ruby/core/hash/store_spec.rb index 45ea8da896..847c2f54e3 100644 --- a/spec/ruby/core/hash/store_spec.rb +++ b/spec/ruby/core/hash/store_spec.rb @@ -3,5 +3,5 @@ require File.expand_path('../fixtures/classes', __FILE__) require File.expand_path('../shared/store', __FILE__) describe "Hash#store" do - it_behaves_like(:hash_store, :store) + it_behaves_like :hash_store, :store end diff --git a/spec/ruby/core/hash/transform_keys_spec.rb b/spec/ruby/core/hash/transform_keys_spec.rb index 30dde2287b..14d5d98117 100644 --- a/spec/ruby/core/hash/transform_keys_spec.rb +++ b/spec/ruby/core/hash/transform_keys_spec.rb @@ -60,17 +60,39 @@ ruby_version_is "2.5" do @hash.should == { 'a' => 1, 'b' => 2, 'c' => 3, 'd' => 4 } end - it "prevents conflicts between new keys and old ones" do - @hash.transform_keys!(&:succ) - @hash.should == { b: 1, c: 2, d: 3, e: 4 } + # https://bugs.ruby-lang.org/issues/14380 + ruby_version_is ""..."2.6" do + it "does not prevent conflicts between new keys and old ones" do + @hash.transform_keys!(&:succ) + @hash.should == { e: 1 } + end end - it "partially modifies the contents if we broke from the block" do - @hash.transform_keys! do |v| - break if v == :c - v.succ + ruby_version_is "2.6" do + it "prevents conflicts between new keys and old ones" do + @hash.transform_keys!(&:succ) + @hash.should == { b: 1, c: 2, d: 3, e: 4 } + end + end + + ruby_version_is ""..."2.6" do + it "partially modifies the contents if we broke from the block" do + @hash.transform_keys! do |v| + break if v == :c + v.succ + end + @hash.should == { c: 1, d: 4 } + end + end + + ruby_version_is "2.6" do + it "returns the processed keys if we broke from the block" do + @hash.transform_keys! do |v| + break if v == :c + v.succ + end + @hash.should == { b: 1, c: 2 } end - @hash.should == { b: 1, c: 2 } end it "keeps later pair if new keys conflict" do diff --git a/spec/ruby/core/hash/update_spec.rb b/spec/ruby/core/hash/update_spec.rb index 6cfedea271..033e3d35b0 100644 --- a/spec/ruby/core/hash/update_spec.rb +++ b/spec/ruby/core/hash/update_spec.rb @@ -3,5 +3,5 @@ require File.expand_path('../fixtures/classes', __FILE__) require File.expand_path('../shared/update', __FILE__) describe "Hash#update" do - it_behaves_like(:hash_update, :update) + it_behaves_like :hash_update, :update end diff --git a/spec/ruby/core/hash/value_spec.rb b/spec/ruby/core/hash/value_spec.rb index acfe1968d5..9681f4d827 100644 --- a/spec/ruby/core/hash/value_spec.rb +++ b/spec/ruby/core/hash/value_spec.rb @@ -3,6 +3,6 @@ require File.expand_path('../fixtures/classes', __FILE__) require File.expand_path('../shared/value', __FILE__) describe "Hash#value?" do - it_behaves_like(:hash_value_p, :value?) + it_behaves_like :hash_value_p, :value? end diff --git a/spec/ruby/core/hash/values_at_spec.rb b/spec/ruby/core/hash/values_at_spec.rb index 7c39e9b573..859034ecd2 100644 --- a/spec/ruby/core/hash/values_at_spec.rb +++ b/spec/ruby/core/hash/values_at_spec.rb @@ -3,5 +3,5 @@ require File.expand_path('../fixtures/classes', __FILE__) require File.expand_path('../shared/values_at', __FILE__) describe "Hash#values_at" do - it_behaves_like(:hash_values_at, :values_at) + it_behaves_like :hash_values_at, :values_at end |