Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add specs for rb_block_call

  • Loading branch information...
commit bf1236114d2bd0534d7dd7c4bdef8a645d479be9 1 parent 0fcb78a
@dbussink dbussink authored
View
43 spec/ruby/optional/capi/ext/kernel_spec.c
@@ -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) {
@@ -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
View
1  spec/ruby/optional/capi/ext/rubyspec.h
@@ -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
View
30 spec/ruby/optional/capi/kernel_spec.rb
@@ -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)
Please sign in to comment.
Something went wrong with that request. Please try again.