From 8c5b60eb22d6d661e87992a65d54e3a5bc0aeed4 Mon Sep 17 00:00:00 2001 From: eregon Date: Sat, 28 Oct 2017 15:15:48 +0000 Subject: Update to ruby/spec@a6b8805 git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@60525 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- spec/ruby/optional/capi/encoding_spec.rb | 2 +- spec/ruby/optional/capi/ext/kernel_spec.c | 14 +++++++++++++ spec/ruby/optional/capi/ext/rubyspec.h | 1 + spec/ruby/optional/capi/ext/string_spec.c | 16 ++++++++++++++ spec/ruby/optional/capi/kernel_spec.rb | 10 +++++++++ spec/ruby/optional/capi/object_spec.rb | 7 +++++++ spec/ruby/optional/capi/proc_spec.rb | 2 +- spec/ruby/optional/capi/spec_helper.rb | 7 +++++++ spec/ruby/optional/capi/string_spec.rb | 35 ++++++++++++++++++++++++++----- spec/ruby/optional/capi/thread_spec.rb | 9 +++++--- 10 files changed, 93 insertions(+), 10 deletions(-) (limited to 'spec/ruby/optional/capi') diff --git a/spec/ruby/optional/capi/encoding_spec.rb b/spec/ruby/optional/capi/encoding_spec.rb index 9408c0c6c5..d6b0bacd70 100644 --- a/spec/ruby/optional/capi/encoding_spec.rb +++ b/spec/ruby/optional/capi/encoding_spec.rb @@ -88,7 +88,7 @@ describe "C-API Encoding function" do end it "returns -1 for an non existing encoding" do - @s.rb_enc_find_index("non-existant-encoding").should == -1 + @s.rb_enc_find_index("non-existent-encoding").should == -1 end end diff --git a/spec/ruby/optional/capi/ext/kernel_spec.c b/spec/ruby/optional/capi/ext/kernel_spec.c index f126bd0a4a..226354e18a 100644 --- a/spec/ruby/optional/capi/ext/kernel_spec.c +++ b/spec/ruby/optional/capi/ext/kernel_spec.c @@ -230,6 +230,19 @@ static VALUE kernel_spec_rb_yield(VALUE self, VALUE obj) { return rb_yield(obj); } +static VALUE kernel_spec_rb_yield_each(int argc, VALUE *args, VALUE self) { + int i; + for(i = 0; i < 4; i++) { + rb_yield(INT2FIX(i)); + } + return INT2FIX(4); +} + +static VALUE kernel_spec_rb_yield_define_each(VALUE self, VALUE cls) { + rb_define_method(cls, "each", kernel_spec_rb_yield_each, -1); + return Qnil; +} + static int kernel_cb(const void *a, const void *b) { rb_yield(Qtrue); return 0; @@ -392,6 +405,7 @@ void Init_kernel_spec(void) { #ifdef HAVE_RB_YIELD rb_define_method(cls, "rb_yield", kernel_spec_rb_yield, 1); rb_define_method(cls, "rb_yield_indirected", kernel_spec_rb_yield_indirected, 1); + rb_define_method(cls, "rb_yield_define_each", kernel_spec_rb_yield_define_each, 1); #endif #ifdef HAVE_RB_YIELD_VALUES diff --git a/spec/ruby/optional/capi/ext/rubyspec.h b/spec/ruby/optional/capi/ext/rubyspec.h index 12c71d98b0..aab74b88ae 100644 --- a/spec/ruby/optional/capi/ext/rubyspec.h +++ b/spec/ruby/optional/capi/ext/rubyspec.h @@ -557,6 +557,7 @@ #define HAVE_RB_STR_SUBSEQ 1 #define HAVE_RB_VSPRINTF 1 #define HAVE_RB_STRING 1 +#define HAVE_SAFE_STRING_VALUE 1 /* Struct */ #define HAVE_RB_STRUCT_AREF 1 diff --git a/spec/ruby/optional/capi/ext/string_spec.c b/spec/ruby/optional/capi/ext/string_spec.c index 0568b42212..929d69f9e5 100644 --- a/spec/ruby/optional/capi/ext/string_spec.c +++ b/spec/ruby/optional/capi/ext/string_spec.c @@ -181,6 +181,10 @@ VALUE string_spec_rb_str_length(VALUE self, VALUE str) { VALUE string_spec_rb_str_new(VALUE self, VALUE str, VALUE len) { return rb_str_new(RSTRING_PTR(str), FIX2INT(len)); } + +VALUE string_spec_rb_str_new_offset(VALUE self, VALUE str, VALUE offset, VALUE len) { + return rb_str_new(RSTRING_PTR(str) + FIX2INT(offset), FIX2INT(len)); +} #endif #ifdef HAVE_RB_STR_NEW2 @@ -370,6 +374,13 @@ VALUE string_spec_StringValue(VALUE self, VALUE str) { } #endif +#ifdef HAVE_SAFE_STRING_VALUE +static VALUE string_spec_SafeStringValue(VALUE self, VALUE str) { + SafeStringValue(str); + return str; +} +#endif + #ifdef HAVE_RB_STR_HASH static VALUE string_spec_rb_str_hash(VALUE self, VALUE str) { st_index_t val = rb_str_hash(str); @@ -529,6 +540,7 @@ void Init_string_spec(void) { #ifdef HAVE_RB_STR_NEW rb_define_method(cls, "rb_str_new", string_spec_rb_str_new, 2); + rb_define_method(cls, "rb_str_new_offset", string_spec_rb_str_new_offset, 3); #endif #ifdef HAVE_RB_STR_NEW2 @@ -639,6 +651,10 @@ void Init_string_spec(void) { rb_define_method(cls, "StringValue", string_spec_StringValue, 1); #endif +#ifdef HAVE_SAFE_STRING_VALUE + rb_define_method(cls, "SafeStringValue", string_spec_SafeStringValue, 1); +#endif + #ifdef HAVE_RB_STR_HASH rb_define_method(cls, "rb_str_hash", string_spec_rb_str_hash, 1); #endif diff --git a/spec/ruby/optional/capi/kernel_spec.rb b/spec/ruby/optional/capi/kernel_spec.rb index 47f368b517..73928b3154 100644 --- a/spec/ruby/optional/capi/kernel_spec.rb +++ b/spec/ruby/optional/capi/kernel_spec.rb @@ -198,6 +198,16 @@ describe "C-API Kernel function" do @s.rb_yield_indirected(1) { break 73 }.should == 73 end end + + it "rb_yield to block passed to enumerator" do + enum_class = Class.new do + include Enumerable + end + @s.rb_yield_define_each(enum_class) + res = enum_class.new.collect { |i| i * 2} + res.should == [0, 2, 4, 6] + end + end describe "rb_yield_values" do diff --git a/spec/ruby/optional/capi/object_spec.rb b/spec/ruby/optional/capi/object_spec.rb index b16d26ce47..cf7ea2e8cf 100644 --- a/spec/ruby/optional/capi/object_spec.rb +++ b/spec/ruby/optional/capi/object_spec.rb @@ -420,6 +420,13 @@ describe "CApiObject" do @o.rb_class_of(0.1).should == Float @o.rb_class_of(ObjectTest.new).should == ObjectTest end + + it "returns the singleton class if it exists" do + o = ObjectTest.new + @o.rb_class_of(o).should equal ObjectTest + s = o.singleton_class + @o.rb_class_of(o).should equal s + end end describe "rb_obj_classname" do diff --git a/spec/ruby/optional/capi/proc_spec.rb b/spec/ruby/optional/capi/proc_spec.rb index 1cd8de9a86..ca6163b574 100644 --- a/spec/ruby/optional/capi/proc_spec.rb +++ b/spec/ruby/optional/capi/proc_spec.rb @@ -64,7 +64,7 @@ describe "C-API when calling Proc.new from a C function" do # In the scenarios below: X -> Y means execution context X called to Y. # For example: Ruby -> C means a Ruby code called a C function. # - # X -> Y <- X -> Z means exection context X called Y which returned to X, + # X -> Y <- X -> Z means execution context X called Y which returned to X, # then X called Z. # For example: C -> Ruby <- C -> Ruby means a C function called into Ruby # code which returned to C, then C called into Ruby code again. diff --git a/spec/ruby/optional/capi/spec_helper.rb b/spec/ruby/optional/capi/spec_helper.rb index 21d2cd04c5..a5f64b51fb 100644 --- a/spec/ruby/optional/capi/spec_helper.rb +++ b/spec/ruby/optional/capi/spec_helper.rb @@ -23,11 +23,18 @@ def compile_extension(name) ext = "#{name}_spec" lib = "#{object_path}/#{ext}.#{RbConfig::CONFIG['DLEXT']}" ruby_header = "#{RbConfig::CONFIG['rubyhdrdir']}/ruby.h" + libruby_so = RbConfig::CONFIG['LIBRUBY_SO'] + ruby_library = "#{RbConfig::CONFIG['libdir']}/#{libruby_so}" + unless libruby_so and File.exist?(ruby_library) + # Statically-compiled lib in the binary + ruby_library = RbConfig.ruby + end return lib if File.exist?(lib) and File.mtime(lib) > File.mtime("#{extension_path}/rubyspec.h") and File.mtime(lib) > File.mtime("#{extension_path}/#{ext}.c") and File.mtime(lib) > File.mtime(ruby_header) and + File.mtime(lib) > File.mtime(ruby_library) and true # sentinel # Copy needed source files to tmpdir diff --git a/spec/ruby/optional/capi/string_spec.rb b/spec/ruby/optional/capi/string_spec.rb index 25ef10b6f6..73e6deb498 100644 --- a/spec/ruby/optional/capi/string_spec.rb +++ b/spec/ruby/optional/capi/string_spec.rb @@ -100,6 +100,10 @@ describe "C-API String function" do it "returns an empty string if len is 0" do @s.rb_str_new("hello", 0).should == "" end + + it "returns a string from an offset char buffer" do + @s.rb_str_new_offset("hello", 1, 3).should == "ell" + end end describe "rb_str_new2" do @@ -439,32 +443,53 @@ describe "C-API String function" do end end - describe "StringValue" do + describe :string_value_macro, shared: true do + before :each do + @s = CApiStringSpecs.new + end + it "does not call #to_str on a String" do str = "genuine" str.should_not_receive(:to_str) - @s.StringValue(str) + @s.send(@method, str) end it "does not call #to_s on a String" do str = "genuine" str.should_not_receive(:to_str) - @s.StringValue(str) + @s.send(@method, str) end it "calls #to_str on non-String objects" do str = mock("fake") str.should_receive(:to_str).and_return("wannabe") - @s.StringValue(str) + @s.send(@method, str).should == "wannabe" end it "does not call #to_s on non-String objects" do str = mock("fake") str.should_not_receive(:to_s) - lambda { @s.StringValue(str) }.should raise_error(TypeError) + lambda { @s.send(@method, str) }.should raise_error(TypeError) end end + describe "StringValue" do + it_behaves_like :string_value_macro, :StringValue + end + + describe "SafeStringValue" do + it "raises for tained string when $SAFE is 1" do + Thread.new { + $SAFE = 1 + lambda { + @s.SafeStringValue("str".taint) + }.should raise_error(SecurityError) + }.join + end + + it_behaves_like :string_value_macro, :SafeStringValue + end + describe "rb_str_resize" do it "reduces the size of the string" do str = @s.rb_str_resize("test", 2) diff --git a/spec/ruby/optional/capi/thread_spec.rb b/spec/ruby/optional/capi/thread_spec.rb index 0ca701e35d..ab3d609bcf 100644 --- a/spec/ruby/optional/capi/thread_spec.rb +++ b/spec/ruby/optional/capi/thread_spec.rb @@ -21,7 +21,7 @@ describe "C-API Thread function" do end describe "rb_thread_wait_for" do - it "sleeps the current thread for the give ammount of time" do + it "sleeps the current thread for the give amount of time" do start = Time.now @t.rb_thread_wait_for(0, 100_000) (Time.now - start).should be_close(0.1, 0.2) @@ -78,8 +78,11 @@ describe "C-API Thread function" do end it "handles throwing an exception in the thread" do - proc = lambda { |x| raise "my error" } - thr = @t.rb_thread_create(proc, nil) + prc = lambda { |x| + Thread.current.report_on_exception = false + raise "my error" + } + thr = @t.rb_thread_create(prc, nil) thr.should be_kind_of(Thread) lambda { -- cgit v1.2.3