Sha256: f00b873b559707d0c923714eb48f0aa2fb347a3df522e3ff0d03a49b7b17b1ff

Contents?: true

Size: 1.83 KB

Versions: 52

Compression:

Stored size: 1.83 KB

Contents

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

describe "Hash#default_proc" do
  it "returns the block passed to Hash.new" do
    h = new_hash { |i| 'Paris' }
    p = h.default_proc
    p.call(1).should == 'Paris'
  end

  it "returns nil if no block was passed to proc" do
    new_hash.default_proc.should == nil
  end
end

describe "Hash#default_proc=" do
  ruby_version_is "1.8.8" do
    it "replaces the block passed to Hash.new" do
      h = new_hash { |i| 'Paris' }
      h.default_proc = Proc.new { 'Montreal' }
      p = h.default_proc
      p.call(1).should == 'Montreal'
    end

    it "uses :to_proc on its argument" do
      h = new_hash { |i| 'Paris' }
      obj = mock('to_proc')
      obj.should_receive(:to_proc).and_return(Proc.new { 'Montreal' })
      (h.default_proc = obj).should equal(obj)
      h[:cool_city].should == 'Montreal'
    end

    it "overrides the static default" do
      h = new_hash(42)
      h.default_proc = Proc.new { 6 }
      h.default.should be_nil
      h.default_proc.call.should == 6
    end

    it "raises an error if passed stuff not convertible to procs" do
      lambda{new_hash.default_proc = nil}.should raise_error(TypeError)
      lambda{new_hash.default_proc = 42}.should raise_error(TypeError)
    end

    it "accepts a lambda with an arity of 2" do
      h = new_hash
      lambda do
        h.default_proc = lambda {|a,b| }
      end.should_not raise_error(TypeError)
    end

    ruby_version_is "1.9" do
      it "raises a TypeError if passed a lambda with an arity other than 2" do
        h = new_hash
        lambda do
          h.default_proc = lambda {|a| }
        end.should raise_error(TypeError)
        lambda do
          h.default_proc = lambda {|a,b,c| }
        end.should raise_error(TypeError)
      end
    end

  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/default_proc_spec.rb
rhodes-7.5.1 spec/framework_spec/app/spec/core/hash/default_proc_spec.rb
rhodes-7.4.1 spec/framework_spec/app/spec/core/hash/default_proc_spec.rb
rhodes-7.1.17 spec/framework_spec/app/spec/core/hash/default_proc_spec.rb
rhodes-6.2.0 spec/framework_spec/app/spec/core/hash/default_proc_spec.rb
rhodes-6.0.11 spec/framework_spec/app/spec/core/hash/default_proc_spec.rb
rhodes-5.5.18 spec/framework_spec/app/spec/core/hash/default_proc_spec.rb
rhodes-5.5.17 spec/framework_spec/app/spec/core/hash/default_proc_spec.rb
rhodes-5.5.15 spec/framework_spec/app/spec/core/hash/default_proc_spec.rb
rhodes-5.5.0.22 spec/framework_spec/app/spec/core/hash/default_proc_spec.rb
rhodes-5.5.2 spec/framework_spec/app/spec/core/hash/default_proc_spec.rb
rhodes-5.5.0.7 spec/framework_spec/app/spec/core/hash/default_proc_spec.rb
rhodes-5.5.0.3 spec/framework_spec/app/spec/core/hash/default_proc_spec.rb
rhodes-5.5.0 spec/framework_spec/app/spec/core/hash/default_proc_spec.rb
tauplatform-1.0.3 spec/framework_spec/app/spec/core/hash/default_proc_spec.rb
tauplatform-1.0.2 spec/framework_spec/app/spec/core/hash/default_proc_spec.rb
tauplatform-1.0.1 spec/framework_spec/app/spec/core/hash/default_proc_spec.rb
rhodes-3.5.1.12 spec/framework_spec/app/spec/core/hash/default_proc_spec.rb
rhodes-3.3.5 spec/framework_spec/app/spec/core/hash/default_proc_spec.rb
rhodes-3.4.2 spec/framework_spec/app/spec/core/hash/default_proc_spec.rb