diff options
Diffstat (limited to 'spec/ruby/optional/capi/proc_spec.rb')
-rw-r--r-- | spec/ruby/optional/capi/proc_spec.rb | 81 |
1 files changed, 53 insertions, 28 deletions
diff --git a/spec/ruby/optional/capi/proc_spec.rb b/spec/ruby/optional/capi/proc_spec.rb index dab143fbe7..8b94432f3e 100644 --- a/spec/ruby/optional/capi/proc_spec.rb +++ b/spec/ruby/optional/capi/proc_spec.rb @@ -82,6 +82,59 @@ describe "C-API Proc function" do end end + describe "rb_proc_call_kw" do + it "passes keyword arguments to the proc" do + prc = proc { |*args, **kw| [args, kw] } + + @p.rb_proc_call_kw(prc, [{}]).should == [[], {}] + @p.rb_proc_call_kw(prc, [{a: 1}]).should == [[], {a: 1}] + @p.rb_proc_call_kw(prc, [{b: 2}, {a: 1}]).should == [[{b: 2}], {a: 1}] + @p.rb_proc_call_kw(prc, [{b: 2}, {}]).should == [[{b: 2}], {}] + end + + it "raises TypeError if the last argument is not a Hash" do + -> { + @p.rb_proc_call_kw(proc {}, [42]) + }.should raise_error(TypeError, 'no implicit conversion of Integer into Hash') + end + end + + describe "rb_proc_call_with_block" do + it "calls the Proc and passes arguments and a block" do + prc = Proc.new { |a, b, &block| block.call(a * b) } + @p.rb_proc_call_with_block(prc, [6, 7], proc { |n| n * 2 }).should == 6 * 7 * 2 + end + + it "calls the Proc and passes arguments when a block is nil" do + prc = Proc.new { |a, b| a * b } + @p.rb_proc_call_with_block(prc, [6, 7], nil).should == 6 * 7 + end + end + + describe "rb_proc_call_with_block_kw" do + it "passes keyword arguments and a block to the proc" do + prc = proc { |*args, **kw, &block| [args, kw, block.call(42)] } + block = proc { |n| n } + + @p.rb_proc_call_with_block_kw(prc, [{}], block).should == [[], {}, 42] + @p.rb_proc_call_with_block_kw(prc, [{a: 1}], block).should == [[], {a: 1}, 42] + @p.rb_proc_call_with_block_kw(prc, [{b: 2}, {a: 1}], block).should == [[{b: 2}], {a: 1}, 42] + @p.rb_proc_call_with_block_kw(prc, [{b: 2}, {}], block).should == [[{b: 2}], {}, 42] + end + + it "raises TypeError if the last argument is not a Hash" do + -> { + @p.rb_proc_call_with_block_kw(proc {}, [42], proc {}) + }.should raise_error(TypeError, 'no implicit conversion of Integer into Hash') + end + + it "passes keyword arguments to the proc when a block is nil" do + prc = proc { |*args, **kw| [args, kw] } + + @p.rb_proc_call_with_block_kw(prc, [{}], nil).should == [[], {}] + end + end + describe "rb_obj_is_proc" do it "returns true for Proc" do prc = Proc.new {|a,b| a * b } @@ -115,20 +168,6 @@ describe "C-API when calling Proc.new from a C function" do # For example: C -> Ruby <- C -> Ruby means a C function called into Ruby # code which returned to C, then C called into Ruby code again. - ruby_version_is ""..."2.7" do - # Ruby -> C -> rb_funcall(Proc.new) - it "returns the Proc passed by the Ruby code calling the C function" do - prc = @p.rb_Proc_new(0) { :called } - prc.call.should == :called - end - - # Ruby -> C -> Ruby <- C -> rb_funcall(Proc.new) - it "returns the Proc passed to the Ruby method when the C function calls other Ruby methods before calling Proc.new" do - prc = @p.rb_Proc_new(1) { :called } - prc.call.should == :called - end - end - # Ruby -> C -> Ruby -> Proc.new it "raises an ArgumentError when the C function calls a Ruby method that calls Proc.new" do -> { @@ -142,20 +181,6 @@ describe "C-API when calling Proc.new from a C function" do -> { @p.rb_Proc_new(3) { :called } }.should raise_error(ArgumentError) end - ruby_version_is ""..."2.7" do - # Ruby -> C -> Ruby -> C (with new block) -> rb_funcall(Proc.new) - it "returns the most recent Proc passed when the Ruby method called the C function" do - prc = @p.rb_Proc_new(4) { :called } - prc.call.should == :calling_with_block - end - - # Ruby -> C -> Ruby -> C (with new block) <- Ruby <- C -> # rb_funcall(Proc.new) - it "returns the Proc passed from the original Ruby call to the C function" do - prc = @p.rb_Proc_new(5) { :called } - prc.call.should == :called - end - end - # Ruby -> C -> Ruby -> block_given? it "returns false from block_given? in a Ruby method called by the C function" do @p.rb_Proc_new(6).should be_false |