Created
July 9, 2010 22:05
-
-
Save pcapriotti/470137 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
From fd64fb7a97d980432161152934035d7d9c724a07 Mon Sep 17 00:00:00 2001 | |
From: Paolo Capriotti <[email protected]> | |
Date: Sat, 10 Jul 2010 12:34:15 +0100 | |
Subject: [PATCH] Add spec for C API rb_throw. | |
--- | |
spec/ruby/optional/capi/ext/kernel_spec.c | 11 +++++++++++ | |
spec/ruby/optional/capi/ext/rubyspec.h | 1 + | |
spec/ruby/optional/capi/kernel_spec.rb | 20 ++++++++++++++++++++ | |
3 files changed, 32 insertions(+), 0 deletions(-) | |
diff --git a/spec/ruby/optional/capi/ext/kernel_spec.c b/spec/ruby/optional/capi/ext/kernel_spec.c | |
index 1cf23fd..e368a55 100644 | |
--- a/spec/ruby/optional/capi/ext/kernel_spec.c | |
+++ b/spec/ruby/optional/capi/ext/kernel_spec.c | |
@@ -56,6 +56,13 @@ VALUE kernel_spec_rb_raise(VALUE self, VALUE hash) { | |
} | |
#endif | |
+#ifdef HAVE_RB_THROW | |
+VALUE kernel_spec_rb_throw(VALUE self, VALUE result) { | |
+ rb_throw("foo", result); | |
+ return ID2SYM(rb_intern("broken")); | |
+} | |
+#endif | |
+ | |
#ifdef HAVE_RB_RESCUE | |
VALUE kernel_spec_rb_rescue(VALUE self, VALUE main_proc, VALUE arg, | |
VALUE raise_proc, VALUE arg2) { | |
@@ -138,6 +145,10 @@ void Init_kernel_spec() { | |
rb_define_method(cls, "rb_raise", kernel_spec_rb_raise, 1); | |
#endif | |
+#ifdef HAVE_RB_THROW | |
+ rb_define_method(cls, "rb_throw", kernel_spec_rb_throw, 1); | |
+#endif | |
+ | |
#ifdef HAVE_RB_RESCUE | |
rb_define_method(cls, "rb_rescue", kernel_spec_rb_rescue, 4); | |
#endif | |
diff --git a/spec/ruby/optional/capi/ext/rubyspec.h b/spec/ruby/optional/capi/ext/rubyspec.h | |
index 44e04cd..72d326e 100644 | |
--- a/spec/ruby/optional/capi/ext/rubyspec.h | |
+++ b/spec/ruby/optional/capi/ext/rubyspec.h | |
@@ -148,6 +148,7 @@ | |
#define HAVE_RB_ENSURE 1 | |
#define HAVE_RB_EVAL_STRING 1 | |
#define HAVE_RB_RAISE 1 | |
+#define HAVE_RB_THROW 1 | |
#define HAVE_RB_RESCUE 1 | |
#define HAVE_RB_RESCUE2 1 | |
#define HAVE_RB_SYS_FAIL 1 | |
diff --git a/spec/ruby/optional/capi/kernel_spec.rb b/spec/ruby/optional/capi/kernel_spec.rb | |
index 447e38d..ff8ee2f 100644 | |
--- a/spec/ruby/optional/capi/kernel_spec.rb | |
+++ b/spec/ruby/optional/capi/kernel_spec.rb | |
@@ -29,6 +29,26 @@ describe "C-API Kernel function" do | |
end | |
end | |
+ describe "rb_throw" do | |
+ it "sets the return value of the catch block to the specified value" do | |
+ res = catch :foo do | |
+ @s.rb_throw(:return_value) | |
+ end | |
+ res.should == :return_value | |
+ end | |
+ | |
+ it "terminates the function at the point it was called" do | |
+ catch(:foo) do | |
+ @s.rb_throw(nil) | |
+ fail("throw didn't transfer control") | |
+ end.should_not == :broken | |
+ end | |
+ | |
+ it "raises a NameError if there is no catch block for the symbol" do | |
+ lambda { @s.rb_throw(nil) }.should raise_error(NameError) | |
+ end | |
+ end | |
+ | |
describe "rb_warn" do | |
before :each do | |
@stderr, $stderr = $stderr, IOStub.new | |
-- | |
1.7.0.4 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment