diff options
Diffstat (limited to 'spec/ruby/core/hash/key_spec.rb')
| -rw-r--r-- | spec/ruby/core/hash/key_spec.rb | 32 |
1 files changed, 26 insertions, 6 deletions
diff --git a/spec/ruby/core/hash/key_spec.rb b/spec/ruby/core/hash/key_spec.rb index dc78174641..c2d7049aed 100644 --- a/spec/ruby/core/hash/key_spec.rb +++ b/spec/ruby/core/hash/key_spec.rb @@ -1,12 +1,32 @@ -require File.expand_path('../../../spec_helper', __FILE__) -require File.expand_path('../fixtures/classes', __FILE__) -require File.expand_path('../shared/key', __FILE__) -require File.expand_path('../shared/index', __FILE__) +require_relative '../../spec_helper' describe "Hash#key?" do - it_behaves_like(:hash_key_p, :key?) + it "is an alias of Hash#include?" do + Hash.instance_method(:key?).should == Hash.instance_method(:include?) + end end describe "Hash#key" do - it_behaves_like(:hash_index, :key) + it "returns the corresponding key for value" do + { 2 => 'a', 1 => 'b' }.key('b').should == 1 + end + + it "returns nil if the value is not found" do + { a: -1, b: 3.14, c: 2.718 }.key(1).should == nil + end + + it "doesn't return default value if the value is not found" do + Hash.new(5).key(5).should == nil + end + + it "compares values using ==" do + { 1 => 0 }.key(0.0).should == 1 + { 1 => 0.0 }.key(0).should == 1 + + needle = mock('needle') + inhash = mock('inhash') + inhash.should_receive(:==).with(needle).and_return(true) + + { 1 => inhash }.key(needle).should == 1 + end end |
