summaryrefslogtreecommitdiff
path: root/spec/ruby/optional
diff options
context:
space:
mode:
authorBenoit Daloze <eregontp@gmail.com>2021-06-02 14:34:07 +0200
committerBenoit Daloze <eregontp@gmail.com>2021-06-02 14:34:07 +0200
commit22e2a6a999b958efe5d84d9c7314e450fda82254 (patch)
treeb2dc946cf2fe2c250d0583675e548c67dca3e71a /spec/ruby/optional
parenta4fbc7e2884ba694278adea3b32ddb8c2ac10efe (diff)
downloadruby-22e2a6a999b958efe5d84d9c7314e450fda82254.tar.gz
Update to ruby/spec@a0b7d0d
Diffstat (limited to 'spec/ruby/optional')
-rw-r--r--spec/ruby/optional/capi/data_spec.rb7
-rw-r--r--spec/ruby/optional/capi/ext/data_spec.c6
-rw-r--r--spec/ruby/optional/capi/ext/object_spec.c6
-rw-r--r--spec/ruby/optional/capi/ext/typed_data_spec.c6
-rw-r--r--spec/ruby/optional/capi/object_spec.rb33
-rw-r--r--spec/ruby/optional/capi/typed_data_spec.rb11
6 files changed, 63 insertions, 6 deletions
diff --git a/spec/ruby/optional/capi/data_spec.rb b/spec/ruby/optional/capi/data_spec.rb
index c03b863678..18c769332e 100644
--- a/spec/ruby/optional/capi/data_spec.rb
+++ b/spec/ruby/optional/capi/data_spec.rb
@@ -36,6 +36,13 @@ describe "CApiWrappedStruct" do
end
end
+ describe "rb_check_type" do
+ it "does not raise an exception when checking data objects" do
+ a = @s.wrap_struct(1024)
+ @s.rb_check_type(a, a).should == true
+ end
+ end
+
describe "DATA_PTR" do
it "returns the struct data" do
a = @s.wrap_struct(1024)
diff --git a/spec/ruby/optional/capi/ext/data_spec.c b/spec/ruby/optional/capi/ext/data_spec.c
index 109bded1d1..ef069ef0ba 100644
--- a/spec/ruby/optional/capi/ext/data_spec.c
+++ b/spec/ruby/optional/capi/ext/data_spec.c
@@ -66,6 +66,11 @@ VALUE sws_change_struct(VALUE self, VALUE obj, VALUE new_val) {
return Qnil;
}
+VALUE sws_rb_check_type(VALUE self, VALUE obj, VALUE other) {
+ rb_check_type(obj, TYPE(other));
+ return Qtrue;
+}
+
void Init_data_spec(void) {
VALUE cls = rb_define_class("CApiAllocSpecs", rb_cObject);
rb_define_alloc_func(cls, sdaf_alloc_func);
@@ -76,6 +81,7 @@ void Init_data_spec(void) {
rb_define_method(cls, "get_struct_rdata", sws_get_struct_rdata, 1);
rb_define_method(cls, "get_struct_data_ptr", sws_get_struct_data_ptr, 1);
rb_define_method(cls, "change_struct", sws_change_struct, 2);
+ rb_define_method(cls, "rb_check_type", sws_rb_check_type, 2);
}
#ifdef __cplusplus
diff --git a/spec/ruby/optional/capi/ext/object_spec.c b/spec/ruby/optional/capi/ext/object_spec.c
index 24f9de260e..950c081818 100644
--- a/spec/ruby/optional/capi/ext/object_spec.c
+++ b/spec/ruby/optional/capi/ext/object_spec.c
@@ -189,6 +189,11 @@ static VALUE object_spec_RTEST(VALUE self, VALUE value) {
return RTEST(value) ? Qtrue : Qfalse;
}
+static VALUE so_check_type(VALUE self, VALUE obj, VALUE other) {
+ rb_check_type(obj, TYPE(other));
+ return Qtrue;
+}
+
static VALUE so_is_type_nil(VALUE self, VALUE obj) {
if(TYPE(obj) == T_NIL) {
return Qtrue;
@@ -434,6 +439,7 @@ void Init_object_spec(void) {
rb_define_method(cls, "rb_to_id", so_to_id, 1);
rb_define_method(cls, "RTEST", object_spec_RTEST, 1);
+ rb_define_method(cls, "rb_check_type", so_check_type, 2);
rb_define_method(cls, "rb_is_type_nil", so_is_type_nil, 1);
rb_define_method(cls, "rb_is_type_object", so_is_type_object, 1);
rb_define_method(cls, "rb_is_type_array", so_is_type_array, 1);
diff --git a/spec/ruby/optional/capi/ext/typed_data_spec.c b/spec/ruby/optional/capi/ext/typed_data_spec.c
index 70f21ce36f..eca2b667cc 100644
--- a/spec/ruby/optional/capi/ext/typed_data_spec.c
+++ b/spec/ruby/optional/capi/ext/typed_data_spec.c
@@ -148,6 +148,11 @@ VALUE sws_typed_change_struct(VALUE self, VALUE obj, VALUE new_val) {
return Qnil;
}
+VALUE sws_typed_rb_check_type(VALUE self, VALUE obj, VALUE other) {
+ rb_check_type(obj, TYPE(other));
+ return Qtrue;
+}
+
VALUE sws_typed_rb_check_typeddata_same_type(VALUE self, VALUE obj) {
return rb_check_typeddata(obj, &sample_typed_wrapped_struct_data_type) == DATA_PTR(obj) ? Qtrue : Qfalse;
}
@@ -172,6 +177,7 @@ void Init_typed_data_spec(void) {
rb_define_method(cls, "typed_get_struct_rdata", sws_typed_get_struct_rdata, 1);
rb_define_method(cls, "typed_get_struct_data_ptr", sws_typed_get_struct_data_ptr, 1);
rb_define_method(cls, "typed_change_struct", sws_typed_change_struct, 2);
+ rb_define_method(cls, "rb_check_type", sws_typed_rb_check_type, 2);
rb_define_method(cls, "rb_check_typeddata_same_type", sws_typed_rb_check_typeddata_same_type, 1);
rb_define_method(cls, "rb_check_typeddata_same_type_parent", sws_typed_rb_check_typeddata_same_type_parent, 1);
rb_define_method(cls, "rb_check_typeddata_different_type", sws_typed_rb_check_typeddata_different_type, 1);
diff --git a/spec/ruby/optional/capi/object_spec.rb b/spec/ruby/optional/capi/object_spec.rb
index 9b3a0593b0..ab11367060 100644
--- a/spec/ruby/optional/capi/object_spec.rb
+++ b/spec/ruby/optional/capi/object_spec.rb
@@ -499,20 +499,43 @@ describe "CApiObject" do
@o.rb_is_type_array([]).should == true
@o.rb_is_type_array(DescArray.new).should == true
@o.rb_is_type_module(ObjectTest).should == false
+ @o.rb_is_type_module(Module.new).should == true
@o.rb_is_type_class(ObjectTest).should == true
@o.rb_is_type_data(Time.now).should == true
end
end
+ describe "rb_check_type" do
+ it "checks if the object is of the given type" do
+ @o.rb_check_type(nil, nil).should == true
+ @o.rb_check_type(ObjectTest.new, Object.new).should == true
+ @o.rb_check_type([], []).should == true
+ @o.rb_check_type(Class.new(Array).new, []).should == true
+ @o.rb_check_type(ObjectTest, Object).should == true
+ end
+
+ it "raises an exception if the object is not of the expected type" do
+ -> {
+ @o.rb_check_type([], Object.new)
+ }.should raise_error(TypeError, 'wrong argument type Array (expected Object)')
+
+ -> {
+ @o.rb_check_type(ObjectTest, Module.new)
+ }.should raise_error(TypeError, 'wrong argument type Class (expected Module)')
+
+ -> {
+ @o.rb_check_type(nil, "string")
+ }.should raise_error(TypeError, 'wrong argument type nil (expected String)')
+ end
+ end
+
describe "rb_type_p" do
it "returns whether object is of the given type" do
- class DescArray < Array
- end
@o.rb_is_rb_type_p_nil(nil).should == true
@o.rb_is_rb_type_p_object([]).should == false
@o.rb_is_rb_type_p_object(ObjectTest.new).should == true
@o.rb_is_rb_type_p_array([]).should == true
- @o.rb_is_rb_type_p_array(DescArray.new).should == true
+ @o.rb_is_rb_type_p_array(Class.new(Array).new).should == true
@o.rb_is_rb_type_p_module(ObjectTest).should == false
@o.rb_is_rb_type_p_class(ObjectTest).should == true
@o.rb_is_rb_type_p_data(Time.now).should == true
@@ -521,12 +544,10 @@ describe "CApiObject" do
describe "BUILTIN_TYPE" do
it "returns the type constant for the object" do
- class DescArray < Array
- end
@o.rb_is_builtin_type_object([]).should == false
@o.rb_is_builtin_type_object(ObjectTest.new).should == true
@o.rb_is_builtin_type_array([]).should == true
- @o.rb_is_builtin_type_array(DescArray.new).should == true
+ @o.rb_is_builtin_type_array(Class.new(Array).new).should == true
@o.rb_is_builtin_type_module(ObjectTest).should == false
@o.rb_is_builtin_type_class(ObjectTest).should == true
@o.rb_is_builtin_type_data(Time.now).should == true
diff --git a/spec/ruby/optional/capi/typed_data_spec.rb b/spec/ruby/optional/capi/typed_data_spec.rb
index 9ccfa562d1..23b7c157ef 100644
--- a/spec/ruby/optional/capi/typed_data_spec.rb
+++ b/spec/ruby/optional/capi/typed_data_spec.rb
@@ -58,6 +58,17 @@ describe "CApiWrappedTypedStruct" do
end
end
+ describe "rb_check_type" do
+ it "raises an exception when checking typed data objects" do
+ -> {
+ a = @s.typed_wrap_struct(1024)
+ @s.rb_check_type(a, a)
+ }.should raise_error(TypeError) { |e|
+ e.message.should == 'wrong argument type Object (expected Data)'
+ }
+ end
+ end
+
describe "rb_check_typeddata" do
it "returns data pointer when the struct has the given type" do
a = @s.typed_wrap_struct(1024)