Skip to content

Commit

Permalink
Add specs for rb_block_call
Browse files Browse the repository at this point in the history
  • Loading branch information
dbussink committed Sep 27, 2012
1 parent 0fcb78a commit bf12361
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 0 deletions.
43 changes: 43 additions & 0 deletions spec/ruby/optional/capi/ext/kernel_spec.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,41 @@ VALUE kernel_spec_rb_block_proc(VALUE self) {
}
#endif

#ifdef HAVE_RB_BLOCK_CALL

VALUE block_call_inject(VALUE yield_value, VALUE data2) {
// yield_value yields the first block argument
VALUE elem = yield_value;
VALUE elem_incr = INT2FIX(FIX2INT(elem) + 1);
return elem_incr;
}

VALUE kernel_spec_rb_block_call(VALUE self, VALUE ary) {
return rb_block_call(ary, rb_intern("map"), 0, NULL, block_call_inject, Qnil);
}

#ifdef RUBY_VERSION_IS_1_9
VALUE block_call_inject_multi_arg(VALUE yield_value, VALUE data2, int argc, VALUE argv[]) {
// yield_value yields the first block argument
VALUE sum = yield_value;
VALUE elem = argv[1];

return INT2FIX(FIX2INT(sum) + FIX2INT(elem));
}

VALUE kernel_spec_rb_block_call_multi_arg(VALUE self, VALUE ary) {
VALUE method_args[1];
method_args[0] = INT2FIX(0);
return rb_block_call(ary, rb_intern("inject"), 1, method_args, block_call_inject_multi_arg, Qnil);
}

VALUE kernel_spec_rb_block_call_no_func(VALUE self, VALUE ary) {
return rb_block_call(ary, rb_intern("map"), 0, NULL, NULL, Qnil);
}

#endif
#endif

#ifdef HAVE_RB_ENSURE
VALUE kernel_spec_rb_ensure(VALUE self, VALUE main_proc, VALUE arg,
VALUE ensure_proc, VALUE arg2) {
Expand Down Expand Up @@ -231,6 +266,14 @@ void Init_kernel_spec() {
rb_define_method(cls, "rb_need_block", kernel_spec_rb_need_block, 0);
#endif

#ifdef HAVE_RB_BLOCK_CALL
rb_define_method(cls, "rb_block_call", kernel_spec_rb_block_call, 1);
#ifdef RUBY_VERSION_IS_1_9
rb_define_method(cls, "rb_block_call_multi_arg", kernel_spec_rb_block_call_multi_arg, 1);
rb_define_method(cls, "rb_block_call_no_func", kernel_spec_rb_block_call_no_func, 1);
#endif
#endif

#ifdef HAVE_RB_BLOCK_PROC
rb_define_method(cls, "rb_block_proc", kernel_spec_rb_block_proc, 0);
#endif
Expand Down
1 change: 1 addition & 0 deletions spec/ruby/optional/capi/ext/rubyspec.h
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@
/* Kernel */
#define HAVE_RB_BLOCK_GIVEN_P 1
#define HAVE_RB_BLOCK_PROC 1
#define HAVE_RB_BLOCK_CALL 1
#define HAVE_RB_ENSURE 1
#define HAVE_RB_EVAL_STRING 1
#define HAVE_RB_EXEC_RECURSIVE 1
Expand Down
30 changes: 30 additions & 0 deletions spec/ruby/optional/capi/kernel_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,36 @@
end
end

describe "rb_block_call" do
before :each do
ScratchPad.record []
end

it "calls the block with a single argument" do
ary = [1, 3, 5]
@s.rb_block_call(ary).should == [2, 4, 6]
end

ruby_version_is "1.9" do
it "calls the block with multiple arguments in argc / argv" do
ary = [1, 3, 5]
@s.rb_block_call_multi_arg(ary).should == 9
end

it "calls the method with no function callback and no block" do
ary = [1, 3, 5]
@s.rb_block_call_no_func(ary).should be_kind_of(Enumerator)
end

it "calls the method with no function callback and a block" do
ary = [1, 3, 5]
@s.rb_block_call_no_func(ary) do |i|
i + 1
end.should == [2, 4, 6]
end
end
end

describe "rb_raise" do
it "raises an exception" do
lambda { @s.rb_raise({}) }.should raise_error(TypeError)
Expand Down

0 comments on commit bf12361

Please sign in to comment.