summaryrefslogtreecommitdiff
path: root/spec/ruby/core/hash
diff options
context:
space:
mode:
Diffstat (limited to 'spec/ruby/core/hash')
-rw-r--r--spec/ruby/core/hash/compare_by_identity_spec.rb2
-rw-r--r--spec/ruby/core/hash/dig_spec.rb106
-rw-r--r--spec/ruby/core/hash/fetch_values_spec.rb38
-rw-r--r--spec/ruby/core/hash/fixtures/classes.rb7
-rw-r--r--spec/ruby/core/hash/gt_spec.rb60
-rw-r--r--spec/ruby/core/hash/gte_spec.rb60
-rw-r--r--spec/ruby/core/hash/initialize_spec.rb23
-rw-r--r--spec/ruby/core/hash/lt_spec.rb60
-rw-r--r--spec/ruby/core/hash/lte_spec.rb60
-rw-r--r--spec/ruby/core/hash/shared/to_s.rb21
-rw-r--r--spec/ruby/core/hash/to_proc_spec.rb118
11 files changed, 278 insertions, 277 deletions
diff --git a/spec/ruby/core/hash/compare_by_identity_spec.rb b/spec/ruby/core/hash/compare_by_identity_spec.rb
index cf15649739..e463c311be 100644
--- a/spec/ruby/core/hash/compare_by_identity_spec.rb
+++ b/spec/ruby/core/hash/compare_by_identity_spec.rb
@@ -108,7 +108,7 @@ describe "Hash#compare_by_identity" do
@idh.keys.first.should equal foo
end
- ruby_bug "#12855", "2.2.0"..."2.4.1" do
+ ruby_bug "#12855", ""..."2.4.1" do
it "gives different identity for string literals" do
@idh['foo'] = 1
@idh['foo'] = 2
diff --git a/spec/ruby/core/hash/dig_spec.rb b/spec/ruby/core/hash/dig_spec.rb
index 91639f4213..dcba049ac4 100644
--- a/spec/ruby/core/hash/dig_spec.rb
+++ b/spec/ruby/core/hash/dig_spec.rb
@@ -1,68 +1,66 @@
require_relative '../../spec_helper'
-ruby_version_is '2.3' do
- describe "Hash#dig" do
+describe "Hash#dig" do
- it "returns #[] with one arg" do
- h = { 0 => false, a: 1 }
- h.dig(:a).should == 1
- h.dig(0).should be_false
- h.dig(1).should be_nil
- end
+ it "returns #[] with one arg" do
+ h = { 0 => false, a: 1 }
+ h.dig(:a).should == 1
+ h.dig(0).should be_false
+ h.dig(1).should be_nil
+ end
- it "returns the nested value specified by the sequence of keys" do
- h = { foo: { bar: { baz: 1 } } }
- h.dig(:foo, :bar, :baz).should == 1
- h.dig(:foo, :bar, :nope).should be_nil
- h.dig(:foo, :baz).should be_nil
- h.dig(:bar, :baz, :foo).should be_nil
- end
+ it "returns the nested value specified by the sequence of keys" do
+ h = { foo: { bar: { baz: 1 } } }
+ h.dig(:foo, :bar, :baz).should == 1
+ h.dig(:foo, :bar, :nope).should be_nil
+ h.dig(:foo, :baz).should be_nil
+ h.dig(:bar, :baz, :foo).should be_nil
+ end
- it "returns the nested value specified if the sequence includes an index" do
- h = { foo: [1, 2, 3] }
- h.dig(:foo, 2).should == 3
- end
+ it "returns the nested value specified if the sequence includes an index" do
+ h = { foo: [1, 2, 3] }
+ h.dig(:foo, 2).should == 3
+ end
- it "returns nil if any intermediate step is nil" do
- h = { foo: { bar: { baz: 1 } } }
- h.dig(:foo, :zot, :xyz).should == nil
- end
+ it "returns nil if any intermediate step is nil" do
+ h = { foo: { bar: { baz: 1 } } }
+ h.dig(:foo, :zot, :xyz).should == nil
+ end
- it "raises an ArgumentError if no arguments provided" do
- lambda { { the: 'borg' }.dig() }.should raise_error(ArgumentError)
- end
+ it "raises an ArgumentError if no arguments provided" do
+ lambda { { the: 'borg' }.dig() }.should raise_error(ArgumentError)
+ end
- it "handles type-mixed deep digging" do
- h = {}
- h[:foo] = [ { bar: [ 1 ] }, [ obj = Object.new, 'str' ] ]
- def obj.dig(*args); [ 42 ] end
+ it "handles type-mixed deep digging" do
+ h = {}
+ h[:foo] = [ { bar: [ 1 ] }, [ obj = Object.new, 'str' ] ]
+ def obj.dig(*args); [ 42 ] end
- h.dig(:foo, 0, :bar).should == [ 1 ]
- h.dig(:foo, 0, :bar, 0).should == 1
- h.dig(:foo, 1, 1).should == 'str'
- # MRI does not recurse values returned from `obj.dig`
- h.dig(:foo, 1, 0, 0).should == [ 42 ]
- h.dig(:foo, 1, 0, 0, 10).should == [ 42 ]
- end
+ h.dig(:foo, 0, :bar).should == [ 1 ]
+ h.dig(:foo, 0, :bar, 0).should == 1
+ h.dig(:foo, 1, 1).should == 'str'
+ # MRI does not recurse values returned from `obj.dig`
+ h.dig(:foo, 1, 0, 0).should == [ 42 ]
+ h.dig(:foo, 1, 0, 0, 10).should == [ 42 ]
+ end
- it "raises TypeError if an intermediate element does not respond to #dig" do
- h = {}
- h[:foo] = [ { bar: [ 1 ] }, [ nil, 'str' ] ]
- lambda { h.dig(:foo, 0, :bar, 0, 0) }.should raise_error(TypeError)
- lambda { h.dig(:foo, 1, 1, 0) }.should raise_error(TypeError)
- end
+ it "raises TypeError if an intermediate element does not respond to #dig" do
+ h = {}
+ h[:foo] = [ { bar: [ 1 ] }, [ nil, 'str' ] ]
+ lambda { h.dig(:foo, 0, :bar, 0, 0) }.should raise_error(TypeError)
+ lambda { h.dig(:foo, 1, 1, 0) }.should raise_error(TypeError)
+ end
- it "calls #dig on the result of #[] with the remaining arguments" do
- h = { foo: { bar: { baz: 42 } } }
- h[:foo].should_receive(:dig).with(:bar, :baz).and_return(42)
- h.dig(:foo, :bar, :baz).should == 42
- end
+ it "calls #dig on the result of #[] with the remaining arguments" do
+ h = { foo: { bar: { baz: 42 } } }
+ h[:foo].should_receive(:dig).with(:bar, :baz).and_return(42)
+ h.dig(:foo, :bar, :baz).should == 42
+ end
- it "respects Hash's default" do
- default = {bar: 42}
- h = Hash.new(default)
- h.dig(:foo).should equal default
- h.dig(:foo, :bar).should == 42
- end
+ it "respects Hash's default" do
+ default = {bar: 42}
+ h = Hash.new(default)
+ h.dig(:foo).should equal default
+ h.dig(:foo, :bar).should == 42
end
end
diff --git a/spec/ruby/core/hash/fetch_values_spec.rb b/spec/ruby/core/hash/fetch_values_spec.rb
index 1b093f4aa9..a4af153bf7 100644
--- a/spec/ruby/core/hash/fetch_values_spec.rb
+++ b/spec/ruby/core/hash/fetch_values_spec.rb
@@ -2,32 +2,30 @@ require_relative '../../spec_helper'
require_relative 'fixtures/classes'
require_relative '../../shared/hash/key_error'
-ruby_version_is "2.3" do
- describe "Hash#fetch_values" do
- before :each do
- @hash = { a: 1, b: 2, c: 3 }
- end
+describe "Hash#fetch_values" do
+ before :each do
+ @hash = { a: 1, b: 2, c: 3 }
+ end
- describe "with matched keys" do
- it "returns the values for keys" do
- @hash.fetch_values(:a).should == [1]
- @hash.fetch_values(:a, :c).should == [1, 3]
- end
+ describe "with matched keys" do
+ it "returns the values for keys" do
+ @hash.fetch_values(:a).should == [1]
+ @hash.fetch_values(:a, :c).should == [1, 3]
end
+ end
- describe "with unmatched keys" do
- it_behaves_like :key_error, ->(obj, key) { obj.fetch_values(key) }, Hash.new(a: 5)
+ describe "with unmatched keys" do
+ 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"]
- @hash.fetch_values(:a, :z) { |key| "`#{key}' is not found" }.should == [1, "`z' is not found"]
- end
+ it "returns the default value from block" do
+ @hash.fetch_values(:z) { |key| "`#{key}' is not found" }.should == ["`z' is not found"]
+ @hash.fetch_values(:a, :z) { |key| "`#{key}' is not found" }.should == [1, "`z' is not found"]
end
+ end
- describe "without keys" do
- it "returns an empty Array" do
- @hash.fetch_values.should == []
- end
+ describe "without keys" do
+ it "returns an empty Array" do
+ @hash.fetch_values.should == []
end
end
end
diff --git a/spec/ruby/core/hash/fixtures/classes.rb b/spec/ruby/core/hash/fixtures/classes.rb
index 3d3df576cf..ae907aaff6 100644
--- a/spec/ruby/core/hash/fixtures/classes.rb
+++ b/spec/ruby/core/hash/fixtures/classes.rb
@@ -17,6 +17,13 @@ module HashSpecs
end
end
+ class SubHashSettingInInitialize < Hash
+ def initialize(*args, &block)
+ self[:foo] = :bar
+ super(*args, &block)
+ end
+ end
+
class DefaultHash < Hash
def default(key)
100
diff --git a/spec/ruby/core/hash/gt_spec.rb b/spec/ruby/core/hash/gt_spec.rb
index 58878e94fc..cd541d4d83 100644
--- a/spec/ruby/core/hash/gt_spec.rb
+++ b/spec/ruby/core/hash/gt_spec.rb
@@ -2,43 +2,41 @@ require_relative '../../spec_helper'
require_relative 'shared/comparison'
require_relative 'shared/greater_than'
-ruby_version_is "2.3" do
- describe "Hash#>" do
- it_behaves_like :hash_comparison, :>
- it_behaves_like :hash_greater_than, :>
+describe "Hash#>" do
+ it_behaves_like :hash_comparison, :>
+ it_behaves_like :hash_greater_than, :>
- it "returns false if both hashes are identical" do
- h = { a: 1, b: 2 }
- (h > h).should be_false
- end
+ it "returns false if both hashes are identical" do
+ h = { a: 1, b: 2 }
+ (h > h).should be_false
end
+end
- describe "Hash#>" do
- before :each do
- @hash = {a:1, b:2}
- @bigger = {a:1, b:2, c:3}
- @unrelated = {c:3, d:4}
- @similar = {a:2, b:3}
- end
+describe "Hash#>" do
+ before :each do
+ @hash = {a:1, b:2}
+ @bigger = {a:1, b:2, c:3}
+ @unrelated = {c:3, d:4}
+ @similar = {a:2, b:3}
+ end
- it "returns false when receiver size is smaller than argument" do
- (@hash > @bigger).should == false
- (@unrelated > @bigger).should == false
- end
+ it "returns false when receiver size is smaller than argument" do
+ (@hash > @bigger).should == false
+ (@unrelated > @bigger).should == false
+ end
- it "returns false when receiver size is the same as argument" do
- (@hash > @hash).should == false
- (@hash > @unrelated).should == false
- (@unrelated > @hash).should == false
- end
+ it "returns false when receiver size is the same as argument" do
+ (@hash > @hash).should == false
+ (@hash > @unrelated).should == false
+ (@unrelated > @hash).should == false
+ end
- it "returns true when argument is a subset of receiver" do
- (@bigger > @hash).should == true
- end
+ it "returns true when argument is a subset of receiver" do
+ (@bigger > @hash).should == true
+ end
- it "returns false when keys match but values don't" do
- (@hash > @similar).should == false
- (@similar > @hash).should == false
- end
+ it "returns false when keys match but values don't" do
+ (@hash > @similar).should == false
+ (@similar > @hash).should == false
end
end
diff --git a/spec/ruby/core/hash/gte_spec.rb b/spec/ruby/core/hash/gte_spec.rb
index bbb86edc7d..99b89e7217 100644
--- a/spec/ruby/core/hash/gte_spec.rb
+++ b/spec/ruby/core/hash/gte_spec.rb
@@ -2,43 +2,41 @@ require_relative '../../spec_helper'
require_relative 'shared/comparison'
require_relative 'shared/greater_than'
-ruby_version_is "2.3" do
- describe "Hash#>=" do
- it_behaves_like :hash_comparison, :>=
- it_behaves_like :hash_greater_than, :>=
+describe "Hash#>=" do
+ it_behaves_like :hash_comparison, :>=
+ it_behaves_like :hash_greater_than, :>=
- it "returns true if both hashes are identical" do
- h = { a: 1, b: 2 }
- (h >= h).should be_true
- end
+ it "returns true if both hashes are identical" do
+ h = { a: 1, b: 2 }
+ (h >= h).should be_true
end
+end
- describe "Hash#>=" do
- before :each do
- @hash = {a:1, b:2}
- @bigger = {a:1, b:2, c:3}
- @unrelated = {c:3, d:4}
- @similar = {a:2, b:3}
- end
+describe "Hash#>=" do
+ before :each do
+ @hash = {a:1, b:2}
+ @bigger = {a:1, b:2, c:3}
+ @unrelated = {c:3, d:4}
+ @similar = {a:2, b:3}
+ end
- it "returns false when receiver size is smaller than argument" do
- (@hash >= @bigger).should == false
- (@unrelated >= @bigger).should == false
- end
+ it "returns false when receiver size is smaller than argument" do
+ (@hash >= @bigger).should == false
+ (@unrelated >= @bigger).should == false
+ end
- it "returns false when argument is not a subset or not equals to receiver" do
- (@hash >= @unrelated).should == false
- (@unrelated >= @hash).should == false
- end
+ it "returns false when argument is not a subset or not equals to receiver" do
+ (@hash >= @unrelated).should == false
+ (@unrelated >= @hash).should == false
+ end
- it "returns true when argument is a subset of receiver or equals to receiver" do
- (@bigger >= @hash).should == true
- (@hash >= @hash).should == true
- end
+ it "returns true when argument is a subset of receiver or equals to receiver" do
+ (@bigger >= @hash).should == true
+ (@hash >= @hash).should == true
+ end
- it "returns false when keys match but values don't" do
- (@hash >= @similar).should == false
- (@similar >= @hash).should == false
- end
+ it "returns false when keys match but values don't" do
+ (@hash >= @similar).should == false
+ (@similar >= @hash).should == false
end
end
diff --git a/spec/ruby/core/hash/initialize_spec.rb b/spec/ruby/core/hash/initialize_spec.rb
index ed7163db48..344571631a 100644
--- a/spec/ruby/core/hash/initialize_spec.rb
+++ b/spec/ruby/core/hash/initialize_spec.rb
@@ -9,16 +9,37 @@ describe "Hash#initialize" do
it "can be used to reset default_proc" do
h = { "foo" => 1, "bar" => 2 }
h.default_proc.should == nil
- h.instance_eval { initialize { |_, k| k * 2 } }
+ h.send(:initialize) { |_, k| k * 2 }
h.default_proc.should_not == nil
h["a"].should == "aa"
end
+ it "can be used to reset the default value" do
+ h = {}
+ h.default = 42
+ h.default.should == 42
+ h.send(:initialize, 1)
+ h.default.should == 1
+ h.send(:initialize)
+ h.default.should == nil
+ end
+
it "receives the arguments passed to Hash#new" do
HashSpecs::NewHash.new(:one, :two)[0].should == :one
HashSpecs::NewHash.new(:one, :two)[1].should == :two
end
+ it "does not change the storage, only the default value or proc" do
+ h = HashSpecs::SubHashSettingInInitialize.new
+ h.to_a.should == [[:foo, :bar]]
+
+ h = HashSpecs::SubHashSettingInInitialize.new(:default)
+ h.to_a.should == [[:foo, :bar]]
+
+ h = HashSpecs::SubHashSettingInInitialize.new { :default_block }
+ h.to_a.should == [[:foo, :bar]]
+ end
+
it "returns self" do
h = Hash.new
h.send(:initialize).should equal(h)
diff --git a/spec/ruby/core/hash/lt_spec.rb b/spec/ruby/core/hash/lt_spec.rb
index 88f1120e86..2219615880 100644
--- a/spec/ruby/core/hash/lt_spec.rb
+++ b/spec/ruby/core/hash/lt_spec.rb
@@ -2,43 +2,41 @@ require_relative '../../spec_helper'
require_relative 'shared/comparison'
require_relative 'shared/less_than'
-ruby_version_is "2.3" do
- describe "Hash#<" do
- it_behaves_like :hash_comparison, :<
- it_behaves_like :hash_less_than, :<
+describe "Hash#<" do
+ it_behaves_like :hash_comparison, :<
+ it_behaves_like :hash_less_than, :<
- it "returns false if both hashes are identical" do
- h = { a: 1, b: 2 }
- (h < h).should be_false
- end
+ it "returns false if both hashes are identical" do
+ h = { a: 1, b: 2 }
+ (h < h).should be_false
end
+end
- describe "Hash#<" do
- before :each do
- @hash = {a:1, b:2}
- @bigger = {a:1, b:2, c:3}
- @unrelated = {c:3, d:4}
- @similar = {a:2, b:3}
- end
+describe "Hash#<" do
+ before :each do
+ @hash = {a:1, b:2}
+ @bigger = {a:1, b:2, c:3}
+ @unrelated = {c:3, d:4}
+ @similar = {a:2, b:3}
+ end
- it "returns false when receiver size is larger than argument" do
- (@bigger < @hash).should == false
- (@bigger < @unrelated).should == false
- end
+ it "returns false when receiver size is larger than argument" do
+ (@bigger < @hash).should == false
+ (@bigger < @unrelated).should == false
+ end
- it "returns false when receiver size is the same as argument" do
- (@hash < @hash).should == false
- (@hash < @unrelated).should == false
- (@unrelated < @hash).should == false
- end
+ it "returns false when receiver size is the same as argument" do
+ (@hash < @hash).should == false
+ (@hash < @unrelated).should == false
+ (@unrelated < @hash).should == false
+ end
- it "returns true when receiver is a subset of argument" do
- (@hash < @bigger).should == true
- end
+ it "returns true when receiver is a subset of argument" do
+ (@hash < @bigger).should == true
+ end
- it "returns false when keys match but values don't" do
- (@hash < @similar).should == false
- (@similar < @hash).should == false
- end
+ it "returns false when keys match but values don't" do
+ (@hash < @similar).should == false
+ (@similar < @hash).should == false
end
end
diff --git a/spec/ruby/core/hash/lte_spec.rb b/spec/ruby/core/hash/lte_spec.rb
index 1463c263d0..a166e5bca4 100644
--- a/spec/ruby/core/hash/lte_spec.rb
+++ b/spec/ruby/core/hash/lte_spec.rb
@@ -2,43 +2,41 @@ require_relative '../../spec_helper'
require_relative 'shared/comparison'
require_relative 'shared/less_than'
-ruby_version_is "2.3" do
- describe "Hash#<=" do
- it_behaves_like :hash_comparison, :<=
- it_behaves_like :hash_less_than, :<=
+describe "Hash#<=" do
+ it_behaves_like :hash_comparison, :<=
+ it_behaves_like :hash_less_than, :<=
- it "returns true if both hashes are identical" do
- h = { a: 1, b: 2 }
- (h <= h).should be_true
- end
+ it "returns true if both hashes are identical" do
+ h = { a: 1, b: 2 }
+ (h <= h).should be_true
end
+end
- describe "Hash#<=" do
- before :each do
- @hash = {a:1, b:2}
- @bigger = {a:1, b:2, c:3}
- @unrelated = {c:3, d:4}
- @similar = {a:2, b:3}
- end
+describe "Hash#<=" do
+ before :each do
+ @hash = {a:1, b:2}
+ @bigger = {a:1, b:2, c:3}
+ @unrelated = {c:3, d:4}
+ @similar = {a:2, b:3}
+ end
- it "returns false when receiver size is larger than argument" do
- (@bigger <= @hash).should == false
- (@bigger <= @unrelated).should == false
- end
+ it "returns false when receiver size is larger than argument" do
+ (@bigger <= @hash).should == false
+ (@bigger <= @unrelated).should == false
+ end
- it "returns false when receiver size is the same as argument" do
- (@hash <= @unrelated).should == false
- (@unrelated <= @hash).should == false
- end
+ it "returns false when receiver size is the same as argument" do
+ (@hash <= @unrelated).should == false
+ (@unrelated <= @hash).should == false
+ end
- it "returns true when receiver is a subset of argument or equals to argument" do
- (@hash <= @bigger).should == true
- (@hash <= @hash).should == true
- end
+ it "returns true when receiver is a subset of argument or equals to argument" do
+ (@hash <= @bigger).should == true
+ (@hash <= @hash).should == true
+ end
- it "returns false when keys match but values don't" do
- (@hash <= @similar).should == false
- (@similar <= @hash).should == false
- end
+ it "returns false when keys match but values don't" do
+ (@hash <= @similar).should == false
+ (@similar <= @hash).should == false
end
end
diff --git a/spec/ruby/core/hash/shared/to_s.rb b/spec/ruby/core/hash/shared/to_s.rb
index 32c86f9d33..88333e0f42 100644
--- a/spec/ruby/core/hash/shared/to_s.rb
+++ b/spec/ruby/core/hash/shared/to_s.rb
@@ -87,23 +87,10 @@ describe :hash_to_s, shared: true do
{ nil => nil }.untrust.send(@method).untrusted?.should be_true
end
- ruby_version_is ''...'2.3' do
- it "raises if inspected result is not default external encoding" do
- utf_16be = mock("utf_16be")
- utf_16be.should_receive(:inspect).and_return(%<"utf_16be \u3042">.encode!(Encoding::UTF_16BE))
-
- lambda {
- {a: utf_16be}.send(@method)
- }.should raise_error(Encoding::CompatibilityError)
- end
- end
+ it "does not raise if inspected result is not default external encoding" do
+ utf_16be = mock("utf_16be")
+ utf_16be.should_receive(:inspect).and_return(%<"utf_16be \u3042">.encode!(Encoding::UTF_16BE))
- ruby_version_is '2.3' do
- it "does not raise if inspected result is not default external encoding" do
- utf_16be = mock("utf_16be")
- utf_16be.should_receive(:inspect).and_return(%<"utf_16be \u3042">.encode!(Encoding::UTF_16BE))
-
- {a: utf_16be}.send(@method).should == '{:a=>"utf_16be \u3042"}'
- end
+ {a: utf_16be}.send(@method).should == '{:a=>"utf_16be \u3042"}'
end
end
diff --git a/spec/ruby/core/hash/to_proc_spec.rb b/spec/ruby/core/hash/to_proc_spec.rb
index 3dc6a91459..ca55604043 100644
--- a/spec/ruby/core/hash/to_proc_spec.rb
+++ b/spec/ruby/core/hash/to_proc_spec.rb
@@ -1,89 +1,87 @@
require_relative '../../spec_helper'
require_relative 'fixtures/classes'
-ruby_version_is "2.3" do
- describe "Hash#to_proc" do
+describe "Hash#to_proc" do
+ before :each do
+ @key = Object.new
+ @value = Object.new
+ @hash = { @key => @value }
+ @default = Object.new
+ @unstored = Object.new
+ end
+
+ it "returns an instance of Proc" do
+ @hash.to_proc.should be_an_instance_of Proc
+ end
+
+ describe "the returned proc" do
before :each do
- @key = Object.new
- @value = Object.new
- @hash = { @key => @value }
- @default = Object.new
- @unstored = Object.new
+ @proc = @hash.to_proc
end
- it "returns an instance of Proc" do
- @hash.to_proc.should be_an_instance_of Proc
+ it "is not a lambda" do
+ @proc.lambda?.should == false
end
- describe "the returned proc" do
- before :each do
- @proc = @hash.to_proc
- end
-
- it "is not a lambda" do
- @proc.lambda?.should == false
- end
+ it "raises ArgumentError if not passed exactly one argument" do
+ lambda {
+ @proc.call
+ }.should raise_error(ArgumentError)
- it "raises ArgumentError if not passed exactly one argument" do
- lambda {
- @proc.call
- }.should raise_error(ArgumentError)
+ lambda {
+ @proc.call 1, 2
+ }.should raise_error(ArgumentError)
+ end
- lambda {
- @proc.call 1, 2
- }.should raise_error(ArgumentError)
+ context "with a stored key" do
+ it "returns the paired value" do
+ @proc.call(@key).should equal(@value)
end
+ end
- context "with a stored key" do
- it "returns the paired value" do
- @proc.call(@key).should equal(@value)
- end
+ context "passed as a block" do
+ it "retrieves the hash's values" do
+ [@key].map(&@proc)[0].should equal(@value)
end
- context "passed as a block" do
- it "retrieves the hash's values" do
- [@key].map(&@proc)[0].should equal(@value)
- end
-
- context "to instance_exec" do
- it "always retrieves the original hash's values" do
- hash = {foo: 1, bar: 2}
- proc = hash.to_proc
+ context "to instance_exec" do
+ it "always retrieves the original hash's values" do
+ hash = {foo: 1, bar: 2}
+ proc = hash.to_proc
- hash.instance_exec(:foo, &proc).should == 1
+ hash.instance_exec(:foo, &proc).should == 1
- hash2 = {quux: 1}
- hash2.instance_exec(:foo, &proc).should == 1
- end
+ hash2 = {quux: 1}
+ hash2.instance_exec(:foo, &proc).should == 1
end
end
+ end
- context "with no stored key" do
- it "returns nil" do
- @proc.call(@unstored).should be_nil
- end
-
- context "when the hash has a default value" do
- before :each do
- @hash.default = @default
- end
+ context "with no stored key" do
+ it "returns nil" do
+ @proc.call(@unstored).should be_nil
+ end
- it "returns the default value" do
- @proc.call(@unstored).should equal(@default)
- end
+ context "when the hash has a default value" do
+ before :each do
+ @hash.default = @default
end
- context "when the hash has a default proc" do
- it "returns an evaluated value from the default proc" do
- @hash.default_proc = -> hash, called_with { [hash.keys, called_with] }
- @proc.call(@unstored).should == [[@key], @unstored]
- end
+ it "returns the default value" do
+ @proc.call(@unstored).should equal(@default)
end
end
- it "raises an ArgumentError when calling #call on the Proc with no arguments" do
- lambda { @hash.to_proc.call }.should raise_error(ArgumentError)
+ context "when the hash has a default proc" do
+ it "returns an evaluated value from the default proc" do
+ @hash.default_proc = -> hash, called_with { [hash.keys, called_with] }
+ @proc.call(@unstored).should == [[@key], @unstored]
+ end
end
end
+
+ it "raises an ArgumentError when calling #call on the Proc with no arguments" do
+ lambda { @hash.to_proc.call }.should raise_error(ArgumentError)
+ end
end
end