Sha256: d3632bae3ca82246b17b9d713ba89fe142805e643744803810c2f15e85e404e1

Contents?: true

Size: 1.29 KB

Versions: 83

Compression:

Stored size: 1.29 KB

Contents

require File.dirname(File.join(__rhoGetCurrentDir(), __FILE__)) + '/../../spec_helper'
require File.dirname(File.join(__rhoGetCurrentDir(), __FILE__)) + '/fixtures/classes'

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

83 entries across 83 versions & 1 rubygems

Version Path
rhodes-3.1.1 spec/framework_spec/app/spec/core/hash/fetch_spec.rb
rhodes-3.1.1.beta spec/framework_spec/app/spec/core/hash/fetch_spec.rb
rhodes-3.1.0 spec/framework_spec/app/spec/core/hash/fetch_spec.rb
rhodes-3.1.0.beta.5 spec/framework_spec/app/spec/core/hash/fetch_spec.rb
rhodes-3.1.0.beta.4 spec/framework_spec/app/spec/core/hash/fetch_spec.rb
rhodes-3.1.0.beta.3 spec/framework_spec/app/spec/core/hash/fetch_spec.rb
rhodes-3.1.0.beta.2 spec/framework_spec/app/spec/core/hash/fetch_spec.rb
rhodes-3.1.0.beta.1 spec/framework_spec/app/spec/core/hash/fetch_spec.rb
rhodes-3.0.2 spec/framework_spec/app/spec/core/hash/fetch_spec.rb
rhodes-3.0.2.beta.1 spec/framework_spec/app/spec/core/hash/fetch_spec.rb
rhodes-3.0.1 spec/framework_spec/app/spec/core/hash/fetch_spec.rb
rhodes-3.0.1.beta.8 spec/framework_spec/app/spec/core/hash/fetch_spec.rb
rhodes-3.0.1.beta.7 spec/framework_spec/app/spec/core/hash/fetch_spec.rb
rhodes-3.0.1.beta.6 spec/framework_spec/app/spec/core/hash/fetch_spec.rb
rhodes-3.0.1.beta.5 spec/framework_spec/app/spec/core/hash/fetch_spec.rb
rhodes-3.0.1.beta.4 spec/framework_spec/app/spec/core/hash/fetch_spec.rb
rhodes-3.0.1.beta.3 spec/framework_spec/app/spec/core/hash/fetch_spec.rb
rhodes-3.0.1.beta.2 spec/framework_spec/app/spec/core/hash/fetch_spec.rb
rhodes-3.0.0 spec/framework_spec/app/spec/core/hash/fetch_spec.rb
rhodes-3.0.0.beta.7 spec/framework_spec/app/spec/core/hash/fetch_spec.rb