diff options
author | Benoit Daloze <eregontp@gmail.com> | 2019-04-27 18:53:23 +0200 |
---|---|---|
committer | Benoit Daloze <eregontp@gmail.com> | 2019-04-27 18:53:23 +0200 |
commit | a1b4816759418ca8fe510e8739622fc5d77ab0f0 (patch) | |
tree | 9d4fb6091d0086817f5bde46bf1150e9130d34fd /spec/ruby/core/hash | |
parent | 00c33d9c232ed1a79eda17acd7231ac93caa162b (diff) | |
download | ruby-a1b4816759418ca8fe510e8739622fc5d77ab0f0.tar.gz |
Update to ruby/spec@15c9619
Diffstat (limited to 'spec/ruby/core/hash')
-rw-r--r-- | spec/ruby/core/hash/compact_spec.rb | 84 | ||||
-rw-r--r-- | spec/ruby/core/hash/compare_by_identity_spec.rb | 12 | ||||
-rw-r--r-- | spec/ruby/core/hash/fetch_spec.rb | 6 | ||||
-rw-r--r-- | spec/ruby/core/hash/merge_spec.rb | 7 | ||||
-rw-r--r-- | spec/ruby/core/hash/shared/each.rb | 2 | ||||
-rw-r--r-- | spec/ruby/core/hash/transform_values_spec.rb | 146 |
6 files changed, 130 insertions, 127 deletions
diff --git a/spec/ruby/core/hash/compact_spec.rb b/spec/ruby/core/hash/compact_spec.rb index b75621b4d4..ec9d4b5d79 100644 --- a/spec/ruby/core/hash/compact_spec.rb +++ b/spec/ruby/core/hash/compact_spec.rb @@ -1,61 +1,59 @@ require_relative '../../spec_helper' require_relative 'fixtures/classes' -ruby_version_is "2.4" do - describe "Hash#compact" do - before :each do - @hash = { truthy: true, false: false, nil: nil, nil => true } - @initial_pairs = @hash.dup - @compact = { truthy: true, false: false, nil => true } - end +describe "Hash#compact" do + before :each do + @hash = { truthy: true, false: false, nil: nil, nil => true } + @initial_pairs = @hash.dup + @compact = { truthy: true, false: false, nil => true } + end - it "returns new object that rejects pair has nil value" do - ret = @hash.compact - ret.should_not equal(@hash) - ret.should == @compact - end + it "returns new object that rejects pair has nil value" do + ret = @hash.compact + ret.should_not equal(@hash) + ret.should == @compact + end - it "keeps own pairs" do - @hash.compact - @hash.should == @initial_pairs - end + it "keeps own pairs" do + @hash.compact + @hash.should == @initial_pairs end +end - describe "Hash#compact!" do - before :each do - @hash = { truthy: true, false: false, nil: nil, nil => true } - @initial_pairs = @hash.dup - @compact = { truthy: true, false: false, nil => true } - end +describe "Hash#compact!" do + before :each do + @hash = { truthy: true, false: false, nil: nil, nil => true } + @initial_pairs = @hash.dup + @compact = { truthy: true, false: false, nil => true } + end - it "returns self" do - @hash.compact!.should equal(@hash) - end + it "returns self" do + @hash.compact!.should equal(@hash) + end - it "rejects own pair has nil value" do + it "rejects own pair has nil value" do + @hash.compact! + @hash.should == @compact + end + + context "when each pair does not have nil value" do + before :each do @hash.compact! - @hash.should == @compact end - context "when each pair does not have nil value" do - before :each do - @hash.compact! - end - - it "returns nil" do - @hash.compact!.should be_nil - end + it "returns nil" do + @hash.compact!.should be_nil end + end - describe "on frozen instance" do - before :each do - @hash.freeze - end + describe "on frozen instance" do + before :each do + @hash.freeze + end - it "keeps pairs and raises a #{frozen_error_class}" do - ->{ @hash.compact! }.should raise_error(frozen_error_class) - @hash.should == @initial_pairs - end + it "keeps pairs and raises a #{frozen_error_class}" do + ->{ @hash.compact! }.should raise_error(frozen_error_class) + @hash.should == @initial_pairs end end end diff --git a/spec/ruby/core/hash/compare_by_identity_spec.rb b/spec/ruby/core/hash/compare_by_identity_spec.rb index e463c311be..33db59124e 100644 --- a/spec/ruby/core/hash/compare_by_identity_spec.rb +++ b/spec/ruby/core/hash/compare_by_identity_spec.rb @@ -108,13 +108,11 @@ describe "Hash#compare_by_identity" do @idh.keys.first.should equal foo end - ruby_bug "#12855", ""..."2.4.1" do - it "gives different identity for string literals" do - @idh['foo'] = 1 - @idh['foo'] = 2 - @idh.values.should == [1, 2] - @idh.size.should == 2 - end + it "gives different identity for string literals" do + @idh['foo'] = 1 + @idh['foo'] = 2 + @idh.values.should == [1, 2] + @idh.size.should == 2 end end diff --git a/spec/ruby/core/hash/fetch_spec.rb b/spec/ruby/core/hash/fetch_spec.rb index 2fee5d0164..197832e311 100644 --- a/spec/ruby/core/hash/fetch_spec.rb +++ b/spec/ruby/core/hash/fetch_spec.rb @@ -8,6 +8,12 @@ describe "Hash#fetch" do 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) + + it "formats the object with #inspect in the KeyError message" do + -> { + {}.fetch('foo') + }.should raise_error(KeyError, 'key not found: "foo"') + end end it "returns the value for key" do diff --git a/spec/ruby/core/hash/merge_spec.rb b/spec/ruby/core/hash/merge_spec.rb index 5ea70610be..e90beae87a 100644 --- a/spec/ruby/core/hash/merge_spec.rb +++ b/spec/ruby/core/hash/merge_spec.rb @@ -69,9 +69,12 @@ describe "Hash#merge" do result.should == { a: 1, b: 2, c: 3, d: 4 } end - it "accepts zero arguments and returns self" do + it "accepts zero arguments and returns a copy of self" do hash = { a: 1 } - hash.merge.should eql(hash) + merged = hash.merge + + merged.should eql(hash) + merged.should_not equal(hash) end end end diff --git a/spec/ruby/core/hash/shared/each.rb b/spec/ruby/core/hash/shared/each.rb index 1d89cfdd39..d1f2e5f672 100644 --- a/spec/ruby/core/hash/shared/each.rb +++ b/spec/ruby/core/hash/shared/each.rb @@ -21,7 +21,7 @@ describe :hash_each, shared: true do ary.sort.should == ["a", "b", "c"] end - it "yields 2 values and not an Array of 2 elements" do + it "yields 2 values and not an Array of 2 elements when given a callable of arity 2" do obj = Object.new def obj.foo(key, value) ScratchPad << key << value diff --git a/spec/ruby/core/hash/transform_values_spec.rb b/spec/ruby/core/hash/transform_values_spec.rb index 80e875097a..8b53b7a522 100644 --- a/spec/ruby/core/hash/transform_values_spec.rb +++ b/spec/ruby/core/hash/transform_values_spec.rb @@ -1,98 +1,96 @@ require_relative '../../spec_helper' -ruby_version_is "2.4" do - describe "Hash#transform_values" do - before :each do - @hash = { a: 1, b: 2, c: 3 } - end +describe "Hash#transform_values" do + before :each do + @hash = { a: 1, b: 2, c: 3 } + end - it "returns new hash" do - ret = @hash.transform_values(&:succ) - ret.should_not equal(@hash) - ret.should be_an_instance_of(Hash) - end + it "returns new hash" do + ret = @hash.transform_values(&:succ) + ret.should_not equal(@hash) + ret.should be_an_instance_of(Hash) + end - it "sets the result as transformed values with the given block" do - @hash.transform_values(&:succ).should == { a: 2, b: 3, c: 4 } - end + it "sets the result as transformed values with the given block" do + @hash.transform_values(&:succ).should == { a: 2, b: 3, c: 4 } + end - it "makes both hashes to share keys" do - key = [1, 2, 3] - new_hash = { key => 1 }.transform_values(&:succ) - new_hash[key].should == 2 - new_hash.keys[0].should equal(key) - end + it "makes both hashes to share keys" do + key = [1, 2, 3] + new_hash = { key => 1 }.transform_values(&:succ) + new_hash[key].should == 2 + new_hash.keys[0].should equal(key) + end - context "when no block is given" do - it "returns a sized Enumerator" do - enumerator = @hash.transform_values - enumerator.should be_an_instance_of(Enumerator) - enumerator.size.should == @hash.size - enumerator.each(&:succ).should == { a: 2, b: 3, c: 4 } - end + context "when no block is given" do + it "returns a sized Enumerator" do + enumerator = @hash.transform_values + enumerator.should be_an_instance_of(Enumerator) + enumerator.size.should == @hash.size + enumerator.each(&:succ).should == { a: 2, b: 3, c: 4 } end + end - it "returns a Hash instance, even on subclasses" do - klass = Class.new(Hash) - h = klass.new - h[:foo] = 42 - r = h.transform_values{|v| 2 * v} - r[:foo].should == 84 - r.class.should == Hash - end + it "returns a Hash instance, even on subclasses" do + klass = Class.new(Hash) + h = klass.new + h[:foo] = 42 + r = h.transform_values{|v| 2 * v} + r[:foo].should == 84 + r.class.should == Hash end +end - describe "Hash#transform_values!" do - before :each do - @hash = { a: 1, b: 2, c: 3 } - @initial_pairs = @hash.dup - end +describe "Hash#transform_values!" do + before :each do + @hash = { a: 1, b: 2, c: 3 } + @initial_pairs = @hash.dup + end - it "returns self" do - @hash.transform_values!(&:succ).should equal(@hash) + it "returns self" do + @hash.transform_values!(&:succ).should equal(@hash) + end + + it "updates self as transformed values with the given block" do + @hash.transform_values!(&:succ) + @hash.should == { a: 2, b: 3, c: 4 } + end + + it "partially modifies the contents if we broke from the block" do + @hash.transform_values! do |v| + break if v == 3 + 100 + v end + @hash.should == { a: 101, b: 102, c: 3} + end - it "updates self as transformed values with the given block" do - @hash.transform_values!(&:succ) + context "when no block is given" do + it "returns a sized Enumerator" do + enumerator = @hash.transform_values! + enumerator.should be_an_instance_of(Enumerator) + enumerator.size.should == @hash.size + enumerator.each(&:succ) @hash.should == { a: 2, b: 3, c: 4 } end + end - it "partially modifies the contents if we broke from the block" do - @hash.transform_values! do |v| - break if v == 3 - 100 + v - end - @hash.should == { a: 101, b: 102, c: 3} + describe "on frozen instance" do + before :each do + @hash.freeze end - context "when no block is given" do - it "returns a sized Enumerator" do - enumerator = @hash.transform_values! - enumerator.should be_an_instance_of(Enumerator) - enumerator.size.should == @hash.size - enumerator.each(&:succ) - @hash.should == { a: 2, b: 3, c: 4 } - end + it "raises a #{frozen_error_class} on an empty hash" do + ->{ {}.freeze.transform_values!(&:succ) }.should raise_error(frozen_error_class) end - describe "on frozen instance" do - before :each do - @hash.freeze - end - - it "raises a #{frozen_error_class} on an empty hash" do - ->{ {}.freeze.transform_values!(&:succ) }.should raise_error(frozen_error_class) - end - - it "keeps pairs and raises a #{frozen_error_class}" do - ->{ @hash.transform_values!(&:succ) }.should raise_error(frozen_error_class) - @hash.should == @initial_pairs - end + it "keeps pairs and raises a #{frozen_error_class}" do + ->{ @hash.transform_values!(&:succ) }.should raise_error(frozen_error_class) + @hash.should == @initial_pairs + end - context "when no block is given" do - it "does not raise an exception" do - @hash.transform_values!.should be_an_instance_of(Enumerator) - end + context "when no block is given" do + it "does not raise an exception" do + @hash.transform_values!.should be_an_instance_of(Enumerator) end end end |