1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@62094 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
eregon 2018-01-29 16:08:16 +00:00
parent 1e658d45e1
commit 3fa5bd38af
494 changed files with 4133 additions and 3109 deletions

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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"]

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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}

View file

@ -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)

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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