diff options
author | Benoit Daloze <[email protected]> | 2020-11-27 14:55:31 +0100 |
---|---|---|
committer | Benoit Daloze <[email protected]> | 2020-11-27 14:55:31 +0100 |
commit | f02d2f82bf10351f480ea312f40cb840e2437f93 (patch) | |
tree | 0b53098bffe6dacadb09880183ebcaea06e95153 /spec/ruby/optional/capi | |
parent | f0bfa266d70651dc295a63b026938b246693499b (diff) |
Update to ruby/spec@ac878ad
Diffstat (limited to 'spec/ruby/optional/capi')
-rw-r--r-- | spec/ruby/optional/capi/exception_spec.rb | 45 | ||||
-rw-r--r-- | spec/ruby/optional/capi/ext/exception_spec.c | 8 |
2 files changed, 53 insertions, 0 deletions
diff --git a/spec/ruby/optional/capi/exception_spec.rb b/spec/ruby/optional/capi/exception_spec.rb index 62d7d3706e..9e82e5c7dd 100644 --- a/spec/ruby/optional/capi/exception_spec.rb +++ b/spec/ruby/optional/capi/exception_spec.rb @@ -55,4 +55,49 @@ describe "C-API Exception function" do -> { @s.rb_set_errinfo("error") }.should raise_error(TypeError) end end + + describe "rb_make_exception" do + it "returns a RuntimeError when given a String argument" do + e = @s.rb_make_exception(["Message"]) + e.class.should == RuntimeError + e.message.should == "Message" + end + + it "returns the exception when given an Exception argument" do + exc = Exception.new + e = @s.rb_make_exception([exc]) + e.should == exc + end + + it "returns the exception with the given class and message" do + e = @s.rb_make_exception([Exception, "Message"]) + e.class.should == Exception + e.message.should == "Message" + end + + it "returns the exception with the given class, message, and backtrace" do + e = @s.rb_make_exception([Exception, "Message", ["backtrace 1"]]) + e.class.should == Exception + e.message.should == "Message" + e.backtrace.should == ["backtrace 1"] + end + + it "raises a TypeError for incorrect types" do + -> { @s.rb_make_exception([nil]) }.should raise_error(TypeError) + -> { @s.rb_make_exception([Object.new]) }.should raise_error(TypeError) + obj = Object.new + def obj.exception + "not exception type" + end + -> { @s.rb_make_exception([obj]) }.should raise_error(TypeError) + end + + it "raises an ArgumentError for too many arguments" do + -> { @s.rb_make_exception([Exception, "Message", ["backtrace 1"], "extra"]) }.should raise_error(ArgumentError) + end + + it "returns nil for empty arguments" do + @s.rb_make_exception([]).should == nil + end + end end diff --git a/spec/ruby/optional/capi/ext/exception_spec.c b/spec/ruby/optional/capi/ext/exception_spec.c index cdd86b183e..7250792b70 100644 --- a/spec/ruby/optional/capi/ext/exception_spec.c +++ b/spec/ruby/optional/capi/ext/exception_spec.c @@ -32,6 +32,13 @@ VALUE exception_spec_rb_set_errinfo(VALUE self, VALUE exc) { return Qnil; } + +VALUE exception_spec_rb_make_exception(VALUE self, VALUE ary) { + int argc = RARRAY_LENINT(ary); + VALUE *argv = RARRAY_PTR(ary); + return rb_make_exception(argc, argv); +} + void Init_exception_spec(void) { VALUE cls = rb_define_class("CApiExceptionSpecs", rb_cObject); rb_define_method(cls, "rb_exc_new", exception_spec_rb_exc_new, 1); @@ -39,6 +46,7 @@ void Init_exception_spec(void) { rb_define_method(cls, "rb_exc_new3", exception_spec_rb_exc_new3, 1); rb_define_method(cls, "rb_exc_raise", exception_spec_rb_exc_raise, 1); rb_define_method(cls, "rb_set_errinfo", exception_spec_rb_set_errinfo, 1); + rb_define_method(cls, "rb_make_exception", exception_spec_rb_make_exception, 1); } #ifdef __cplusplus |