Sha256: ce617c4885c483ee9d29baf2ae7df95d276a38566d7a780847fbabeb2291eb53

Contents?: true

Size: 1.24 KB

Versions: 52

Compression:

Stored size: 1.24 KB

Contents

require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../fixtures/classes', __FILE__)

describe "Hash#fetch" do
  it "returns the value for key" do
    new_hash(:a => 1, :b => -1).fetch(:b).should == -1
  end

  it "raises an IndexError if key is not found" do
    lambda { new_hash.fetch(:a)             }.should raise_error(IndexError)
    lambda { new_hash(5).fetch(:a)    }.should raise_error(IndexError)
    lambda { new_hash { 5 }.fetch(:a) }.should raise_error(IndexError)
  end

  it "returns default if key is not found when passed a default" do
    new_hash.fetch(:a, nil).should == nil
    new_hash.fetch(:a, 'not here!').should == "not here!"
    new_hash(:a => nil).fetch(:a, 'not here!').should == nil
  end

  it "returns value of block if key is not found when passed a block" do
    new_hash.fetch('a') { |k| k + '!' }.should == "a!"
  end

  it "gives precedence to the default block over the default argument when passed both" do
    new_hash.fetch(9, :foo) { |i| i * i }.should == 81
  end

  it "raises an ArgumentError when not passed one or two arguments" do
    lambda { new_hash.fetch()        }.should raise_error(ArgumentError)
    lambda { new_hash.fetch(1, 2, 3) }.should raise_error(ArgumentError)
  end
end

Version data entries

52 entries across 52 versions & 2 rubygems

Version Path
rhodes-7.6.0 spec/framework_spec/app/spec/core/hash/fetch_spec.rb
rhodes-7.5.1 spec/framework_spec/app/spec/core/hash/fetch_spec.rb
rhodes-7.4.1 spec/framework_spec/app/spec/core/hash/fetch_spec.rb
rhodes-7.1.17 spec/framework_spec/app/spec/core/hash/fetch_spec.rb
rhodes-6.2.0 spec/framework_spec/app/spec/core/hash/fetch_spec.rb
rhodes-6.0.11 spec/framework_spec/app/spec/core/hash/fetch_spec.rb
rhodes-5.5.18 spec/framework_spec/app/spec/core/hash/fetch_spec.rb
rhodes-5.5.17 spec/framework_spec/app/spec/core/hash/fetch_spec.rb
rhodes-5.5.15 spec/framework_spec/app/spec/core/hash/fetch_spec.rb
rhodes-5.5.0.22 spec/framework_spec/app/spec/core/hash/fetch_spec.rb
rhodes-5.5.2 spec/framework_spec/app/spec/core/hash/fetch_spec.rb
rhodes-5.5.0.7 spec/framework_spec/app/spec/core/hash/fetch_spec.rb
rhodes-5.5.0.3 spec/framework_spec/app/spec/core/hash/fetch_spec.rb
rhodes-5.5.0 spec/framework_spec/app/spec/core/hash/fetch_spec.rb
tauplatform-1.0.3 spec/framework_spec/app/spec/core/hash/fetch_spec.rb
tauplatform-1.0.2 spec/framework_spec/app/spec/core/hash/fetch_spec.rb
tauplatform-1.0.1 spec/framework_spec/app/spec/core/hash/fetch_spec.rb
rhodes-3.5.1.12 spec/framework_spec/app/spec/core/hash/fetch_spec.rb
rhodes-3.3.5 spec/framework_spec/app/spec/core/hash/fetch_spec.rb
rhodes-3.4.2 spec/framework_spec/app/spec/core/hash/fetch_spec.rb