1 shared :proc_call do |cmd|
2 describe "Proc##{cmd}" do
4 Proc.new { "test!" }.send(cmd).should == "test!"
5 lambda { "test!" }.send(cmd).should == "test!"
6 proc { "test!" }.send(cmd).should == "test!"
9 it "sets self's parameters to the given values" do
10 Proc.new { |a, b| a + b }.send(cmd, 1, 2).should == 3
11 Proc.new { |*args| args }.send(cmd, 1, 2, 3, 4).should == [1, 2, 3, 4]
12 Proc.new { |_, *args| args }.send(cmd, 1, 2, 3).should == [2, 3]
14 lambda { |a, b| a + b }.send(cmd, 1, 2).should == 3
15 lambda { |*args| args }.send(cmd, 1, 2, 3, 4).should == [1, 2, 3, 4]
16 lambda { |_, *args| args }.send(cmd, 1, 2, 3).should == [2, 3]
18 proc { |a, b| a + b }.send(cmd, 1, 2).should == 3
19 proc { |*args| args }.send(cmd, 1, 2, 3, 4).should == [1, 2, 3, 4]
20 proc { |_, *args| args }.send(cmd, 1, 2, 3).should == [2, 3]
23 it "sets self's single parameter to an Array of all given values" do
24 [Proc.new { |x| [x] }, lambda { |x| [x] }, proc { |x| [x] }].each do |p|
26 a.class.should == Array
30 a.class.should == Array
34 a.class.should == Array
37 a = p.send(cmd, 1, 2, 3)
38 a.class.should == Array
39 a.should == [[1, 2, 3]]
44 describe "Proc##{cmd} on a Proc created with Proc.new" do
45 it "replaces missing arguments with nil" do
46 Proc.new { |a, b| [a, b] }.send(cmd).should == [nil, nil]
47 Proc.new { |a, b| [a, b] }.send(cmd, 1).should == [1, nil]
50 it "silently ignores extra arguments" do
51 Proc.new { |a, b| a + b }.send(cmd, 1, 2, 5).should == 3
54 it "auto-explodes a single Array argument" do
55 p = Proc.new { |a, b| [a, b] }
56 p.send(cmd, 1, 2).should == [1, 2]
57 p.send(cmd, [1, 2]).should == [1, 2]
58 p.send(cmd, [1, 2, 3]).should == [1, 2]
59 p.send(cmd, [1, 2, 3], 4).should == [[1, 2, 3], 4]
63 describe "Proc##{cmd} on a Proc created with Kernel#lambda or Kernel#proc" do
64 it "raises an ArgumentError when called with too few arguments" do
65 lambda { lambda { |a, b| [a, b] }.send(cmd) }.should raise_error(ArgumentError)
66 lambda { lambda { |a, b| [a, b] }.send(cmd, 1) }.should raise_error(ArgumentError)
67 lambda { proc { |a, b| [a, b] }.send(cmd) }.should raise_error(ArgumentError)
68 lambda { proc { |a, b| [a, b] }.send(cmd, 1) }.should raise_error(ArgumentError)
71 it "raises an ArgumentError when called with too many arguments" do
72 lambda { lambda { |a, b| [a, b] }.send(cmd, 1, 2, 3) }.should raise_error(ArgumentError)
73 lambda { proc { |a, b| [a, b] }.send(cmd, 1, 2, 3) }.should raise_error(ArgumentError)
76 it "treats a single Array argument as a single argument" do
77 lambda { |a| [a] }.send(cmd, [1, 2]).should == [[1, 2]]
78 lambda { lambda { |a, b| [a, b] }.send(cmd, [1, 2]) }.should raise_error(ArgumentError)
79 proc { |a| [a] }.send(cmd, [1, 2]).should == [[1, 2]]
80 lambda { proc { |a, b| [a, b] }.send(cmd, [1, 2]) }.should raise_error(ArgumentError)