File: to_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 (98 lines) | stat: -rw-r--r-- 2,929 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../fixtures/classes', __FILE__)

describe "Method#to_proc" do
  before(:each) do
    ScratchPad.record []

    @m = MethodSpecs::Methods.new
    @meth = @m.method(:foo)
  end

  it "returns a Proc object corresponding to the method" do
    @meth.to_proc.kind_of?(Proc).should == true
  end

  it "returns a Proc object with the correct arity" do
    # This may seem redundant but this bug has cropped up in jruby, mri and yarv.
    # http://jira.codehaus.org/browse/JRUBY-124
    [ :zero, :one_req, :two_req,
      :zero_with_block, :one_req_with_block, :two_req_with_block,
      :one_opt, :one_req_one_opt, :one_req_two_opt, :two_req_one_opt,
      :one_opt_with_block, :one_req_one_opt_with_block, :one_req_two_opt_with_block, :two_req_one_opt_with_block,
      :zero_with_splat, :one_req_with_splat, :two_req_with_splat,
      :one_req_one_opt_with_splat, :one_req_two_opt_with_splat, :two_req_one_opt_with_splat,
      :zero_with_splat_and_block, :one_req_with_splat_and_block, :two_req_with_splat_and_block,
      :one_req_one_opt_with_splat_and_block, :one_req_two_opt_with_splat_and_block, :two_req_one_opt_with_splat_and_block
    ].each do |m|
      @m.method(m).to_proc.arity.should == @m.method(m).arity
    end
  end

  it "returns a proc that can be used by define_method" do
    x = 'test'
    to_s = class << x
      define_method :foo, method(:to_s).to_proc
      to_s
    end

    x.foo.should == to_s
  end

  it "returns a proc that can be yielded to" do
    x = Object.new
    def x.foo(*a); a; end
    def x.bar; yield; end
    def x.baz(*a); yield(*a); end

    m = x.method :foo
    x.bar(&m).should == []
    x.baz(1,2,3,&m).should == [1,2,3]
  end
  
  ruby_bug "#5926", "1.9.2" do
    it "returns a proc that can receive a block" do
      x = Object.new
      def x.foo; yield 'bar'; end
      
      m = x.method :foo
      result = nil
      m.to_proc.call {|val| result = val}
      result.should == 'bar'
    end
  end
  
  ruby_version_is ""..."1.9" do
    it "returns a proc that accepts passed arguments like a block would" do
      obj = MethodSpecs::ToProc.new

      array = [["text", :comment], ["space", :chunk]]
      array.each(&obj)

      ScratchPad.recorded.should == array = [["text", :comment], ["space", :chunk]]
    end
  end

  it "can be called directly and not unwrap arguments like a block" do
    obj = MethodSpecs::ToProcBeta.new
    obj.to_proc.call([1]).should == [1]
  end

  it "should correct handle arguments (unwrap)" do
    obj = MethodSpecs::ToProcBeta.new

    array = [[1]]
    array.each(&obj)
    ScratchPad.recorded.should == [[1]]
  end

  ruby_version_is "1.9" do
    it "executes method with whole array (one argument)" do
      obj = MethodSpecs::ToProcBeta.new

      array = [[1, 2]]
      array.each(&obj)
      ScratchPad.recorded.should == [[1, 2]]
    end
  end
end