aboutsummaryrefslogtreecommitdiffstats
path: root/spec/ruby/optional
diff options
context:
space:
mode:
Diffstat (limited to 'spec/ruby/optional')
-rw-r--r--spec/ruby/optional/capi/class_spec.rb10
-rw-r--r--spec/ruby/optional/capi/constants_spec.rb12
-rw-r--r--spec/ruby/optional/capi/data_spec.rb4
-rw-r--r--spec/ruby/optional/capi/ext/io_spec.c11
-rw-r--r--spec/ruby/optional/capi/ext/kernel_spec.c1
-rw-r--r--spec/ruby/optional/capi/ext/object_spec.c12
-rw-r--r--spec/ruby/optional/capi/ext/string_spec.c5
-rw-r--r--spec/ruby/optional/capi/io_spec.rb20
-rw-r--r--spec/ruby/optional/capi/kernel_spec.rb9
-rw-r--r--spec/ruby/optional/capi/object_spec.rb23
-rw-r--r--spec/ruby/optional/capi/string_spec.rb25
-rw-r--r--spec/ruby/optional/capi/struct_spec.rb8
-rw-r--r--spec/ruby/optional/capi/thread_spec.rb2
-rw-r--r--spec/ruby/optional/capi/time_spec.rb2
-rw-r--r--spec/ruby/optional/capi/util_spec.rb5
15 files changed, 124 insertions, 25 deletions
diff --git a/spec/ruby/optional/capi/class_spec.rb b/spec/ruby/optional/capi/class_spec.rb
index a4dac6707a..57636f3819 100644
--- a/spec/ruby/optional/capi/class_spec.rb
+++ b/spec/ruby/optional/capi/class_spec.rb
@@ -237,12 +237,10 @@ describe "C-API Class function" do
}.should raise_error(TypeError)
end
- ruby_version_is "2.4" do
- it "raises a ArgumentError when given NULL as superclass" do
- lambda {
- @s.rb_define_class("ClassSpecDefineClass4", nil)
- }.should raise_error(ArgumentError)
- end
+ it "raises a ArgumentError when given NULL as superclass" do
+ lambda {
+ @s.rb_define_class("ClassSpecDefineClass4", nil)
+ }.should raise_error(ArgumentError)
end
end
diff --git a/spec/ruby/optional/capi/constants_spec.rb b/spec/ruby/optional/capi/constants_spec.rb
index a99fdac7e5..11a328a91f 100644
--- a/spec/ruby/optional/capi/constants_spec.rb
+++ b/spec/ruby/optional/capi/constants_spec.rb
@@ -11,12 +11,6 @@ describe "C-API constant" do
@s.rb_cArray.should == Array
end
- ruby_version_is ""..."2.4" do
- specify "rb_cBignum references the Bignum class" do
- @s.rb_cBignum.should == Bignum
- end
- end
-
specify "rb_cClass references the Class class" do
@s.rb_cClass.should == Class
end
@@ -43,12 +37,6 @@ describe "C-API constant" do
@s.rb_cFile.should == File
end
- ruby_version_is ""..."2.4" do
- specify "rb_cFixnum references the Fixnum class" do
- @s.rb_cFixnum.should == Fixnum
- end
- end
-
specify "rb_cFloat references the Float class" do
@s.rb_cFloat.should == Float
end
diff --git a/spec/ruby/optional/capi/data_spec.rb b/spec/ruby/optional/capi/data_spec.rb
index b7d1b8fd65..dd99581824 100644
--- a/spec/ruby/optional/capi/data_spec.rb
+++ b/spec/ruby/optional/capi/data_spec.rb
@@ -30,6 +30,10 @@ describe "CApiWrappedStruct" do
@s.change_struct(a, 100)
@s.get_struct(a).should == 100
end
+
+ it "raises a TypeError if the object does not wrap a struct" do
+ lambda { @s.get_struct(Object.new) }.should raise_error(TypeError)
+ end
end
describe "DATA_PTR" do
diff --git a/spec/ruby/optional/capi/ext/io_spec.c b/spec/ruby/optional/capi/ext/io_spec.c
index e3fbb872cf..a8f5a29145 100644
--- a/spec/ruby/optional/capi/ext/io_spec.c
+++ b/spec/ruby/optional/capi/ext/io_spec.c
@@ -167,6 +167,16 @@ VALUE io_spec_rb_thread_wait_fd(VALUE self, VALUE io) {
return Qnil;
}
+VALUE io_spec_rb_wait_for_single_fd(VALUE self, VALUE io, VALUE events, VALUE secs, VALUE usecs) {
+ int fd = io_spec_get_fd(io);
+ struct timeval tv;
+ if (!NIL_P(secs)) {
+ tv.tv_sec = FIX2INT(secs);
+ tv.tv_usec = FIX2INT(usecs);
+ }
+ return INT2FIX(rb_wait_for_single_fd(fd, FIX2INT(events), NIL_P(secs) ? NULL : &tv));
+}
+
VALUE io_spec_rb_thread_fd_writable(VALUE self, VALUE io) {
rb_thread_fd_writable(io_spec_get_fd(io));
return Qnil;
@@ -220,6 +230,7 @@ void Init_io_spec(void) {
rb_define_method(cls, "rb_io_wait_writable", io_spec_rb_io_wait_writable, 1);
rb_define_method(cls, "rb_thread_wait_fd", io_spec_rb_thread_wait_fd, 1);
rb_define_method(cls, "rb_thread_fd_writable", io_spec_rb_thread_fd_writable, 1);
+ rb_define_method(cls, "rb_wait_for_single_fd", io_spec_rb_wait_for_single_fd, 4);
rb_define_method(cls, "rb_io_binmode", io_spec_rb_io_binmode, 1);
rb_define_method(cls, "rb_fd_fix_cloexec", io_spec_rb_fd_fix_cloexec, 1);
rb_define_method(cls, "rb_cloexec_open", io_spec_rb_cloexec_open, 3);
diff --git a/spec/ruby/optional/capi/ext/kernel_spec.c b/spec/ruby/optional/capi/ext/kernel_spec.c
index 5ec45f542c..48e2b1ca95 100644
--- a/spec/ruby/optional/capi/ext/kernel_spec.c
+++ b/spec/ruby/optional/capi/ext/kernel_spec.c
@@ -168,6 +168,7 @@ static VALUE kernel_spec_rb_protect_yield(VALUE self, VALUE obj, VALUE ary) {
int status = 0;
VALUE res = rb_protect(rb_yield, obj, &status);
rb_ary_store(ary, 0, INT2NUM(23));
+ rb_ary_store(ary, 1, res);
if (status) {
rb_jump_tag(status);
}
diff --git a/spec/ruby/optional/capi/ext/object_spec.c b/spec/ruby/optional/capi/ext/object_spec.c
index c70c2ecf3a..cd32050f14 100644
--- a/spec/ruby/optional/capi/ext/object_spec.c
+++ b/spec/ruby/optional/capi/ext/object_spec.c
@@ -327,6 +327,16 @@ static VALUE object_spec_rb_ivar_defined(VALUE self, VALUE obj, VALUE sym_name)
return rb_ivar_defined(obj, SYM2ID(sym_name));
}
+static VALUE object_spec_rb_copy_generic_ivar(VALUE self, VALUE clone, VALUE obj) {
+ rb_copy_generic_ivar(clone, obj);
+ return self;
+}
+
+static VALUE object_spec_rb_free_generic_ivar(VALUE self, VALUE obj) {
+ rb_free_generic_ivar(obj);
+ return self;
+}
+
static VALUE object_spec_rb_equal(VALUE self, VALUE a, VALUE b) {
return rb_equal(a, b);
}
@@ -400,6 +410,8 @@ void Init_object_spec(void) {
rb_define_method(cls, "rb_ivar_get", object_spec_rb_ivar_get, 2);
rb_define_method(cls, "rb_ivar_set", object_spec_rb_ivar_set, 3);
rb_define_method(cls, "rb_ivar_defined", object_spec_rb_ivar_defined, 2);
+ rb_define_method(cls, "rb_copy_generic_ivar", object_spec_rb_copy_generic_ivar, 2);
+ rb_define_method(cls, "rb_free_generic_ivar", object_spec_rb_free_generic_ivar, 1);
}
#ifdef __cplusplus
diff --git a/spec/ruby/optional/capi/ext/string_spec.c b/spec/ruby/optional/capi/ext/string_spec.c
index dfb1ee51cb..6ad0b46ae4 100644
--- a/spec/ruby/optional/capi/ext/string_spec.c
+++ b/spec/ruby/optional/capi/ext/string_spec.c
@@ -176,6 +176,10 @@ VALUE string_spec_rb_str_encode(VALUE self, VALUE str, VALUE enc, VALUE flags, V
return rb_str_encode(str, enc, FIX2INT(flags), opts);
}
+VALUE string_spec_rb_str_export_to_enc(VALUE self, VALUE str, VALUE enc) {
+ return rb_str_export_to_enc(str, rb_to_encoding(enc));
+}
+
VALUE string_spec_rb_str_new_cstr(VALUE self, VALUE str) {
if(NIL_P(str)) {
return rb_str_new_cstr("");
@@ -435,6 +439,7 @@ void Init_string_spec(void) {
rb_define_method(cls, "rb_str_new_offset", string_spec_rb_str_new_offset, 3);
rb_define_method(cls, "rb_str_new2", string_spec_rb_str_new2, 1);
rb_define_method(cls, "rb_str_encode", string_spec_rb_str_encode, 4);
+ rb_define_method(cls, "rb_str_export_to_enc", string_spec_rb_str_export_to_enc, 2);
rb_define_method(cls, "rb_str_new_cstr", string_spec_rb_str_new_cstr, 1);
rb_define_method(cls, "rb_external_str_new", string_spec_rb_external_str_new, 1);
rb_define_method(cls, "rb_external_str_new_cstr", string_spec_rb_external_str_new_cstr, 1);
diff --git a/spec/ruby/optional/capi/io_spec.rb b/spec/ruby/optional/capi/io_spec.rb
index a832c6a93b..82dc1fc0df 100644
--- a/spec/ruby/optional/capi/io_spec.rb
+++ b/spec/ruby/optional/capi/io_spec.rb
@@ -299,6 +299,26 @@ describe "C-API IO function" do
end
end
+ describe "rb_wait_for_single_fd" do
+ it "waits til an fd is ready for reading" do
+ start = false
+ thr = Thread.new do
+ start = true
+ sleep 0.05
+ @w_io.write "rb_io_wait_readable"
+ end
+
+ Thread.pass until start
+
+ @o.rb_wait_for_single_fd(@r_io, 1, nil, nil).should == 1
+
+ thr.join
+ end
+
+ it "polls whether an fd is ready for reading if timeout is 0" do
+ @o.rb_wait_for_single_fd(@r_io, 1, 0, 0).should == 0
+ end
+ end
end
describe "rb_fd_fix_cloexec" do
diff --git a/spec/ruby/optional/capi/kernel_spec.rb b/spec/ruby/optional/capi/kernel_spec.rb
index f68a46ed50..ab6c2bceef 100644
--- a/spec/ruby/optional/capi/kernel_spec.rb
+++ b/spec/ruby/optional/capi/kernel_spec.rb
@@ -281,6 +281,15 @@ describe "C-API Kernel function" do
end.should raise_error(NameError)
proof[0].should == 23
end
+
+ it "will return nil if an error was raised" do
+ proof = [] # Hold proof of work performed after the yield.
+ lambda do
+ @s.rb_protect_yield(7, proof) { |x| raise NameError}
+ end.should raise_error(NameError)
+ proof[0].should == 23
+ proof[1].should == nil
+ end
end
describe "rb_rescue" do
diff --git a/spec/ruby/optional/capi/object_spec.rb b/spec/ruby/optional/capi/object_spec.rb
index 541b58b48c..34aae636c7 100644
--- a/spec/ruby/optional/capi/object_spec.rb
+++ b/spec/ruby/optional/capi/object_spec.rb
@@ -853,5 +853,28 @@ describe "CApiObject" do
@o.rb_ivar_defined(@test, :bar).should == false
end
end
+
+ # The `generic_iv_tbl` table and `*_generic_ivar` functions are for mutable
+ # objects which do not store ivars directly in MRI such as RString, because
+ # there is no member iv_index_tbl (ivar table) such as in RObject and RClass.
+
+ describe "rb_copy_generic_ivar for objects which do not store ivars directly" do
+ it "copies the instance variables from one object to another" do
+ original = "abc"
+ original.instance_variable_set(:@foo, :bar)
+ clone = "def"
+ @o.rb_copy_generic_ivar(clone, original)
+ clone.instance_variable_get(:@foo).should == :bar
+ end
+ end
+
+ describe "rb_free_generic_ivar for objects which do not store ivars directly" do
+ it "removes the instance variables from an object" do
+ o = "abc"
+ o.instance_variable_set(:@baz, :flibble)
+ @o.rb_free_generic_ivar(o)
+ o.instance_variables.should == []
+ end
+ end
end
end
diff --git a/spec/ruby/optional/capi/string_spec.rb b/spec/ruby/optional/capi/string_spec.rb
index ac23198662..79cdb524ad 100644
--- a/spec/ruby/optional/capi/string_spec.rb
+++ b/spec/ruby/optional/capi/string_spec.rb
@@ -167,6 +167,10 @@ describe "C-API String function" do
@s.rb_str_new("hello", 3).should == "hel"
end
+ it "returns a non-tainted string" do
+ @s.rb_str_new("hello", 5).tainted?.should == false
+ end
+
it "returns an empty string if len is 0" do
@s.rb_str_new("hello", 0).should == ""
end
@@ -877,6 +881,27 @@ describe "C-API String function" do
end
end
+ describe "rb_str_export_to_enc" do
+ it "returns a copy of an ascii string converted to the new encoding" do
+ source = "A simple string".encode(Encoding::US_ASCII)
+ result = @s.rb_str_export_to_enc(source, Encoding::UTF_8)
+ result.should == source.encode(Encoding::UTF_8)
+ result.encoding.should == Encoding::UTF_8
+ end
+
+ it "returns the source string if it can not be converted" do
+ source = ["00ff"].pack("H*");
+ result = @s.rb_str_export_to_enc(source, Encoding::UTF_8)
+ result.should equal(source)
+ end
+
+ it "does not alter the source string if it can not be converted" do
+ source = ["00ff"].pack("H*");
+ result = @s.rb_str_export_to_enc(source, Encoding::UTF_8)
+ source.bytes.should == [0, 255]
+ end
+end
+
describe "rb_sprintf" do
it "replaces the parts like sprintf" do
@s.rb_sprintf1("Awesome %s is replaced", "string").should == "Awesome string is replaced"
diff --git a/spec/ruby/optional/capi/struct_spec.rb b/spec/ruby/optional/capi/struct_spec.rb
index b3acd02b61..3b0972926c 100644
--- a/spec/ruby/optional/capi/struct_spec.rb
+++ b/spec/ruby/optional/capi/struct_spec.rb
@@ -203,11 +203,9 @@ describe "C-API Struct function" do
end
end
- ruby_version_is "2.4" do
- describe "rb_struct_size" do
- it "returns the number of struct members" do
- @s.rb_struct_size(@struct).should == 3
- end
+ describe "rb_struct_size" do
+ it "returns the number of struct members" do
+ @s.rb_struct_size(@struct).should == 3
end
end
end
diff --git a/spec/ruby/optional/capi/thread_spec.rb b/spec/ruby/optional/capi/thread_spec.rb
index 52070198fd..a46290203a 100644
--- a/spec/ruby/optional/capi/thread_spec.rb
+++ b/spec/ruby/optional/capi/thread_spec.rb
@@ -24,7 +24,7 @@ describe "C-API Thread function" 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)
+ (Time.now - start).should be_close(0.1, TIME_TOLERANCE)
end
end
diff --git a/spec/ruby/optional/capi/time_spec.rb b/spec/ruby/optional/capi/time_spec.rb
index 1191ceabd2..427507a3bb 100644
--- a/spec/ruby/optional/capi/time_spec.rb
+++ b/spec/ruby/optional/capi/time_spec.rb
@@ -294,7 +294,7 @@ describe "CApiTimeSpecs" do
now = Time.now
time = @s.rb_time_from_timespec(now.utc_offset)
time.should be_an_instance_of(Time)
- (time - now).should be_close(0, 10)
+ (time - now).should be_close(0, TIME_TOLERANCE)
end
end
end
diff --git a/spec/ruby/optional/capi/util_spec.rb b/spec/ruby/optional/capi/util_spec.rb
index b3e43d29ce..33dc30df85 100644
--- a/spec/ruby/optional/capi/util_spec.rb
+++ b/spec/ruby/optional/capi/util_spec.rb
@@ -119,6 +119,11 @@ describe "C-API Util function" do
ScratchPad.recorded.should == [1, nil]
end
+ it "assigns required and optional arguments with no hash argument given" do
+ @o.rb_scan_args([1, 7, 4], "21:", 3, @acc).should == 3
+ ScratchPad.recorded.should == [1, 7, 4]
+ end
+
it "assigns required, optional, splat, post-splat, Hash and block arguments" do
h = {a: 1, b: 2}
@o.rb_scan_args([1, 2, 3, 4, 5, h], "11*1:&", 6, @acc, &@prc).should == 5