File: default_proc_spec.rb

package info (click to toggle)
jruby 1.7.26-1%2Bdeb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 84,572 kB
  • sloc: ruby: 669,910; java: 253,056; xml: 35,152; ansic: 9,187; yacc: 7,267; cpp: 5,244; sh: 1,036; makefile: 345; jsp: 48; tcl: 40
file content (76 lines) | stat: -rw-r--r-- 2,153 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
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.9" 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 = 42}.should raise_error(TypeError)
    end

    ruby_version_is "1.9"..."2.0" do
      it "raises an error if passed nil" do
        lambda{new_hash.default_proc = nil}.should raise_error(TypeError)
      end
    end

    ruby_version_is "2.0" do
      it "clears the default proc if passed nil" do
        h = new_hash { |i| 'Paris' }
        h.default_proc = nil
        h.default_proc.should == nil
        h[:city].should == nil
      end
    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

    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