diff options
author | Benoit Daloze <eregontp@gmail.com> | 2021-01-28 17:08:57 +0100 |
---|---|---|
committer | Benoit Daloze <eregontp@gmail.com> | 2021-01-28 17:08:57 +0100 |
commit | 2e32b919b4f2f5b7f2e1509d6fa985526ef1f61c (patch) | |
tree | aedadac3c99ca0097c2bbeaa95830332d6fb9971 /spec/ruby/optional | |
parent | 1b377b32c8616f85c0a97e68758c5c2db83f2169 (diff) | |
download | ruby-2e32b919b4f2f5b7f2e1509d6fa985526ef1f61c.tar.gz |
Update to ruby/spec@8cafaa5
Diffstat (limited to 'spec/ruby/optional')
-rw-r--r-- | spec/ruby/optional/capi/debug_spec.rb | 68 | ||||
-rw-r--r-- | spec/ruby/optional/capi/ext/debug_spec.c | 93 | ||||
-rw-r--r-- | spec/ruby/optional/capi/ext/object_spec.c | 1 | ||||
-rw-r--r-- | spec/ruby/optional/capi/fixtures/module.rb | 4 | ||||
-rw-r--r-- | spec/ruby/optional/capi/module_spec.rb | 4 | ||||
-rw-r--r-- | spec/ruby/optional/capi/object_spec.rb | 5 |
6 files changed, 175 insertions, 0 deletions
diff --git a/spec/ruby/optional/capi/debug_spec.rb b/spec/ruby/optional/capi/debug_spec.rb new file mode 100644 index 0000000000..89e72f0049 --- /dev/null +++ b/spec/ruby/optional/capi/debug_spec.rb @@ -0,0 +1,68 @@ +require_relative 'spec_helper' + +load_extension('debug') + +describe "C-API Debug function" do + before :each do + @o = CApiDebugSpecs.new + end + + describe "rb_debug_inspector_open" do + it "creates a debug context and calls the given callback" do + @o.rb_debug_inspector_open(42).should be_kind_of(Array) + @o.debug_spec_callback_data.should == 42 + end + end + + describe "rb_debug_inspector_frame_self_get" do + it "returns self" do + @o.rb_debug_inspector_frame_self_get(0).should == @o + end + end + + describe "rb_debug_inspector_frame_class_get" do + it "returns the frame class" do + @o.rb_debug_inspector_frame_class_get(0).should == CApiDebugSpecs + end + end + + describe "rb_debug_inspector_frame_binding_get" do + it "returns the current binding" do + a = "test" + b = @o.rb_debug_inspector_frame_binding_get(1) + b.should be_an_instance_of(Binding) + b.local_variable_get(:a).should == "test" + end + + ruby_version_is "2.6" do + it "matches the locations in rb_debug_inspector_backtrace_locations" do + frames = @o.rb_debug_inspector_open(42); + frames.each do |_s, _klass, binding, _iseq, backtrace_location| + if binding + "#{backtrace_location.path}:#{backtrace_location.lineno}".should == "#{binding.source_location[0]}:#{binding.source_location[1]}" + end + end + end + end + end + + describe "rb_debug_inspector_frame_iseq_get" do + it "returns an InstructionSequence" do + if defined?(RubyVM::InstructionSequence) + @o.rb_debug_inspector_frame_iseq_get(1).should be_an_instance_of(RubyVM::InstructionSequence) + else + @o.rb_debug_inspector_frame_iseq_get(1).should == nil + end + end + end + + describe "rb_debug_inspector_backtrace_locations" do + it "returns an array of Thread::Backtrace::Location" do + bts = @o.rb_debug_inspector_backtrace_locations + bts.should_not.empty? + bts.each { |bt| bt.should be_kind_of(Thread::Backtrace::Location) } + location = "#{__FILE__}:#{__LINE__ - 3}" + bts[1].to_s.should include(location) + end + end +end diff --git a/spec/ruby/optional/capi/ext/debug_spec.c b/spec/ruby/optional/capi/ext/debug_spec.c new file mode 100644 index 0000000000..344dfc33fa --- /dev/null +++ b/spec/ruby/optional/capi/ext/debug_spec.c @@ -0,0 +1,93 @@ +#include "ruby.h" +#include "rubyspec.h" +#include "ruby/debug.h" + +#ifdef __cplusplus +extern "C" { +#endif + +static VALUE callback_data = Qfalse; + +static VALUE rb_debug_inspector_open_callback(const rb_debug_inspector_t *dc, void *ptr) { + if (!dc) { + rb_raise(rb_eRuntimeError, "rb_debug_inspector_t should not be NULL"); + } + + VALUE locations = rb_debug_inspector_backtrace_locations(dc); + int len = RARRAY_LENINT(locations); + VALUE results = rb_ary_new2(len); + for (int i = 0; i < len; i++) { + VALUE ary = rb_ary_new2(5); // [self, klass, binding, iseq, backtrace_location] + rb_ary_store(ary, 0, rb_debug_inspector_frame_self_get(dc, i)); + rb_ary_store(ary, 1, rb_debug_inspector_frame_class_get(dc, i)); + rb_ary_store(ary, 2, rb_debug_inspector_frame_binding_get(dc, i)); + rb_ary_store(ary, 3, rb_debug_inspector_frame_iseq_get(dc, i)); + rb_ary_store(ary, 4, rb_ary_entry(locations, i)); + rb_ary_push(results, ary); + } + callback_data = (VALUE)ptr; + return results; +} + +static VALUE rb_debug_inspector_frame_self_get_callback(const rb_debug_inspector_t *dc, void *ptr) { + return rb_debug_inspector_frame_self_get(dc, NUM2LONG((VALUE) ptr)); +} + +static VALUE rb_debug_inspector_frame_class_get_callback(const rb_debug_inspector_t *dc, void *ptr) { + return rb_debug_inspector_frame_class_get(dc, NUM2LONG((VALUE) ptr)); +} + +static VALUE rb_debug_inspector_frame_binding_get_callback(const rb_debug_inspector_t *dc, void *ptr) { + return rb_debug_inspector_frame_binding_get(dc, NUM2LONG((VALUE) ptr)); +} + +static VALUE rb_debug_inspector_frame_iseq_get_callback(const rb_debug_inspector_t *dc, void *ptr) { + return rb_debug_inspector_frame_iseq_get(dc, NUM2LONG((VALUE) ptr)); +} + +static VALUE debug_spec_callback_data(VALUE self){ + return callback_data; +} + +VALUE debug_spec_rb_debug_inspector_open(VALUE self, VALUE index) { + return rb_debug_inspector_open(rb_debug_inspector_open_callback, (void *)index); +} + +VALUE debug_spec_rb_debug_inspector_frame_self_get(VALUE self, VALUE index) { + return rb_debug_inspector_open(rb_debug_inspector_frame_self_get_callback, (void *)index); +} + +VALUE debug_spec_rb_debug_inspector_frame_class_get(VALUE self, VALUE index) { + return rb_debug_inspector_open(rb_debug_inspector_frame_class_get_callback, (void *)index); +} + +VALUE debug_spec_rb_debug_inspector_frame_binding_get(VALUE self, VALUE index) { + return rb_debug_inspector_open(rb_debug_inspector_frame_binding_get_callback, (void *)index); +} + +VALUE debug_spec_rb_debug_inspector_frame_iseq_get(VALUE self, VALUE index) { + return rb_debug_inspector_open(rb_debug_inspector_frame_iseq_get_callback, (void *)index); +} + +static VALUE rb_debug_inspector_backtrace_locations_func(const rb_debug_inspector_t *dc, void *ptr) { + return rb_debug_inspector_backtrace_locations(dc); +} + +VALUE debug_spec_rb_debug_inspector_backtrace_locations(VALUE self) { + return rb_debug_inspector_open(rb_debug_inspector_backtrace_locations_func, (void *)self); +} + +void Init_debug_spec(void) { + VALUE cls = rb_define_class("CApiDebugSpecs", rb_cObject); + rb_define_method(cls, "rb_debug_inspector_open", debug_spec_rb_debug_inspector_open, 1); + rb_define_method(cls, "rb_debug_inspector_frame_self_get", debug_spec_rb_debug_inspector_frame_self_get, 1); + rb_define_method(cls, "rb_debug_inspector_frame_class_get", debug_spec_rb_debug_inspector_frame_class_get, 1); + rb_define_method(cls, "rb_debug_inspector_frame_binding_get", debug_spec_rb_debug_inspector_frame_binding_get, 1); + rb_define_method(cls, "rb_debug_inspector_frame_iseq_get", debug_spec_rb_debug_inspector_frame_iseq_get, 1); + rb_define_method(cls, "rb_debug_inspector_backtrace_locations", debug_spec_rb_debug_inspector_backtrace_locations, 0); + rb_define_method(cls, "debug_spec_callback_data", debug_spec_callback_data, 0); +} + +#ifdef __cplusplus +} +#endif diff --git a/spec/ruby/optional/capi/ext/object_spec.c b/spec/ruby/optional/capi/ext/object_spec.c index 59237e8549..a229301f40 100644 --- a/spec/ruby/optional/capi/ext/object_spec.c +++ b/spec/ruby/optional/capi/ext/object_spec.c @@ -462,6 +462,7 @@ void Init_object_spec(void) { rb_define_method(cls, "rb_undef_alloc_func", undef_alloc_func, 1); rb_define_method(cls, "speced_allocator?", speced_allocator_p, 1); rb_define_method(cls, "custom_alloc_func?", custom_alloc_func_p, 1); + rb_define_method(cls, "not_implemented_method", rb_f_notimplement, 1); } #ifdef __cplusplus diff --git a/spec/ruby/optional/capi/fixtures/module.rb b/spec/ruby/optional/capi/fixtures/module.rb index ba90eb7181..aac8bfbfb3 100644 --- a/spec/ruby/optional/capi/fixtures/module.rb +++ b/spec/ruby/optional/capi/fixtures/module.rb @@ -13,6 +13,10 @@ class CApiModuleSpecs autoload :D, File.expand_path('../const_get.rb', __FILE__) X = 1 + Q = 1 + R = 2 + S = 3 + T = 5 end class B < A diff --git a/spec/ruby/optional/capi/module_spec.rb b/spec/ruby/optional/capi/module_spec.rb index fde86d2223..9a0c707263 100644 --- a/spec/ruby/optional/capi/module_spec.rb +++ b/spec/ruby/optional/capi/module_spec.rb @@ -134,6 +134,10 @@ describe "CApiModule" do @m.rb_const_get(CApiModuleSpecs::A, :X).should == 1 end + it "returns a constant defined in the module for multiple constants" do + [:Q, :R, :S, :T].each { |x| @m.rb_const_get(CApiModuleSpecs::A, x).should == CApiModuleSpecs::A.const_get(x) } + end + it "returns a constant defined at toplevel" do @m.rb_const_get(CApiModuleSpecs::A, :Integer).should == Integer end diff --git a/spec/ruby/optional/capi/object_spec.rb b/spec/ruby/optional/capi/object_spec.rb index be149898bd..83d8f023b3 100644 --- a/spec/ruby/optional/capi/object_spec.rb +++ b/spec/ruby/optional/capi/object_spec.rb @@ -115,6 +115,11 @@ describe "CApiObject" do @o.rb_respond_to(true, :object_id).should == true @o.rb_respond_to(14, :succ).should == true end + + it "returns 0 if the method has been defined as rb_f_notimplement" do + @o.respond_to?(:not_implemented_method).should == false + @o.rb_respond_to(@o, :not_implemented_method).should == false + end end describe "rb_obj_respond_to" do |