Skip to content

Instantly share code, notes, and snippets.

@jakedouglas
Created July 14, 2010 17:55
Show Gist options
  • Save jakedouglas/475761 to your computer and use it in GitHub Desktop.
Save jakedouglas/475761 to your computer and use it in GitHub Desktop.
diff --git a/spec/ruby/optional/capi/ext/kernel_spec.c b/spec/ruby/optional/capi/ext/kernel_spec.c
index 667a1e8..e98af04 100644
--- a/spec/ruby/optional/capi/ext/kernel_spec.c
+++ b/spec/ruby/optional/capi/ext/kernel_spec.c
@@ -64,6 +64,21 @@ VALUE kernel_spec_rb_throw(VALUE self, VALUE result) {
#endif
#ifdef HAVE_RB_RESCUE
+VALUE kernel_spec_call_proc_with_raised_exc(VALUE arg_array, VALUE raised_exc) {
+ VALUE argv[2];
+ int argc;
+
+ VALUE arg = rb_ary_pop(arg_array);
+ VALUE proc = rb_ary_pop(arg_array);
+
+ argv[0] = arg;
+ argv[1] = raised_exc;
+
+ argc = 2;
+
+ return rb_funcall2(proc, rb_intern("call"), argc, argv);
+}
+
VALUE kernel_spec_rb_rescue(VALUE self, VALUE main_proc, VALUE arg,
VALUE raise_proc, VALUE arg2) {
VALUE main_array = rb_ary_new();
@@ -75,7 +90,7 @@ VALUE kernel_spec_rb_rescue(VALUE self, VALUE main_proc, VALUE arg,
rb_ary_push(raise_array, arg2);
return rb_rescue(kernel_spec_call_proc, main_array,
- kernel_spec_call_proc, raise_array);
+ kernel_spec_call_proc_with_raised_exc, raise_array);
}
#endif
diff --git a/spec/ruby/optional/capi/kernel_spec.rb b/spec/ruby/optional/capi/kernel_spec.rb
index 5d34cc4..58a115e 100644
--- a/spec/ruby/optional/capi/kernel_spec.rb
+++ b/spec/ruby/optional/capi/kernel_spec.rb
@@ -123,22 +123,40 @@ describe "C-API Kernel function" do
describe "rb_rescue" do
before :each do
@proc = lambda { |x| x }
+ @raise_proc_returns_sentinel = lambda {|arg| :raise_proc_executed }
+ @raise_proc_returns_arg = lambda {|arg| arg }
@arg_error_proc = lambda { raise ArgumentError, '' }
@std_error_proc = lambda { raise StandardError, '' }
@exc_error_proc = lambda { raise Exception, '' }
end
it "executes passed function" do
- @s.rb_rescue(@proc, :no_exc, @proc, :exc).should == :no_exc
+ @s.rb_rescue(@proc, :no_exc, @raise_proc_returns_arg, :exc).should == :no_exc
end
- it "executes passed 'raise function' if a StardardError exception is raised" do
- @s.rb_rescue(@arg_error_proc, nil, @proc, :exc).should == :exc
- @s.rb_rescue(@std_error_proc, nil, @proc, :exc).should == :exc
+ it "executes passed 'raise function' if a StandardError exception is raised" do
+ @s.rb_rescue(@arg_error_proc, nil, @raise_proc_returns_sentinel, :exc).should == :raise_proc_executed
+ @s.rb_rescue(@std_error_proc, nil, @raise_proc_returns_sentinel, :exc).should == :raise_proc_executed
end
- it "raises an exception if passed function raises an exception other than StardardError" do
- lambda { @s.rb_rescue(@exc_error_proc, nil, @proc, nil) }.should raise_error(Exception)
+ it "passes the user supplied argument to the 'raise function' if a StandardError exception is raised" do
+ arg1, _ = @s.rb_rescue(@arg_error_proc, nil, @raise_proc_returns_arg, :exc1)
+ arg1.should == :exc1
+
+ arg2, _ = @s.rb_rescue(@std_error_proc, nil, @raise_proc_returns_arg, :exc2)
+ arg2.should == :exc2
+ end
+
+ it "passes the raised exception to the 'raise function' if a StandardError exception is raised" do
+ _, exc1 = @s.rb_rescue(@arg_error_proc, nil, @raise_proc_returns_arg, :exc)
+ exc1.class.should == ArgumentError
+
+ _, exc2 = @s.rb_rescue(@std_error_proc, nil, @raise_proc_returns_arg, :exc)
+ exc2.class.should == StandardError
+ end
+
+ it "raises an exception if passed function raises an exception other than StandardError" do
+ lambda { @s.rb_rescue(@exc_error_proc, nil, @raise_proc_returns_arg, nil) }.should raise_error(Exception)
end
it "raises an exception if any exception is raised inside 'raise function'" do
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment