diff options
Diffstat (limited to 'spec/ruby/optional')
60 files changed, 2466 insertions, 1180 deletions
diff --git a/spec/ruby/optional/capi/array_spec.rb b/spec/ruby/optional/capi/array_spec.rb index 9c35017e21..0b997f774c 100644 --- a/spec/ruby/optional/capi/array_spec.rb +++ b/spec/ruby/optional/capi/array_spec.rb @@ -8,7 +8,7 @@ describe :rb_ary_new2, shared: true do end it "raises an ArgumentError when the given argument is negative" do - -> { @s.send(@method, -1) }.should raise_error(ArgumentError) + -> { @s.send(@method, -1) }.should.raise(ArgumentError) end end @@ -84,7 +84,7 @@ describe "C-API Array function" do end it "raises a FrozenError if the array is frozen" do - -> { @s.rb_ary_cat([].freeze, 1) }.should raise_error(FrozenError) + -> { @s.rb_ary_cat([].freeze, 1) }.should.raise(FrozenError) end end @@ -120,7 +120,7 @@ describe "C-API Array function" do it "returns the original array" do a = [1,2,3] - @s.rb_ary_reverse(a).should equal(a) + @s.rb_ary_reverse(a).should.equal?(a) end end @@ -131,7 +131,7 @@ describe "C-API Array function" do end it "raises a FrozenError if the array is frozen" do - -> { @s.rb_ary_rotate([].freeze, 1) }.should raise_error(FrozenError) + -> { @s.rb_ary_rotate([].freeze, 1) }.should.raise(FrozenError) end end @@ -166,7 +166,7 @@ describe "C-API Array function" do b = @s.rb_ary_dup(a) b.should == a - b.should_not equal(a) + b.should_not.equal?(a) end end @@ -221,7 +221,7 @@ describe "C-API Array function" do it "raises an IndexError if the negative index is greater than the length" do a = [1, 2, 3] - -> { @s.rb_ary_store(a, -10, 5) }.should raise_error(IndexError) + -> { @s.rb_ary_store(a, -10, 5) }.should.raise(IndexError) end it "enlarges the array as needed" do @@ -232,7 +232,7 @@ describe "C-API Array function" do it "raises a FrozenError if the array is frozen" do a = [1, 2, 3].freeze - -> { @s.rb_ary_store(a, 1, 5) }.should raise_error(FrozenError) + -> { @s.rb_ary_store(a, 1, 5) }.should.raise(FrozenError) end end @@ -307,11 +307,11 @@ describe "C-API Array function" do describe "rb_ary_includes" do it "returns true if the array includes the element" do - @s.rb_ary_includes([1, 2, 3], 2).should be_true + @s.rb_ary_includes([1, 2, 3], 2).should == true end it "returns false if the array does not include the element" do - @s.rb_ary_includes([1, 2, 3], 4).should be_false + @s.rb_ary_includes([1, 2, 3], 4).should == false end end @@ -322,7 +322,7 @@ describe "C-API Array function" do end it "returns nil for an out of range index" do - @s.rb_ary_aref([1, 2, 3], 6).should be_nil + @s.rb_ary_aref([1, 2, 3], 6).should == nil end it "returns a new array where the first argument is the index and the second is the length" do @@ -335,7 +335,7 @@ describe "C-API Array function" do end it "returns nil when the start of a range is out of bounds" do - @s.rb_ary_aref([1, 2, 3, 4], 6..10).should be_nil + @s.rb_ary_aref([1, 2, 3, 4], 6..10).should == nil end it "returns an empty array when the start of a range equals the last element" do @@ -343,37 +343,39 @@ describe "C-API Array function" do end end - describe "rb_iterate" do - it "calls an callback function as a block passed to an method" do - s = [1,2,3,4] - s2 = @s.rb_iterate(s) + ruby_version_is ""..."4.0" do + describe "rb_iterate" do + it "calls an callback function as a block passed to an method" do + s = [1,2,3,4] + s2 = @s.rb_iterate(s) - s2.should == s + s2.should == s - # Make sure they're different objects - s2.equal?(s).should be_false - end + # Make sure they're different objects + s2.equal?(s).should == false + end - it "calls a function with the other function available as a block" do - h = {a: 1, b: 2} + it "calls a function with the other function available as a block" do + h = {a: 1, b: 2} - @s.rb_iterate_each_pair(h).sort.should == [1,2] - end + @s.rb_iterate_each_pair(h).sort.should == [1,2] + end - it "calls a function which can yield into the original block" do - s2 = [] + it "calls a function which can yield into the original block" do + s2 = [] - o = Object.new - def o.each - yield 1 - yield 2 - yield 3 - yield 4 - end + o = Object.new + def o.each + yield 1 + yield 2 + yield 3 + yield 4 + end - @s.rb_iterate_then_yield(o) { |x| s2 << x } + @s.rb_iterate_then_yield(o) { |x| s2 << x } - s2.should == [1,2,3,4] + s2.should == [1,2,3,4] + end end end @@ -385,7 +387,7 @@ describe "C-API Array function" do s2.should == s # Make sure they're different objects - s2.equal?(s).should be_false + s2.equal?(s).should == false end it "calls a function with the other function available as a block" do @@ -420,7 +422,7 @@ describe "C-API Array function" do it "returns nil if the element is not in the array" do ary = [1, 2, 3, 4] - @s.rb_ary_delete(ary, 5).should be_nil + @s.rb_ary_delete(ary, 5).should == nil ary.should == [1, 2, 3, 4] end end @@ -435,7 +437,7 @@ describe "C-API Array function" do it "freezes the object exactly like Kernel#freeze" do ary = [1,2] @s.rb_ary_freeze(ary) - ary.frozen?.should be_true + ary.frozen?.should == true end end @@ -455,12 +457,12 @@ describe "C-API Array function" do end it "returns nil if the index is out of bounds" do - @s.rb_ary_delete_at(@array, 4).should be_nil + @s.rb_ary_delete_at(@array, 4).should == nil @array.should == [1, 2, 3, 4] end it "returns nil if the negative index is out of bounds" do - @s.rb_ary_delete_at(@array, -5).should be_nil + @s.rb_ary_delete_at(@array, -5).should == nil @array.should == [1, 2, 3, 4] end end @@ -471,7 +473,7 @@ describe "C-API Array function" do it "returns the given array" do array = [1, 2, 3] - @s.rb_ary_to_ary(array).should equal(array) + @s.rb_ary_to_ary(array).should.equal?(array) end end @@ -517,7 +519,7 @@ describe "C-API Array function" do end it "returns nil if the begin index is out of bound" do - @s.rb_ary_subseq([1, 2, 3, 4, 5], 6, 3).should be_nil + @s.rb_ary_subseq([1, 2, 3, 4, 5], 6, 3).should == nil end it "returns the existing subsequence of the length is out of bounds" do @@ -525,7 +527,7 @@ describe "C-API Array function" do end it "returns nil if the size is negative" do - @s.rb_ary_subseq([1, 2, 3, 4, 5], 1, -1).should be_nil + @s.rb_ary_subseq([1, 2, 3, 4, 5], 1, -1).should == nil end end end diff --git a/spec/ruby/optional/capi/bignum_spec.rb b/spec/ruby/optional/capi/bignum_spec.rb index 179f053eec..ae7552b3f5 100644 --- a/spec/ruby/optional/capi/bignum_spec.rb +++ b/spec/ruby/optional/capi/bignum_spec.rb @@ -35,8 +35,8 @@ describe "CApiBignumSpecs" do end it "raises RangeError if passed Bignum overflow long" do - -> { @s.rb_big2long(ensure_bignum(@max_long + 1)) }.should raise_error(RangeError) - -> { @s.rb_big2long(ensure_bignum(@min_long - 1)) }.should raise_error(RangeError) + -> { @s.rb_big2long(ensure_bignum(@max_long + 1)) }.should.raise(RangeError) + -> { @s.rb_big2long(ensure_bignum(@min_long - 1)) }.should.raise(RangeError) end end @@ -49,8 +49,8 @@ describe "CApiBignumSpecs" do end it "raises RangeError if passed Bignum overflow long" do - -> { @s.rb_big2ll(ensure_bignum(@max_long << 40)) }.should raise_error(RangeError) - -> { @s.rb_big2ll(ensure_bignum(@min_long << 40)) }.should raise_error(RangeError) + -> { @s.rb_big2ll(ensure_bignum(@max_long << 40)) }.should.raise(RangeError) + -> { @s.rb_big2ll(ensure_bignum(@min_long << 40)) }.should.raise(RangeError) end end @@ -67,8 +67,8 @@ describe "CApiBignumSpecs" do end it "raises RangeError if passed Bignum overflow long" do - -> { @s.rb_big2ulong(ensure_bignum(@max_ulong + 1)) }.should raise_error(RangeError) - -> { @s.rb_big2ulong(ensure_bignum(@min_long - 1)) }.should raise_error(RangeError) + -> { @s.rb_big2ulong(ensure_bignum(@max_ulong + 1)) }.should.raise(RangeError) + -> { @s.rb_big2ulong(ensure_bignum(@min_long - 1)) }.should.raise(RangeError) end end @@ -214,13 +214,13 @@ describe "CApiBignumSpecs" do it "raises FloatDomainError for Infinity values" do inf = 1.0 / 0 - -> { @s.rb_dbl2big(inf) }.should raise_error(FloatDomainError) + -> { @s.rb_dbl2big(inf) }.should.raise(FloatDomainError) end it "raises FloatDomainError for NaN values" do nan = 0.0 / 0 - -> { @s.rb_dbl2big(nan) }.should raise_error(FloatDomainError) + -> { @s.rb_dbl2big(nan) }.should.raise(FloatDomainError) end end end diff --git a/spec/ruby/optional/capi/binding_spec.rb b/spec/ruby/optional/capi/binding_spec.rb index 2165705457..1edd20b8f3 100644 --- a/spec/ruby/optional/capi/binding_spec.rb +++ b/spec/ruby/optional/capi/binding_spec.rb @@ -8,21 +8,9 @@ describe "CApiBindingSpecs" do end describe "Kernel#binding" do - ruby_version_is '3.2' do - it "raises when called from C" do - foo = 14 - -> { @b.get_binding }.should raise_error(RuntimeError) - end - end - - ruby_version_is ''...'3.2' do - it "gives the top-most Ruby binding when called from C" do - foo = 14 - b = @b.get_binding - b.local_variable_get(:foo).should == 14 - b.local_variable_set :foo, 12 - foo.should == 12 - end + it "raises when called from C" do + foo = 14 + -> { @b.get_binding }.should.raise(RuntimeError) end end end diff --git a/spec/ruby/optional/capi/class_spec.rb b/spec/ruby/optional/capi/class_spec.rb index a231245ebe..07e9e42ad1 100644 --- a/spec/ruby/optional/capi/class_spec.rb +++ b/spec/ruby/optional/capi/class_spec.rb @@ -11,8 +11,8 @@ autoload :ClassIdUnderAutoload, "#{object_path}/class_id_under_autoload_spec" describe :rb_path_to_class, shared: true do it "returns a class or module from a scoped String" do - @s.send(@method, "CApiClassSpecs::A::B").should equal(CApiClassSpecs::A::B) - @s.send(@method, "CApiClassSpecs::A::M").should equal(CApiClassSpecs::A::M) + @s.send(@method, "CApiClassSpecs::A::B").should.equal?(CApiClassSpecs::A::B) + @s.send(@method, "CApiClassSpecs::A::M").should.equal?(CApiClassSpecs::A::M) end it "resolves autoload constants" do @@ -20,21 +20,21 @@ describe :rb_path_to_class, shared: true do end it "raises an ArgumentError if a constant in the path does not exist" do - -> { @s.send(@method, "CApiClassSpecs::NotDefined::B") }.should raise_error(ArgumentError) + -> { @s.send(@method, "CApiClassSpecs::NotDefined::B") }.should.raise(ArgumentError) end it "raises an ArgumentError if the final constant does not exist" do - -> { @s.send(@method, "CApiClassSpecs::NotDefined") }.should raise_error(ArgumentError) + -> { @s.send(@method, "CApiClassSpecs::NotDefined") }.should.raise(ArgumentError) end it "raises a TypeError if the constant is not a class or module" do -> { @s.send(@method, "CApiClassSpecs::A::C") - }.should raise_error(TypeError, 'CApiClassSpecs::A::C does not refer to class/module') + }.should.raise(TypeError, 'CApiClassSpecs::A::C does not refer to class/module') end it "raises an ArgumentError even if a constant in the path exists on toplevel" do - -> { @s.send(@method, "CApiClassSpecs::Object") }.should raise_error(ArgumentError) + -> { @s.send(@method, "CApiClassSpecs::Object") }.should.raise(ArgumentError) end end @@ -46,29 +46,31 @@ describe "C-API Class function" do describe "rb_class_instance_methods" do it "returns the public and protected methods of self and its ancestors" do methods = @s.rb_class_instance_methods(ModuleSpecs::Basic) - methods.should include(:protected_module, :public_module) + methods.should.include?(:protected_module) + methods.should.include?(:public_module) methods = @s.rb_class_instance_methods(ModuleSpecs::Basic, true) - methods.should include(:protected_module, :public_module) + methods.should.include?(:protected_module) + methods.should.include?(:public_module) end it "when passed false as a parameter, returns the instance methods of the class" do methods = @s.rb_class_instance_methods(ModuleSpecs::Child, false) - methods.should include(:protected_child, :public_child) + methods.to_set.should >= Set[:protected_child, :public_child] end end describe "rb_class_public_instance_methods" do it "returns a list of public methods in module and its ancestors" do methods = @s.rb_class_public_instance_methods(ModuleSpecs::CountsChild) - methods.should include(:public_3) - methods.should include(:public_2) - methods.should include(:public_1) + methods.should.include?(:public_3) + methods.should.include?(:public_2) + methods.should.include?(:public_1) methods = @s.rb_class_public_instance_methods(ModuleSpecs::CountsChild, true) - methods.should include(:public_3) - methods.should include(:public_2) - methods.should include(:public_1) + methods.should.include?(:public_3) + methods.should.include?(:public_2) + methods.should.include?(:public_1) end it "when passed false as a parameter, should return only methods defined in that module" do @@ -79,14 +81,14 @@ describe "C-API Class function" do describe "rb_class_protected_instance_methods" do it "returns a list of protected methods in module and its ancestors" do methods = @s.rb_class_protected_instance_methods(ModuleSpecs::CountsChild) - methods.should include(:protected_3) - methods.should include(:protected_2) - methods.should include(:protected_1) + methods.should.include?(:protected_3) + methods.should.include?(:protected_2) + methods.should.include?(:protected_1) methods = @s.rb_class_protected_instance_methods(ModuleSpecs::CountsChild, true) - methods.should include(:protected_3) - methods.should include(:protected_2) - methods.should include(:protected_1) + methods.should.include?(:protected_3) + methods.should.include?(:protected_2) + methods.should.include?(:protected_1) end it "when passed false as a parameter, should return only methods defined in that module" do @@ -110,7 +112,7 @@ describe "C-API Class function" do it "allocates and initializes a new object" do o = @s.rb_class_new_instance([], CApiClassSpecs::Alloc) o.class.should == CApiClassSpecs::Alloc - o.initialized.should be_true + o.initialized.should == true end it "passes arguments to the #initialize method" do @@ -133,7 +135,7 @@ describe "C-API Class function" do it "raises TypeError if the last argument is not a Hash" do -> { @s.rb_class_new_instance_kw([42], CApiClassSpecs::KeywordAlloc) - }.should raise_error(TypeError, 'no implicit conversion of Integer into Hash') + }.should.raise(TypeError, 'no implicit conversion of Integer into Hash') end end @@ -141,9 +143,9 @@ describe "C-API Class function" do it "includes a module into a class" do c = Class.new o = c.new - -> { o.included? }.should raise_error(NameError) + -> { o.included? }.should.raise(NameError) @s.rb_include_module(c, CApiClassSpecs::M) - o.included?.should be_true + o.included?.should == true end end @@ -155,12 +157,12 @@ describe "C-API Class function" do it "defines an attr_reader when passed true, false" do @s.rb_define_attr(CApiClassSpecs::Attr, :foo, true, false) @a.foo.should == 1 - -> { @a.foo = 5 }.should raise_error(NameError) + -> { @a.foo = 5 }.should.raise(NameError) end it "defines an attr_writer when passed false, true" do @s.rb_define_attr(CApiClassSpecs::Attr, :bar, false, true) - -> { @a.bar }.should raise_error(NameError) + -> { @a.bar }.should.raise(NameError) @a.bar = 5 @a.instance_variable_get(:@bar).should == 5 end @@ -183,7 +185,7 @@ describe "C-API Class function" do it "calls the method in the superclass with the correct self" do @s.define_call_super_method CApiClassSpecs::SubSelf, "call_super_method" obj = CApiClassSpecs::SubSelf.new - obj.call_super_method.should equal obj + obj.call_super_method.should.equal? obj end it "calls the method in the superclass through two native levels" do @@ -200,7 +202,7 @@ describe "C-API Class function" do end it "returns a string for an anonymous class" do - @s.rb_class2name(Class.new).should be_kind_of(String) + @s.rb_class2name(Class.new).should.is_a?(String) end it "returns a string beginning with # for an anonymous class" do @@ -224,7 +226,7 @@ describe "C-API Class function" do end it "returns a string for an anonymous class" do - @s.rb_class_name(Class.new).should be_kind_of(String) + @s.rb_class_name(Class.new).should.is_a?(String) end end @@ -238,22 +240,22 @@ describe "C-API Class function" do describe "rb_cvar_defined" do it "returns false when the class variable is not defined" do - @s.rb_cvar_defined(CApiClassSpecs::CVars, "@@nocvar").should be_false + @s.rb_cvar_defined(CApiClassSpecs::CVars, "@@nocvar").should == false end it "returns true when the class variable is defined" do - @s.rb_cvar_defined(CApiClassSpecs::CVars, "@@cvar").should be_true + @s.rb_cvar_defined(CApiClassSpecs::CVars, "@@cvar").should == true end it "returns true if the class instance variable is defined" do - @s.rb_cvar_defined(CApiClassSpecs::CVars, "@c_ivar").should be_true + @s.rb_cvar_defined(CApiClassSpecs::CVars, "@c_ivar").should == true end end describe "rb_cv_set" do it "sets a class variable" do o = CApiClassSpecs::CVars.new - o.new_cv.should be_nil + o.new_cv.should == nil @s.rb_cv_set(CApiClassSpecs::CVars, "@@new_cv", 1) o.new_cv.should == 1 CApiClassSpecs::CVars.remove_class_variable :@@new_cv @@ -268,14 +270,14 @@ describe "C-API Class function" do it "raises a NameError if the class variable is not defined" do -> { @s.rb_cv_get(CApiClassSpecs::CVars, "@@no_cvar") - }.should raise_error(NameError, /class variable @@no_cvar/) + }.should.raise(NameError, /class variable @@no_cvar/) end end describe "rb_cvar_set" do it "sets a class variable" do o = CApiClassSpecs::CVars.new - o.new_cvar.should be_nil + o.new_cvar.should == nil @s.rb_cvar_set(CApiClassSpecs::CVars, "@@new_cvar", 1) o.new_cvar.should == 1 CApiClassSpecs::CVars.remove_class_variable :@@new_cvar @@ -289,8 +291,8 @@ describe "C-API Class function" do end it "creates a subclass of the superclass" do - @cls.should be_kind_of(Class) - ClassSpecDefineClass.should equal(@cls) + @cls.should.is_a?(Class) + ClassSpecDefineClass.should.equal?(@cls) @cls.superclass.should == CApiClassSpecs::Super end @@ -307,19 +309,19 @@ describe "C-API Class function" do it "raises a TypeError when given a non class object to superclass" do -> { @s.rb_define_class("ClassSpecDefineClass3", Module.new) - }.should raise_error(TypeError) + }.should.raise(TypeError) end it "raises a TypeError when given a mismatched class to superclass" do -> { @s.rb_define_class("ClassSpecDefineClass", Object) - }.should raise_error(TypeError) + }.should.raise(TypeError) end it "raises a ArgumentError when given NULL as superclass" do -> { @s.rb_define_class("ClassSpecDefineClass4", nil) - }.should raise_error(ArgumentError) + }.should.raise(ArgumentError) end it "allows arbitrary names, including constant names not valid in Ruby" do @@ -328,7 +330,7 @@ describe "C-API Class function" do -> { Object.const_get(cls.name) - }.should raise_error(NameError, /wrong constant name/) + }.should.raise(NameError, /wrong constant name/) end end @@ -337,8 +339,8 @@ describe "C-API Class function" do cls = @s.rb_define_class_under(CApiClassSpecs, "ClassUnder1", CApiClassSpecs::Super) - cls.should be_kind_of(Class) - CApiClassSpecs::Super.should be_ancestor_of(CApiClassSpecs::ClassUnder1) + cls.should.is_a?(Class) + CApiClassSpecs::ClassUnder1.ancestors.should.include?(CApiClassSpecs::Super) end it "sets the class name" do @@ -356,7 +358,7 @@ describe "C-API Class function" do -> { @s.rb_define_class_under(CApiClassSpecs, "ClassUnder5", Module.new) - }.should raise_error(TypeError) + }.should.raise(TypeError) end it "raises a TypeError when given a mismatched class to superclass" do @@ -364,7 +366,9 @@ describe "C-API Class function" do -> { @s.rb_define_class_under(CApiClassSpecs, "ClassUnder6", Class.new) - }.should raise_error(TypeError) + }.should.raise(TypeError) + ensure + CApiClassSpecs.send(:remove_const, :ClassUnder6) end it "defines a class for an existing Autoload" do @@ -372,7 +376,7 @@ describe "C-API Class function" do end it "raises a TypeError if class is defined and its superclass mismatches the given one" do - -> { @s.rb_define_class_under(CApiClassSpecs, "Sub", Object) }.should raise_error(TypeError) + -> { @s.rb_define_class_under(CApiClassSpecs, "Sub", Object) }.should.raise(TypeError) end it "allows arbitrary names, including constant names not valid in Ruby" do @@ -381,15 +385,15 @@ describe "C-API Class function" do -> { CApiClassSpecs.const_get(cls.name) - }.should raise_error(NameError, /wrong constant name/) + }.should.raise(NameError, /wrong constant name/) end end describe "rb_define_class_id_under" do it "creates a subclass of the superclass contained in a module" do cls = @s.rb_define_class_id_under(CApiClassSpecs, :ClassIdUnder1, CApiClassSpecs::Super) - cls.should be_kind_of(Class) - CApiClassSpecs::Super.should be_ancestor_of(CApiClassSpecs::ClassIdUnder1) + cls.should.is_a?(Class) + CApiClassSpecs::ClassIdUnder1.ancestors.should.include?(CApiClassSpecs::Super) end it "sets the class name" do @@ -408,7 +412,7 @@ describe "C-API Class function" do end it "raises a TypeError if class is defined and its superclass mismatches the given one" do - -> { @s.rb_define_class_id_under(CApiClassSpecs, :Sub, Object) }.should raise_error(TypeError) + -> { @s.rb_define_class_id_under(CApiClassSpecs, :Sub, Object) }.should.raise(TypeError) end it "allows arbitrary names, including constant names not valid in Ruby" do @@ -417,14 +421,14 @@ describe "C-API Class function" do -> { CApiClassSpecs.const_get(cls.name) - }.should raise_error(NameError, /wrong constant name/) + }.should.raise(NameError, /wrong constant name/) end end describe "rb_define_class_variable" do it "sets a class variable" do o = CApiClassSpecs::CVars.new - o.rbdcv_cvar.should be_nil + o.rbdcv_cvar.should == nil @s.rb_define_class_variable(CApiClassSpecs::CVars, "@@rbdcv_cvar", 1) o.rbdcv_cvar.should == 1 CApiClassSpecs::CVars.remove_class_variable :@@rbdcv_cvar @@ -439,23 +443,23 @@ describe "C-API Class function" do it "raises a NameError if the class variable is not defined" do -> { @s.rb_cvar_get(CApiClassSpecs::CVars, "@@no_cvar") - }.should raise_error(NameError, /class variable @@no_cvar/) + }.should.raise(NameError, /class variable @@no_cvar/) end end describe "rb_class_new" do it "returns a new subclass of the superclass" do subclass = @s.rb_class_new(CApiClassSpecs::NewClass) - CApiClassSpecs::NewClass.should be_ancestor_of(subclass) + subclass.ancestors.should.include?(CApiClassSpecs::NewClass) end it "raises a TypeError if passed Class as the superclass" do - -> { @s.rb_class_new(Class) }.should raise_error(TypeError) + -> { @s.rb_class_new(Class) }.should.raise(TypeError) end it "raises a TypeError if passed a singleton class as the superclass" do metaclass = Object.new.singleton_class - -> { @s.rb_class_new(metaclass) }.should raise_error(TypeError) + -> { @s.rb_class_new(metaclass) }.should.raise(TypeError) end end @@ -466,7 +470,7 @@ describe "C-API Class function" do end it "returns nil if the class has no superclass" do - @s.rb_class_superclass(BasicObject).should be_nil + @s.rb_class_superclass(BasicObject).should == nil end end diff --git a/spec/ruby/optional/capi/constants_spec.rb b/spec/ruby/optional/capi/constants_spec.rb index 172d10a788..74584690ad 100644 --- a/spec/ruby/optional/capi/constants_spec.rb +++ b/spec/ruby/optional/capi/constants_spec.rb @@ -199,7 +199,7 @@ describe "C-API exception constant" do specify "rb_eFatal references the fatal class" do fatal = @s.rb_eFatal - fatal.should be_kind_of(Class) + fatal.should.is_a?(Class) fatal.should < Exception end diff --git a/spec/ruby/optional/capi/data_spec.rb b/spec/ruby/optional/capi/data_spec.rb index d000eba6e3..af1da10f48 100644 --- a/spec/ruby/optional/capi/data_spec.rb +++ b/spec/ruby/optional/capi/data_spec.rb @@ -32,7 +32,7 @@ ruby_version_is ""..."3.4" do end it "raises a TypeError if the object does not wrap a struct" do - -> { @s.get_struct(Object.new) }.should raise_error(TypeError) + -> { @s.get_struct(Object.new) }.should.raise(TypeError) end end diff --git a/spec/ruby/optional/capi/debug_spec.rb b/spec/ruby/optional/capi/debug_spec.rb index 14ba25609c..547b24cfcc 100644 --- a/spec/ruby/optional/capi/debug_spec.rb +++ b/spec/ruby/optional/capi/debug_spec.rb @@ -9,7 +9,7 @@ describe "C-API Debug function" do 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.rb_debug_inspector_open(42).should.is_a?(Array) @o.debug_spec_callback_data.should == 42 end end @@ -31,7 +31,7 @@ describe "C-API Debug function" 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.should.instance_of?(Binding) b.local_variable_get(:a).should == "test" end @@ -55,7 +55,7 @@ describe "C-API Debug function" do 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) + @o.rb_debug_inspector_frame_iseq_get(1).should.instance_of?(RubyVM::InstructionSequence) else @o.rb_debug_inspector_frame_iseq_get(1).should == nil end @@ -66,9 +66,9 @@ describe "C-API Debug function" 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) } + bts.each { |bt| bt.should.is_a?(Thread::Backtrace::Location) } location = "#{__FILE__}:#{__LINE__ - 3}" - bts[1].to_s.should include(location) + bts[1].to_s.should.include?(location) end end end diff --git a/spec/ruby/optional/capi/digest_spec.rb b/spec/ruby/optional/capi/digest_spec.rb new file mode 100644 index 0000000000..65c5ecebb1 --- /dev/null +++ b/spec/ruby/optional/capi/digest_spec.rb @@ -0,0 +1,103 @@ +require_relative 'spec_helper' + +begin + require 'fiddle' +rescue LoadError + return +end + +load_extension('digest') + +describe "C-API Digest functions" do + before :each do + @s = CApiDigestSpecs.new + end + + describe "rb_digest_make_metadata" do + before :each do + @metadata = @s.rb_digest_make_metadata + end + + it "should store the block length" do + @s.block_length(@metadata).should == 40 + end + + it "should store the digest length" do + @s.digest_length(@metadata).should == 20 + end + + it "should store the context size" do + @s.context_size(@metadata).should == 129 + end + end + + describe "digest plugin" do + before :each do + @s = CApiDigestSpecs.new + @digest = Digest::TestDigest.new + + # A pointer to the CTX type defined in the extension for this spec. Digest does not make the context directly + # accessible as part of its API. However, to ensure we are properly loading the plugin, it's useful to have + # direct access to the context pointer to verify its contents. + @context = Fiddle::Pointer.new(@s.context(@digest)) + end + + it "should report the block length" do + @digest.block_length.should == 40 + end + + it "should report the digest length" do + @digest.digest_length.should == 20 + end + + it "should initialize the context" do + # Our test plugin always writes the string "Initialized\n" when its init function is called. + verify_context("Initialized\n") + end + + it "should update the digest" do + @digest.update("hello world") + + # Our test plugin always writes the string "Updated: <data>\n" when its update function is called. + current = "Initialized\nUpdated: hello world" + verify_context(current) + + @digest << "blah" + + current = "Initialized\nUpdated: hello worldUpdated: blah" + verify_context(current) + end + + it "should finalize the digest" do + @digest.update("") + + finish_string = @digest.instance_eval { finish } + + # We expect the plugin to write out the last `@digest.digest_length` bytes, followed by the string "Finished\n". + # + finish_string.should == "d\nUpdated: Finished\n" + finish_string.encoding.should == Encoding::ASCII_8BIT + end + + it "should reset the context" do + @digest.update("foo") + verify_context("Initialized\nUpdated: foo") + + @digest.reset + + # The context will be recreated as a result of the `reset` so we must fetch the latest context pointer. + @context = Fiddle::Pointer.new(@s.context(@digest)) + + verify_context("Initialized\n") + end + + def verify_context(current_body) + # In the CTX type, the length of the current context contents is stored in the first byte. + byte_count = @context[0] + byte_count.should == current_body.bytesize + + # After the size byte follows a string. + @context[1, byte_count].should == current_body + end + end +end diff --git a/spec/ruby/optional/capi/encoding_spec.rb b/spec/ruby/optional/capi/encoding_spec.rb index 1529e012b0..b77a967b1e 100644 --- a/spec/ruby/optional/capi/encoding_spec.rb +++ b/spec/ruby/optional/capi/encoding_spec.rb @@ -36,7 +36,7 @@ describe :rb_enc_set_index, shared: true do obj = Object.new -> { result = @s.send(@method, obj, 1) - }.should raise_error(ArgumentError, "cannot set encoding on non-encoding capable object") + }.should.raise(ArgumentError, "cannot set encoding on non-encoding capable object") end end @@ -165,13 +165,13 @@ describe "C-API Encoding function" do describe "rb_enc_from_index" do it "returns an Encoding" do - @s.rb_enc_from_index(0).should be_an_instance_of(String) + @s.rb_enc_from_index(0).should.instance_of?(String) end end describe "rb_enc_mbc_to_codepoint" do it "returns the correct codepoint for the given character and size" do - @s.rb_enc_mbc_to_codepoint("é").should == 0xE9 + @s.rb_enc_mbc_to_codepoint("é").should == 0xE9 end it "returns 0 if p == e" do @@ -298,7 +298,7 @@ describe "C-API Encoding function" do it "returns a String in US-ASCII encoding when high bits are set" do xEE = [0xEE].pack('C').force_encoding('utf-8') result = @s.rb_enc_str_new(xEE, 1, Encoding::US_ASCII) - result.encoding.should equal(Encoding::US_ASCII) + result.encoding.should.equal?(Encoding::US_ASCII) end end @@ -389,12 +389,12 @@ describe "C-API Encoding function" do it "returns true if the object encoding is only ASCII" do str = "abc".force_encoding("us-ascii") str.valid_encoding? # make sure to set the coderange - @s.ENC_CODERANGE_ASCIIONLY(str).should be_true + @s.ENC_CODERANGE_ASCIIONLY(str).should == true end it "returns false if the object encoding is not ASCII only" do str = "ありがとう".force_encoding("utf-8") - @s.ENC_CODERANGE_ASCIIONLY(str).should be_false + @s.ENC_CODERANGE_ASCIIONLY(str).should == false end end @@ -421,7 +421,7 @@ describe "C-API Encoding function" do describe "when the rb_encoding struct is stored in native memory" do it "can still read the name of the encoding" do address = @s.rb_to_encoding_native_store(Encoding::UTF_8) - address.should be_kind_of(Integer) + address.should.is_a?(Integer) @s.rb_to_encoding_native_name(address).should == "UTF-8" end end @@ -452,7 +452,7 @@ describe "C-API Encoding function" do describe "rb_enc_compatible" do it "returns 0 if the encodings of the Strings are not compatible" do a = [0xff].pack('C').force_encoding "binary" - b = "\u3042".encode("utf-8") + b = "あ" @s.rb_enc_compatible(a, b).should == 0 end @@ -461,11 +461,25 @@ describe "C-API Encoding function" do # Encoding.compatible? it "returns the same value as Encoding.compatible? if the Strings have a compatible encoding" do a = "abc".force_encoding("us-ascii") - b = "\u3042".encode("utf-8") + b = "あ" @s.rb_enc_compatible(a, b).should == Encoding.compatible?(a, b) end end + describe "rb_enc_check" do + it "returns the compatible encoding of the two Strings" do + a = "abc".force_encoding("us-ascii") + b = "あ" + @s.rb_enc_check(a, b).should == Encoding::UTF_8 + end + + it "raises Encoding::CompatibilityError if the encodings are not compatible" do + a = [0xff].pack('C').b + b = "あ" + -> { @s.rb_enc_check(a, b) }.should.raise(Encoding::CompatibilityError) + end + end + describe "rb_enc_copy" do before :each do @obj = "rb_enc_copy".encode(Encoding::US_ASCII) @@ -475,12 +489,20 @@ describe "C-API Encoding function" do @s.rb_enc_copy("string", @obj).encoding.should == Encoding::US_ASCII end - it "raises a RuntimeError if the second argument is a Symbol" do - -> { @s.rb_enc_copy(:symbol, @obj) }.should raise_error(RuntimeError) + it "raises a RuntimeError if the first argument is a Symbol" do + -> { @s.rb_enc_copy(:symbol, @obj) }.should.raise(RuntimeError) end - it "sets the encoding of a Regexp to that of the second argument" do - @s.rb_enc_copy(/regexp/.dup, @obj).encoding.should == Encoding::US_ASCII + ruby_version_is "4.1" do + it "raises a FrozenError if the first argument is a Regexp" do + -> { @s.rb_enc_copy(/regexp/.dup, @obj) }.should.raise(FrozenError) + end + end + + ruby_version_is ""..."4.1" do + it "sets the encoding of a Regexp to that of the second argument" do + @s.rb_enc_copy(/regexp/.dup, @obj).encoding.should == Encoding::US_ASCII + end end end @@ -495,7 +517,7 @@ describe "C-API Encoding function" do it "returns 0 if Encoding.default_internal is nil" do Encoding.default_internal = nil - @s.rb_default_internal_encoding.should be_nil + @s.rb_default_internal_encoding.should == nil end it "returns the encoding for Encoding.default_internal" do @@ -527,11 +549,19 @@ describe "C-API Encoding function" do end it "raises a RuntimeError if the argument is Symbol" do - -> { @s.rb_enc_associate(:symbol, "US-ASCII") }.should raise_error(RuntimeError) + -> { @s.rb_enc_associate(:symbol, "US-ASCII") }.should.raise(RuntimeError) + end + + ruby_version_is "4.1" do + it "raises a FrozenError if the argument is a Regexp" do + -> { @s.rb_enc_associate(/regexp/.dup, "BINARY") }.should.raise(FrozenError) + end end - it "sets the encoding of a Regexp to the encoding" do - @s.rb_enc_associate(/regexp/.dup, "BINARY").encoding.should == Encoding::BINARY + ruby_version_is ""..."4.1" do + it "sets the encoding of a Regexp to the encoding" do + @s.rb_enc_associate(/regexp/.dup, "BINARY").encoding.should == Encoding::BINARY + end end it "sets the encoding of a String to a default when the encoding is NULL" do @@ -546,15 +576,24 @@ describe "C-API Encoding function" do enc.should == Encoding::BINARY end - it "sets the encoding of a Regexp to the encoding" do - index = @s.rb_enc_find_index("UTF-8") - enc = @s.rb_enc_associate_index(/regexp/.dup, index).encoding - enc.should == Encoding::UTF_8 + ruby_version_is "4.1" do + it "raises a FrozenError if the argument is a Regexp" do + index = @s.rb_enc_find_index("UTF-8") + -> { @s.rb_enc_associate_index(/regexp/.dup, index) }.should.raise(FrozenError) + end + end + + ruby_version_is ""..."4.1" do + it "sets the encoding of a Regexp to the encoding" do + index = @s.rb_enc_find_index("UTF-8") + enc = @s.rb_enc_associate_index(/regexp/.dup, index).encoding + enc.should == Encoding::UTF_8 + end end it "sets the encoding of a Symbol to the encoding" do index = @s.rb_enc_find_index("UTF-8") - -> { @s.rb_enc_associate_index(:symbol, index) }.should raise_error(RuntimeError) + -> { @s.rb_enc_associate_index(:symbol, index) }.should.raise(RuntimeError) end end @@ -610,13 +649,13 @@ describe "C-API Encoding function" do it "raises ArgumentError if an empty string is given" do -> do @s.rb_enc_codepoint_len("") - end.should raise_error(ArgumentError) + end.should.raise(ArgumentError) end it "raises ArgumentError if an invalid byte sequence is given" do -> do @s.rb_enc_codepoint_len([0xa0, 0xa1].pack('CC').force_encoding('utf-8')) # Invalid sequence identifier - end.should raise_error(ArgumentError) + end.should.raise(ArgumentError) end it "returns codepoint 0x24 and length 1 for character '$'" do @@ -650,11 +689,11 @@ describe "C-API Encoding function" do describe "rb_enc_str_asciionly_p" do it "returns true for an ASCII string" do - @s.rb_enc_str_asciionly_p("hello").should be_true + @s.rb_enc_str_asciionly_p("hello").should == true end it "returns false for a non-ASCII string" do - @s.rb_enc_str_asciionly_p("hüllo").should be_false + @s.rb_enc_str_asciionly_p("hüllo").should == false end end @@ -664,7 +703,7 @@ describe "C-API Encoding function" do -> { @s.rb_enc_raise(Encoding::UTF_8, RuntimeError, utf_8_incompatible_string) - }.should raise_error { |e| + }.should.raise { |e| e.message.encoding.should == Encoding::UTF_8 e.message.valid_encoding?.should == false e.message.bytes.should == utf_8_incompatible_string.bytes @@ -724,25 +763,55 @@ describe "C-API Encoding function" do end describe "rb_define_dummy_encoding" do + run = 0 + it "defines the dummy encoding" do - @s.rb_define_dummy_encoding("FOO") - enc = Encoding.find("FOO") + @s.rb_define_dummy_encoding("FOO#{run += 1}") + enc = Encoding.find("FOO#{run}") enc.should.dummy? end it "returns the index of the dummy encoding" do - index = @s.rb_define_dummy_encoding("BAR") + index = @s.rb_define_dummy_encoding("BAR#{run += 1}") index.should == Encoding.list.size - 1 end - ruby_version_is "3.2" do - it "raises EncodingError if too many encodings" do - code = <<-RUBY - require #{extension_path.dump} - 1_000.times {|i| CApiEncodingSpecs.new.rb_define_dummy_encoding("R_\#{i}") } - RUBY - ruby_exe(code, args: "2>&1", exit_status: 1).should.include?('too many encoding (> 256) (EncodingError)') + it "raises EncodingError if too many encodings" do + code = <<-RUBY + require #{extension_path.dump} + 1_000.times {|i| CApiEncodingSpecs.new.rb_define_dummy_encoding("R_\#{i}") } + RUBY + ruby_exe(code, args: "2>&1", exit_status: 1).should.include?('too many encoding (> 256) (EncodingError)') + end + end + + describe "ONIGENC_IS_UNICODE" do + it "is true only for select UTF-related encodings" do + unicode = [ + Encoding::UTF_8, + Encoding::UTF8_DOCOMO, + Encoding::UTF8_KDDI, + Encoding::UTF8_MAC, + Encoding::UTF8_SOFTBANK, + Encoding::CESU_8, + Encoding::UTF_16LE, + Encoding::UTF_16BE, + Encoding::UTF_32LE, + Encoding::UTF_32BE + ] + unicode.each do |enc| + @s.should.ONIGENC_IS_UNICODE(enc) end + + (Encoding.list - unicode).each { |enc| + @s.should_not.ONIGENC_IS_UNICODE(enc) + } + end + + # Redundant with the above but more explicit + it "is false for the dummy UTF-16 and UTF-32 encodings" do + @s.should_not.ONIGENC_IS_UNICODE(Encoding::UTF_16) + @s.should_not.ONIGENC_IS_UNICODE(Encoding::UTF_32) end end end diff --git a/spec/ruby/optional/capi/exception_spec.rb b/spec/ruby/optional/capi/exception_spec.rb index 5bb60608b2..429da093d1 100644 --- a/spec/ruby/optional/capi/exception_spec.rb +++ b/spec/ruby/optional/capi/exception_spec.rb @@ -28,13 +28,13 @@ describe "C-API Exception function" do describe "rb_exc_raise" do it "raises passed exception" do runtime_error = RuntimeError.new '42' - -> { @s.rb_exc_raise(runtime_error) }.should raise_error(RuntimeError, '42') + -> { @s.rb_exc_raise(runtime_error) }.should.raise(RuntimeError, '42') end it "raises an exception with an empty backtrace" do runtime_error = RuntimeError.new '42' runtime_error.set_backtrace [] - -> { @s.rb_exc_raise(runtime_error) }.should raise_error(RuntimeError, '42') + -> { @s.rb_exc_raise(runtime_error) }.should.raise(RuntimeError, '42') end it "sets $! to the raised exception when not rescuing from an another exception" do @@ -88,15 +88,35 @@ describe "C-API Exception function" do end it "accepts nil" do - @s.rb_set_errinfo(nil).should be_nil + @s.rb_set_errinfo(nil).should == nil end it "accepts an Exception instance" do - @s.rb_set_errinfo(Exception.new).should be_nil + @s.rb_set_errinfo(Exception.new).should == nil end it "raises a TypeError if the object is not nil or an Exception instance" do - -> { @s.rb_set_errinfo("error") }.should raise_error(TypeError) + -> { @s.rb_set_errinfo("error") }.should.raise(TypeError) + end + end + + describe "rb_error_frozen_object" do + it "raises a FrozenError regardless of the object's frozen state" do + # The type of the argument we supply doesn't matter. The choice here is arbitrary and we only change the type + # of the argument to ensure the exception messages are set correctly. + -> { @s.rb_error_frozen_object(Array.new) }.should.raise(FrozenError, "can't modify frozen Array: []") + -> { @s.rb_error_frozen_object(Array.new.freeze) }.should.raise(FrozenError, "can't modify frozen Array: []") + end + + it "properly handles recursive rb_error_frozen_object calls" do + klass = Class.new(Object) + object = klass.new + s = @s + klass.define_method :inspect do + s.rb_error_frozen_object(object) + end + + -> { @s.rb_error_frozen_object(object) }.should.raise(FrozenError, "can't modify frozen #{klass}: ...") end end @@ -104,14 +124,14 @@ describe "C-API Exception function" do it "returns system error with default message when passed message is NULL" do exception = @s.rb_syserr_new(Errno::ENOENT::Errno, nil) exception.class.should == Errno::ENOENT - exception.message.should include("No such file or directory") + exception.message.should.include?("No such file or directory") exception.should.is_a?(SystemCallError) end it "returns system error with custom message" do exception = @s.rb_syserr_new(Errno::ENOENT::Errno, "custom message") - exception.message.should include("custom message") + exception.message.should.include?("custom message") exception.class.should == Errno::ENOENT exception.should.is_a?(SystemCallError) end @@ -121,14 +141,14 @@ describe "C-API Exception function" do it "returns system error with default message when passed message is nil" do exception = @s.rb_syserr_new_str(Errno::ENOENT::Errno, nil) - exception.message.should include("No such file or directory") + exception.message.should.include?("No such file or directory") exception.class.should == Errno::ENOENT exception.should.is_a?(SystemCallError) end it "returns system error with custom message" do exception = @s.rb_syserr_new_str(Errno::ENOENT::Errno, "custom message") - exception.message.should include("custom message") + exception.message.should.include?("custom message") exception.class.should == Errno::ENOENT exception.should.is_a?(SystemCallError) end @@ -161,17 +181,17 @@ describe "C-API Exception function" do 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) + -> { @s.rb_make_exception([nil]) }.should.raise(TypeError) + -> { @s.rb_make_exception([Object.new]) }.should.raise(TypeError) obj = Object.new def obj.exception "not exception type" end - -> { @s.rb_make_exception([obj]) }.should raise_error(TypeError) + -> { @s.rb_make_exception([obj]) }.should.raise(TypeError) end it "raises an ArgumentError for too many arguments" do - -> { @s.rb_make_exception([Exception, "Message", ["backtrace 1"], "extra"]) }.should raise_error(ArgumentError) + -> { @s.rb_make_exception([Exception, "Message", ["backtrace 1"], "extra"]) }.should.raise(ArgumentError) end it "returns nil for empty arguments" do diff --git a/spec/ruby/optional/capi/ext/array_spec.c b/spec/ruby/optional/capi/ext/array_spec.c index 2347798bb4..628c4df9d7 100644 --- a/spec/ruby/optional/capi/ext/array_spec.c +++ b/spec/ruby/optional/capi/ext/array_spec.c @@ -196,6 +196,7 @@ static VALUE copy_ary(RB_BLOCK_CALL_FUNC_ARGLIST(el, new_ary)) { return rb_ary_push(new_ary, el); } +#ifndef RUBY_VERSION_IS_4_0 static VALUE array_spec_rb_iterate(VALUE self, VALUE ary) { VALUE new_ary = rb_ary_new(); @@ -203,6 +204,7 @@ static VALUE array_spec_rb_iterate(VALUE self, VALUE ary) { return new_ary; } +#endif static VALUE array_spec_rb_block_call(VALUE self, VALUE ary) { VALUE new_ary = rb_ary_new(); @@ -216,6 +218,7 @@ static VALUE sub_pair(RB_BLOCK_CALL_FUNC_ARGLIST(el, holder)) { return rb_ary_push(holder, rb_ary_entry(el, 1)); } +#ifndef RUBY_VERSION_IS_4_0 static VALUE each_pair(VALUE obj) { return rb_funcall(obj, rb_intern("each_pair"), 0); } @@ -227,6 +230,7 @@ static VALUE array_spec_rb_iterate_each_pair(VALUE self, VALUE obj) { return new_ary; } +#endif static VALUE array_spec_rb_block_call_each_pair(VALUE self, VALUE obj) { VALUE new_ary = rb_ary_new(); @@ -241,10 +245,12 @@ static VALUE iter_yield(RB_BLOCK_CALL_FUNC_ARGLIST(el, ary)) { return Qnil; } +#ifndef RUBY_VERSION_IS_4_0 static VALUE array_spec_rb_iterate_then_yield(VALUE self, VALUE obj) { rb_iterate(rb_each, obj, iter_yield, obj); return Qnil; } +#endif static VALUE array_spec_rb_block_call_then_yield(VALUE self, VALUE obj) { rb_block_call(obj, rb_intern("each"), 0, 0, iter_yield, obj); @@ -308,9 +314,11 @@ void Init_array_spec(void) { rb_define_method(cls, "rb_ary_plus", array_spec_rb_ary_plus, 2); rb_define_method(cls, "rb_ary_unshift", array_spec_rb_ary_unshift, 2); rb_define_method(cls, "rb_assoc_new", array_spec_rb_assoc_new, 2); +#ifndef RUBY_VERSION_IS_4_0 rb_define_method(cls, "rb_iterate", array_spec_rb_iterate, 1); rb_define_method(cls, "rb_iterate_each_pair", array_spec_rb_iterate_each_pair, 1); rb_define_method(cls, "rb_iterate_then_yield", array_spec_rb_iterate_then_yield, 1); +#endif rb_define_method(cls, "rb_block_call", array_spec_rb_block_call, 1); rb_define_method(cls, "rb_block_call_each_pair", array_spec_rb_block_call_each_pair, 1); rb_define_method(cls, "rb_block_call_then_yield", array_spec_rb_block_call_then_yield, 1); diff --git a/spec/ruby/optional/capi/ext/class_spec.c b/spec/ruby/optional/capi/ext/class_spec.c index c13f02ecf2..8ac0e7a93f 100644 --- a/spec/ruby/optional/capi/ext/class_spec.c +++ b/spec/ruby/optional/capi/ext/class_spec.c @@ -65,11 +65,9 @@ static VALUE class_spec_rb_class_new_instance(VALUE self, VALUE args, VALUE klas return rb_class_new_instance(RARRAY_LENINT(args), RARRAY_PTR(args), klass); } -#ifdef RUBY_VERSION_IS_3_0 static VALUE class_spec_rb_class_new_instance_kw(VALUE self, VALUE args, VALUE klass) { return rb_class_new_instance_kw(RARRAY_LENINT(args), RARRAY_PTR(args), klass, RB_PASS_KEYWORDS); } -#endif static VALUE class_spec_rb_class_real(VALUE self, VALUE object) { if (rb_type_p(object, T_FIXNUM)) { @@ -160,9 +158,7 @@ void Init_class_spec(void) { rb_define_method(cls, "rb_class_private_instance_methods", class_spec_rb_class_private_instance_methods, -1); rb_define_method(cls, "rb_class_new", class_spec_rb_class_new, 1); rb_define_method(cls, "rb_class_new_instance", class_spec_rb_class_new_instance, 2); -#ifdef RUBY_VERSION_IS_3_0 rb_define_method(cls, "rb_class_new_instance_kw", class_spec_rb_class_new_instance_kw, 2); -#endif rb_define_method(cls, "rb_class_real", class_spec_rb_class_real, 1); rb_define_method(cls, "rb_class_get_superclass", class_spec_rb_class_get_superclass, 1); rb_define_method(cls, "rb_class_superclass", class_spec_rb_class_superclass, 1); diff --git a/spec/ruby/optional/capi/ext/constants_spec.c b/spec/ruby/optional/capi/ext/constants_spec.c index 9aee8db37f..05819ea476 100644 --- a/spec/ruby/optional/capi/ext/constants_spec.c +++ b/spec/ruby/optional/capi/ext/constants_spec.c @@ -14,9 +14,6 @@ defconstfunc(rb_cBinding) defconstfunc(rb_cClass) defconstfunc(rb_cComplex) defconstfunc(rb_mComparable) -#ifndef RUBY_VERSION_IS_3_0 -defconstfunc(rb_cData) -#endif defconstfunc(rb_cDir) defconstfunc(rb_cEncoding) defconstfunc(rb_mEnumerable) @@ -97,9 +94,6 @@ void Init_constants_spec(void) { rb_define_method(cls, "rb_cClass", constants_spec_rb_cClass, 0); rb_define_method(cls, "rb_cComplex", constants_spec_rb_cComplex, 0); rb_define_method(cls, "rb_mComparable", constants_spec_rb_mComparable, 0); - #ifndef RUBY_VERSION_IS_3_0 - rb_define_method(cls, "rb_cData", constants_spec_rb_cData, 0); - #endif rb_define_method(cls, "rb_cDir", constants_spec_rb_cDir, 0); rb_define_method(cls, "rb_cEncoding", constants_spec_rb_cEncoding, 0); rb_define_method(cls, "rb_mEnumerable", constants_spec_rb_mEnumerable, 0); diff --git a/spec/ruby/optional/capi/ext/digest_spec.c b/spec/ruby/optional/capi/ext/digest_spec.c new file mode 100644 index 0000000000..65c8defa20 --- /dev/null +++ b/spec/ruby/optional/capi/ext/digest_spec.c @@ -0,0 +1,168 @@ +#include "ruby.h" +#include "rubyspec.h" + +#include "ruby/digest.h" + +#ifdef __cplusplus +extern "C" { +#endif + +#define DIGEST_LENGTH 20 +#define BLOCK_LENGTH 40 + +const char *init_string = "Initialized\n"; +const char *update_string = "Updated: "; +const char *finish_string = "Finished\n"; + +#define PAYLOAD_SIZE 128 + +typedef struct CTX { + uint8_t pos; + char payload[PAYLOAD_SIZE]; +} CTX; + +void* context = NULL; + +int digest_spec_plugin_init(void *raw_ctx) { + // Make the context accessible to tests. This isn't safe, but there's no way to access the context otherwise. + context = raw_ctx; + + struct CTX *ctx = (struct CTX *)raw_ctx; + size_t len = strlen(init_string); + + // Clear the payload since this init function will be invoked as part of the `reset` operation. + memset(ctx->payload, 0, PAYLOAD_SIZE); + + // Write a simple value we can verify in tests. + // This is not what a real digest would do, but we're using a dummy digest plugin to test interactions. + memcpy(ctx->payload, init_string, len); + ctx->pos = (uint8_t) len; + + return 1; +} + +void digest_spec_plugin_update(void *raw_ctx, unsigned char *ptr, size_t size) { + struct CTX *ctx = (struct CTX *)raw_ctx; + size_t update_str_len = strlen(update_string); + + if (ctx->pos + update_str_len + size >= PAYLOAD_SIZE) { + rb_raise(rb_eRuntimeError, "update size too large; reset the digest and write fewer updates"); + } + + // Write the supplied value to the payload so it can be easily verified in test. + // This is not what a real digest would do, but we're using a dummy digest plugin to test interactions. + memcpy(ctx->payload + ctx->pos, update_string, update_str_len); + ctx->pos += update_str_len; + + memcpy(ctx->payload + ctx->pos, ptr, size); + ctx->pos += size; + + return; +} + +int digest_spec_plugin_finish(void *raw_ctx, unsigned char *ptr) { + struct CTX *ctx = (struct CTX *)raw_ctx; + size_t finish_string_len = strlen(finish_string); + + // We're always going to write DIGEST_LENGTH bytes. In a real plugin, this would be the digest value. Here we + // write out a text string in order to make validation in tests easier. + // + // In order to delineate the output more clearly from an `Digest#update` call, we always write out the + // `finish_string` message. That leaves `DIGEST_LENGTH - finish_string_len` bytes to read out of the context. + size_t context_bytes = DIGEST_LENGTH - finish_string_len; + + memcpy(ptr, ctx->payload + (ctx->pos - context_bytes), context_bytes); + memcpy(ptr + context_bytes, finish_string, finish_string_len); + + return 1; +} + +static const rb_digest_metadata_t metadata = { + // The RUBY_DIGEST_API_VERSION value comes from ruby/digest.h and may vary based on the Ruby being tested. Since + // it isn't publicly exposed in the digest gem, we ignore for these tests. Either the test hard-codes an expected + // value and is subject to breaking depending on the Ruby being run or we publicly expose `RUBY_DIGEST_API_VERSION`, + // in which case the test would pass trivially. + RUBY_DIGEST_API_VERSION, + DIGEST_LENGTH, + BLOCK_LENGTH, + sizeof(CTX), + (rb_digest_hash_init_func_t) digest_spec_plugin_init, + (rb_digest_hash_update_func_t) digest_spec_plugin_update, + (rb_digest_hash_finish_func_t) digest_spec_plugin_finish, +}; + +// The `get_metadata_ptr` function is not publicly available in the digest gem. However, we need to use +// to extract the `rb_digest_metadata_t*` value set up by the plugin so we reproduce and adjust the +// definition here. +// +// Taken and adapted from https://github.com/ruby/digest/blob/v3.2.0/ext/digest/digest.c#L558-L568 +static rb_digest_metadata_t * get_metadata_ptr(VALUE obj) { + rb_digest_metadata_t *algo; + +#ifdef DIGEST_USE_RB_EXT_RESOLVE_SYMBOL + // In the digest gem there is an additional data type check performed before reading the value out. + // Since the type definition isn't public, we can't use it as part of a type check here so we omit it. + // This is safe to do because this code is intended to only load digest plugins written as part of this test suite. + algo = (rb_digest_metadata_t *) RTYPEDDATA_DATA(obj); +#else +# undef RUBY_UNTYPED_DATA_WARNING +# define RUBY_UNTYPED_DATA_WARNING 0 + Data_Get_Struct(obj, rb_digest_metadata_t, algo); +#endif + + return algo; +} + +VALUE digest_spec_rb_digest_make_metadata(VALUE self) { + return rb_digest_make_metadata(&metadata); +} + +VALUE digest_spec_block_length(VALUE self, VALUE meta) { + rb_digest_metadata_t* algo = get_metadata_ptr(meta); + + return SIZET2NUM(algo->block_len); +} + +VALUE digest_spec_digest_length(VALUE self, VALUE meta) { + rb_digest_metadata_t* algo = get_metadata_ptr(meta); + + return SIZET2NUM(algo->digest_len); +} + +VALUE digest_spec_context_size(VALUE self, VALUE meta) { + rb_digest_metadata_t* algo = get_metadata_ptr(meta); + + return SIZET2NUM(algo->ctx_size); +} + +#ifndef PTR2NUM +#define PTR2NUM(x) (rb_int2inum((intptr_t)(void *)(x))) +#endif + +VALUE digest_spec_context(VALUE self, VALUE digest) { + return PTR2NUM(context); +} + +void Init_digest_spec(void) { + VALUE cls; + + cls = rb_define_class("CApiDigestSpecs", rb_cObject); + rb_define_method(cls, "rb_digest_make_metadata", digest_spec_rb_digest_make_metadata, 0); + rb_define_method(cls, "block_length", digest_spec_block_length, 1); + rb_define_method(cls, "digest_length", digest_spec_digest_length, 1); + rb_define_method(cls, "context_size", digest_spec_context_size, 1); + rb_define_method(cls, "context", digest_spec_context, 1); + + VALUE mDigest, cDigest_Base, cDigest; + + mDigest = rb_define_module("Digest"); + mDigest = rb_digest_namespace(); + cDigest_Base = rb_const_get(mDigest, rb_intern_const("Base")); + + cDigest = rb_define_class_under(mDigest, "TestDigest", cDigest_Base); + rb_iv_set(cDigest, "metadata", rb_digest_make_metadata(&metadata)); +} + +#ifdef __cplusplus +} +#endif diff --git a/spec/ruby/optional/capi/ext/encoding_spec.c b/spec/ruby/optional/capi/ext/encoding_spec.c index aa8662cfbd..2038a5d4a8 100644 --- a/spec/ruby/optional/capi/ext/encoding_spec.c +++ b/spec/ruby/optional/capi/ext/encoding_spec.c @@ -91,6 +91,11 @@ static VALUE encoding_spec_rb_enc_compatible(VALUE self, VALUE a, VALUE b) { return rb_enc_from_encoding(enc); } +static VALUE encoding_spec_rb_enc_check(VALUE self, VALUE a, VALUE b) { + rb_encoding* enc = rb_enc_check(a, b); + return rb_enc_from_encoding(enc); +} + static VALUE encoding_spec_rb_enc_copy(VALUE self, VALUE dest, VALUE src) { rb_enc_copy(dest, src); return dest; @@ -324,6 +329,10 @@ static VALUE encoding_spec_rb_define_dummy_encoding(VALUE self, VALUE name) { return INT2NUM(rb_define_dummy_encoding(RSTRING_PTR(name))); } +static VALUE encoding_spec_ONIGENC_IS_UNICODE(VALUE self, VALUE encoding) { + return ONIGENC_IS_UNICODE(rb_to_encoding(encoding)) ? Qtrue : Qfalse; +} + void Init_encoding_spec(void) { VALUE cls; native_rb_encoding_pointer = (rb_encoding**) malloc(sizeof(rb_encoding*)); @@ -349,6 +358,7 @@ void Init_encoding_spec(void) { rb_define_method(cls, "rb_enc_associate", encoding_spec_rb_enc_associate, 2); rb_define_method(cls, "rb_enc_associate_index", encoding_spec_rb_enc_associate_index, 2); rb_define_method(cls, "rb_enc_compatible", encoding_spec_rb_enc_compatible, 2); + rb_define_method(cls, "rb_enc_check", encoding_spec_rb_enc_check, 2); rb_define_method(cls, "rb_enc_copy", encoding_spec_rb_enc_copy, 2); rb_define_method(cls, "rb_enc_codelen", encoding_spec_rb_enc_codelen, 2); rb_define_method(cls, "rb_enc_strlen", encoding_spec_rb_enc_strlen, 3); @@ -384,6 +394,7 @@ void Init_encoding_spec(void) { rb_define_method(cls, "ONIGENC_MBC_CASE_FOLD", encoding_spec_ONIGENC_MBC_CASE_FOLD, 1); rb_define_method(cls, "rb_enc_left_char_head", encoding_spec_rb_enc_left_char_head, 2); rb_define_method(cls, "rb_define_dummy_encoding", encoding_spec_rb_define_dummy_encoding, 1); + rb_define_method(cls, "ONIGENC_IS_UNICODE", encoding_spec_ONIGENC_IS_UNICODE, 1); } #ifdef __cplusplus diff --git a/spec/ruby/optional/capi/ext/exception_spec.c b/spec/ruby/optional/capi/ext/exception_spec.c index 0e8347ab0d..c3b94d7bcd 100644 --- a/spec/ruby/optional/capi/ext/exception_spec.c +++ b/spec/ruby/optional/capi/ext/exception_spec.c @@ -36,6 +36,13 @@ VALUE exception_spec_rb_set_errinfo(VALUE self, VALUE exc) { return Qnil; } +NORETURN(VALUE exception_spec_rb_error_frozen_object(VALUE self, VALUE object)); + +VALUE exception_spec_rb_error_frozen_object(VALUE self, VALUE object) { + rb_error_frozen_object(object); + UNREACHABLE_RETURN(Qnil); +} + VALUE exception_spec_rb_syserr_new(VALUE self, VALUE num, VALUE msg) { int n = NUM2INT(num); char *cstr = NULL; @@ -66,6 +73,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_error_frozen_object", exception_spec_rb_error_frozen_object, 1); rb_define_method(cls, "rb_syserr_new", exception_spec_rb_syserr_new, 2); rb_define_method(cls, "rb_syserr_new_str", exception_spec_rb_syserr_new_str, 2); rb_define_method(cls, "rb_make_exception", exception_spec_rb_make_exception, 1); diff --git a/spec/ruby/optional/capi/ext/fiber_spec.c b/spec/ruby/optional/capi/ext/fiber_spec.c index f06a54494e..db54f7ad8c 100644 --- a/spec/ruby/optional/capi/ext/fiber_spec.c +++ b/spec/ruby/optional/capi/ext/fiber_spec.c @@ -44,12 +44,10 @@ VALUE fiber_spec_rb_fiber_new(VALUE self) { return rb_fiber_new(fiber_spec_rb_fiber_new_function, Qnil); } -#ifdef RUBY_VERSION_IS_3_1 VALUE fiber_spec_rb_fiber_raise(int argc, VALUE *argv, VALUE self) { VALUE fiber = argv[0]; return rb_fiber_raise(fiber, argc-1, argv+1); } -#endif void Init_fiber_spec(void) { VALUE cls = rb_define_class("CApiFiberSpecs", rb_cObject); @@ -58,10 +56,7 @@ void Init_fiber_spec(void) { rb_define_method(cls, "rb_fiber_resume", fiber_spec_rb_fiber_resume, 2); rb_define_method(cls, "rb_fiber_yield", fiber_spec_rb_fiber_yield, 1); rb_define_method(cls, "rb_fiber_new", fiber_spec_rb_fiber_new, 0); - -#ifdef RUBY_VERSION_IS_3_1 rb_define_method(cls, "rb_fiber_raise", fiber_spec_rb_fiber_raise, -1); -#endif } #ifdef __cplusplus diff --git a/spec/ruby/optional/capi/ext/finalizer_spec.c b/spec/ruby/optional/capi/ext/finalizer_spec.c index ef546aa5b1..83347da912 100644 --- a/spec/ruby/optional/capi/ext/finalizer_spec.c +++ b/spec/ruby/optional/capi/ext/finalizer_spec.c @@ -10,13 +10,7 @@ static VALUE define_finalizer(VALUE self, VALUE obj, VALUE finalizer) { } static VALUE undefine_finalizer(VALUE self, VALUE obj) { -// Ruby 3.4.0 and 3.4.1 have a bug where rb_undefine_finalizer is missing -// See: https://bugs.ruby-lang.org/issues/20981 -#if RUBY_API_VERSION_CODE == 30400 && (RUBY_VERSION_TEENY == 0 || RUBY_VERSION_TEENY == 1) - return Qnil; -#else return rb_undefine_finalizer(obj); -#endif } void Init_finalizer_spec(void) { diff --git a/spec/ruby/optional/capi/ext/hash_spec.c b/spec/ruby/optional/capi/ext/hash_spec.c index 0e5b3d1c0a..653917f2c4 100644 --- a/spec/ruby/optional/capi/ext/hash_spec.c +++ b/spec/ruby/optional/capi/ext/hash_spec.c @@ -105,11 +105,9 @@ VALUE hash_spec_rb_hash_new(VALUE self) { return rb_hash_new(); } -#ifdef RUBY_VERSION_IS_3_2 VALUE hash_spec_rb_hash_new_capa(VALUE self, VALUE capacity) { return rb_hash_new_capa(NUM2LONG(capacity)); } -#endif VALUE rb_ident_hash_new(void); /* internal.h, used in ripper */ @@ -169,9 +167,7 @@ void Init_hash_spec(void) { rb_define_method(cls, "rb_hash_lookup2", hash_spec_rb_hash_lookup2, 3); rb_define_method(cls, "rb_hash_lookup2_default_undef", hash_spec_rb_hash_lookup2_default_undef, 2); rb_define_method(cls, "rb_hash_new", hash_spec_rb_hash_new, 0); -#ifdef RUBY_VERSION_IS_3_2 rb_define_method(cls, "rb_hash_new_capa", hash_spec_rb_hash_new_capa, 1); -#endif rb_define_method(cls, "rb_ident_hash_new", hash_spec_rb_ident_hash_new, 0); rb_define_method(cls, "rb_hash_size", hash_spec_rb_hash_size, 1); rb_define_method(cls, "rb_hash_set_ifnone", hash_spec_rb_hash_set_ifnone, 2); diff --git a/spec/ruby/optional/capi/ext/io_spec.c b/spec/ruby/optional/capi/ext/io_spec.c index 3b7e8c4481..fe31cffb49 100644 --- a/spec/ruby/optional/capi/ext/io_spec.c +++ b/spec/ruby/optional/capi/ext/io_spec.c @@ -28,13 +28,7 @@ static int set_non_blocking(int fd) { } static int io_spec_get_fd(VALUE io) { -#ifdef RUBY_VERSION_IS_3_1 return rb_io_descriptor(io); -#else - rb_io_t* fp; - GetOpenFile(io, fp); - return fp->fd; -#endif } VALUE io_spec_GetOpenFile_fd(VALUE self, VALUE io) { @@ -143,11 +137,7 @@ VALUE io_spec_rb_io_wait_readable(VALUE self, VALUE io, VALUE read_p) { errno = saved_errno; } -#ifdef RUBY_VERSION_IS_3_1 ret = rb_io_maybe_wait_readable(errno, io, Qnil); -#else - ret = rb_io_wait_readable(fd); -#endif if (RTEST(read_p)) { ssize_t r = read(fd, buf, RB_IO_WAIT_READABLE_BUF); @@ -166,22 +156,15 @@ VALUE io_spec_rb_io_wait_readable(VALUE self, VALUE io, VALUE read_p) { } VALUE io_spec_rb_io_wait_writable(VALUE self, VALUE io) { -#ifdef RUBY_VERSION_IS_3_1 int ret = rb_io_maybe_wait_writable(errno, io, Qnil); -#else - int ret = rb_io_wait_writable(io_spec_get_fd(io)); -#endif return ret ? Qtrue : Qfalse; } -#ifdef RUBY_VERSION_IS_3_1 VALUE io_spec_rb_io_maybe_wait_writable(VALUE self, VALUE error, VALUE io, VALUE timeout) { int ret = rb_io_maybe_wait_writable(NUM2INT(error), io, timeout); return INT2NUM(ret); } -#endif -#ifdef RUBY_VERSION_IS_3_1 #ifdef SET_NON_BLOCKING_FAILS_ALWAYS NORETURN(VALUE io_spec_rb_io_maybe_wait_readable(VALUE self, VALUE error, VALUE io, VALUE timeout, VALUE read_p)); #endif @@ -224,13 +207,10 @@ VALUE io_spec_rb_io_maybe_wait_readable(VALUE self, VALUE error, VALUE io, VALUE UNREACHABLE_RETURN(Qnil); #endif } -#endif -#ifdef RUBY_VERSION_IS_3_1 VALUE io_spec_rb_io_maybe_wait(VALUE self, VALUE error, VALUE io, VALUE events, VALUE timeout) { return rb_io_maybe_wait(NUM2INT(error), io, events, timeout); } -#endif VALUE io_spec_rb_thread_wait_fd(VALUE self, VALUE io) { rb_thread_wait_fd(io_spec_get_fd(io)); @@ -238,23 +218,13 @@ VALUE io_spec_rb_thread_wait_fd(VALUE self, VALUE io) { } VALUE io_spec_rb_wait_for_single_fd(VALUE self, VALUE io, VALUE events, VALUE secs, VALUE usecs) { -#ifdef RUBY_VERSION_IS_3_0 VALUE timeout = Qnil; if (!NIL_P(secs)) { - timeout = rb_float_new((double)FIX2INT(secs) + (0.000001f * FIX2INT(usecs))); + timeout = rb_float_new((double)FIX2INT(secs) + (0.000001 * FIX2INT(usecs))); } VALUE result = rb_io_wait(io, events, timeout); if (result == Qfalse) return INT2FIX(0); else return result; -#else - struct timeval tv; - if (!NIL_P(secs)) { - tv.tv_sec = FIX2INT(secs); - tv.tv_usec = FIX2INT(usecs); - } - int fd = io_spec_get_fd(io); - return INT2FIX(rb_wait_for_single_fd(fd, FIX2INT(events), NIL_P(secs) ? NULL : &tv)); -#endif } VALUE io_spec_rb_thread_fd_writable(VALUE self, VALUE io) { @@ -317,6 +287,18 @@ VALUE io_spec_rb_cloexec_open(VALUE self, VALUE path, VALUE flags, VALUE mode) { return rb_funcall(rb_cIO, rb_intern("for_fd"), 1, INT2FIX(fd)); } +VALUE io_spec_rb_cloexec_dup(VALUE self, VALUE io) { + int fd = io_spec_get_fd(io); + int new_fd = rb_cloexec_dup(fd); + return rb_funcall(rb_cIO, rb_intern("for_fd"), 1, INT2FIX(new_fd)); +} + +VALUE io_spec_rb_cloexec_fcntl_dupfd(VALUE self, VALUE io, VALUE minfd) { + int fd = io_spec_get_fd(io); + int new_fd = rb_cloexec_fcntl_dupfd(fd, FIX2INT(minfd)); + return rb_funcall(rb_cIO, rb_intern("for_fd"), 1, INT2FIX(new_fd)); +} + VALUE io_spec_rb_io_close(VALUE self, VALUE io) { return rb_io_close(io); } @@ -349,13 +331,7 @@ static VALUE io_spec_errno_set(VALUE self, VALUE val) { VALUE io_spec_mode_sync_flag(VALUE self, VALUE io) { int mode; -#ifdef RUBY_VERSION_IS_3_3 mode = rb_io_mode(io); -#else - rb_io_t *fp; - GetOpenFile(io, fp); - mode = fp->mode; -#endif if (mode & FMODE_SYNC) { return Qtrue; } else { @@ -363,7 +339,6 @@ VALUE io_spec_mode_sync_flag(VALUE self, VALUE io) { } } -#if defined(RUBY_VERSION_IS_3_3) || defined(TRUFFLERUBY) static VALUE io_spec_rb_io_mode(VALUE self, VALUE io) { return INT2FIX(rb_io_mode(io)); } @@ -390,7 +365,6 @@ static VALUE io_spec_rb_io_open_descriptor(VALUE self, VALUE klass, VALUE descri static VALUE io_spec_rb_io_open_descriptor_without_encoding(VALUE self, VALUE klass, VALUE descriptor, VALUE mode, VALUE path, VALUE timeout) { return rb_io_open_descriptor(klass, FIX2INT(descriptor), FIX2INT(mode), path, timeout, NULL); } -#endif void Init_io_spec(void) { VALUE cls = rb_define_class("CApiIOSpecs", rb_cObject); @@ -409,11 +383,9 @@ void Init_io_spec(void) { rb_define_method(cls, "rb_io_taint_check", io_spec_rb_io_taint_check, 1); rb_define_method(cls, "rb_io_wait_readable", io_spec_rb_io_wait_readable, 2); rb_define_method(cls, "rb_io_wait_writable", io_spec_rb_io_wait_writable, 1); -#ifdef RUBY_VERSION_IS_3_1 rb_define_method(cls, "rb_io_maybe_wait_writable", io_spec_rb_io_maybe_wait_writable, 3); rb_define_method(cls, "rb_io_maybe_wait_readable", io_spec_rb_io_maybe_wait_readable, 4); rb_define_method(cls, "rb_io_maybe_wait", io_spec_rb_io_maybe_wait, 4); -#endif 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_thread_fd_select_read", io_spec_rb_thread_fd_select_read, 1); @@ -423,9 +395,10 @@ void Init_io_spec(void) { 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); + rb_define_method(cls, "rb_cloexec_dup", io_spec_rb_cloexec_dup, 1); + rb_define_method(cls, "rb_cloexec_fcntl_dupfd", io_spec_rb_cloexec_fcntl_dupfd, 2); rb_define_method(cls, "errno=", io_spec_errno_set, 1); rb_define_method(cls, "rb_io_mode_sync_flag", io_spec_mode_sync_flag, 1); -#if defined(RUBY_VERSION_IS_3_3) || defined(TRUFFLERUBY) rb_define_method(cls, "rb_io_mode", io_spec_rb_io_mode, 1); rb_define_method(cls, "rb_io_path", io_spec_rb_io_path, 1); rb_define_method(cls, "rb_io_closed_p", io_spec_rb_io_closed_p, 1); @@ -436,7 +409,6 @@ void Init_io_spec(void) { rb_define_const(cls, "FMODE_BINMODE", INT2FIX(FMODE_BINMODE)); rb_define_const(cls, "FMODE_TEXTMODE", INT2FIX(FMODE_TEXTMODE)); rb_define_const(cls, "ECONV_UNIVERSAL_NEWLINE_DECORATOR", INT2FIX(ECONV_UNIVERSAL_NEWLINE_DECORATOR)); -#endif } #ifdef __cplusplus diff --git a/spec/ruby/optional/capi/ext/kernel_spec.c b/spec/ruby/optional/capi/ext/kernel_spec.c index abd8d20ff4..eee324052d 100644 --- a/spec/ruby/optional/capi/ext/kernel_spec.c +++ b/spec/ruby/optional/capi/ext/kernel_spec.c @@ -1,4 +1,5 @@ #include "ruby.h" +#include "ruby/vm.h" #include "rubyspec.h" #include <errno.h> @@ -117,9 +118,11 @@ VALUE kernel_spec_rb_eval_string(VALUE self, VALUE str) { return rb_eval_string(RSTRING_PTR(str)); } +#ifndef RUBY_VERSION_IS_4_0 VALUE kernel_spec_rb_eval_cmd_kw(VALUE self, VALUE cmd, VALUE args, VALUE kw_splat) { return rb_eval_cmd_kw(cmd, args, NUM2INT(kw_splat)); } +#endif VALUE kernel_spec_rb_raise(VALUE self, VALUE hash) { rb_hash_aset(hash, ID2SYM(rb_intern("stage")), ID2SYM(rb_intern("before"))); @@ -335,6 +338,15 @@ static VALUE kernel_spec_rb_set_end_proc(VALUE self, VALUE io) { return Qnil; } +static void at_exit_hook(ruby_vm_t *vm) { + puts("ruby_vm_at_exit hook ran"); +} + +static VALUE kernel_spec_ruby_vm_at_exit(VALUE self) { + ruby_vm_at_exit(at_exit_hook); + return self; +} + static VALUE kernel_spec_rb_f_sprintf(VALUE self, VALUE ary) { return rb_f_sprintf((int)RARRAY_LEN(ary), RARRAY_PTR(ary)); } @@ -351,7 +363,6 @@ static VALUE kernel_spec_rb_funcallv(VALUE self, VALUE obj, VALUE method, VALUE return rb_funcallv(obj, SYM2ID(method), RARRAY_LENINT(args), RARRAY_PTR(args)); } -#ifdef RUBY_VERSION_IS_3_0 static VALUE kernel_spec_rb_funcallv_kw(VALUE self, VALUE obj, VALUE method, VALUE args) { return rb_funcallv_kw(obj, SYM2ID(method), RARRAY_LENINT(args), RARRAY_PTR(args), RB_PASS_KEYWORDS); } @@ -359,7 +370,6 @@ static VALUE kernel_spec_rb_funcallv_kw(VALUE self, VALUE obj, VALUE method, VAL static VALUE kernel_spec_rb_keyword_given_p(int argc, VALUE *args, VALUE self) { return rb_keyword_given_p() ? Qtrue : Qfalse; } -#endif static VALUE kernel_spec_rb_funcallv_public(VALUE self, VALUE obj, VALUE method) { return rb_funcallv_public(obj, SYM2ID(method), 0, NULL); @@ -405,7 +415,9 @@ void Init_kernel_spec(void) { rb_define_method(cls, "rb_category_warn_deprecated_with_integer_extra_value", kernel_spec_rb_category_warn_deprecated_with_integer_extra_value, 1); rb_define_method(cls, "rb_ensure", kernel_spec_rb_ensure, 4); rb_define_method(cls, "rb_eval_string", kernel_spec_rb_eval_string, 1); +#ifndef RUBY_VERSION_IS_4_0 rb_define_method(cls, "rb_eval_cmd_kw", kernel_spec_rb_eval_cmd_kw, 3); +#endif rb_define_method(cls, "rb_raise", kernel_spec_rb_raise, 1); rb_define_method(cls, "rb_throw", kernel_spec_rb_throw, 1); rb_define_method(cls, "rb_throw_obj", kernel_spec_rb_throw_obj, 2); @@ -432,14 +444,13 @@ void Init_kernel_spec(void) { rb_define_method(cls, "rb_yield_splat", kernel_spec_rb_yield_splat, 1); rb_define_method(cls, "rb_exec_recursive", kernel_spec_rb_exec_recursive, 1); rb_define_method(cls, "rb_set_end_proc", kernel_spec_rb_set_end_proc, 1); + rb_define_method(cls, "ruby_vm_at_exit", kernel_spec_ruby_vm_at_exit, 0); rb_define_method(cls, "rb_f_sprintf", kernel_spec_rb_f_sprintf, 1); rb_define_method(cls, "rb_str_format", kernel_spec_rb_str_format, 3); rb_define_method(cls, "rb_make_backtrace", kernel_spec_rb_make_backtrace, 0); rb_define_method(cls, "rb_funcallv", kernel_spec_rb_funcallv, 3); -#ifdef RUBY_VERSION_IS_3_0 rb_define_method(cls, "rb_funcallv_kw", kernel_spec_rb_funcallv_kw, 3); rb_define_method(cls, "rb_keyword_given_p", kernel_spec_rb_keyword_given_p, -1); -#endif rb_define_method(cls, "rb_funcallv_public", kernel_spec_rb_funcallv_public, 2); rb_define_method(cls, "rb_funcall_many_args", kernel_spec_rb_funcall_many_args, 2); rb_define_method(cls, "rb_funcall_with_block", kernel_spec_rb_funcall_with_block, 4); diff --git a/spec/ruby/optional/capi/ext/object_spec.c b/spec/ruby/optional/capi/ext/object_spec.c index aa60662e1e..995bc38fcf 100644 --- a/spec/ruby/optional/capi/ext/object_spec.c +++ b/spec/ruby/optional/capi/ext/object_spec.c @@ -15,11 +15,6 @@ static VALUE object_spec_FL_ABLE(VALUE self, VALUE obj) { static int object_spec_FL_TEST_flag(VALUE flag_string) { char *flag_cstr = StringValueCStr(flag_string); -#ifndef RUBY_VERSION_IS_3_1 - if (strcmp(flag_cstr, "FL_TAINT") == 0) { - return FL_TAINT; - } -#endif if (strcmp(flag_cstr, "FL_FREEZE") == 0) { return FL_FREEZE; } @@ -30,22 +25,6 @@ static VALUE object_spec_FL_TEST(VALUE self, VALUE obj, VALUE flag) { return INT2FIX(FL_TEST(obj, object_spec_FL_TEST_flag(flag))); } -#ifndef RUBY_VERSION_IS_3_1 -static VALUE object_spec_OBJ_TAINT(VALUE self, VALUE obj) { - OBJ_TAINT(obj); - return Qnil; -} - -static VALUE object_spec_OBJ_TAINTED(VALUE self, VALUE obj) { - return OBJ_TAINTED(obj) ? Qtrue : Qfalse; -} - -static VALUE object_spec_OBJ_INFECT(VALUE self, VALUE host, VALUE source) { - OBJ_INFECT(host, source); - return Qnil; -} -#endif - static VALUE object_spec_rb_any_to_s(VALUE self, VALUE obj) { return rb_any_to_s(obj); } @@ -154,12 +133,6 @@ static VALUE object_specs_rb_obj_method(VALUE self, VALUE obj, VALUE method) { return rb_obj_method(obj, method); } -#ifndef RUBY_VERSION_IS_3_2 -static VALUE object_spec_rb_obj_taint(VALUE self, VALUE obj) { - return rb_obj_taint(obj); -} -#endif - static VALUE so_require(VALUE self) { rb_require("fixtures/foo"); return Qnil; @@ -410,15 +383,20 @@ static VALUE object_spec_custom_alloc_func_p(VALUE self, VALUE klass) { return allocator ? Qtrue : Qfalse; } +static VALUE object_spec_redefine_frozen(VALUE self) { + // The purpose of this spec is to verify that `frozen?` + // and `RB_OBJ_FROZEN` do not mutually recurse infinitely. + if (RB_OBJ_FROZEN(self)) { + return Qtrue; + } + + return Qfalse; +} + void Init_object_spec(void) { VALUE cls = rb_define_class("CApiObjectSpecs", rb_cObject); rb_define_method(cls, "FL_ABLE", object_spec_FL_ABLE, 1); rb_define_method(cls, "FL_TEST", object_spec_FL_TEST, 2); -#ifndef RUBY_VERSION_IS_3_1 - rb_define_method(cls, "OBJ_TAINT", object_spec_OBJ_TAINT, 1); - rb_define_method(cls, "OBJ_TAINTED", object_spec_OBJ_TAINTED, 1); - rb_define_method(cls, "OBJ_INFECT", object_spec_OBJ_INFECT, 2); -#endif rb_define_method(cls, "rb_any_to_s", object_spec_rb_any_to_s, 1); rb_define_method(cls, "rb_attr_get", so_attr_get, 2); rb_define_method(cls, "rb_obj_instance_variables", object_spec_rb_obj_instance_variables, 1); @@ -443,15 +421,11 @@ void Init_object_spec(void) { rb_define_method(cls, "rb_obj_is_kind_of", so_kind_of, 2); rb_define_method(cls, "rb_obj_method_arity", object_specs_rb_obj_method_arity, 2); rb_define_method(cls, "rb_obj_method", object_specs_rb_obj_method, 2); -#ifndef RUBY_VERSION_IS_3_2 - rb_define_method(cls, "rb_obj_taint", object_spec_rb_obj_taint, 1); -#endif rb_define_method(cls, "rb_require", so_require, 0); rb_define_method(cls, "rb_respond_to", so_respond_to, 2); rb_define_method(cls, "rb_method_boundp", object_spec_rb_method_boundp, 3); rb_define_method(cls, "rb_obj_respond_to", so_obj_respond_to, 3); rb_define_method(cls, "rb_special_const_p", object_spec_rb_special_const_p, 1); - 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); @@ -491,6 +465,9 @@ void Init_object_spec(void) { rb_define_method(cls, "custom_alloc_func?", object_spec_custom_alloc_func_p, 1); rb_define_method(cls, "not_implemented_method", rb_f_notimplement, -1); rb_define_method(cls, "rb_ivar_foreach", object_spec_rb_ivar_foreach, 1); + + cls = rb_define_class("CApiObjectRedefinitionSpecs", rb_cObject); + rb_define_method(cls, "frozen?", object_spec_redefine_frozen, 0); } #ifdef __cplusplus diff --git a/spec/ruby/optional/capi/ext/range_spec.c b/spec/ruby/optional/capi/ext/range_spec.c index b0cf1a8662..9faed3e5ee 100644 --- a/spec/ruby/optional/capi/ext/range_spec.c +++ b/spec/ruby/optional/capi/ext/range_spec.c @@ -25,9 +25,9 @@ VALUE range_spec_rb_range_values(VALUE self, VALUE range) { return ary; } -VALUE range_spec_rb_range_beg_len(VALUE self, VALUE range, VALUE begpv, VALUE lenpv, VALUE lenv, VALUE errv) { - long begp = FIX2LONG(begpv); - long lenp = FIX2LONG(lenpv); +VALUE range_spec_rb_range_beg_len(VALUE self, VALUE range, VALUE lenv, VALUE errv) { + long begp = 0; + long lenp = 0; long len = FIX2LONG(lenv); int err = FIX2INT(errv); VALUE ary = rb_ary_new(); @@ -38,11 +38,51 @@ VALUE range_spec_rb_range_beg_len(VALUE self, VALUE range, VALUE begpv, VALUE le return ary; } +VALUE range_spec_rb_arithmetic_sequence_extract(VALUE self, VALUE object) { + VALUE ary = rb_ary_new(); + rb_arithmetic_sequence_components_t components; + + int status = rb_arithmetic_sequence_extract(object, &components); + + if (!status) { + rb_ary_store(ary, 0, LONG2FIX(status)); + return ary; + } + + rb_ary_store(ary, 0, LONG2FIX(status)); + rb_ary_store(ary, 1, components.begin); + rb_ary_store(ary, 2, components.end); + rb_ary_store(ary, 3, components.step); + rb_ary_store(ary, 4, components.exclude_end ? Qtrue : Qfalse); + return ary; +} + +VALUE range_spec_rb_arithmetic_sequence_beg_len_step(VALUE self, VALUE aseq, VALUE lenv, VALUE errv) { + long begp = 0; + long lenp = 0; + long stepp = 0; + + long len = FIX2LONG(lenv); + int err = FIX2INT(errv); + + VALUE success = rb_arithmetic_sequence_beg_len_step(aseq, &begp, &lenp, &stepp, len, err); + + VALUE ary = rb_ary_new(); + rb_ary_store(ary, 0, success); + rb_ary_store(ary, 1, LONG2FIX(begp)); + rb_ary_store(ary, 2, LONG2FIX(lenp)); + rb_ary_store(ary, 3, LONG2FIX(stepp)); + + return ary; +} + void Init_range_spec(void) { VALUE cls = rb_define_class("CApiRangeSpecs", rb_cObject); rb_define_method(cls, "rb_range_new", range_spec_rb_range_new, -1); rb_define_method(cls, "rb_range_values", range_spec_rb_range_values, 1); - rb_define_method(cls, "rb_range_beg_len", range_spec_rb_range_beg_len, 5); + rb_define_method(cls, "rb_range_beg_len", range_spec_rb_range_beg_len, 3); + rb_define_method(cls, "rb_arithmetic_sequence_extract", range_spec_rb_arithmetic_sequence_extract, 1); + rb_define_method(cls, "rb_arithmetic_sequence_beg_len_step", range_spec_rb_arithmetic_sequence_beg_len_step, 3); } #ifdef __cplusplus diff --git a/spec/ruby/optional/capi/ext/rbasic_spec.c b/spec/ruby/optional/capi/ext/rbasic_spec.c index 26be2fed6d..5a95b92804 100644 --- a/spec/ruby/optional/capi/ext/rbasic_spec.c +++ b/spec/ruby/optional/capi/ext/rbasic_spec.c @@ -31,13 +31,6 @@ static const VALUE DATA_VISIBLE_BITS = FL_FREEZE | ~(FL_USER0 - 1); #error "unsupported" #endif - -#ifndef RUBY_VERSION_IS_3_1 -VALUE rbasic_spec_taint_flag(VALUE self) { - return VALUE2NUM(RUBY_FL_TAINT); -} -#endif - VALUE rbasic_spec_freeze_flag(VALUE self) { return VALUE2NUM(RUBY_FL_FREEZE); } @@ -93,9 +86,6 @@ static VALUE rbasic_rdata_spec_get_klass(VALUE self, VALUE structure) { void Init_rbasic_spec(void) { VALUE cls = rb_define_class("CApiRBasicSpecs", rb_cObject); -#ifndef RUBY_VERSION_IS_3_1 - rb_define_method(cls, "taint_flag", rbasic_spec_taint_flag, 0); -#endif rb_define_method(cls, "freeze_flag", rbasic_spec_freeze_flag, 0); rb_define_method(cls, "get_flags", rbasic_spec_get_flags, 1); rb_define_method(cls, "set_flags", rbasic_spec_set_flags, 2); diff --git a/spec/ruby/optional/capi/ext/rubyspec.h b/spec/ruby/optional/capi/ext/rubyspec.h index 1df274ead1..5a92645785 100644 --- a/spec/ruby/optional/capi/ext/rubyspec.h +++ b/spec/ruby/optional/capi/ext/rubyspec.h @@ -5,11 +5,7 @@ * guards to assist with version incompatibilities. */ #include <ruby.h> -#ifdef HAVE_RUBY_VERSION_H -# include <ruby/version.h> -#else -# include <version.h> -#endif +#include <ruby/version.h> /* copied from ext/-test-/cxxanyargs/cxxanyargs.cpp */ #if 0 /* Ignore deprecation warnings */ @@ -34,36 +30,21 @@ #endif -#ifndef RUBY_VERSION_MAJOR -#define RUBY_VERSION_MAJOR RUBY_API_VERSION_MAJOR -#define RUBY_VERSION_MINOR RUBY_API_VERSION_MINOR -#define RUBY_VERSION_TEENY RUBY_API_VERSION_TEENY -#endif - -#define RUBY_VERSION_BEFORE(major,minor,teeny) \ - ((RUBY_VERSION_MAJOR < (major)) || \ - (RUBY_VERSION_MAJOR == (major) && RUBY_VERSION_MINOR < (minor)) || \ - (RUBY_VERSION_MAJOR == (major) && RUBY_VERSION_MINOR == (minor) && RUBY_VERSION_TEENY < (teeny))) -#define RUBY_VERSION_SINCE(major,minor,teeny) (!RUBY_VERSION_BEFORE(major, minor, teeny)) - -#if RUBY_VERSION_SINCE(3, 4, 0) -#define RUBY_VERSION_IS_3_4 -#endif - -#if RUBY_VERSION_SINCE(3, 3, 0) -#define RUBY_VERSION_IS_3_3 -#endif +#define RUBY_VERSION_BEFORE(major,minor) \ + ((RUBY_API_VERSION_MAJOR < (major)) || \ + (RUBY_API_VERSION_MAJOR == (major) && RUBY_API_VERSION_MINOR < (minor))) +#define RUBY_VERSION_SINCE(major,minor) (!RUBY_VERSION_BEFORE(major, minor)) -#if RUBY_VERSION_SINCE(3, 2, 0) -#define RUBY_VERSION_IS_3_2 +#if RUBY_VERSION_SINCE(4, 1) +#define RUBY_VERSION_IS_4_1 #endif -#if RUBY_VERSION_SINCE(3, 1, 0) -#define RUBY_VERSION_IS_3_1 +#if RUBY_VERSION_SINCE(4, 0) +#define RUBY_VERSION_IS_4_0 #endif -#if RUBY_VERSION_SINCE(3, 0, 0) -#define RUBY_VERSION_IS_3_0 +#if RUBY_VERSION_SINCE(3, 4) +#define RUBY_VERSION_IS_3_4 #endif #endif diff --git a/spec/ruby/optional/capi/ext/set_spec.c b/spec/ruby/optional/capi/ext/set_spec.c new file mode 100644 index 0000000000..11a271b361 --- /dev/null +++ b/spec/ruby/optional/capi/ext/set_spec.c @@ -0,0 +1,65 @@ +#include "ruby.h" +#include "rubyspec.h" + +#ifdef RUBY_VERSION_IS_4_0 +#ifdef __cplusplus +extern "C" { +#endif + +#define RBOOL(x) ((x) ? Qtrue : Qfalse) + +int yield_element_and_arg(VALUE element, VALUE arg) { + return RTEST(rb_yield_values(2, element, arg)) ? ST_CONTINUE : ST_STOP; +} + +VALUE set_spec_rb_set_foreach(VALUE self, VALUE set, VALUE arg) { + rb_set_foreach(set, yield_element_and_arg, arg); + return Qnil; +} + +VALUE set_spec_rb_set_new(VALUE self) { + return rb_set_new(); +} + +VALUE set_spec_rb_set_new_capa(VALUE self, VALUE capa) { + return rb_set_new_capa(NUM2INT(capa)); +} + +VALUE set_spec_rb_set_lookup(VALUE self, VALUE set, VALUE element) { + return RBOOL(rb_set_lookup(set, element)); +} + +VALUE set_spec_rb_set_add(VALUE self, VALUE set, VALUE element) { + return RBOOL(rb_set_add(set, element)); +} + +VALUE set_spec_rb_set_clear(VALUE self, VALUE set) { + return rb_set_clear(set); +} + +VALUE set_spec_rb_set_delete(VALUE self, VALUE set, VALUE element) { + return RBOOL(rb_set_delete(set, element)); +} + +VALUE set_spec_rb_set_size(VALUE self, VALUE set) { + return SIZET2NUM(rb_set_size(set)); +} + +void Init_set_spec(void) { + VALUE cls = rb_define_class("CApiSetSpecs", rb_cObject); + + rb_define_method(cls, "rb_set_foreach", set_spec_rb_set_foreach, 2); + rb_define_method(cls, "rb_set_new", set_spec_rb_set_new, 0); + rb_define_method(cls, "rb_set_new_capa", set_spec_rb_set_new_capa, 1); + rb_define_method(cls, "rb_set_lookup", set_spec_rb_set_lookup, 2); + rb_define_method(cls, "rb_set_add", set_spec_rb_set_add, 2); + rb_define_method(cls, "rb_set_clear", set_spec_rb_set_clear, 1); + rb_define_method(cls, "rb_set_delete", set_spec_rb_set_delete, 2); + rb_define_method(cls, "rb_set_size", set_spec_rb_set_size, 1); +} + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/spec/ruby/optional/capi/ext/string_spec.c b/spec/ruby/optional/capi/ext/string_spec.c index a140c86347..8291b73ea4 100644 --- a/spec/ruby/optional/capi/ext/string_spec.c +++ b/spec/ruby/optional/capi/ext/string_spec.c @@ -12,6 +12,13 @@ extern "C" { #endif +#ifdef PTR2NUM +#elif SIZEOF_VOIDP <= SIZEOF_LONG +# define PTR2NUM(x) (LONG2NUM((long)(x))) +#elif SIZEOF_VOIDP <= SIZEOF_LONG_LONG +# define PTR2NUM(x) (LL2NUM((LONG_LONG)(x))) +#endif + /* Make sure the RSTRING_PTR and the bytes are in native memory. * On TruffleRuby RSTRING_PTR and the bytes remain in managed memory * until they must be written to native memory. @@ -252,16 +259,6 @@ VALUE string_spec_rb_str_new5(VALUE self, VALUE str, VALUE ptr, VALUE len) { return rb_str_new5(str, RSTRING_PTR(ptr), FIX2INT(len)); } -#ifndef RUBY_VERSION_IS_3_2 -VALUE string_spec_rb_tainted_str_new(VALUE self, VALUE str, VALUE len) { - return rb_tainted_str_new(RSTRING_PTR(str), FIX2INT(len)); -} - -VALUE string_spec_rb_tainted_str_new2(VALUE self, VALUE str) { - return rb_tainted_str_new2(RSTRING_PTR(str)); -} -#endif - VALUE string_spec_rb_str_plus(VALUE self, VALUE str1, VALUE str2) { return rb_str_plus(str1, str2); } @@ -306,6 +303,26 @@ VALUE string_spec_rb_str_substr(VALUE self, VALUE str, VALUE beg, VALUE len) { return rb_str_substr(str, FIX2INT(beg), FIX2INT(len)); } +VALUE string_spec_rb_str_subpos(VALUE self, VALUE str, VALUE beg) { + char* original = RSTRING_PTR(str); + char* end = RSTRING_END(str); + long len = rb_str_strlen(str); + char *p = rb_str_subpos(str, FIX2LONG(beg), &len); + if (p == NULL) { + return Qnil; + } + + if (p >= original && p <= end) { + return rb_ary_new_from_args(2, LONG2FIX(p - RSTRING_PTR(str)), LONG2FIX(len)); + } else { + rb_raise(rb_eRuntimeError, "the returned pointer is not inside the original string buffer"); + } +} + +VALUE string_spec_rb_str_sublen(VALUE self, VALUE str, VALUE pos) { + return LONG2FIX(rb_str_sublen(str, FIX2LONG(pos))); +} + VALUE string_spec_rb_str_to_str(VALUE self, VALUE arg) { return rb_str_to_str(arg); } @@ -318,6 +335,11 @@ VALUE string_spec_RSTRING_LENINT(VALUE self, VALUE str) { return INT2FIX(RSTRING_LENINT(str)); } +VALUE string_spec_RSTRING_PTR(VALUE self, VALUE str) { + char* ptr = RSTRING_PTR(str); + return PTR2NUM(ptr); +} + VALUE string_spec_RSTRING_PTR_iterate(VALUE self, VALUE str) { int i; char* ptr; @@ -403,6 +425,7 @@ VALUE string_spec_RSTRING_PTR_read(VALUE self, VALUE str, VALUE path) { if (read(fd, buffer, 30) < 0) { rb_syserr_fail(errno, "read"); } + rb_str_set_len(str, 30); rb_str_modify_expand(str, 53); rb_ary_push(capacities, SIZET2NUM(rb_str_capacity(str))); @@ -450,6 +473,7 @@ static VALUE string_spec_rb_str_free(VALUE self, VALUE str) { static VALUE string_spec_rb_sprintf1(VALUE self, VALUE str, VALUE repl) { return rb_sprintf(RSTRING_PTR(str), RSTRING_PTR(repl)); } + static VALUE string_spec_rb_sprintf2(VALUE self, VALUE str, VALUE repl1, VALUE repl2) { return rb_sprintf(RSTRING_PTR(str), RSTRING_PTR(repl1), RSTRING_PTR(repl2)); } @@ -540,7 +564,10 @@ static VALUE string_spec_rb_str_modify(VALUE self, VALUE str) { } static VALUE string_spec_rb_utf8_str_new_static(VALUE self) { - return rb_utf8_str_new_static("nokogiri", 8); + const char* literal = "nokogiri"; + return rb_ary_new_from_args(2, + rb_utf8_str_new_static("nokogiri", 8), + PTR2NUM(literal)); } static VALUE string_spec_rb_utf8_str_new(VALUE self) { @@ -590,6 +617,14 @@ static VALUE string_spec_rb_str_to_interned_str(VALUE self, VALUE str) { return rb_str_to_interned_str(str); } +static VALUE string_spec_rb_interned_str(VALUE self, VALUE str, VALUE len) { + return rb_interned_str(RSTRING_PTR(str), FIX2LONG(len)); +} + +static VALUE string_spec_rb_interned_str_cstr(VALUE self, VALUE str) { + return rb_interned_str_cstr(RSTRING_PTR(str)); +} + void Init_string_spec(void) { VALUE cls = rb_define_class("CApiStringSpecs", rb_cObject); rb_define_method(cls, "rb_cstr2inum", string_spec_rb_cstr2inum, 2); @@ -635,10 +670,6 @@ void Init_string_spec(void) { rb_define_method(cls, "rb_str_new3", string_spec_rb_str_new3, 1); rb_define_method(cls, "rb_str_new4", string_spec_rb_str_new4, 1); rb_define_method(cls, "rb_str_new5", string_spec_rb_str_new5, 3); -#ifndef RUBY_VERSION_IS_3_2 - rb_define_method(cls, "rb_tainted_str_new", string_spec_rb_tainted_str_new, 2); - rb_define_method(cls, "rb_tainted_str_new2", string_spec_rb_tainted_str_new2, 1); -#endif rb_define_method(cls, "rb_str_plus", string_spec_rb_str_plus, 2); rb_define_method(cls, "rb_str_times", string_spec_rb_str_times, 2); rb_define_method(cls, "rb_str_modify_expand", string_spec_rb_str_modify_expand, 2); @@ -650,9 +681,12 @@ void Init_string_spec(void) { rb_define_method(cls, "rb_str_split", string_spec_rb_str_split, 1); rb_define_method(cls, "rb_str_subseq", string_spec_rb_str_subseq, 3); rb_define_method(cls, "rb_str_substr", string_spec_rb_str_substr, 3); + rb_define_method(cls, "rb_str_subpos", string_spec_rb_str_subpos, 2); + rb_define_method(cls, "rb_str_sublen", string_spec_rb_str_sublen, 2); rb_define_method(cls, "rb_str_to_str", string_spec_rb_str_to_str, 1); rb_define_method(cls, "RSTRING_LEN", string_spec_RSTRING_LEN, 1); rb_define_method(cls, "RSTRING_LENINT", string_spec_RSTRING_LENINT, 1); + rb_define_method(cls, "RSTRING_PTR", string_spec_RSTRING_PTR, 1); rb_define_method(cls, "RSTRING_PTR_iterate", string_spec_RSTRING_PTR_iterate, 1); rb_define_method(cls, "RSTRING_PTR_iterate_uint32", string_spec_RSTRING_PTR_iterate_uint32, 1); rb_define_method(cls, "RSTRING_PTR_short_memcpy", string_spec_RSTRING_PTR_short_memcpy, 1); @@ -694,6 +728,8 @@ void Init_string_spec(void) { rb_define_method(cls, "rb_enc_interned_str_cstr", string_spec_rb_enc_interned_str_cstr, 2); rb_define_method(cls, "rb_enc_interned_str", string_spec_rb_enc_interned_str, 3); rb_define_method(cls, "rb_str_to_interned_str", string_spec_rb_str_to_interned_str, 1); + rb_define_method(cls, "rb_interned_str", string_spec_rb_interned_str, 2); + rb_define_method(cls, "rb_interned_str_cstr", string_spec_rb_interned_str_cstr, 1); } #ifdef __cplusplus diff --git a/spec/ruby/optional/capi/ext/struct_spec.c b/spec/ruby/optional/capi/ext/struct_spec.c index 413249e828..1c669d153e 100644 --- a/spec/ruby/optional/capi/ext/struct_spec.c +++ b/spec/ruby/optional/capi/ext/struct_spec.c @@ -62,7 +62,10 @@ static VALUE struct_spec_rb_struct_size(VALUE self, VALUE st) { return rb_struct_size(st); } -#if defined(RUBY_VERSION_IS_3_3) +static VALUE struct_spec_rb_struct_initialize(VALUE self, VALUE st, VALUE values) { + return rb_struct_initialize(st, values); +} + /* Only allow setting three attributes, should be sufficient for testing. */ static VALUE struct_spec_rb_data_define(VALUE self, VALUE superclass, VALUE attr1, VALUE attr2, VALUE attr3) { @@ -77,7 +80,6 @@ static VALUE struct_spec_rb_data_define(VALUE self, VALUE superclass, return rb_data_define(superclass, a1, a2, a3, NULL); } -#endif void Init_struct_spec(void) { VALUE cls = rb_define_class("CApiStructSpecs", rb_cObject); @@ -90,9 +92,8 @@ void Init_struct_spec(void) { rb_define_method(cls, "rb_struct_define_under", struct_spec_rb_struct_define_under, 5); rb_define_method(cls, "rb_struct_new", struct_spec_rb_struct_new, 4); rb_define_method(cls, "rb_struct_size", struct_spec_rb_struct_size, 1); -#if defined(RUBY_VERSION_IS_3_3) + rb_define_method(cls, "rb_struct_initialize", struct_spec_rb_struct_initialize, 2); rb_define_method(cls, "rb_data_define", struct_spec_rb_data_define, 4); -#endif } #ifdef __cplusplus diff --git a/spec/ruby/optional/capi/ext/thread_spec.c b/spec/ruby/optional/capi/ext/thread_spec.c index 3511c2fbcf..ac77e4e813 100644 --- a/spec/ruby/optional/capi/ext/thread_spec.c +++ b/spec/ruby/optional/capi/ext/thread_spec.c @@ -118,7 +118,6 @@ static VALUE thread_spec_rb_thread_wait_for(VALUE self, VALUE s, VALUE ms) { return Qnil; } - VALUE thread_spec_call_proc(void *arg_ptr) { VALUE arg_array = (VALUE)arg_ptr; VALUE arg = rb_ary_pop(arg_array); @@ -167,6 +166,12 @@ static VALUE thread_spec_ruby_native_thread_p_new_thread(VALUE self) { #endif } +#ifdef RUBY_VERSION_IS_4_0 +static VALUE thread_spec_ruby_thread_has_gvl_p(VALUE self) { + return ruby_thread_has_gvl_p() ? Qtrue : Qfalse; +} +#endif + void Init_thread_spec(void) { VALUE cls = rb_define_class("CApiThreadSpecs", rb_cObject); rb_define_method(cls, "rb_thread_alone", thread_spec_rb_thread_alone, 0); @@ -180,6 +185,9 @@ void Init_thread_spec(void) { rb_define_method(cls, "rb_thread_create", thread_spec_rb_thread_create, 2); rb_define_method(cls, "ruby_native_thread_p", thread_spec_ruby_native_thread_p, 0); rb_define_method(cls, "ruby_native_thread_p_new_thread", thread_spec_ruby_native_thread_p_new_thread, 0); +#ifdef RUBY_VERSION_IS_4_0 + rb_define_method(cls, "ruby_thread_has_gvl_p", thread_spec_ruby_thread_has_gvl_p, 0); +#endif } #ifdef __cplusplus diff --git a/spec/ruby/optional/capi/ext/typed_data_spec.c b/spec/ruby/optional/capi/ext/typed_data_spec.c index 221f1c8ac4..c6fcfa3bc8 100644 --- a/spec/ruby/optional/capi/ext/typed_data_spec.c +++ b/spec/ruby/optional/capi/ext/typed_data_spec.c @@ -106,6 +106,7 @@ VALUE sws_typed_wrap_struct(VALUE self, VALUE val) { return TypedData_Wrap_Struct(rb_cObject, &sample_typed_wrapped_struct_data_type, bar); } +#ifndef RUBY_VERSION_IS_4_1 #undef RUBY_UNTYPED_DATA_WARNING #define RUBY_UNTYPED_DATA_WARNING 0 VALUE sws_untyped_wrap_struct(VALUE self, VALUE val) { @@ -113,6 +114,7 @@ VALUE sws_untyped_wrap_struct(VALUE self, VALUE val) { *data = FIX2INT(val); return Data_Wrap_Struct(rb_cObject, NULL, free, data); } +#endif VALUE sws_typed_get_struct(VALUE self, VALUE obj) { struct sample_typed_wrapped_struct* bar; @@ -173,9 +175,11 @@ VALUE sws_typed_rb_check_typeddata_different_type(VALUE self, VALUE obj) { return rb_check_typeddata(obj, &sample_typed_wrapped_struct_other_data_type) == DATA_PTR(obj) ? Qtrue : Qfalse; } +#ifndef RUBY_VERSION_IS_4_1 VALUE sws_typed_RTYPEDDATA_P(VALUE self, VALUE obj) { return RTYPEDDATA_P(obj) ? Qtrue : Qfalse; } +#endif void Init_typed_data_spec(void) { VALUE cls = rb_define_class("CApiAllocTypedSpecs", rb_cObject); @@ -183,7 +187,9 @@ void Init_typed_data_spec(void) { rb_define_method(cls, "typed_wrapped_data", sdaf_typed_get_struct, 0); cls = rb_define_class("CApiWrappedTypedStructSpecs", rb_cObject); rb_define_method(cls, "typed_wrap_struct", sws_typed_wrap_struct, 1); +#ifndef RUBY_VERSION_IS_4_1 rb_define_method(cls, "untyped_wrap_struct", sws_untyped_wrap_struct, 1); +#endif rb_define_method(cls, "typed_get_struct", sws_typed_get_struct, 1); rb_define_method(cls, "typed_get_struct_other", sws_typed_get_struct_different_type, 1); rb_define_method(cls, "typed_get_struct_parent", sws_typed_get_struct_parent_type, 1); @@ -194,10 +200,11 @@ void Init_typed_data_spec(void) { 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); +#ifndef RUBY_VERSION_IS_4_1 rb_define_method(cls, "RTYPEDDATA_P", sws_typed_RTYPEDDATA_P, 1); +#endif } #ifdef __cplusplus } #endif - diff --git a/spec/ruby/optional/capi/ext/util_spec.c b/spec/ruby/optional/capi/ext/util_spec.c index b5bde420d2..043da99ace 100644 --- a/spec/ruby/optional/capi/ext/util_spec.c +++ b/spec/ruby/optional/capi/ext/util_spec.c @@ -20,15 +20,11 @@ VALUE util_spec_rb_scan_args(VALUE self, VALUE argv, VALUE fmt, VALUE expected, a1 = a2 = a3 = a4 = a5 = a6 = INT2FIX(-1); -#ifdef RB_SCAN_ARGS_KEYWORDS if (*RSTRING_PTR(fmt) == 'k') { result = rb_scan_args_kw(RB_SCAN_ARGS_KEYWORDS, argc, args, RSTRING_PTR(fmt)+1, &a1, &a2, &a3, &a4, &a5, &a6); } else { -#endif result = rb_scan_args(argc, args, RSTRING_PTR(fmt), &a1, &a2, &a3, &a4, &a5, &a6); -#ifdef RB_SCAN_ARGS_KEYWORDS } -#endif switch(NUM2INT(expected)) { case 6: diff --git a/spec/ruby/optional/capi/fiber_spec.rb b/spec/ruby/optional/capi/fiber_spec.rb index 357033f860..c820ba17c2 100644 --- a/spec/ruby/optional/capi/fiber_spec.rb +++ b/spec/ruby/optional/capi/fiber_spec.rb @@ -1,5 +1,4 @@ require_relative 'spec_helper' -require 'fiber' load_extension('fiber') @@ -11,7 +10,7 @@ describe "C-API Fiber function" do describe "rb_fiber_current" do it "returns the current fiber" do result = @s.rb_fiber_current() - result.should be_an_instance_of(Fiber) + result.should.instance_of?(Fiber) result.should == Fiber.current end end @@ -20,9 +19,9 @@ describe "C-API Fiber function" do it "returns the fibers alive status" do fiber = Fiber.new { Fiber.yield } fiber.resume - @s.rb_fiber_alive_p(fiber).should be_true + @s.rb_fiber_alive_p(fiber).should == true fiber.resume - @s.rb_fiber_alive_p(fiber).should be_false + @s.rb_fiber_alive_p(fiber).should == false end end @@ -44,46 +43,44 @@ describe "C-API Fiber function" do describe "rb_fiber_new" do it "returns a new fiber" do fiber = @s.rb_fiber_new - fiber.should be_an_instance_of(Fiber) + fiber.should.instance_of?(Fiber) fiber.resume(42).should == "42" end end - ruby_version_is '3.1' do - describe "rb_fiber_raise" do - it "raises an exception on the resumed fiber" do - fiber = Fiber.new do - begin - Fiber.yield - rescue => error - error - end + describe "rb_fiber_raise" do + it "raises an exception on the resumed fiber" do + fiber = Fiber.new do + begin + Fiber.yield + rescue => error + error end + end - fiber.resume + fiber.resume - result = @s.rb_fiber_raise(fiber, "Boom!") - result.should be_an_instance_of(RuntimeError) - result.message.should == "Boom!" - end + result = @s.rb_fiber_raise(fiber, "Boom!") + result.should.instance_of?(RuntimeError) + result.message.should == "Boom!" + end - it "raises an exception on the transferred fiber" do - main = Fiber.current + it "raises an exception on the transferred fiber" do + main = Fiber.current - fiber = Fiber.new do - begin - main.transfer - rescue => error - error - end + fiber = Fiber.new do + begin + main.transfer + rescue => error + error end + end - fiber.transfer + fiber.transfer - result = @s.rb_fiber_raise(fiber, "Boom!") - result.should be_an_instance_of(RuntimeError) - result.message.should == "Boom!" - end + result = @s.rb_fiber_raise(fiber, "Boom!") + result.should.instance_of?(RuntimeError) + result.message.should == "Boom!" end end end diff --git a/spec/ruby/optional/capi/file_spec.rb b/spec/ruby/optional/capi/file_spec.rb index 12449b4e34..fd63aed316 100644 --- a/spec/ruby/optional/capi/file_spec.rb +++ b/spec/ruby/optional/capi/file_spec.rb @@ -5,13 +5,13 @@ load_extension('file') describe :rb_file_open, shared: true do it "raises an ArgumentError if passed an empty mode string" do touch @name - -> { @s.rb_file_open(@name, "") }.should raise_error(ArgumentError) + -> { @s.rb_file_open(@name, "") }.should.raise(ArgumentError) end it "opens a file in read-only mode with 'r'" do touch(@name) { |f| f.puts "readable" } @file = @s.send(@method, @name, "r") - @file.should be_an_instance_of(File) + @file.should.instance_of?(File) @file.read.chomp.should == "readable" end @@ -65,13 +65,13 @@ describe "C-API File function" do describe "FilePathValue" do it "returns a String argument unchanged" do obj = "path" - @s.FilePathValue(obj).should eql(obj) + @s.FilePathValue(obj).should.eql?(obj) end it "does not call #to_str on a String" do obj = +"path" obj.should_not_receive(:to_str) - @s.FilePathValue(obj).should eql(obj) + @s.FilePathValue(obj).should.eql?(obj) end it "calls #to_path to convert an object to a String" do diff --git a/spec/ruby/optional/capi/fixnum_spec.rb b/spec/ruby/optional/capi/fixnum_spec.rb index e691aa3893..73751c4895 100644 --- a/spec/ruby/optional/capi/fixnum_spec.rb +++ b/spec/ruby/optional/capi/fixnum_spec.rb @@ -27,7 +27,7 @@ describe "CApiFixnumSpecs" do platform_is c_long_size: 64 do # sizeof(long) > sizeof(int) it "raises a TypeError if passed nil" do - -> { @s.FIX2INT(nil) }.should raise_error(TypeError) + -> { @s.FIX2INT(nil) }.should.raise(TypeError) end it "converts a Float" do @@ -39,16 +39,16 @@ describe "CApiFixnumSpecs" do end it "raises a RangeError if the value does not fit a native int" do - -> { @s.FIX2INT(0x7fff_ffff+1) }.should raise_error(RangeError) - -> { @s.FIX2INT(-(1 << 31) - 1) }.should raise_error(RangeError) + -> { @s.FIX2INT(0x7fff_ffff+1) }.should.raise(RangeError) + -> { @s.FIX2INT(-(1 << 31) - 1) }.should.raise(RangeError) end it "raises a RangeError if the value is more than 32bits" do - -> { @s.FIX2INT(0xffff_ffff+1) }.should raise_error(RangeError) + -> { @s.FIX2INT(0xffff_ffff+1) }.should.raise(RangeError) end it "raises a RangeError if the value is more than 64bits" do - -> { @s.FIX2INT(0xffff_ffff_ffff_ffff+1) }.should raise_error(RangeError) + -> { @s.FIX2INT(0xffff_ffff_ffff_ffff+1) }.should.raise(RangeError) end it "calls #to_int to coerce the value" do @@ -76,7 +76,7 @@ describe "CApiFixnumSpecs" do platform_is c_long_size: 64 do # sizeof(long) > sizeof(int) it "raises a TypeError if passed nil" do - -> { @s.FIX2UINT(nil) }.should raise_error(TypeError) + -> { @s.FIX2UINT(nil) }.should.raise(TypeError) end it "converts a Float" do @@ -85,16 +85,16 @@ describe "CApiFixnumSpecs" do it "raises a RangeError if the value does not fit a native uint" do # Interestingly, on MRI FIX2UINT(-1) is allowed - -> { @s.FIX2UINT(0xffff_ffff+1) }.should raise_error(RangeError) - -> { @s.FIX2UINT(-(1 << 31) - 1) }.should raise_error(RangeError) + -> { @s.FIX2UINT(0xffff_ffff+1) }.should.raise(RangeError) + -> { @s.FIX2UINT(-(1 << 31) - 1) }.should.raise(RangeError) end it "raises a RangeError if the value is more than 32bits" do - -> { @s.FIX2UINT(0xffff_ffff+1) }.should raise_error(RangeError) + -> { @s.FIX2UINT(0xffff_ffff+1) }.should.raise(RangeError) end it "raises a RangeError if the value is more than 64bits" do - -> { @s.FIX2UINT(0xffff_ffff_ffff_ffff+1) }.should raise_error(RangeError) + -> { @s.FIX2UINT(0xffff_ffff_ffff_ffff+1) }.should.raise(RangeError) end end end diff --git a/spec/ruby/optional/capi/float_spec.rb b/spec/ruby/optional/capi/float_spec.rb index 4b98902b59..5b02483b28 100644 --- a/spec/ruby/optional/capi/float_spec.rb +++ b/spec/ruby/optional/capi/float_spec.rb @@ -23,8 +23,8 @@ describe "CApiFloatSpecs" do describe "rb_Float" do it "creates a new Float from a String" do f = @f.rb_Float("101.99") - f.should be_kind_of(Float) - f.should eql(101.99) + f.should.is_a?(Float) + f.should.eql?(101.99) end end diff --git a/spec/ruby/optional/capi/gc_spec.rb b/spec/ruby/optional/capi/gc_spec.rb index d9661328ab..6695026c6f 100644 --- a/spec/ruby/optional/capi/gc_spec.rb +++ b/spec/ruby/optional/capi/gc_spec.rb @@ -10,9 +10,9 @@ describe "CApiGCSpecs" do describe "rb_gc_register_address" do it "correctly gets the value from a registered address" do @f.registered_tagged_address.should == 10 - @f.registered_tagged_address.should equal(@f.registered_tagged_address) + @f.registered_tagged_address.should.equal?(@f.registered_tagged_address) @f.registered_reference_address.should == "Globally registered data" - @f.registered_reference_address.should equal(@f.registered_reference_address) + @f.registered_reference_address.should.equal?(@f.registered_reference_address) end it "keeps the value alive even if the value is assigned after rb_gc_register_address() is called" do @@ -67,22 +67,22 @@ describe "CApiGCSpecs" do it "enables GC when disabled" do GC.disable - @f.rb_gc_enable.should be_true + @f.rb_gc_enable.should == true end it "GC stays enabled when enabled" do GC.enable - @f.rb_gc_enable.should be_false + @f.rb_gc_enable.should == false end it "disables GC when enabled" do GC.enable - @f.rb_gc_disable.should be_false + @f.rb_gc_disable.should == false end it "GC stays disabled when disabled" do GC.disable - @f.rb_gc_disable.should be_true + @f.rb_gc_disable.should == true end end @@ -100,13 +100,13 @@ describe "CApiGCSpecs" do -> { @f.rb_gc_adjust_memory_usage(8) @f.rb_gc_adjust_memory_usage(-8) - }.should_not raise_error + }.should_not.raise end end describe "rb_gc_register_mark_object" do it "can be called with an object" do - @f.rb_gc_register_mark_object(Object.new).should be_nil + @f.rb_gc_register_mark_object(Object.new).should == nil end it "keeps the value alive even if the value is not referenced by any Ruby object" do @@ -116,11 +116,11 @@ describe "CApiGCSpecs" do describe "rb_gc_latest_gc_info" do it "raises a TypeError when hash or symbol not given" do - -> { @f.rb_gc_latest_gc_info("foo") }.should raise_error(TypeError) + -> { @f.rb_gc_latest_gc_info("foo") }.should.raise(TypeError) end it "raises an ArgumentError when unknown symbol given" do - -> { @f.rb_gc_latest_gc_info(:unknown) }.should raise_error(ArgumentError) + -> { @f.rb_gc_latest_gc_info(:unknown) }.should.raise(ArgumentError) end it "returns the populated hash when a hash is given" do @@ -130,7 +130,7 @@ describe "CApiGCSpecs" do end it "returns a value when symbol is given" do - @f.rb_gc_latest_gc_info(:state).should be_kind_of(Symbol) + @f.rb_gc_latest_gc_info(:state).should.is_a?(Symbol) end end end diff --git a/spec/ruby/optional/capi/globals_spec.rb b/spec/ruby/optional/capi/globals_spec.rb index 48677620bc..03316c256c 100644 --- a/spec/ruby/optional/capi/globals_spec.rb +++ b/spec/ruby/optional/capi/globals_spec.rb @@ -41,14 +41,19 @@ describe "CApiGlobalSpecs" do @f.sb_get_global_value.should == "XYZ" end + run = 0 + it "rb_define_readonly_variable should define a new readonly global variable" do + name = "ro_gvar#{run += 1}" + eval <<~RUBY # Check the gvar doesn't exist and ensure rb_gv_get doesn't implicitly declare the gvar, # otherwise the rb_define_readonly_variable call will conflict. - suppress_warning { @f.sb_gv_get("ro_gvar") } .should == nil + suppress_warning { @f.sb_gv_get("#{name}") }.should == nil - @f.rb_define_readonly_variable("ro_gvar", 15) - $ro_gvar.should == 15 - -> { $ro_gvar = 10 }.should raise_error(NameError) + @f.rb_define_readonly_variable("#{name}", 15) + $#{name}.should == 15 + -> { $#{name} = 10 }.should.raise(NameError) + RUBY end it "rb_define_hooked_variable should define a C hooked global variable" do @@ -75,7 +80,7 @@ describe "CApiGlobalSpecs" do end it "is read-only" do - -> { $virtual_variable_default_accessors = 10 }.should raise_error(NameError, /read-only/) + -> { $virtual_variable_default_accessors = 10 }.should.raise(NameError, /read-only/) end it "returns false with the default getter" do @@ -159,7 +164,7 @@ describe "CApiGlobalSpecs" do it "returns $stdin" do $stdin = @stream - @f.rb_stdin.should equal($stdin) + @f.rb_stdin.should.equal?($stdin) end end @@ -170,7 +175,7 @@ describe "CApiGlobalSpecs" do it "returns $stdout" do $stdout = @stream - @f.rb_stdout.should equal($stdout) + @f.rb_stdout.should.equal?($stdout) end end @@ -181,7 +186,7 @@ describe "CApiGlobalSpecs" do it "returns $stderr" do $stderr = @stream - @f.rb_stderr.should equal($stderr) + @f.rb_stderr.should.equal?($stderr) end end @@ -192,7 +197,7 @@ describe "CApiGlobalSpecs" do it "is an alias of rb_stdout" do $stdout = @stream - @f.rb_defout.should equal($stdout) + @f.rb_defout.should.equal?($stdout) end end end @@ -213,7 +218,7 @@ describe "CApiGlobalSpecs" do end it "returns nil by default" do - @f.rb_output_rs.should be_nil + @f.rb_output_rs.should == nil end it "returns the value of $\\" do @@ -232,7 +237,7 @@ describe "CApiGlobalSpecs" do end it "returns nil by default" do - @f.rb_output_fs.should be_nil + @f.rb_output_fs.should == nil end it "returns the value of $\\" do @@ -258,7 +263,7 @@ describe "CApiGlobalSpecs" do end Thread.pass while thr.status and !running - $_.should be_nil + $_.should == nil thr.join end @@ -285,7 +290,7 @@ describe "CApiGlobalSpecs" do end Thread.pass while thr.status and !running - $_.should be_nil + $_.should == nil thr.join end diff --git a/spec/ruby/optional/capi/hash_spec.rb b/spec/ruby/optional/capi/hash_spec.rb index 08e03fcb26..842bc82b97 100644 --- a/spec/ruby/optional/capi/hash_spec.rb +++ b/spec/ruby/optional/capi/hash_spec.rb @@ -36,7 +36,7 @@ describe "C-API Hash function" do obj = mock("rb_hash no to_int") obj.should_receive(:hash).and_return(nil) - -> { @s.rb_hash(obj) }.should raise_error(TypeError) + -> { @s.rb_hash(obj) }.should.raise(TypeError) end end @@ -46,23 +46,21 @@ describe "C-API Hash function" do end it "creates a hash with no default proc" do - @s.rb_hash_new {}.default_proc.should be_nil + @s.rb_hash_new {}.default_proc.should == nil end end - ruby_version_is '3.2' do - describe "rb_hash_new_capa" do - it "returns a new hash" do - @s.rb_hash_new_capa(3).should == {} - end + describe "rb_hash_new_capa" do + it "returns a new hash" do + @s.rb_hash_new_capa(3).should == {} + end - it "creates a hash with no default proc" do - @s.rb_hash_new_capa(3) {}.default_proc.should be_nil - end + it "creates a hash with no default proc" do + @s.rb_hash_new_capa(3) {}.default_proc.should == nil + end - it "raises RuntimeError when negative index is provided" do - -> { @s.rb_hash_new_capa(-1) }.should raise_error(RuntimeError, "st_table too big") - end + it "raises RuntimeError when negative index is provided" do + -> { @s.rb_hash_new_capa(-1) }.should.raise(RuntimeError, "st_table too big") end end @@ -79,13 +77,13 @@ describe "C-API Hash function" do hsh = {} dup = @s.rb_hash_dup(hsh) dup.should == hsh - dup.should_not equal(hsh) + dup.should_not.equal?(hsh) end end describe "rb_hash_freeze" do it "freezes the hash" do - @s.rb_hash_freeze({}).frozen?.should be_true + @s.rb_hash_freeze({}).frozen?.should == true end end @@ -98,13 +96,13 @@ describe "C-API Hash function" do it "returns the default value if it exists" do hsh = Hash.new(0) @s.rb_hash_aref(hsh, :chunky).should == 0 - @s.rb_hash_aref_nil(hsh, :chunky).should be_false + @s.rb_hash_aref_nil(hsh, :chunky).should == false end it "returns nil if the key does not exist" do hsh = { } - @s.rb_hash_aref(hsh, :chunky).should be_nil - @s.rb_hash_aref_nil(hsh, :chunky).should be_true + @s.rb_hash_aref(hsh, :chunky).should == nil + @s.rb_hash_aref_nil(hsh, :chunky).should == true end end @@ -119,7 +117,7 @@ describe "C-API Hash function" do describe "rb_hash_clear" do it "returns self that cleared keys and values" do hsh = { :key => 'value' } - @s.rb_hash_clear(hsh).should equal(hsh) + @s.rb_hash_clear(hsh).should.equal?(hsh) hsh.should == {} end end @@ -140,7 +138,7 @@ describe "C-API Hash function" do end it "returns an Enumerator when no block is passed" do - @s.rb_hash_delete_if({a: 1}).should be_an_instance_of(Enumerator) + @s.rb_hash_delete_if({a: 1}).should.instance_of?(Enumerator) end end @@ -155,11 +153,11 @@ describe "C-API Hash function" do it "raises a KeyError if the key is not found and default is set" do @hsh.default = :d - -> { @s.rb_hash_fetch(@hsh, :c) }.should raise_error(KeyError) + -> { @s.rb_hash_fetch(@hsh, :c) }.should.raise(KeyError) end it "raises a KeyError if the key is not found and no default is set" do - -> { @s.rb_hash_fetch(@hsh, :c) }.should raise_error(KeyError) + -> { @s.rb_hash_fetch(@hsh, :c) }.should.raise(KeyError) end context "when key is not found" do @@ -268,14 +266,14 @@ describe "C-API Hash function" do it "does not return the default value if it exists" do hsh = Hash.new(0) - @s.rb_hash_lookup(hsh, :chunky).should be_nil - @s.rb_hash_lookup_nil(hsh, :chunky).should be_true + @s.rb_hash_lookup(hsh, :chunky).should == nil + @s.rb_hash_lookup_nil(hsh, :chunky).should == true end it "returns nil if the key does not exist" do hsh = { } - @s.rb_hash_lookup(hsh, :chunky).should be_nil - @s.rb_hash_lookup_nil(hsh, :chunky).should be_true + @s.rb_hash_lookup(hsh, :chunky).should == nil + @s.rb_hash_lookup_nil(hsh, :chunky).should == true end describe "rb_hash_lookup2" do @@ -293,7 +291,7 @@ describe "C-API Hash function" do it "returns undefined if that is the default value specified" do hsh = Hash.new(0) - @s.rb_hash_lookup2_default_undef(hsh, :chunky).should be_true + @s.rb_hash_lookup2_default_undef(hsh, :chunky).should == true end end end @@ -324,20 +322,20 @@ describe "C-API Hash function" do end it "raises a TypeError if the argument does not respond to #to_hash" do - -> { @s.rb_Hash(42) }.should raise_error(TypeError) + -> { @s.rb_Hash(42) }.should.raise(TypeError) end it "raises a TypeError if #to_hash does not return a hash" do h = BasicObject.new def h.to_hash; 42; end - -> { @s.rb_Hash(h) }.should raise_error(TypeError) + -> { @s.rb_Hash(h) }.should.raise(TypeError) end end describe "hash code functions" do it "computes a deterministic number" do hash_code = @s.compute_a_hash_code(53) - hash_code.should be_an_instance_of(Integer) + hash_code.should.instance_of?(Integer) hash_code.should == @s.compute_a_hash_code(53) @s.compute_a_hash_code(90).should == @s.compute_a_hash_code(90) end diff --git a/spec/ruby/optional/capi/integer_spec.rb b/spec/ruby/optional/capi/integer_spec.rb index 089872381c..f177374569 100644 --- a/spec/ruby/optional/capi/integer_spec.rb +++ b/spec/ruby/optional/capi/integer_spec.rb @@ -1,4 +1,4 @@ -# -*- encoding: binary -*- +# encoding: binary require_relative 'spec_helper' load_extension("integer") diff --git a/spec/ruby/optional/capi/io_spec.rb b/spec/ruby/optional/capi/io_spec.rb index dbfaf556f6..35bd856e00 100644 --- a/spec/ruby/optional/capi/io_spec.rb +++ b/spec/ruby/optional/capi/io_spec.rb @@ -15,7 +15,7 @@ describe "C-API IO function" do end after :each do - @io.close unless @io.closed? + @io.close rm_r @name end @@ -34,7 +34,7 @@ describe "C-API IO function" do end it "returns the io" do - @o.rb_io_addstr(@io, "rb_io_addstr data").should eql(@io) + @o.rb_io_addstr(@io, "rb_io_addstr data").should.eql?(@io) end end @@ -118,15 +118,15 @@ describe "C-API IO function" do end after :each do - @io.close unless @io.closed? + @io.close rm_r @name end describe "rb_io_close" do it "closes an IO object" do - @io.closed?.should be_false + @io.closed?.should == false @o.rb_io_close(@io) - @io.closed?.should be_true + @io.closed?.should == true end end @@ -136,19 +136,19 @@ describe "C-API IO function" do end it "returns nil for non IO objects" do - @o.rb_io_check_io({}).should be_nil + @o.rb_io_check_io({}).should == nil end end describe "rb_io_check_closed" do it "does not raise an exception if the IO is not closed" do # The MRI function is void, so we use should_not raise_error - -> { @o.rb_io_check_closed(@io) }.should_not raise_error + -> { @o.rb_io_check_closed(@io) }.should_not.raise end it "raises an error if the IO is closed" do @io.close - -> { @o.rb_io_check_closed(@io) }.should raise_error(IOError) + -> { @o.rb_io_check_closed(@io) }.should.raise(IOError) end end @@ -157,7 +157,7 @@ describe "C-API IO function" do it "returns true when nonblock flag is set" do require 'io/nonblock' @o.rb_io_set_nonblock(@io) - @io.nonblock?.should be_true + @io.nonblock?.should == true end end end @@ -166,13 +166,13 @@ describe "C-API IO function" do # object is frozen, *not* if it's tainted. describe "rb_io_taint_check" do it "does not raise an exception if the IO is not frozen" do - -> { @o.rb_io_taint_check(@io) }.should_not raise_error + -> { @o.rb_io_taint_check(@io) }.should_not.raise end it "raises an exception if the IO is frozen" do @io.freeze - -> { @o.rb_io_taint_check(@io) }.should raise_error(RuntimeError) + -> { @o.rb_io_taint_check(@io) }.should.raise(RuntimeError) end end @@ -186,7 +186,7 @@ describe "C-API IO function" do it "raises IOError if the IO is closed" do @io.close - -> { @o.GetOpenFile_fd(@io) }.should raise_error(IOError, "closed stream") + -> { @o.GetOpenFile_fd(@io) }.should.raise(IOError, "closed stream") end end @@ -197,7 +197,7 @@ describe "C-API IO function" do it "sets binmode" do @o.rb_io_binmode(@io) - @io.binmode?.should be_true + @io.binmode?.should == true end end end @@ -213,24 +213,24 @@ describe "C-API IO function" do end after :each do - @r_io.close unless @r_io.closed? - @w_io.close unless @w_io.closed? - @rw_io.close unless @rw_io.closed? + @r_io.close + @w_io.close + @rw_io.close rm_r @name end describe "rb_io_check_readable" do it "does not raise an exception if the IO is opened for reading" do # The MRI function is void, so we use should_not raise_error - -> { @o.rb_io_check_readable(@r_io) }.should_not raise_error + -> { @o.rb_io_check_readable(@r_io) }.should_not.raise end it "does not raise an exception if the IO is opened for read and write" do - -> { @o.rb_io_check_readable(@rw_io) }.should_not raise_error + -> { @o.rb_io_check_readable(@rw_io) }.should_not.raise end it "raises an IOError if the IO is not opened for reading" do - -> { @o.rb_io_check_readable(@w_io) }.should raise_error(IOError) + -> { @o.rb_io_check_readable(@w_io) }.should.raise(IOError) end end @@ -238,70 +238,68 @@ describe "C-API IO function" do describe "rb_io_check_writable" do it "does not raise an exception if the IO is opened for writing" do # The MRI function is void, so we use should_not raise_error - -> { @o.rb_io_check_writable(@w_io) }.should_not raise_error + -> { @o.rb_io_check_writable(@w_io) }.should_not.raise end it "does not raise an exception if the IO is opened for read and write" do - -> { @o.rb_io_check_writable(@rw_io) }.should_not raise_error + -> { @o.rb_io_check_writable(@rw_io) }.should_not.raise end it "raises an IOError if the IO is not opened for reading" do - -> { @o.rb_io_check_writable(@r_io) }.should raise_error(IOError) + -> { @o.rb_io_check_writable(@r_io) }.should.raise(IOError) end end describe "rb_io_wait_writable" do it "returns false if there is no error condition" do @o.errno = 0 - @o.rb_io_wait_writable(@w_io).should be_false + @o.rb_io_wait_writable(@w_io).should == false end it "raises an IOError if the IO is closed" do @w_io.close - -> { @o.rb_io_wait_writable(@w_io) }.should raise_error(IOError) + -> { @o.rb_io_wait_writable(@w_io) }.should.raise(IOError) end end - ruby_version_is "3.1" do - describe "rb_io_maybe_wait_writable" do - it "returns mask for events if operation was interrupted" do - @o.rb_io_maybe_wait_writable(Errno::EINTR::Errno, @w_io, nil).should == IO::WRITABLE - end + describe "rb_io_maybe_wait_writable" do + it "returns mask for events if operation was interrupted" do + @o.rb_io_maybe_wait_writable(Errno::EINTR::Errno, @w_io, nil).should == IO::WRITABLE + end - it "returns 0 if there is no error condition" do - @o.rb_io_maybe_wait_writable(0, @w_io, nil).should == 0 - end + it "returns 0 if there is no error condition" do + @o.rb_io_maybe_wait_writable(0, @w_io, nil).should == 0 + end - it "raises an IOError if the IO is closed" do - @w_io.close - -> { @o.rb_io_maybe_wait_writable(0, @w_io, nil) }.should raise_error(IOError, "closed stream") - end + it "raises an IOError if the IO is closed" do + @w_io.close + -> { @o.rb_io_maybe_wait_writable(0, @w_io, nil) }.should.raise(IOError, "closed stream") + end - it "raises an IOError if the IO is not initialized" do - -> { @o.rb_io_maybe_wait_writable(0, IO.allocate, nil) }.should raise_error(IOError, "uninitialized stream") - end + it "raises an IOError if the IO is not initialized" do + -> { @o.rb_io_maybe_wait_writable(0, IO.allocate, nil) }.should.raise(IOError, "uninitialized stream") + end - it "can be interrupted" do - IOSpec.exhaust_write_buffer(@w_io) - start = Process.clock_gettime(Process::CLOCK_MONOTONIC) + it "can be interrupted" do + IOSpec.exhaust_write_buffer(@w_io) + start = Process.clock_gettime(Process::CLOCK_MONOTONIC) - t = Thread.new do - @o.rb_io_maybe_wait_writable(0, @w_io, 10) - end + t = Thread.new do + @o.rb_io_maybe_wait_writable(0, @w_io, 10) + end - Thread.pass until t.stop? - t.kill - t.join + Thread.pass until t.stop? + t.kill + t.join - finish = Process.clock_gettime(Process::CLOCK_MONOTONIC) - (finish - start).should < 9 - end + finish = Process.clock_gettime(Process::CLOCK_MONOTONIC) + (finish - start).should < 9 end end describe "rb_thread_fd_writable" do it "waits til an fd is ready for writing" do - @o.rb_thread_fd_writable(@w_io).should be_nil + @o.rb_thread_fd_writable(@w_io).should == nil end end @@ -324,14 +322,14 @@ describe "C-API IO function" do describe "rb_io_wait_readable" do it "returns false if there is no error condition" do @o.errno = 0 - @o.rb_io_wait_readable(@r_io, false).should be_false + @o.rb_io_wait_readable(@r_io, false).should == false end it "raises and IOError if passed a closed stream" do @r_io.close -> { @o.rb_io_wait_readable(@r_io, false) - }.should raise_error(IOError) + }.should.raise(IOError) end it "blocks until the io is readable and returns true" do @@ -342,59 +340,57 @@ describe "C-API IO function" do end @o.errno = Errno::EAGAIN.new.errno - @o.rb_io_wait_readable(@r_io, true).should be_true + @o.rb_io_wait_readable(@r_io, true).should == true @o.instance_variable_get(:@read_data).should == "rb_io_wait_re" thr.join end end - ruby_version_is "3.1" do - describe "rb_io_maybe_wait_readable" do - it "returns mask for events if operation was interrupted" do - @o.rb_io_maybe_wait_readable(Errno::EINTR::Errno, @r_io, nil, false).should == IO::READABLE - end - - it "returns 0 if there is no error condition" do - @o.rb_io_maybe_wait_readable(0, @r_io, nil, false).should == 0 - end - - it "blocks until the io is readable and returns events that actually occurred" do - @o.instance_variable_set :@write_data, false - thr = Thread.new do - Thread.pass until @o.instance_variable_get(:@write_data) - @w_io.write "rb_io_wait_readable" - end + describe "rb_io_maybe_wait_readable" do + it "returns mask for events if operation was interrupted" do + @o.rb_io_maybe_wait_readable(Errno::EINTR::Errno, @r_io, nil, false).should == IO::READABLE + end - @o.rb_io_maybe_wait_readable(Errno::EAGAIN::Errno, @r_io, IO::READABLE, true).should == IO::READABLE - @o.instance_variable_get(:@read_data).should == "rb_io_wait_re" + it "returns 0 if there is no error condition" do + @o.rb_io_maybe_wait_readable(0, @r_io, nil, false).should == 0 + end - thr.join + it "blocks until the io is readable and returns events that actually occurred" do + @o.instance_variable_set :@write_data, false + thr = Thread.new do + Thread.pass until @o.instance_variable_get(:@write_data) + @w_io.write "rb_io_wait_readable" end - it "can be interrupted" do - start = Process.clock_gettime(Process::CLOCK_MONOTONIC) + @o.rb_io_maybe_wait_readable(Errno::EAGAIN::Errno, @r_io, IO::READABLE, true).should == IO::READABLE + @o.instance_variable_get(:@read_data).should == "rb_io_wait_re" - t = Thread.new do - @o.rb_io_maybe_wait_readable(0, @r_io, 10, false) - end + thr.join + end - Thread.pass until t.stop? - t.kill - t.join + it "can be interrupted" do + start = Process.clock_gettime(Process::CLOCK_MONOTONIC) - finish = Process.clock_gettime(Process::CLOCK_MONOTONIC) - (finish - start).should < 9 + t = Thread.new do + @o.rb_io_maybe_wait_readable(0, @r_io, 10, false) end - it "raises an IOError if the IO is closed" do - @r_io.close - -> { @o.rb_io_maybe_wait_readable(0, @r_io, nil, false) }.should raise_error(IOError, "closed stream") - end + Thread.pass until t.stop? + t.kill + t.join - it "raises an IOError if the IO is not initialized" do - -> { @o.rb_io_maybe_wait_readable(0, IO.allocate, nil, false) }.should raise_error(IOError, "uninitialized stream") - end + finish = Process.clock_gettime(Process::CLOCK_MONOTONIC) + (finish - start).should < 9 + end + + it "raises an IOError if the IO is closed" do + @r_io.close + -> { @o.rb_io_maybe_wait_readable(0, @r_io, nil, false) }.should.raise(IOError, "closed stream") + end + + it "raises an IOError if the IO is not initialized" do + -> { @o.rb_io_maybe_wait_readable(0, IO.allocate, nil, false) }.should.raise(IOError, "uninitialized stream") end end end @@ -410,7 +406,7 @@ describe "C-API IO function" do Thread.pass until start - @o.rb_thread_wait_fd(@r_io).should be_nil + @o.rb_thread_wait_fd(@r_io).should == nil thr.join end @@ -437,229 +433,225 @@ describe "C-API IO function" do end end - ruby_version_is "3.1" do - describe "rb_io_maybe_wait" 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_maybe_wait" - end + describe "rb_io_maybe_wait" 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_maybe_wait" + end - Thread.pass until start + Thread.pass until start - @o.rb_io_maybe_wait(Errno::EAGAIN::Errno, @r_io, IO::READABLE, nil).should == IO::READABLE + @o.rb_io_maybe_wait(Errno::EAGAIN::Errno, @r_io, IO::READABLE, nil).should == IO::READABLE - thr.join - end + thr.join + end - it "returns mask for events if operation was interrupted" do - @o.rb_io_maybe_wait(Errno::EINTR::Errno, @w_io, IO::WRITABLE, nil).should == IO::WRITABLE - end + it "returns mask for events if operation was interrupted" do + @o.rb_io_maybe_wait(Errno::EINTR::Errno, @w_io, IO::WRITABLE, nil).should == IO::WRITABLE + end - it "raises an IOError if the IO is closed" do - @w_io.close - -> { @o.rb_io_maybe_wait(0, @w_io, IO::WRITABLE, nil) }.should raise_error(IOError, "closed stream") - end + it "raises an IOError if the IO is closed" do + @w_io.close + -> { @o.rb_io_maybe_wait(0, @w_io, IO::WRITABLE, nil) }.should.raise(IOError, "closed stream") + end - it "raises an IOError if the IO is not initialized" do - -> { @o.rb_io_maybe_wait(0, IO.allocate, IO::WRITABLE, nil) }.should raise_error(IOError, "uninitialized stream") + it "raises an IOError if the IO is not initialized" do + -> { @o.rb_io_maybe_wait(0, IO.allocate, IO::WRITABLE, nil) }.should.raise(IOError, "uninitialized stream") + end + + it "can be interrupted when waiting for READABLE event" do + start = Process.clock_gettime(Process::CLOCK_MONOTONIC) + + t = Thread.new do + @o.rb_io_maybe_wait(0, @r_io, IO::READABLE, 10) end - it "can be interrupted when waiting for READABLE event" do - start = Process.clock_gettime(Process::CLOCK_MONOTONIC) + Thread.pass until t.stop? + t.kill + t.join - t = Thread.new do - @o.rb_io_maybe_wait(0, @r_io, IO::READABLE, 10) - end + finish = Process.clock_gettime(Process::CLOCK_MONOTONIC) + (finish - start).should < 9 + end - Thread.pass until t.stop? - t.kill - t.join + it "can be interrupted when waiting for WRITABLE event" do + IOSpec.exhaust_write_buffer(@w_io) + start = Process.clock_gettime(Process::CLOCK_MONOTONIC) - finish = Process.clock_gettime(Process::CLOCK_MONOTONIC) - (finish - start).should < 9 + t = Thread.new do + @o.rb_io_maybe_wait(0, @w_io, IO::WRITABLE, 10) end - it "can be interrupted when waiting for WRITABLE event" do - IOSpec.exhaust_write_buffer(@w_io) - start = Process.clock_gettime(Process::CLOCK_MONOTONIC) + Thread.pass until t.stop? + t.kill + t.join - t = Thread.new do - @o.rb_io_maybe_wait(0, @w_io, IO::WRITABLE, 10) - end + finish = Process.clock_gettime(Process::CLOCK_MONOTONIC) + (finish - start).should < 9 + end + end - Thread.pass until t.stop? - t.kill - t.join + describe "rb_io_mode" do + it "returns the mode" do + (@o.rb_io_mode(@r_io) & 0b11).should == 0b01 + (@o.rb_io_mode(@w_io) & 0b11).should == 0b10 + (@o.rb_io_mode(@rw_io) & 0b11).should == 0b11 + end + end - finish = Process.clock_gettime(Process::CLOCK_MONOTONIC) - (finish - start).should < 9 - end + describe "rb_io_path" do + it "returns the IO#path" do + @o.rb_io_path(@r_io).should == @r_io.path + @o.rb_io_path(@rw_io).should == @rw_io.path + @o.rb_io_path(@rw_io).should == @name end end - ruby_version_is "3.3" do - describe "rb_io_mode" do - it "returns the mode" do - (@o.rb_io_mode(@r_io) & 0b11).should == 0b01 - (@o.rb_io_mode(@w_io) & 0b11).should == 0b10 - (@o.rb_io_mode(@rw_io) & 0b11).should == 0b11 - end + describe "rb_io_closed_p" do + it "returns false when io is not closed" do + @o.rb_io_closed_p(@r_io).should == false + @r_io.closed?.should == false end - describe "rb_io_path" do - it "returns the IO#path" do - @o.rb_io_path(@r_io).should == @r_io.path - @o.rb_io_path(@rw_io).should == @rw_io.path - @o.rb_io_path(@rw_io).should == @name - end + it "returns true when io is closed" do + @r_io.close + + @o.rb_io_closed_p(@r_io).should == true + @r_io.closed?.should == true end + end - describe "rb_io_closed_p" do - it "returns false when io is not closed" do - @o.rb_io_closed_p(@r_io).should == false - @r_io.closed?.should == false + quarantine! do # "Errno::EBADF: Bad file descriptor" at closing @r_io, @rw_io etc in the after :each hook + describe "rb_io_open_descriptor" do + it "creates a new IO instance" do + io = @o.rb_io_open_descriptor(File, @r_io.fileno, 0, "a.txt", 60, "US-ASCII", "UTF-8", 0, {}) + io.should.is_a?(IO) end - it "returns true when io is closed" do - @r_io.close + it "return an instance of the specified class" do + io = @o.rb_io_open_descriptor(File, @r_io.fileno, 0, "a.txt", 60, "US-ASCII", "UTF-8", 0, {}) + io.class.should == File - @o.rb_io_closed_p(@r_io).should == true - @r_io.closed?.should == true + io = @o.rb_io_open_descriptor(IO, @r_io.fileno, 0, "a.txt", 60, "US-ASCII", "UTF-8", 0, {}) + io.class.should == IO end - end - quarantine! do # "Errno::EBADF: Bad file descriptor" at closing @r_io, @rw_io etc in the after :each hook - describe "rb_io_open_descriptor" do - it "creates a new IO instance" do - io = @o.rb_io_open_descriptor(File, @r_io.fileno, 0, "a.txt", 60, "US-ASCII", "UTF-8", 0, {}) - io.should.is_a?(IO) - end - - it "return an instance of the specified class" do - io = @o.rb_io_open_descriptor(File, @r_io.fileno, 0, "a.txt", 60, "US-ASCII", "UTF-8", 0, {}) - io.class.should == File + it "sets the specified file descriptor" do + io = @o.rb_io_open_descriptor(File, @r_io.fileno, 0, "a.txt", 60, "US-ASCII", "UTF-8", 0, {}) + io.fileno.should == @r_io.fileno + end - io = @o.rb_io_open_descriptor(IO, @r_io.fileno, 0, "a.txt", 60, "US-ASCII", "UTF-8", 0, {}) - io.class.should == IO - end + it "sets the specified path" do + io = @o.rb_io_open_descriptor(File, @r_io.fileno, 0, "a.txt", 60, "US-ASCII", "UTF-8", 0, {}) + io.path.should == "a.txt" + end - it "sets the specified file descriptor" do - io = @o.rb_io_open_descriptor(File, @r_io.fileno, 0, "a.txt", 60, "US-ASCII", "UTF-8", 0, {}) - io.fileno.should == @r_io.fileno - end + it "sets the specified mode" do + io = @o.rb_io_open_descriptor(File, @r_io.fileno, CApiIOSpecs::FMODE_BINMODE, "a.txt", 60, "US-ASCII", "UTF-8", 0, {}) + io.should.binmode? - it "sets the specified path" do - io = @o.rb_io_open_descriptor(File, @r_io.fileno, 0, "a.txt", 60, "US-ASCII", "UTF-8", 0, {}) - io.path.should == "a.txt" - end + io = @o.rb_io_open_descriptor(File, @r_io.fileno, CApiIOSpecs::FMODE_TEXTMODE, "a.txt", 60, "US-ASCII", "UTF-8", 0, {}) + io.should_not.binmode? + end - it "sets the specified mode" do - io = @o.rb_io_open_descriptor(File, @r_io.fileno, CApiIOSpecs::FMODE_BINMODE, "a.txt", 60, "US-ASCII", "UTF-8", 0, {}) - io.should.binmode? + it "sets the specified timeout" do + io = @o.rb_io_open_descriptor(File, @r_io.fileno, 0, "a.txt", 60, "US-ASCII", "UTF-8", 0, {}) + io.timeout.should == 60 + end - io = @o.rb_io_open_descriptor(File, @r_io.fileno, CApiIOSpecs::FMODE_TEXTMODE, "a.txt", 60, "US-ASCII", "UTF-8", 0, {}) - io.should_not.binmode? - end + it "sets the specified internal encoding" do + io = @o.rb_io_open_descriptor(File, @r_io.fileno, 0, "a.txt", 60, "US-ASCII", "UTF-8", 0, {}) + io.internal_encoding.should == Encoding::US_ASCII + end - it "sets the specified timeout" do - io = @o.rb_io_open_descriptor(File, @r_io.fileno, 0, "a.txt", 60, "US-ASCII", "UTF-8", 0, {}) - io.timeout.should == 60 - end + it "sets the specified external encoding" do + io = @o.rb_io_open_descriptor(File, @r_io.fileno, 0, "a.txt", 60, "US-ASCII", "UTF-8", 0, {}) + io.external_encoding.should == Encoding::UTF_8 + end - it "sets the specified internal encoding" do - io = @o.rb_io_open_descriptor(File, @r_io.fileno, 0, "a.txt", 60, "US-ASCII", "UTF-8", 0, {}) - io.internal_encoding.should == Encoding::US_ASCII - end + it "does not apply the specified encoding flags" do + name = tmp("rb_io_open_descriptor_specs") + File.write(name, "123\r\n456\n89") + file = File.open(name, "r") - it "sets the specified external encoding" do - io = @o.rb_io_open_descriptor(File, @r_io.fileno, 0, "a.txt", 60, "US-ASCII", "UTF-8", 0, {}) - io.external_encoding.should == Encoding::UTF_8 - end + io = @o.rb_io_open_descriptor(File, file.fileno, CApiIOSpecs::FMODE_READABLE, "a.txt", 60, "US-ASCII", "UTF-8", CApiIOSpecs::ECONV_UNIVERSAL_NEWLINE_DECORATOR, {}) + io.read_nonblock(20).should == "123\r\n456\n89" + ensure + file.close + rm_r name + end - it "does not apply the specified encoding flags" do - name = tmp("rb_io_open_descriptor_specs") - File.write(name, "123\r\n456\n89") - file = File.open(name, "r") + it "ignores the IO open options" do + io = @o.rb_io_open_descriptor(File, @r_io.fileno, 0, "a.txt", 60, "US-ASCII", "UTF-8", 0, {external_encoding: "windows-1251"}) + io.external_encoding.should == Encoding::UTF_8 - io = @o.rb_io_open_descriptor(File, file.fileno, CApiIOSpecs::FMODE_READABLE, "a.txt", 60, "US-ASCII", "UTF-8", CApiIOSpecs::ECONV_UNIVERSAL_NEWLINE_DECORATOR, {}) - io.read_nonblock(20).should == "123\r\n456\n89" - ensure - file.close - rm_r name - end + io = @o.rb_io_open_descriptor(File, @r_io.fileno, 0, "a.txt", 60, "US-ASCII", "UTF-8", 0, {internal_encoding: "windows-1251"}) + io.internal_encoding.should == Encoding::US_ASCII - it "ignores the IO open options" do - io = @o.rb_io_open_descriptor(File, @r_io.fileno, 0, "a.txt", 60, "US-ASCII", "UTF-8", 0, {external_encoding: "windows-1251"}) - io.external_encoding.should == Encoding::UTF_8 + io = @o.rb_io_open_descriptor(File, @r_io.fileno, 0, "a.txt", 60, "US-ASCII", "UTF-8", 0, {encoding: "windows-1251:binary"}) + io.external_encoding.should == Encoding::UTF_8 + io.internal_encoding.should == Encoding::US_ASCII - io = @o.rb_io_open_descriptor(File, @r_io.fileno, 0, "a.txt", 60, "US-ASCII", "UTF-8", 0, {internal_encoding: "windows-1251"}) - io.internal_encoding.should == Encoding::US_ASCII + io = @o.rb_io_open_descriptor(File, @r_io.fileno, 0, "a.txt", 60, "US-ASCII", "UTF-8", 0, {textmode: false}) + io.should_not.binmode? - io = @o.rb_io_open_descriptor(File, @r_io.fileno, 0, "a.txt", 60, "US-ASCII", "UTF-8", 0, {encoding: "windows-1251:binary"}) - io.external_encoding.should == Encoding::UTF_8 - io.internal_encoding.should == Encoding::US_ASCII + io = @o.rb_io_open_descriptor(File, @r_io.fileno, 0, "a.txt", 60, "US-ASCII", "UTF-8", 0, {binmode: true}) + io.should_not.binmode? - io = @o.rb_io_open_descriptor(File, @r_io.fileno, 0, "a.txt", 60, "US-ASCII", "UTF-8", 0, {textmode: false}) - io.should_not.binmode? + io = @o.rb_io_open_descriptor(File, @r_io.fileno, 0, "a.txt", 60, "US-ASCII", "UTF-8", 0, {autoclose: false}) + io.should.autoclose? - io = @o.rb_io_open_descriptor(File, @r_io.fileno, 0, "a.txt", 60, "US-ASCII", "UTF-8", 0, {binmode: true}) - io.should_not.binmode? + io = @o.rb_io_open_descriptor(File, @r_io.fileno, 0, "a.txt", 60, "US-ASCII", "UTF-8", 0, {path: "a.txt"}) + io.path.should == "a.txt" + end - io = @o.rb_io_open_descriptor(File, @r_io.fileno, 0, "a.txt", 60, "US-ASCII", "UTF-8", 0, {autoclose: false}) - io.should.autoclose? + it "ignores the IO encoding options" do + io = @o.rb_io_open_descriptor(File, @w_io.fileno, CApiIOSpecs::FMODE_WRITABLE, "a.txt", 60, "US-ASCII", "UTF-8", 0, {crlf_newline: true}) - io = @o.rb_io_open_descriptor(File, @r_io.fileno, 0, "a.txt", 60, "US-ASCII", "UTF-8", 0, {path: "a.txt"}) - io.path.should == "a.txt" - end + io.write("123\r\n456\n89") + io.flush - it "ignores the IO encoding options" do - io = @o.rb_io_open_descriptor(File, @w_io.fileno, CApiIOSpecs::FMODE_WRITABLE, "a.txt", 60, "US-ASCII", "UTF-8", 0, {crlf_newline: true}) + @r_io.read_nonblock(20).should == "123\r\n456\n89" + end - io.write("123\r\n456\n89") - io.flush + it "allows wrong mode" do + io = @o.rb_io_open_descriptor(File, @w_io.fileno, CApiIOSpecs::FMODE_READABLE, "a.txt", 60, "US-ASCII", "UTF-8", 0, {}) + io.should.is_a?(File) - @r_io.read_nonblock(20).should == "123\r\n456\n89" + platform_is_not :windows do + -> { io.read_nonblock(1) }.should.raise(Errno::EBADF) end - it "allows wrong mode" do - io = @o.rb_io_open_descriptor(File, @w_io.fileno, CApiIOSpecs::FMODE_READABLE, "a.txt", 60, "US-ASCII", "UTF-8", 0, {}) - io.should.is_a?(File) - - platform_is_not :windows do - -> { io.read_nonblock(1) }.should raise_error(Errno::EBADF) - end - - platform_is :windows do - -> { io.read_nonblock(1) }.should raise_error(IO::EWOULDBLOCKWaitReadable) - end + platform_is :windows do + -> { io.read_nonblock(1) }.should.raise(IO::EWOULDBLOCKWaitReadable) end + end - it "tolerates NULL as rb_io_encoding *encoding parameter" do - io = @o.rb_io_open_descriptor_without_encoding(File, @r_io.fileno, 0, "a.txt", 60) - io.should.is_a?(File) - end + it "tolerates NULL as rb_io_encoding *encoding parameter" do + io = @o.rb_io_open_descriptor_without_encoding(File, @r_io.fileno, 0, "a.txt", 60) + io.should.is_a?(File) + end - it "deduplicates path String" do - path = "a.txt".dup - io = @o.rb_io_open_descriptor(File, @r_io.fileno, 0, path, 60, "US-ASCII", "UTF-8", 0, {}) - io.path.should_not equal(path) + it "deduplicates path String" do + path = "a.txt".dup + io = @o.rb_io_open_descriptor(File, @r_io.fileno, 0, path, 60, "US-ASCII", "UTF-8", 0, {}) + io.path.should_not.equal?(path) - path = "a.txt".freeze - io = @o.rb_io_open_descriptor(File, @r_io.fileno, 0, path, 60, "US-ASCII", "UTF-8", 0, {}) - io.path.should_not equal(path) - end + path = "a.txt".freeze + io = @o.rb_io_open_descriptor(File, @r_io.fileno, 0, path, 60, "US-ASCII", "UTF-8", 0, {}) + io.path.should_not.equal?(path) + end - it "calls #to_str to convert a path to a String" do - path = Object.new - def path.to_str; "a.txt"; end + it "calls #to_str to convert a path to a String" do + path = Object.new + def path.to_str; "a.txt"; end - io = @o.rb_io_open_descriptor(File, @r_io.fileno, 0, path, 60, "US-ASCII", "UTF-8", 0, {}) + io = @o.rb_io_open_descriptor(File, @r_io.fileno, 0, path, 60, "US-ASCII", "UTF-8", 0, {}) - io.path.should == "a.txt" - end + io.path.should == "a.txt" end end end @@ -686,13 +678,13 @@ describe "rb_fd_fix_cloexec" do end after :each do - @io.close unless @io.closed? + @io.close rm_r @name end it "sets close_on_exec on the IO" do @o.rb_fd_fix_cloexec(@io) - @io.close_on_exec?.should be_true + @io.close_on_exec?.should == true end end @@ -707,13 +699,65 @@ describe "rb_cloexec_open" do end after :each do - @io.close unless @io.nil? || @io.closed? + @io.close if @io rm_r @name end it "sets close_on_exec on the newly-opened IO" do @io = @o.rb_cloexec_open(@name, 0, 0) - @io.close_on_exec?.should be_true + @io.close_on_exec?.should == true + end +end + +describe "rb_cloexec_dup" do + before :each do + @o = CApiIOSpecs.new + @name = tmp("c_api_rb_io_specs") + touch @name + + @io = new_io @name, "r" + @dup = nil + end + + after :each do + @dup.close if @dup + @io.close + rm_r @name + end + + it "duplicates a file descriptor and sets close_on_exec" do + @dup = @o.rb_cloexec_dup(@io) + @dup.should.close_on_exec? + @dup.fileno.should_not == @io.fileno + end +end + +describe "rb_cloexec_fcntl_dupfd" do + before :each do + @o = CApiIOSpecs.new + @name = tmp("c_api_rb_io_specs") + touch @name + + @io = new_io @name, "r" + @dup = nil + end + + after :each do + @dup.close if @dup + @io.close + rm_r @name + end + + it "duplicates a file descriptor and sets close_on_exec" do + @dup = @o.rb_cloexec_fcntl_dupfd(@io, 3) + @dup.close_on_exec?.should == true + @dup.fileno.should_not == @io.fileno + end + + it "returns a file descriptor greater than or equal to minfd" do + @dup = @o.rb_cloexec_fcntl_dupfd(@io, 100) + @dup.fileno.should >= 100 + @dup.close_on_exec?.should == true end end diff --git a/spec/ruby/optional/capi/kernel_spec.rb b/spec/ruby/optional/capi/kernel_spec.rb index c90e50f1db..62eb21448b 100644 --- a/spec/ruby/optional/capi/kernel_spec.rb +++ b/spec/ruby/optional/capi/kernel_spec.rb @@ -28,7 +28,7 @@ describe "C-API Kernel function" do describe "rb_need_block" do it "raises a LocalJumpError if no block is given" do - -> { @s.rb_need_block }.should raise_error(LocalJumpError) + -> { @s.rb_need_block }.should.raise(LocalJumpError) end it "does not raise a LocalJumpError if a block is given" do @@ -53,7 +53,7 @@ describe "C-API Kernel function" do it "calls the method with no function callback and no block" do ary = [1, 3, 5] - @s.rb_block_call_no_func(ary).should be_kind_of(Enumerator) + @s.rb_block_call_no_func(ary).should.is_a?(Enumerator) end it "calls the method with no function callback and a block" do @@ -65,7 +65,7 @@ describe "C-API Kernel function" do it "can pass extra data to the function" do ary = [3] - @s.rb_block_call_extra_data(ary).should equal(ary) + @s.rb_block_call_extra_data(ary).should.equal?(ary) end end @@ -78,12 +78,12 @@ describe "C-API Kernel function" do describe "rb_raise" do it "raises an exception" do - -> { @s.rb_raise({}) }.should raise_error(TypeError) + -> { @s.rb_raise({}) }.should.raise(TypeError) end it "terminates the function at the point it was called" do h = {} - -> { @s.rb_raise(h) }.should raise_error(TypeError) + -> { @s.rb_raise(h) }.should.raise(TypeError) h[:stage].should == :before end @@ -100,7 +100,7 @@ describe "C-API Kernel function" do # should raise StandardError "aaa" raise end - end.should raise_error(StandardError, "aaa") + end.should.raise(StandardError, "aaa") end end @@ -125,7 +125,7 @@ describe "C-API Kernel function" do end it "raises an ArgumentError if there is no catch block for the symbol" do - -> { @s.rb_throw(nil) }.should raise_error(ArgumentError) + -> { @s.rb_throw(nil) }.should.raise(ArgumentError) end end @@ -151,7 +151,7 @@ describe "C-API Kernel function" do end it "raises an ArgumentError if there is no catch block for the symbol" do - -> { @s.rb_throw(nil) }.should raise_error(ArgumentError) + -> { @s.rb_throw(nil) }.should.raise(ArgumentError) end end @@ -173,13 +173,13 @@ describe "C-API Kernel function" do it "raises an exception from the value of errno" do -> do @s.rb_sys_fail("additional info") - end.should raise_error(SystemCallError, /additional info/) + end.should.raise(SystemCallError, /additional info/) end it "can take a NULL message" do -> do @s.rb_sys_fail(nil) - end.should raise_error(Errno::EPERM) + end.should.raise(Errno::EPERM) end end @@ -187,23 +187,17 @@ describe "C-API Kernel function" do it "raises an exception from the given error" do -> do @s.rb_syserr_fail(Errno::EINVAL::Errno, "additional info") - end.should raise_error(Errno::EINVAL, "Invalid argument - additional info") + end.should.raise(Errno::EINVAL, "Invalid argument - additional info") end it "can take a NULL message" do -> do @s.rb_syserr_fail(Errno::EINVAL::Errno, nil) - end.should raise_error(Errno::EINVAL, "Invalid argument") + end.should.raise(Errno::EINVAL, "Invalid argument") end - it "uses an 'unknown error' message when errno is unknown" do - platform_is_not :windows do - -> { @s.rb_syserr_fail(-10, nil) }.should raise_error(SystemCallError, /Unknown error(:)? -10/) - end - - platform_is :windows do - -> { @s.rb_syserr_fail(-1, nil) }.should raise_error(SystemCallError, "The operation completed successfully.") - end + it "uses some kind of string as message when errno is unknown" do + -> { @s.rb_syserr_fail(-10, nil) }.should.raise(SystemCallError, /[[:graph:]]+/) end end @@ -211,23 +205,17 @@ describe "C-API Kernel function" do it "raises an exception from the given error" do -> do @s.rb_syserr_fail_str(Errno::EINVAL::Errno, "additional info") - end.should raise_error(Errno::EINVAL, "Invalid argument - additional info") + end.should.raise(Errno::EINVAL, "Invalid argument - additional info") end it "can take nil as a message" do -> do @s.rb_syserr_fail_str(Errno::EINVAL::Errno, nil) - end.should raise_error(Errno::EINVAL, "Invalid argument") + end.should.raise(Errno::EINVAL, "Invalid argument") end - it "uses an 'unknown error' message when errno is unknown" do - platform_is_not :windows do - -> { @s.rb_syserr_fail_str(-10, nil) }.should raise_error(SystemCallError, /Unknown error(:)? -10/) - end - - platform_is :windows do - -> { @s.rb_syserr_fail_str(-1, nil) }.should raise_error(SystemCallError, "The operation completed successfully.") - end + it "uses some kind of string as message when errno is unknown" do + -> { @s.rb_syserr_fail_str(-10, nil) }.should.raise(SystemCallError, /[[:graph:]]+/) end end @@ -243,7 +231,7 @@ describe "C-API Kernel function" do end it "raises LocalJumpError when no block is given" do - -> { @s.rb_yield(1) }.should raise_error(LocalJumpError) + -> { @s.rb_yield(1) }.should.raise(LocalJumpError) end it "rb_yield to a block that breaks does not raise an error" do @@ -254,10 +242,8 @@ describe "C-API Kernel function" do @s.rb_yield(1) { break 73 }.should == 73 end - platform_is_not :"solaris2.10" do # NOTE: i386-pc-solaris2.10 - it "rb_yield through a callback to a block that breaks with a value returns the value" do - @s.rb_yield_indirected(1) { break 73 }.should == 73 - end + it "rb_yield through a callback to a block that breaks with a value returns the value" do + @s.rb_yield_indirected(1) { break 73 }.should == 73 end it "rb_yield to block passed to enumerator" do @@ -283,7 +269,7 @@ describe "C-API Kernel function" do end it "raises LocalJumpError when no block is given" do - -> { @s.rb_yield_splat([1, 2]) }.should raise_error(LocalJumpError) + -> { @s.rb_yield_splat([1, 2]) }.should.raise(LocalJumpError) end end @@ -315,7 +301,7 @@ describe "C-API Kernel function" do end it "raises LocalJumpError when no block is given" do - -> { @s.rb_yield_splat([1, 2]) }.should raise_error(LocalJumpError) + -> { @s.rb_yield_splat([1, 2]) }.should.raise(LocalJumpError) end end @@ -344,7 +330,7 @@ describe "C-API Kernel function" do proof = [] # Hold proof of work performed after the yield. -> do @s.rb_protect_yield(77, proof) { |x| raise NameError } - end.should raise_error(NameError) + end.should.raise(NameError) proof[0].should == 23 end @@ -352,7 +338,7 @@ describe "C-API Kernel function" do proof = [] # Hold proof of work performed after the yield. -> do @s.rb_protect_yield(77, proof) { |x| raise NameError } - end.should raise_error(NameError) + end.should.raise(NameError) proof[0].should == 23 proof[1].should == nil end @@ -389,7 +375,7 @@ describe "C-API Kernel function" do proof = [] -> do @s.rb_eval_string_protect('raise RuntimeError', proof) - end.should raise_error(RuntimeError) + end.should.raise(RuntimeError) proof.should == [23, nil] end end @@ -430,11 +416,11 @@ describe "C-API Kernel function" do end it "raises an exception if passed function raises an exception other than StandardError" do - -> { @s.rb_rescue(@exc_error_proc, nil, @rescue_proc_returns_arg, nil) }.should raise_error(Exception) + -> { @s.rb_rescue(@exc_error_proc, nil, @rescue_proc_returns_arg, nil) }.should.raise(Exception) end it "raises an exception if any exception is raised inside the 'rescue function'" do - -> { @s.rb_rescue(@std_error_proc, nil, @std_error_proc, nil) }.should raise_error(StandardError) + -> { @s.rb_rescue(@std_error_proc, nil, @std_error_proc, nil) }.should.raise(StandardError) end it "sets $! and rb_errinfo() during the 'rescue function' execution" do @@ -477,13 +463,13 @@ describe "C-API Kernel function" do @s.rb_rescue2(run_error_proc, :no_exc, proc, :exc, ArgumentError, RuntimeError).should == :exc -> { @s.rb_rescue2(type_error_proc, :no_exc, proc, :exc, ArgumentError, RuntimeError) - }.should raise_error(Exception, 'custom error') + }.should.raise(Exception, 'custom error') end it "raises TypeError if one of the passed exceptions is not a Module" do -> { @s.rb_rescue2(-> *_ { raise RuntimeError, "foo" }, :no_exc, -> x { x }, :exc, Object.new, 42) - }.should raise_error(TypeError, /class or module required/) + }.should.raise(TypeError, /class or module required/) end it "sets $! and rb_errinfo() during the 'rescue function' execution" do @@ -519,12 +505,12 @@ describe "C-API Kernel function" do throw :thrown_value ScratchPad << :after_throw end - @s.rb_catch("thrown_value", proc).should be_nil + @s.rb_catch("thrown_value", proc).should == nil ScratchPad.recorded.should == [:before_throw] end it "raises an ArgumentError if the throw symbol isn't caught" do - -> { @s.rb_catch("foo", -> { throw :bar }) }.should raise_error(ArgumentError) + -> { @s.rb_catch("foo", -> { throw :bar }) }.should.raise(ArgumentError) end end @@ -545,12 +531,12 @@ describe "C-API Kernel function" do throw @tag ScratchPad << :after_throw end - @s.rb_catch_obj(@tag, proc).should be_nil + @s.rb_catch_obj(@tag, proc).should == nil ScratchPad.recorded.should == [:before_throw] end it "raises an ArgumentError if the throw symbol isn't caught" do - -> { @s.rb_catch("foo", -> { throw :bar }) }.should raise_error(ArgumentError) + -> { @s.rb_catch("foo", -> { throw :bar }) }.should.raise(ArgumentError) end end @@ -608,7 +594,7 @@ describe "C-API Kernel function" do ensure_proc = -> x { foo = x } -> { @s.rb_ensure(raise_proc, nil, ensure_proc, :foo) - }.should raise_error(exception_class) + }.should.raise(exception_class) foo.should == :foo end @@ -618,14 +604,14 @@ describe "C-API Kernel function" do $!.should.is_a?(exception_class) @s.rb_errinfo.should.is_a?(exception_class) }, nil) - }.should raise_error(exception_class) + }.should.raise(exception_class) -> { @s.rb_ensure(-> _ { @s.rb_raise({}) }, nil, -> _ { $!.should.is_a?(TypeError) @s.rb_errinfo.should.is_a?(TypeError) }, nil) - }.should raise_error(TypeError) + }.should.raise(TypeError) $!.should == nil @s.rb_errinfo.should == nil @@ -634,7 +620,7 @@ describe "C-API Kernel function" do it "raises the same exception raised inside passed function" do raise_proc = -> *_ { raise RuntimeError, 'foo' } proc = -> *_ { } - -> { @s.rb_ensure(raise_proc, nil, proc, nil) }.should raise_error(RuntimeError, 'foo') + -> { @s.rb_ensure(raise_proc, nil, proc, nil) }.should.raise(RuntimeError, 'foo') end end @@ -649,29 +635,31 @@ describe "C-API Kernel function" do end end - describe "rb_eval_cmd_kw" do - it "evaluates a string of ruby code" do - @s.rb_eval_cmd_kw("1+1", [], 0).should == 2 - end + ruby_version_is ""..."4.0" do + describe "rb_eval_cmd_kw" do + it "evaluates a string of ruby code" do + @s.rb_eval_cmd_kw("1+1", [], 0).should == 2 + end - it "calls a proc with the supplied arguments" do - @s.rb_eval_cmd_kw(-> *x { x.map { |i| i + 1 } }, [1, 3, 7], 0).should == [2, 4, 8] - end + it "calls a proc with the supplied arguments" do + @s.rb_eval_cmd_kw(-> *x { x.map { |i| i + 1 } }, [1, 3, 7], 0).should == [2, 4, 8] + end - it "calls a proc with keyword arguments if kw_splat is non zero" do - a_proc = -> *x, **y { - res = x.map { |i| i + 1 } - y.each { |k, v| res << k; res << v } - res - } - @s.rb_eval_cmd_kw(a_proc, [1, 3, 7, {a: 1, b: 2, c: 3}], 1).should == [2, 4, 8, :a, 1, :b, 2, :c, 3] + it "calls a proc with keyword arguments if kw_splat is non zero" do + a_proc = -> *x, **y { + res = x.map { |i| i + 1 } + y.each { |k, v| res << k; res << v } + res + } + @s.rb_eval_cmd_kw(a_proc, [1, 3, 7, {a: 1, b: 2, c: 3}], 1).should == [2, 4, 8, :a, 1, :b, 2, :c, 3] + end end end describe "rb_block_proc" do it "converts the implicit block into a proc" do proc = @s.rb_block_proc { 1+1 } - proc.should be_kind_of(Proc) + proc.should.is_a?(Proc) proc.call.should == 2 proc.should_not.lambda? end @@ -679,7 +667,7 @@ describe "C-API Kernel function" do it "passes through an existing lambda and does not convert to a proc" do b = -> { 1+1 } proc = @s.rb_block_proc(&b) - proc.should equal(b) + proc.should.equal?(b) proc.call.should == 2 proc.should.lambda? end @@ -688,7 +676,7 @@ describe "C-API Kernel function" do describe "rb_block_lambda" do it "converts the implicit block into a lambda" do proc = @s.rb_block_lambda { 1+1 } - proc.should be_kind_of(Proc) + proc.should.is_a?(Proc) proc.call.should == 2 proc.should.lambda? end @@ -696,7 +684,7 @@ describe "C-API Kernel function" do it "passes through an existing Proc and does not convert to a lambda" do b = proc { 1+1 } proc = @s.rb_block_lambda(&b) - proc.should equal(b) + proc.should.equal?(b) proc.call.should == 2 proc.should_not.lambda? end @@ -715,6 +703,12 @@ describe "C-API Kernel function" do end end + describe "ruby_vm_at_exit" do + it "runs a C function after the VM is terminated" do + ruby_exe("require #{kernel_path.inspect}; CApiKernelSpecs.new.ruby_vm_at_exit").should == "ruby_vm_at_exit hook ran\n" + end + end + describe "rb_f_sprintf" do it "returns a string according to format and arguments" do @s.rb_f_sprintf(["%d %f %s", 10, 2.5, "test"]).should == "10 2.500000 test" @@ -725,7 +719,7 @@ describe "C-API Kernel function" do it "returns a caller backtrace" do backtrace = @s.rb_make_backtrace lines = backtrace.select {|l| l =~ /#{__FILE__}/ } - lines.should_not be_empty + lines.should_not.empty? end end @@ -783,7 +777,7 @@ describe "C-API Kernel function" do -> { @s.rb_funcallv_kw(self, :m, [42]) - }.should raise_error(TypeError, 'no implicit conversion of Integer into Hash') + }.should.raise(TypeError, 'no implicit conversion of Integer into Hash') end end @@ -819,7 +813,7 @@ describe "C-API Kernel function" do end it "does not call a private method" do - -> { @s.rb_funcallv_public(@obj, :method_private) }.should raise_error(NoMethodError, /private/) + -> { @s.rb_funcallv_public(@obj, :method_private) }.should.raise(NoMethodError, /private/) end end @@ -853,7 +847,7 @@ describe "C-API Kernel function" do -> { @s.rb_funcall_with_block(object, :private_method, [], proc { }) - }.should raise_error(NoMethodError, /private/) + }.should.raise(NoMethodError, /private/) end it "does not call a protected method" do @@ -861,7 +855,7 @@ describe "C-API Kernel function" do -> { @s.rb_funcall_with_block(object, :protected_method, [], proc { }) - }.should raise_error(NoMethodError, /protected/) + }.should.raise(NoMethodError, /protected/) end end @@ -880,7 +874,7 @@ describe "C-API Kernel function" do -> { @s.rb_funcall_with_block_kw(object, :private_method, [{}], proc { }) - }.should raise_error(NoMethodError, /private/) + }.should.raise(NoMethodError, /private/) end it "does not call a protected method" do @@ -888,7 +882,7 @@ describe "C-API Kernel function" do -> { @s.rb_funcall_with_block_kw(object, :protected_method, [{}], proc { }) - }.should raise_error(NoMethodError, /protected/) + }.should.raise(NoMethodError, /protected/) end end diff --git a/spec/ruby/optional/capi/module_spec.rb b/spec/ruby/optional/capi/module_spec.rb index d7c0ab9c52..b9c36f569f 100644 --- a/spec/ruby/optional/capi/module_spec.rb +++ b/spec/ruby/optional/capi/module_spec.rb @@ -22,6 +22,8 @@ describe "CApiModule" do it "sets a new constant on a module" do @m.rb_const_set(CApiModuleSpecs::C, :W, 7) CApiModuleSpecs::C::W.should == 7 + ensure + CApiModuleSpecs::C.send(:remove_const, :W) end it "sets an existing constant's value" do @@ -34,15 +36,15 @@ describe "CApiModule" do it "allows arbitrary names, including constant names not valid in Ruby" do -> { CApiModuleSpecs::C.const_set(:_INVALID, 1) - }.should raise_error(NameError, /wrong constant name/) + }.should.raise(NameError, /wrong constant name/) - @m.rb_const_set(CApiModuleSpecs::C, :_INVALID, 2) + suppress_warning { @m.rb_const_set(CApiModuleSpecs::C, :_INVALID, 2) } @m.rb_const_get(CApiModuleSpecs::C, :_INVALID).should == 2 # Ruby-level should still not allow access -> { CApiModuleSpecs::C.const_get(:_INVALID) - }.should raise_error(NameError, /wrong constant name/) + }.should.raise(NameError, /wrong constant name/) end end @@ -54,15 +56,15 @@ describe "CApiModule" do it "raises a TypeError if the constant is not a module" do ::CApiModuleSpecsGlobalConst = 7 - -> { @m.rb_define_module("CApiModuleSpecsGlobalConst") }.should raise_error(TypeError) + -> { @m.rb_define_module("CApiModuleSpecsGlobalConst") }.should.raise(TypeError) Object.send :remove_const, :CApiModuleSpecsGlobalConst end it "defines a new module at toplevel" do mod = @m.rb_define_module("CApiModuleSpecsModuleB") - mod.should be_kind_of(Module) + mod.should.is_a?(Module) mod.name.should == "CApiModuleSpecsModuleB" - ::CApiModuleSpecsModuleB.should be_kind_of(Module) + ::CApiModuleSpecsModuleB.should.is_a?(Module) Object.send :remove_const, :CApiModuleSpecsModuleB end end @@ -70,7 +72,7 @@ describe "CApiModule" do describe "rb_define_module_under" do it "creates a new module inside the inner class" do mod = @m.rb_define_module_under(CApiModuleSpecs, "ModuleSpecsModuleUnder1") - mod.should be_kind_of(Module) + mod.should.is_a?(Module) end it "sets the module name" do @@ -93,6 +95,8 @@ describe "CApiModule" do it "defines a new constant on a module" do @m.rb_define_const(CApiModuleSpecs::C, "V", 7) CApiModuleSpecs::C::V.should == 7 + ensure + CApiModuleSpecs::C.send(:remove_const, :V) end it "sets an existing constant's value" do @@ -106,26 +110,26 @@ describe "CApiModule" do describe "rb_const_defined" do # The fixture converts C boolean test to Ruby 'true' / 'false' it "returns C non-zero if a constant is defined" do - @m.rb_const_defined(CApiModuleSpecs::A, :X).should be_true + @m.rb_const_defined(CApiModuleSpecs::A, :X).should == true end it "returns C non-zero if a constant is defined in Object" do - @m.rb_const_defined(CApiModuleSpecs::A, :Module).should be_true + @m.rb_const_defined(CApiModuleSpecs::A, :Module).should == true end end describe "rb_const_defined_at" do # The fixture converts C boolean test to Ruby 'true' / 'false' it "returns C non-zero if a constant is defined" do - @m.rb_const_defined_at(CApiModuleSpecs::A, :X).should be_true + @m.rb_const_defined_at(CApiModuleSpecs::A, :X).should == true end it "does not search in ancestors for the constant" do - @m.rb_const_defined_at(CApiModuleSpecs::B, :X).should be_false + @m.rb_const_defined_at(CApiModuleSpecs::B, :X).should == false end it "does not search in Object" do - @m.rb_const_defined_at(CApiModuleSpecs::A, :Module).should be_false + @m.rb_const_defined_at(CApiModuleSpecs::A, :Module).should == false end end @@ -162,11 +166,11 @@ describe "CApiModule" do it "allows arbitrary names, including constant names not valid in Ruby" do -> { CApiModuleSpecs::A.const_get(:_INVALID) - }.should raise_error(NameError, /wrong constant name/) + }.should.raise(NameError, /wrong constant name/) -> { @m.rb_const_get(CApiModuleSpecs::A, :_INVALID) - }.should raise_error(NameError, /uninitialized constant/) + }.should.raise(NameError, /uninitialized constant/) end end @@ -233,7 +237,7 @@ describe "CApiModule" do describe "rb_define_global_function" do it "defines a method on Kernel" do @m.rb_define_global_function("module_specs_global_function") - Kernel.should have_method(:module_specs_global_function) + Kernel.should.respond_to?(:module_specs_global_function) module_specs_global_function.should == :test_method end end @@ -242,7 +246,7 @@ describe "CApiModule" do it "defines a method on a class" do cls = Class.new @m.rb_define_method(cls, "test_method") - cls.should have_instance_method(:test_method) + cls.should.method_defined?(:test_method, false) cls.new.test_method.should == :test_method end @@ -281,7 +285,7 @@ describe "CApiModule" do it "defines a method on a module" do mod = Module.new @m.rb_define_method(mod, "test_method") - mod.should have_instance_method(:test_method) + mod.should.method_defined?(:test_method, false) end it "returns the correct arity of the method in module" do @@ -309,7 +313,7 @@ describe "CApiModule" do cls = Class.new cls.include(@mod) - cls.should have_private_instance_method(:test_module_function) + cls.private_instance_methods(true).should.include?(:test_module_function) end it "returns the correct arity for private instance method" do @@ -324,14 +328,14 @@ describe "CApiModule" do it "defines a private method on a class" do cls = Class.new @m.rb_define_private_method(cls, "test_method") - cls.should have_private_instance_method(:test_method) + cls.private_instance_methods(false).should.include?(:test_method) cls.new.send(:test_method).should == :test_method end it "defines a private method on a module" do mod = Module.new @m.rb_define_private_method(mod, "test_method") - mod.should have_private_instance_method(:test_method) + mod.private_instance_methods(false).should.include?(:test_method) end end @@ -339,14 +343,14 @@ describe "CApiModule" do it "defines a protected method on a class" do cls = Class.new @m.rb_define_protected_method(cls, "test_method") - cls.should have_protected_instance_method(:test_method) + cls.protected_instance_methods(false).should.include?(:test_method) cls.new.send(:test_method).should == :test_method end it "defines a protected method on a module" do mod = Module.new @m.rb_define_protected_method(mod, "test_method") - mod.should have_protected_instance_method(:test_method) + mod.protected_instance_methods(false).should.include?(:test_method) end end @@ -356,7 +360,7 @@ describe "CApiModule" do a = cls.new @m.rb_define_singleton_method a, "module_specs_singleton_method" a.module_specs_singleton_method.should == :test_method - -> { cls.new.module_specs_singleton_method }.should raise_error(NoMethodError) + -> { cls.new.module_specs_singleton_method }.should.raise(NoMethodError) end end @@ -372,16 +376,16 @@ describe "CApiModule" do it "undef'ines a method on a class" do @class.new.ruby_test_method.should == :ruby_test_method @m.rb_undef_method @class, "ruby_test_method" - @class.should_not have_instance_method(:ruby_test_method) + @class.should_not.method_defined?(:ruby_test_method) end it "undefines private methods also" do @m.rb_undef_method @class, "initialize_copy" - -> { @class.new.dup }.should raise_error(NoMethodError) + -> { @class.new.dup }.should.raise(NoMethodError) end it "does not raise exceptions when passed a missing name" do - -> { @m.rb_undef_method @class, "not_exist" }.should_not raise_error + -> { @m.rb_undef_method @class, "not_exist" }.should_not.raise end describe "when given a frozen Class" do @@ -390,11 +394,11 @@ describe "CApiModule" do end it "raises a FrozenError when passed a name" do - -> { @m.rb_undef_method @frozen, "ruby_test_method" }.should raise_error(FrozenError) + -> { @m.rb_undef_method @frozen, "ruby_test_method" }.should.raise(FrozenError) end it "raises a FrozenError when passed a missing name" do - -> { @m.rb_undef_method @frozen, "not_exist" }.should raise_error(FrozenError) + -> { @m.rb_undef_method @frozen, "not_exist" }.should.raise(FrozenError) end end end @@ -409,7 +413,7 @@ describe "CApiModule" do cls.new.ruby_test_method.should == :ruby_test_method @m.rb_undef cls, :ruby_test_method - cls.should_not have_instance_method(:ruby_test_method) + cls.should_not.method_defined?(:ruby_test_method) end end diff --git a/spec/ruby/optional/capi/mutex_spec.rb b/spec/ruby/optional/capi/mutex_spec.rb index 71a2212e36..fad9b4d1a2 100644 --- a/spec/ruby/optional/capi/mutex_spec.rb +++ b/spec/ruby/optional/capi/mutex_spec.rb @@ -10,64 +10,64 @@ describe "C-API Mutex functions" do describe "rb_mutex_new" do it "creates a new mutex" do - @s.rb_mutex_new.should be_an_instance_of(Mutex) + @s.rb_mutex_new.should.instance_of?(Mutex) end end describe "rb_mutex_locked_p" do it "returns false if the mutex is not locked" do - @s.rb_mutex_locked_p(@m).should be_false + @s.rb_mutex_locked_p(@m).should == false end it "returns true if the mutex is locked" do @m.lock - @s.rb_mutex_locked_p(@m).should be_true + @s.rb_mutex_locked_p(@m).should == true end end describe "rb_mutex_trylock" do it "locks the mutex if not locked" do - @s.rb_mutex_trylock(@m).should be_true - @m.locked?.should be_true + @s.rb_mutex_trylock(@m).should == true + @m.locked?.should == true end it "returns false if the mutex is already locked" do @m.lock - @s.rb_mutex_trylock(@m).should be_false - @m.locked?.should be_true + @s.rb_mutex_trylock(@m).should == false + @m.locked?.should == true end end describe "rb_mutex_lock" do it "returns when the mutex isn't locked" do @s.rb_mutex_lock(@m).should == @m - @m.locked?.should be_true + @m.locked?.should == true end it "throws an exception when already locked in the same thread" do @m.lock - -> { @s.rb_mutex_lock(@m) }.should raise_error(ThreadError) - @m.locked?.should be_true + -> { @s.rb_mutex_lock(@m) }.should.raise(ThreadError) + @m.locked?.should == true end end describe "rb_mutex_unlock" do it "raises an exception when not locked" do - -> { @s.rb_mutex_unlock(@m) }.should raise_error(ThreadError) - @m.locked?.should be_false + -> { @s.rb_mutex_unlock(@m) }.should.raise(ThreadError) + @m.locked?.should == false end it "unlocks the mutex when locked" do @m.lock @s.rb_mutex_unlock(@m).should == @m - @m.locked?.should be_false + @m.locked?.should == false end end describe "rb_mutex_sleep" do it "throws an exception when the mutex is not locked" do - -> { @s.rb_mutex_sleep(@m, 0.1) }.should raise_error(ThreadError) - @m.locked?.should be_false + -> { @s.rb_mutex_sleep(@m, 0.1) }.should.raise(ThreadError) + @m.locked?.should == false end it "sleeps when the mutex is locked" do @@ -76,13 +76,13 @@ describe "C-API Mutex functions" do @s.rb_mutex_sleep(@m, 0.001) t2 = Process.clock_gettime(Process::CLOCK_MONOTONIC) (t2 - t1).should >= 0 - @m.locked?.should be_true + @m.locked?.should == true end end describe "rb_mutex_synchronize" do it "calls the function while the mutex is locked" do - callback = -> { @m.locked?.should be_true } + callback = -> { @m.locked?.should == true } @s.rb_mutex_synchronize(@m, callback) end diff --git a/spec/ruby/optional/capi/numeric_spec.rb b/spec/ruby/optional/capi/numeric_spec.rb index e9667da5ba..abfed13503 100644 --- a/spec/ruby/optional/capi/numeric_spec.rb +++ b/spec/ruby/optional/capi/numeric_spec.rb @@ -9,7 +9,7 @@ describe "CApiNumericSpecs" do describe "NUM2INT" do it "raises a TypeError if passed nil" do - -> { @s.NUM2INT(nil) }.should raise_error(TypeError) + -> { @s.NUM2INT(nil) }.should.raise(TypeError) end it "converts a Float" do @@ -33,7 +33,7 @@ describe "CApiNumericSpecs" do end it "raises a RangeError if the value is more than 32bits" do - -> { @s.NUM2INT(0xffff_ffff+1) }.should raise_error(RangeError) + -> { @s.NUM2INT(0xffff_ffff+1) }.should.raise(RangeError) end it "calls #to_int to coerce the value" do @@ -45,7 +45,7 @@ describe "CApiNumericSpecs" do describe "NUM2UINT" do it "raises a TypeError if passed nil" do - -> { @s.NUM2UINT(nil) }.should raise_error(TypeError) + -> { @s.NUM2UINT(nil) }.should.raise(TypeError) end it "converts a Float" do @@ -69,17 +69,17 @@ describe "CApiNumericSpecs" do end it "raises a RangeError if the value is more than 32bits" do - -> { @s.NUM2UINT(0xffff_ffff+1) }.should raise_error(RangeError) + -> { @s.NUM2UINT(0xffff_ffff+1) }.should.raise(RangeError) end it "raises a RangeError if the value is less than 32bits negative" do - -> { @s.NUM2UINT(-0x8000_0000-1) }.should raise_error(RangeError) + -> { @s.NUM2UINT(-0x8000_0000-1) }.should.raise(RangeError) end it "raises a RangeError if the value is more than 64bits" do -> do @s.NUM2UINT(0xffff_ffff_ffff_ffff+1) - end.should raise_error(RangeError) + end.should.raise(RangeError) end it "calls #to_int to coerce the value" do @@ -91,7 +91,7 @@ describe "CApiNumericSpecs" do describe "NUM2LONG" do it "raises a TypeError if passed nil" do - -> { @s.NUM2LONG(nil) }.should raise_error(TypeError) + -> { @s.NUM2LONG(nil) }.should.raise(TypeError) end it "converts a Float" do @@ -116,7 +116,7 @@ describe "CApiNumericSpecs" do end it "raises a RangeError if the value is more than 32bits" do - -> { @s.NUM2LONG(0xffff_ffff+1) }.should raise_error(RangeError) + -> { @s.NUM2LONG(0xffff_ffff+1) }.should.raise(RangeError) end end @@ -132,7 +132,7 @@ describe "CApiNumericSpecs" do it "raises a RangeError if the value is more than 64bits" do -> do @s.NUM2LONG(0xffff_ffff_ffff_ffff+1) - end.should raise_error(RangeError) + end.should.raise(RangeError) end end @@ -145,7 +145,7 @@ describe "CApiNumericSpecs" do describe "NUM2SHORT" do it "raises a TypeError if passed nil" do - -> { @s.NUM2SHORT(nil) }.should raise_error(TypeError) + -> { @s.NUM2SHORT(nil) }.should.raise(TypeError) end it "converts a Float" do @@ -161,7 +161,7 @@ describe "CApiNumericSpecs" do end it "raises a RangeError if the value is more than 32bits" do - -> { @s.NUM2SHORT(0xffff_ffff+1) }.should raise_error(RangeError) + -> { @s.NUM2SHORT(0xffff_ffff+1) }.should.raise(RangeError) end it "calls #to_int to coerce the value" do @@ -173,7 +173,7 @@ describe "CApiNumericSpecs" do describe "INT2NUM" do it "raises a TypeError if passed nil" do - -> { @s.INT2NUM(nil) }.should raise_error(TypeError) + -> { @s.INT2NUM(nil) }.should.raise(TypeError) end it "converts a Float" do @@ -181,7 +181,7 @@ describe "CApiNumericSpecs" do end it "raises a RangeError when passed a Bignum" do - -> { @s.INT2NUM(bignum_value) }.should raise_error(RangeError) + -> { @s.INT2NUM(bignum_value) }.should.raise(RangeError) end it "converts a Fixnum" do @@ -195,7 +195,7 @@ describe "CApiNumericSpecs" do describe "NUM2ULONG" do it "raises a TypeError if passed nil" do - -> { @s.NUM2ULONG(nil) }.should raise_error(TypeError) + -> { @s.NUM2ULONG(nil) }.should.raise(TypeError) end it "converts a Float" do @@ -227,7 +227,7 @@ describe "CApiNumericSpecs" do end it "raises a RangeError if the value is more than 32bits" do - -> { @s.NUM2ULONG(0xffff_ffff+1) }.should raise_error(RangeError) + -> { @s.NUM2ULONG(0xffff_ffff+1) }.should.raise(RangeError) end end @@ -250,7 +250,7 @@ describe "CApiNumericSpecs" do it "raises a RangeError if the value is more than 64bits" do -> do @s.NUM2ULONG(0xffff_ffff_ffff_ffff+1) - end.should raise_error(RangeError) + end.should.raise(RangeError) end end @@ -308,11 +308,11 @@ describe "CApiNumericSpecs" do describe "NUM2DBL" do it "raises a TypeError if passed nil" do - -> { @s.NUM2DBL(nil) }.should raise_error(TypeError) + -> { @s.NUM2DBL(nil) }.should.raise(TypeError) end it "raises a TypeError if passed a String" do - -> { @s.NUM2DBL("1.2") }.should raise_error(TypeError) + -> { @s.NUM2DBL("1.2") }.should.raise(TypeError) end it "converts a Float" do @@ -348,13 +348,13 @@ describe "CApiNumericSpecs" do end it "raises a TypeError when passed an empty String" do - -> { @s.NUM2CHR("") }.should raise_error(TypeError) + -> { @s.NUM2CHR("") }.should.raise(TypeError) end end describe "rb_num_zerodiv" do it "raises a RuntimeError" do - -> { @s.rb_num_zerodiv() }.should raise_error(ZeroDivisionError, 'divided by 0') + -> { @s.rb_num_zerodiv() }.should.raise(ZeroDivisionError, 'divided by 0') end end @@ -386,7 +386,7 @@ describe "CApiNumericSpecs" do it "raises an ArgumentError when passed nil" do -> { @s.rb_cmpint(nil, 4) - }.should raise_error(ArgumentError) + }.should.raise(ArgumentError) end end @@ -410,7 +410,7 @@ describe "CApiNumericSpecs" do obj = mock("rb_num_coerce_bin") obj.should_receive(:coerce).with(2).and_return(nil) - -> { @s.rb_num_coerce_bin(2, obj, :+) }.should raise_error(TypeError) + -> { @s.rb_num_coerce_bin(2, obj, :+) }.should.raise(TypeError) end end @@ -435,14 +435,14 @@ describe "CApiNumericSpecs" do obj.should_receive(:coerce).with(2).and_raise(RuntimeError.new("my error")) -> { @s.rb_num_coerce_cmp(2, obj, :<=>) - }.should raise_error(RuntimeError, "my error") + }.should.raise(RuntimeError, "my error") end it "returns nil if #coerce does not return an Array" do obj = mock("rb_num_coerce_cmp") obj.should_receive(:coerce).with(2).and_return(nil) - @s.rb_num_coerce_cmp(2, obj, :<=>).should be_nil + @s.rb_num_coerce_cmp(2, obj, :<=>).should == nil end end @@ -451,7 +451,7 @@ describe "CApiNumericSpecs" do obj = mock("rb_num_coerce_relop") obj.should_receive(:coerce).with(2).and_return([1, 2]) - @s.rb_num_coerce_relop(2, obj, :<).should be_true + @s.rb_num_coerce_relop(2, obj, :<).should == true end it "calls the specified method on the first argument returned by #coerce" do @@ -459,7 +459,7 @@ describe "CApiNumericSpecs" do obj.should_receive(:coerce).with(2).and_return([obj, 2]) obj.should_receive(:<).with(2).and_return(false) - @s.rb_num_coerce_relop(2, obj, :<).should be_false + @s.rb_num_coerce_relop(2, obj, :<).should == false end it "raises an ArgumentError if #<op> returns nil" do @@ -467,14 +467,14 @@ describe "CApiNumericSpecs" do obj.should_receive(:coerce).with(2).and_return([obj, 2]) obj.should_receive(:<).with(2).and_return(nil) - -> { @s.rb_num_coerce_relop(2, obj, :<) }.should raise_error(ArgumentError) + -> { @s.rb_num_coerce_relop(2, obj, :<) }.should.raise(ArgumentError) end it "raises an ArgumentError if #coerce does not return an Array" do obj = mock("rb_num_coerce_relop") obj.should_receive(:coerce).with(2).and_return(nil) - -> { @s.rb_num_coerce_relop(2, obj, :<) }.should raise_error(ArgumentError) + -> { @s.rb_num_coerce_relop(2, obj, :<) }.should.raise(ArgumentError) end end diff --git a/spec/ruby/optional/capi/object_spec.rb b/spec/ruby/optional/capi/object_spec.rb index 7bc7bd992a..8e907a5a8c 100644 --- a/spec/ruby/optional/capi/object_spec.rb +++ b/spec/ruby/optional/capi/object_spec.rb @@ -64,7 +64,7 @@ describe "CApiObject" do it "allocates a new uninitialized object" do o = @o.rb_obj_alloc(CApiObjectSpecs::Alloc) o.class.should == CApiObjectSpecs::Alloc - o.initialized.should be_nil + o.initialized.should == nil end end @@ -77,17 +77,17 @@ describe "CApiObject" do obj2.foo.should == obj1.foo - obj2.should_not equal(obj1) + obj2.should_not.equal?(obj1) end end describe "rb_obj_call_init" do it "sends #initialize" do o = @o.rb_obj_alloc(CApiObjectSpecs::Alloc) - o.initialized.should be_nil + o.initialized.should == nil @o.rb_obj_call_init(o, 2, [:one, :two]) - o.initialized.should be_true + o.initialized.should == true o.arguments.should == [:one, :two] end @@ -235,7 +235,7 @@ describe "CApiObject" do describe "rb_obj_instance_variables" do it "returns an array with instance variable names as symbols" do o = ObjectTest.new - @o.rb_obj_instance_variables(o).should include(:@foo) + @o.rb_obj_instance_variables(o).should.include?(:@foo) end end @@ -244,21 +244,21 @@ describe "CApiObject" do ary = [1, 2] ary.should_not_receive(:to_ary) - @o.rb_check_convert_type(ary, "Array", "to_ary").should equal(ary) + @o.rb_check_convert_type(ary, "Array", "to_ary").should.equal?(ary) end it "returns the passed object and does not call the converting method if the object is a subclass of the specified type" do obj = CApiObjectSpecs::SubArray.new obj.should_not_receive(:to_array) - @o.rb_check_convert_type(obj, "Array", "to_array").should equal(obj) + @o.rb_check_convert_type(obj, "Array", "to_array").should.equal?(obj) end it "returns nil if the converting method returns nil" do obj = mock("rb_check_convert_type") obj.should_receive(:to_array).and_return(nil) - @o.rb_check_convert_type(obj, "Array", "to_array").should be_nil + @o.rb_check_convert_type(obj, "Array", "to_array").should == nil end it "raises a TypeError if the converting method returns an object that is not the specified type" do @@ -267,7 +267,7 @@ describe "CApiObject" do -> do @o.rb_check_convert_type(obj, "Array", "to_array") - end.should raise_error(TypeError) + end.should.raise(TypeError) end end @@ -276,14 +276,14 @@ describe "CApiObject" do ary = [1, 2] ary.should_not_receive(:to_ary) - @o.rb_convert_type(ary, "Array", "to_ary").should equal(ary) + @o.rb_convert_type(ary, "Array", "to_ary").should.equal?(ary) end it "returns the passed object and does not call the converting method if the object is a subclass of the specified type" do obj = CApiObjectSpecs::SubArray.new obj.should_not_receive(:to_array) - @o.rb_convert_type(obj, "Array", "to_array").should equal(obj) + @o.rb_convert_type(obj, "Array", "to_array").should.equal?(obj) end it "raises a TypeError if the converting method returns nil" do @@ -292,7 +292,7 @@ describe "CApiObject" do -> do @o.rb_convert_type(obj, "Array", "to_array") - end.should raise_error(TypeError) + end.should.raise(TypeError) end it "raises a TypeError if the converting method returns an object that is not the specified type" do @@ -301,109 +301,109 @@ describe "CApiObject" do -> do @o.rb_convert_type(obj, "Array", "to_array") - end.should raise_error(TypeError) + end.should.raise(TypeError) end end describe "rb_check_array_type" do it "returns the argument if it's an Array" do x = Array.new - @o.rb_check_array_type(x).should equal(x) + @o.rb_check_array_type(x).should.equal?(x) end it "returns the argument if it's a kind of Array" do x = AryChild.new - @o.rb_check_array_type(x).should equal(x) + @o.rb_check_array_type(x).should.equal?(x) end it "returns nil when the argument does not respond to #to_ary" do - @o.rb_check_array_type(Object.new).should be_nil + @o.rb_check_array_type(Object.new).should == nil end it "sends #to_ary to the argument and returns the result if it's nil" do obj = mock("to_ary") obj.should_receive(:to_ary).and_return(nil) - @o.rb_check_array_type(obj).should be_nil + @o.rb_check_array_type(obj).should == nil end it "sends #to_ary to the argument and returns the result if it's an Array" do x = Array.new obj = mock("to_ary") obj.should_receive(:to_ary).and_return(x) - @o.rb_check_array_type(obj).should equal(x) + @o.rb_check_array_type(obj).should.equal?(x) end it "sends #to_ary to the argument and returns the result if it's a kind of Array" do x = AryChild.new obj = mock("to_ary") obj.should_receive(:to_ary).and_return(x) - @o.rb_check_array_type(obj).should equal(x) + @o.rb_check_array_type(obj).should.equal?(x) end it "sends #to_ary to the argument and raises TypeError if it's not a kind of Array" do obj = mock("to_ary") obj.should_receive(:to_ary).and_return(Object.new) - -> { @o.rb_check_array_type obj }.should raise_error(TypeError) + -> { @o.rb_check_array_type obj }.should.raise(TypeError) end it "does not rescue exceptions raised by #to_ary" do obj = mock("to_ary") obj.should_receive(:to_ary).and_raise(FrozenError) - -> { @o.rb_check_array_type obj }.should raise_error(FrozenError) + -> { @o.rb_check_array_type obj }.should.raise(FrozenError) end end describe "rb_check_string_type" do it "returns the argument if it's a String" do x = String.new - @o.rb_check_string_type(x).should equal(x) + @o.rb_check_string_type(x).should.equal?(x) end it "returns the argument if it's a kind of String" do x = StrChild.new - @o.rb_check_string_type(x).should equal(x) + @o.rb_check_string_type(x).should.equal?(x) end it "returns nil when the argument does not respond to #to_str" do - @o.rb_check_string_type(Object.new).should be_nil + @o.rb_check_string_type(Object.new).should == nil end it "sends #to_str to the argument and returns the result if it's nil" do obj = mock("to_str") obj.should_receive(:to_str).and_return(nil) - @o.rb_check_string_type(obj).should be_nil + @o.rb_check_string_type(obj).should == nil end it "sends #to_str to the argument and returns the result if it's a String" do x = String.new obj = mock("to_str") obj.should_receive(:to_str).and_return(x) - @o.rb_check_string_type(obj).should equal(x) + @o.rb_check_string_type(obj).should.equal?(x) end it "sends #to_str to the argument and returns the result if it's a kind of String" do x = StrChild.new obj = mock("to_str") obj.should_receive(:to_str).and_return(x) - @o.rb_check_string_type(obj).should equal(x) + @o.rb_check_string_type(obj).should.equal?(x) end it "sends #to_str to the argument and raises TypeError if it's not a kind of String" do obj = mock("to_str") obj.should_receive(:to_str).and_return(Object.new) - -> { @o.rb_check_string_type obj }.should raise_error(TypeError) + -> { @o.rb_check_string_type obj }.should.raise(TypeError) end it "does not rescue exceptions raised by #to_str" do obj = mock("to_str") obj.should_receive(:to_str).and_raise(RuntimeError) - -> { @o.rb_check_string_type obj }.should raise_error(RuntimeError) + -> { @o.rb_check_string_type obj }.should.raise(RuntimeError) end end describe "rb_check_to_integer" do it "returns the object when passed a Fixnum" do - @o.rb_check_to_integer(5, "to_int").should equal(5) + @o.rb_check_to_integer(5, "to_int").should.equal?(5) end it "returns the object when passed a Bignum" do @@ -414,7 +414,7 @@ describe "CApiObject" do obj = mock("rb_check_to_integer") obj.should_receive(:to_integer).and_return(10) - @o.rb_check_to_integer(obj, "to_integer").should equal(10) + @o.rb_check_to_integer(obj, "to_integer").should.equal?(10) end it "calls the converting method and returns a Bignum value" do @@ -428,23 +428,23 @@ describe "CApiObject" do obj = mock("rb_check_to_integer") obj.should_receive(:to_integer).and_return(nil) - @o.rb_check_to_integer(obj, "to_integer").should be_nil + @o.rb_check_to_integer(obj, "to_integer").should == nil end it "returns nil when the converting method does not return an Integer" do obj = mock("rb_check_to_integer") obj.should_receive(:to_integer).and_return("string") - @o.rb_check_to_integer(obj, "to_integer").should be_nil + @o.rb_check_to_integer(obj, "to_integer").should == nil end end describe "FL_ABLE" do it "returns correct boolean for type" do - @o.FL_ABLE(Object.new).should be_true - @o.FL_ABLE(true).should be_false - @o.FL_ABLE(nil).should be_false - @o.FL_ABLE(1).should be_false + @o.FL_ABLE(Object.new).should == true + @o.FL_ABLE(true).should == false + @o.FL_ABLE(nil).should == false + @o.FL_ABLE(1).should == false end end @@ -476,9 +476,9 @@ describe "CApiObject" do it "returns the singleton class if it exists" do o = ObjectTest.new - @o.rb_class_of(o).should equal ObjectTest + @o.rb_class_of(o).should.equal? ObjectTest s = o.singleton_class - @o.rb_class_of(o).should equal s + @o.rb_class_of(o).should.equal? s end end @@ -493,7 +493,7 @@ describe "CApiObject" do it "does not return the singleton class if it exists" do o = ObjectTest.new o.singleton_class - @o.rb_obj_class(o).should equal ObjectTest + @o.rb_obj_class(o).should.equal? ObjectTest end end @@ -553,15 +553,15 @@ describe "CApiObject" do 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)') + }.should.raise(TypeError, 'wrong argument type Array (expected Object)') -> { @o.rb_check_type(ObjectTest, Module.new) - }.should raise_error(TypeError, 'wrong argument type Class (expected Module)') + }.should.raise(TypeError, 'wrong argument type Class (expected Module)') -> { @o.rb_check_type(nil, "string") - }.should raise_error(TypeError, 'wrong argument type nil (expected String)') + }.should.raise(TypeError, 'wrong argument type nil (expected String)') end end @@ -592,49 +592,49 @@ describe "CApiObject" do describe "RTEST" do it "returns C false if passed Qfalse" do - @o.RTEST(false).should be_false + @o.RTEST(false).should == false end it "returns C false if passed Qnil" do - @o.RTEST(nil).should be_false + @o.RTEST(nil).should == false end it "returns C true if passed Qtrue" do - @o.RTEST(true).should be_true + @o.RTEST(true).should == true end it "returns C true if passed a Symbol" do - @o.RTEST(:test).should be_true + @o.RTEST(:test).should == true end it "returns C true if passed an Object" do - @o.RTEST(Object.new).should be_true + @o.RTEST(Object.new).should == true end end describe "rb_special_const_p" do it "returns true if passed Qfalse" do - @o.rb_special_const_p(false).should be_true + @o.rb_special_const_p(false).should == true end it "returns true if passed Qtrue" do - @o.rb_special_const_p(true).should be_true + @o.rb_special_const_p(true).should == true end it "returns true if passed Qnil" do - @o.rb_special_const_p(nil).should be_true + @o.rb_special_const_p(nil).should == true end it "returns true if passed a Symbol" do - @o.rb_special_const_p(:test).should be_true + @o.rb_special_const_p(:test).should == true end it "returns true if passed a Fixnum" do - @o.rb_special_const_p(10).should be_true + @o.rb_special_const_p(10).should == true end it "returns false if passed an Object" do - @o.rb_special_const_p(Object.new).should be_false + @o.rb_special_const_p(Object.new).should == false end end @@ -665,7 +665,7 @@ describe "CApiObject" do it "freezes the object passed to it" do obj = "" @o.rb_obj_freeze(obj).should == obj - obj.frozen?.should be_true + obj.frozen?.should == true end end @@ -674,7 +674,7 @@ describe "CApiObject" do obj = ObjectTest -> do @o.rb_obj_instance_eval(obj) { include Kernel } - end.should_not raise_error(NoMethodError) + end.should_not.raise(NoMethodError) end end @@ -691,17 +691,27 @@ describe "CApiObject" do end end + describe "redefining frozen? works" do + it "allows an object to override frozen?" do + obj = CApiObjectRedefinitionSpecs.new + + obj.frozen?.should == false + obj.freeze + obj.frozen?.should == true + end + end + describe "rb_obj_taint" do end describe "rb_check_frozen" do it "raises a FrozenError if the obj is frozen" do - -> { @o.rb_check_frozen("".freeze) }.should raise_error(FrozenError) + -> { @o.rb_check_frozen("".freeze) }.should.raise(FrozenError) end it "does nothing when object isn't frozen" do obj = +"" - -> { @o.rb_check_frozen(obj) }.should_not raise_error(TypeError) + -> { @o.rb_check_frozen(obj) }.should_not.raise(TypeError) end end @@ -709,13 +719,13 @@ describe "CApiObject" do it "converts an Integer to string" do obj = 1 i = @o.rb_any_to_s(obj) - i.should be_kind_of(String) + i.should.is_a?(String) end it "converts an Object to string" do obj = Object.new i = @o.rb_any_to_s(obj) - i.should be_kind_of(String) + i.should.is_a?(String) end end @@ -741,61 +751,61 @@ describe "CApiObject" do it "raises a TypeError if #to_int does not return an Integer" do x = mock("to_int") x.should_receive(:to_int).and_return("5") - -> { @o.rb_to_int(x) }.should raise_error(TypeError) + -> { @o.rb_to_int(x) }.should.raise(TypeError) end it "raises a TypeError if called with nil" do - -> { @o.rb_to_int(nil) }.should raise_error(TypeError) + -> { @o.rb_to_int(nil) }.should.raise(TypeError) end it "raises a TypeError if called with true" do - -> { @o.rb_to_int(true) }.should raise_error(TypeError) + -> { @o.rb_to_int(true) }.should.raise(TypeError) end it "raises a TypeError if called with false" do - -> { @o.rb_to_int(false) }.should raise_error(TypeError) + -> { @o.rb_to_int(false) }.should.raise(TypeError) end it "raises a TypeError if called with a String" do - -> { @o.rb_to_int("1") }.should raise_error(TypeError) + -> { @o.rb_to_int("1") }.should.raise(TypeError) end end describe "rb_equal" do it "returns true if the arguments are the same exact object" do s = "hello" - @o.rb_equal(s, s).should be_true + @o.rb_equal(s, s).should == true end it "calls == to check equality and coerces to true/false" do m = mock("string") m.should_receive(:==).and_return(8) - @o.rb_equal(m, "hello").should be_true + @o.rb_equal(m, "hello").should == true m2 = mock("string") m2.should_receive(:==).and_return(nil) - @o.rb_equal(m2, "hello").should be_false + @o.rb_equal(m2, "hello").should == false end end describe "rb_class_inherited_p" do it "returns true if mod equals arg" do - @o.rb_class_inherited_p(Array, Array).should be_true + @o.rb_class_inherited_p(Array, Array).should == true end it "returns true if mod is a subclass of arg" do - @o.rb_class_inherited_p(Array, Object).should be_true + @o.rb_class_inherited_p(Array, Object).should == true end it "returns nil if mod is not a subclass of arg" do - @o.rb_class_inherited_p(Array, Hash).should be_nil + @o.rb_class_inherited_p(Array, Hash).should == nil end it "raises a TypeError if arg is no class or module" do ->{ @o.rb_class_inherited_p(1, 2) - }.should raise_error(TypeError) + }.should.raise(TypeError) end end @@ -888,7 +898,7 @@ describe "CApiObject" do end end - # The `generic_iv_tbl` table and `*_generic_ivar` functions are for mutable + # The `generic_fields_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. @@ -919,7 +929,7 @@ describe "CApiObject" do @o.rb_define_alloc_func(klass) obj = klass.allocate obj.class.should.equal?(klass) - obj.should have_instance_variable(:@from_custom_allocator) + obj.should.instance_variable_defined?(:@from_custom_allocator) end it "sets up the allocator for a subclass of String" do @@ -927,7 +937,7 @@ describe "CApiObject" do @o.rb_define_alloc_func(klass) obj = klass.allocate obj.class.should.equal?(klass) - obj.should have_instance_variable(:@from_custom_allocator) + obj.should.instance_variable_defined?(:@from_custom_allocator) obj.should == "" end @@ -936,7 +946,7 @@ describe "CApiObject" do @o.rb_define_alloc_func(klass) obj = klass.allocate obj.class.should.equal?(klass) - obj.should have_instance_variable(:@from_custom_allocator) + obj.should.instance_variable_defined?(:@from_custom_allocator) obj.should == [] end end @@ -994,7 +1004,6 @@ describe "CApiObject" do it "calls the callback function for each cvar and ivar on a class" do exp = [:@@cvar, :foo, :@@cvar2, :bar, :@ivar, :baz] - exp.unshift(:__classpath__, 'CApiObjectSpecs::CVars') if RUBY_VERSION < "3.3" ary = @o.rb_ivar_foreach(CApiObjectSpecs::CVars) ary.should == exp @@ -1002,7 +1011,6 @@ describe "CApiObject" do it "calls the callback function for each cvar and ivar on a module" do exp = [:@@mvar, :foo, :@@mvar2, :bar, :@ivar, :baz] - exp.unshift(:__classpath__, 'CApiObjectSpecs::MVars') if RUBY_VERSION < "3.3" ary = @o.rb_ivar_foreach(CApiObjectSpecs::MVars) ary.should == exp diff --git a/spec/ruby/optional/capi/proc_spec.rb b/spec/ruby/optional/capi/proc_spec.rb index 8b94432f3e..1c250fee76 100644 --- a/spec/ruby/optional/capi/proc_spec.rb +++ b/spec/ruby/optional/capi/proc_spec.rb @@ -36,7 +36,7 @@ describe "C-API Proc function" do it "calls the C function with arguments in argv" do @prc2.call(1, :foo).should == :foo @prc2.call(2, :foo, :bar).should == :bar - -> { @prc2.call(3, :foo, :bar) }.should raise_error(ArgumentError) + -> { @prc2.call(3, :foo, :bar) }.should.raise(ArgumentError) end it "calls the C function with the block passed in blockarg" do @@ -95,7 +95,7 @@ describe "C-API Proc function" do it "raises TypeError if the last argument is not a Hash" do -> { @p.rb_proc_call_kw(proc {}, [42]) - }.should raise_error(TypeError, 'no implicit conversion of Integer into Hash') + }.should.raise(TypeError, 'no implicit conversion of Integer into Hash') end end @@ -125,7 +125,7 @@ describe "C-API Proc function" do it "raises TypeError if the last argument is not a Hash" do -> { @p.rb_proc_call_with_block_kw(proc {}, [42], proc {}) - }.should raise_error(TypeError, 'no implicit conversion of Integer into Hash') + }.should.raise(TypeError, 'no implicit conversion of Integer into Hash') end it "passes keyword arguments to the proc when a block is nil" do @@ -138,19 +138,19 @@ describe "C-API Proc function" do describe "rb_obj_is_proc" do it "returns true for Proc" do prc = Proc.new {|a,b| a * b } - @p.rb_obj_is_proc(prc).should be_true + @p.rb_obj_is_proc(prc).should == true end it "returns true for subclass of Proc" do prc = Class.new(Proc).new {} - @p.rb_obj_is_proc(prc).should be_true + @p.rb_obj_is_proc(prc).should == true end it "returns false for non Proc instances" do - @p.rb_obj_is_proc("aoeui").should be_false - @p.rb_obj_is_proc(123).should be_false - @p.rb_obj_is_proc(true).should be_false - @p.rb_obj_is_proc([]).should be_false + @p.rb_obj_is_proc("aoeui").should == false + @p.rb_obj_is_proc(123).should == false + @p.rb_obj_is_proc(true).should == false + @p.rb_obj_is_proc([]).should == false end end end @@ -172,17 +172,17 @@ describe "C-API when calling Proc.new from a C function" do it "raises an ArgumentError when the C function calls a Ruby method that calls Proc.new" do -> { @p.rb_Proc_new(2) { :called } - }.should raise_error(ArgumentError) + }.should.raise(ArgumentError) end # Ruby -> C -> Ruby -> C -> rb_funcall(Proc.new) it "raises an ArgumentError when the C function calls a Ruby method and that method calls a C function that calls Proc.new" do def @p.redispatch() rb_Proc_new(0) end - -> { @p.rb_Proc_new(3) { :called } }.should raise_error(ArgumentError) + -> { @p.rb_Proc_new(3) { :called } }.should.raise(ArgumentError) end # Ruby -> C -> Ruby -> block_given? it "returns false from block_given? in a Ruby method called by the C function" do - @p.rb_Proc_new(6).should be_false + @p.rb_Proc_new(6).should == false end end diff --git a/spec/ruby/optional/capi/range_spec.rb b/spec/ruby/optional/capi/range_spec.rb index 7a52dc7ff8..9213862aa4 100644 --- a/spec/ruby/optional/capi/range_spec.rb +++ b/spec/ruby/optional/capi/range_spec.rb @@ -31,8 +31,8 @@ describe "C-API Range function" do end it "raises an ArgumentError when the given start and end can't be compared by using #<=>" do - -> { @s.rb_range_new(1, mock('x')) }.should raise_error(ArgumentError) - -> { @s.rb_range_new(mock('x'), mock('y')) }.should raise_error(ArgumentError) + -> { @s.rb_range_new(1, mock('x')) }.should.raise(ArgumentError) + -> { @s.rb_range_new(mock('x'), mock('y')) }.should.raise(ArgumentError) end end @@ -41,7 +41,7 @@ describe "C-API Range function" do beg, fin, excl = @s.rb_range_values(10..20) beg.should == 10 fin.should == 20 - excl.should be_false + excl.should == false end it "stores the range properties of non-Range object" do @@ -62,34 +62,170 @@ describe "C-API Range function" do beg, fin, excl = @s.rb_range_values(range_like) beg.should == 10 fin.should == 20 - excl.should be_false + excl.should == false end end describe "rb_range_beg_len" do it "returns correct begin, length and result" do r = 2..5 - begp, lenp, result = @s.rb_range_beg_len(r, 0, 0, 10, 0) - result.should be_true + begp, lenp, result = @s.rb_range_beg_len(r, 10, 0) + result.should == true begp.should == 2 lenp.should == 4 end it "returns nil when not in range" do r = 2..5 - begp, lenp, result = @s.rb_range_beg_len(r, 0, 0, 1, 0) - result.should be_nil + begp, lenp, result = @s.rb_range_beg_len(r, 1, 0) + result.should == nil end it "raises a RangeError when not in range and err is 1" do r = -5..-1 - -> { @s.rb_range_beg_len(r, 0, 0, 1, 1) }.should raise_error(RangeError) + -> { @s.rb_range_beg_len(r, 1, 1) }.should.raise(RangeError) end it "returns nil when not in range and err is 0" do r = -5..-1 - begp, lenp, result = @s.rb_range_beg_len(r, 0, 0, 1, 0) - result.should be_nil + begp, lenp, result = @s.rb_range_beg_len(r, 1, 0) + result.should == nil + end + end + + describe "rb_arithmetic_sequence_extract" do + it "returns begin, end, step, exclude end of an instance of an Enumerator::ArithmeticSequence" do + enum = (10..20).step(5) + enum.should.kind_of?(Enumerator::ArithmeticSequence) + + @s.rb_arithmetic_sequence_extract(enum).should == [1, 10, 20, 5, false] + end + + it "returns begin, end, step, exclude end of an instance of a Range" do + range = (10..20) + @s.rb_arithmetic_sequence_extract(range).should == [1, 10, 20, 1, false] + end + + it "returns begin, end, step, exclude end of a non-Range object with Range properties" do + object = Object.new + def object.begin + 10 + end + def object.end + 20 + end + def object.exclude_end? + false + end + + @s.rb_arithmetic_sequence_extract(object).should == [1, 10, 20, 1, false] + end + + it "returns failed status if given object is not Enumerator::ArithmeticSequence or Range or Range-like object" do + object = Object.new + @s.rb_arithmetic_sequence_extract(object).should == [0] + end + end + + describe "rb_arithmetic_sequence_beg_len_step" do + it "returns correct begin, length, step and result" do + as = (2..5).step(5) + error_code = 0 + + success, beg, len, step = @s.rb_arithmetic_sequence_beg_len_step(as, 6, error_code) + success.should == true + + beg.should == 2 + len.should == 4 + step.should == 5 + end + + it "takes into account excluded end boundary" do + as = (2...5).step(1) + error_code = 0 + + success, _, len, _ = @s.rb_arithmetic_sequence_beg_len_step(as, 6, error_code) + success.should == true + len.should == 3 + end + + it "adds length to negative begin boundary" do + as = (-2..5).step(1) + error_code = 0 + + success, beg, len, _ = @s.rb_arithmetic_sequence_beg_len_step(as, 6, error_code) + success.should == true + + beg.should == 4 + len.should == 2 + end + + it "adds length to negative end boundary" do + as = (2..-1).step(1) + error_code = 0 + + success, beg, len, _ = @s.rb_arithmetic_sequence_beg_len_step(as, 6, error_code) + success.should == true + + beg.should == 2 + len.should == 4 + end + + it "truncates arithmetic sequence length if end boundary greater than specified length value" do + as = (2..10).step(1) + error_code = 0 + + success, _, len, _ = @s.rb_arithmetic_sequence_beg_len_step(as, 6, error_code) + success.should == true + len.should == 4 + end + + it "returns inverted begin and end boundaries when step is negative" do + as = (2..5).step(-2) + error_code = 0 + + success, beg, len, step = @s.rb_arithmetic_sequence_beg_len_step(as, 6, error_code) + success.should == true + + beg.should == 5 + len.should == 0 + step.should == -2 + end + + it "returns nil when not in range and error code = 0" do + as = (2..5).step(1) + error_code = 0 + + success, = @s.rb_arithmetic_sequence_beg_len_step(as, 1, error_code) + success.should == nil + end + + it "returns nil when not in range, negative boundaries and error code = 0" do + as = (-5..-1).step(1) + error_code = 0 + + success, = @s.rb_arithmetic_sequence_beg_len_step(as, 1, 0) + success.should == nil + end + + it "returns begin, length and step and doesn't raise a RangeError when not in range and error code = 1" do + as = (2..5).step(1) + error_code = 1 + + success, beg, len, step = @s.rb_arithmetic_sequence_beg_len_step(as, 1, error_code) + success.should == true + + beg.should == 2 + len.should == 4 + step.should == 1 + end + + it "returns nil and doesn't raise a RangeError when not in range, negative boundaries and error code = 1" do + as = (-5..-1).step(1) + error_code = 1 + + success, = @s.rb_arithmetic_sequence_beg_len_step(as, 1, error_code) + success.should == nil end end end diff --git a/spec/ruby/optional/capi/regexp_spec.rb b/spec/ruby/optional/capi/regexp_spec.rb index af366e17a2..f233b5e3b3 100644 --- a/spec/ruby/optional/capi/regexp_spec.rb +++ b/spec/ruby/optional/capi/regexp_spec.rb @@ -77,7 +77,7 @@ describe "C-API Regexp function" do end it "returns MatchData when used with rb_reg_match" do - @p.rb_reg_match_backref_get(/a/, 'ab')[0].should == 'a' + @p.rb_reg_match_backref_get(/a/, 'ab')[0].should == 'a' end end @@ -104,13 +104,13 @@ describe "C-API Regexp function" do end Thread.pass while thr.status and !running - $~.should be_nil + $~.should == nil thr.join end end - describe "rb_memicmp" do + describe "rb_memcicmp" do it "returns 0 for identical strings" do @p.rb_memcicmp('Hello', 'Hello').should == 0 end diff --git a/spec/ruby/optional/capi/set_spec.rb b/spec/ruby/optional/capi/set_spec.rb new file mode 100644 index 0000000000..21a9756236 --- /dev/null +++ b/spec/ruby/optional/capi/set_spec.rb @@ -0,0 +1,96 @@ +require_relative 'spec_helper' + +ruby_version_is "4.0" do + load_extension("set") + + describe "C-API Set function" do + before :each do + @s = CApiSetSpecs.new + end + + describe "rb_set_foreach" do + it "calls function with each element and arg" do + a = [] + @s.rb_set_foreach(Set[1, 2], 3) {|*args| a.concat(args) } + a.should == [1, 3, 2, 3] + end + + it "respects function return value" do + a = [] + @s.rb_set_foreach(Set[1, 2], 3) do |*args| + a.concat(args) + false + end + a.should == [1, 3] + end + end + + describe "rb_set_new" do + it "returns a new set" do + @s.rb_set_new.should == Set[] + end + end + + describe "rb_set_new_capa" do + it "returns a new set" do + @s.rb_set_new_capa(3).should == Set[] + end + end + + describe "rb_set_lookup" do + it "returns whether the element is in the set" do + set = Set[1] + @s.rb_set_lookup(set, 1).should == true + @s.rb_set_lookup(set, 2).should == false + end + end + + describe "rb_set_add" do + it "adds element to set" do + set = Set[] + @s.rb_set_add(set, 1).should == true + set.should == Set[1] + @s.rb_set_add(set, 2).should == true + set.should == Set[1, 2] + end + + it "returns false if element is already in set" do + set = Set[1] + @s.rb_set_add(set, 1).should == false + set.should == Set[1] + end + end + + describe "rb_set_clear" do + it "empties and returns self" do + set = Set[1] + @s.rb_set_clear(set).should.equal?(set) + set.should == Set[] + end + end + + describe "rb_set_delete" do + it "removes element from set" do + set = Set[1, 2] + @s.rb_set_delete(set, 1).should == true + set.should == Set[2] + @s.rb_set_delete(set, 2).should == true + set.should == Set[] + end + + it "returns false if element is not already in set" do + set = Set[2] + @s.rb_set_delete(set, 1).should == false + set.should == Set[2] + end + end + + describe "rb_set_size" do + it "returns number of elements in set" do + @s.rb_set_size(Set[]).should == 0 + @s.rb_set_size(Set[1]).should == 1 + @s.rb_set_size(Set[1,2]).should == 2 + end + end + end +end diff --git a/spec/ruby/optional/capi/shared/rbasic.rb b/spec/ruby/optional/capi/shared/rbasic.rb index 9d80a93e1d..e3485d4b7b 100644 --- a/spec/ruby/optional/capi/shared/rbasic.rb +++ b/spec/ruby/optional/capi/shared/rbasic.rb @@ -1,7 +1,6 @@ describe :rbasic, shared: true do before :all do specs = CApiRBasicSpecs.new - @taint = ruby_version_is(''...'3.1') ? specs.taint_flag : 0 @freeze = specs.freeze_flag end diff --git a/spec/ruby/optional/capi/spec_helper.rb b/spec/ruby/optional/capi/spec_helper.rb index 2691aa1332..49ce23d874 100644 --- a/spec/ruby/optional/capi/spec_helper.rb +++ b/spec/ruby/optional/capi/spec_helper.rb @@ -59,7 +59,11 @@ def compile_extension(name) tmpdir = tmp("cext_#{name}") Dir.mkdir(tmpdir) begin - ["#{core_ext_dir}/rubyspec.h", "#{spec_ext_dir}/#{ext}.c"].each do |file| + files = ["#{core_ext_dir}/rubyspec.h", "#{spec_ext_dir}/#{ext}.c"] + if spec_ext_dir != core_ext_dir + files += Dir.glob("#{spec_ext_dir}/*.h") + end + files.each do |file| if cxx and file.end_with?('.c') cp file, "#{tmpdir}/#{File.basename(file, '.c')}.cpp" else @@ -74,12 +78,25 @@ def compile_extension(name) init_mkmf unless required create_makefile(ext, tmpdir) else + # Workaround for digest C-API specs to find the ruby/digest.h header + # when run in the CRuby repository via make test-spec + if MSpecScript.instance_variable_defined?(:@testing_ruby) + ruby_repository_extra_include_dir = "-I#{RbConfig::CONFIG.fetch("prefix")}/#{RbConfig::CONFIG.fetch("EXTOUT")}/include" + end + File.write("extconf.rb", <<-RUBY) require 'mkmf' $ruby = ENV.values_at('RUBY_EXE', 'RUBY_FLAGS').join(' ') # MRI magic to consider building non-bundled extensions $extout = nil + if RbConfig::CONFIG.key?("buildlibdir") # The top directory where the libruby is built. + # Prepend the dummy macro to bypass the conversion in `with_destdir` on DOSISH + # platforms, where the drive letter is replaced with `$(DESTDIR)`. `DESTDIR` is + # overridden by the command line argument bellow. + RbConfig::MAKEFILE_CONFIG["buildlibdir"] = "$(empty)" + RbConfig::CONFIG["buildlibdir"] + end append_cflags '-Wno-declaration-after-statement' + #{"append_cflags #{ruby_repository_extra_include_dir.inspect}" if ruby_repository_extra_include_dir} create_makefile(#{ext.inspect}) RUBY output = ruby_exe("extconf.rb") @@ -89,7 +106,7 @@ def compile_extension(name) # Do not capture stderr as we want to show compiler warnings make, opts = setup_make - output = IO.popen([make, "V=1", "DESTDIR=", opts], &:read) + output = IO.popen([*make, "V=1", "DESTDIR=", opts], &:read) raise "#{make} failed:\n#{output}" unless $?.success? $stderr.puts output if debug @@ -106,26 +123,34 @@ end def setup_make make = ENV['MAKE'] make ||= (RbConfig::CONFIG['host_os'].include?("mswin") ? "nmake" : "make") - make_flags = ENV["MAKEFLAGS"] || '' + env = %w[MFLAGS MAKEFLAGS GNUMAKEFLAGS].to_h {|var| [var, ENV[var]]} + make_flags = env["MAKEFLAGS"] || '' # suppress logo of nmake.exe to stderr if File.basename(make, ".*").downcase == "nmake" and !make_flags.include?("l") - ENV["MAKEFLAGS"] = "l#{make_flags}" + env["MAKEFLAGS"] = "l#{make_flags}" end opts = {} if /(?:\A|\s)--jobserver-(?:auth|fds)=(\d+),(\d+)/ =~ make_flags - begin - r = IO.for_fd($1.to_i(10), "rb", autoclose: false) - w = IO.for_fd($2.to_i(10), "wb", autoclose: false) - rescue Errno::EBADF - else - opts[r] = r - opts[w] = w + [$1, $2].each do |fd| + fd = IO.for_fd(fd.to_i(10), autoclose: false) + opts[fd] = fd + rescue + # Jobserver is not usable, maybe no `+` flag or on Windows. + job_options = /\A\s*(?:-j\d+\s*|-\S+\Kj\d+)|(?:\G|\s)\K--jobserver-(?:auth|fds)=\S+\s*/ + env["MAKEFLAGS"] = make_flags.gsub(job_options, '') + %w[GNUMAKEFLAGS MFLAGS].each do |var| + if flags = env[var] + env[var] = flags.gsub(job_options, '') + end + end + opts.clear + break end end - [make, opts] + [[env, make], opts] end def load_extension(name) diff --git a/spec/ruby/optional/capi/string_spec.rb b/spec/ruby/optional/capi/string_spec.rb index 801d50f117..3e095f05c8 100644 --- a/spec/ruby/optional/capi/string_spec.rb +++ b/spec/ruby/optional/capi/string_spec.rb @@ -191,11 +191,19 @@ describe "C-API String function" do end it "returns a new String object filled with \\0 bytes" do - s = @s.rb_str_tmp_new(4) - s.encoding.should == Encoding::BINARY - s.bytesize.should == 4 - s.size.should == 4 - s.should == "\x00\x00\x00\x00" + lens = [4] + + ruby_version_is "4.0" do + lens << 100 + end + + lens.each do |len| + s = @s.rb_str_tmp_new(len) + s.encoding.should == Encoding::BINARY + s.bytesize.should == len + s.size.should == len + s.should == "\x00" * len + end end end @@ -278,13 +286,13 @@ describe "C-API String function" do it "returns a dup of the original String" do a = "abc" b = @s.rb_str_encode("abc", "us-ascii", 0, nil) - a.should_not equal(b) + a.should_not.equal?(b) end it "returns a duplicate of the original when the encoding doesn't change" do a = "abc" b = @s.rb_str_encode("abc", Encoding::UTF_8, 0, nil) - a.should_not equal(b) + a.should_not.equal?(b) end it "accepts encoding flags" do @@ -312,7 +320,7 @@ describe "C-API String function" do str1 = "hi" str2 = @s.rb_str_new3 str1 str1.should == str2 - str1.should_not equal str2 + str1.should_not.equal? str2 end end @@ -322,7 +330,7 @@ describe "C-API String function" do str1.freeze str2 = @s.rb_str_new4 str1 str1.should == str2 - str1.should equal(str2) + str1.should.equal?(str2) str1.should.frozen? str2.should.frozen? end @@ -331,7 +339,7 @@ describe "C-API String function" do str1 = "hi" str2 = @s.rb_str_new4 str1 str1.should == str2 - str1.should_not equal(str2) + str1.should_not.equal?(str2) str2.should.frozen? end end @@ -341,7 +349,7 @@ describe "C-API String function" do str1 = "hi" str2 = @s.rb_str_dup str1 str1.should == str2 - str1.should_not equal str2 + str1.should_not.equal? str2 end end @@ -362,7 +370,7 @@ describe "C-API String function" do end it "raises a TypeError trying to append non-String-like object" do - -> { @s.rb_str_append("Hello", 32323)}.should raise_error(TypeError) + -> { @s.rb_str_append("Hello", 32323)}.should.raise(TypeError) end it "changes Encoding if a string is appended to an empty string" do @@ -491,7 +499,7 @@ describe "C-API String function" do end it "converts a C string to a Fixnum strictly if base is 0" do - -> { @s.rb_cstr2inum("1234a", 0) }.should raise_error(ArgumentError) + -> { @s.rb_cstr2inum("1234a", 0) }.should.raise(ArgumentError) end end @@ -509,7 +517,7 @@ describe "C-API String function" do end it "converts a C string to a Fixnum strictly" do - -> { @s.rb_cstr_to_inum("1234a", 10, true) }.should raise_error(ArgumentError) + -> { @s.rb_cstr_to_inum("1234a", 10, true) }.should.raise(ArgumentError) end end @@ -528,6 +536,61 @@ describe "C-API String function" do end end + describe "rb_str_sublen" do + it "returns the character length for a given byte offset in an ASCII string" do + @s.rb_str_sublen("hello", 3).should == 3 + end + + it "returns the character length for a given byte offset in a multibyte string" do + # "hëllo" where 'ë' is 2 bytes in UTF-8, total 6 bytes + str = "hëllo" + @s.rb_str_sublen(str, 3).should == 2 + end + + it "returns 0 for byte offset 0" do + @s.rb_str_sublen("hello", 0).should == 0 + end + + it "returns the full character length for the total byte length" do + str = "hëllo" + @s.rb_str_sublen(str, str.bytesize).should == str.length + end + end + + describe "rb_str_subpos" do + it "returns [byte_offset, byte_length] for a valid character offset in an ASCII string" do + @s.rb_str_subpos("hello", 1).should == [1, 4] + end + + it "returns [byte_offset, byte_length] for a valid character offset in a multibyte string" do + # "hëllo" where 'ë' is 2 bytes in UTF-8 + str = "hëllo" + @s.rb_str_subpos(str, 0).should == [0, 6] + @s.rb_str_subpos(str, 1).should == [1, 5] + @s.rb_str_subpos(str, 2).should == [3, 3] + end + + it "returns [0, byte_length] for offset 0" do + @s.rb_str_subpos("hello", 0).should == [0, 5] + end + + it "returns nil for a negative offset that is out of range" do + @s.rb_str_subpos("hello", -6).should == nil + end + + it "returns the correct position for a negative offset" do + @s.rb_str_subpos("hello", -2).should == [3, 2] + end + + it "returns [byte_length, 0] when offset equals string length" do + @s.rb_str_subpos("hello", 5).should == [5, 0] + end + + it "returns nil when offset is beyond string length" do + @s.rb_str_subpos("hello", 6).should == nil + end + end + describe "rb_str_to_str" do it "calls #to_str to coerce the value to a String" do @s.rb_str_to_str("foo").should == "foo" @@ -535,8 +598,8 @@ describe "C-API String function" do end it "raises a TypeError if coercion fails" do - -> { @s.rb_str_to_str(0) }.should raise_error(TypeError) - -> { @s.rb_str_to_str(CApiStringSpecs::InvalidTostrTest.new) }.should raise_error(TypeError) + -> { @s.rb_str_to_str(0) }.should.raise(TypeError) + -> { @s.rb_str_to_str(CApiStringSpecs::InvalidTostrTest.new) }.should.raise(TypeError) end end @@ -660,7 +723,7 @@ describe "C-API String function" do it "does not call #to_s on non-String objects" do str = mock("fake") str.should_not_receive(:to_s) - -> { @s.send(@method, str) }.should raise_error(TypeError) + -> { @s.send(@method, str) }.should.raise(TypeError) end end @@ -673,7 +736,7 @@ describe "C-API String function" do describe "rb_str_modify" do it "raises an error if the string is frozen" do - -> { @s.rb_str_modify("frozen".freeze) }.should raise_error(FrozenError) + -> { @s.rb_str_modify("frozen".freeze) }.should.raise(FrozenError) end end @@ -704,7 +767,7 @@ describe "C-API String function" do end it "raises an error if the string is frozen" do - -> { @s.rb_str_modify_expand("frozen".freeze, 10) }.should raise_error(FrozenError) + -> { @s.rb_str_modify_expand("frozen".freeze, 10) }.should.raise(FrozenError) end end @@ -760,14 +823,14 @@ describe "C-API String function" do it "freezes the string" do s = "" @s.rb_str_freeze(s).should == s - s.frozen?.should be_true + s.frozen?.should == true end end describe "rb_str_hash" do it "hashes the string into a number" do s = "hello" - @s.rb_str_hash(s).should be_kind_of(Integer) + @s.rb_str_hash(s).should.is_a?(Integer) end end @@ -783,7 +846,7 @@ describe "rb_str_free" do # is available. There is no guarantee this even does # anything at all it "indicates data for a string might be freed" do - @s.rb_str_free("xyz").should be_nil + @s.rb_str_free("xyz").should == nil end end @@ -825,12 +888,12 @@ describe "C-API String function" do describe "rb_str_equal" do it "compares two same strings" do s = "hello" - @s.rb_str_equal(s, "hello").should be_true + @s.rb_str_equal(s, "hello").should == true end it "compares two different strings" do s = "hello" - @s.rb_str_equal(s, "hella").should be_false + @s.rb_str_equal(s, "hella").should == false end end @@ -865,7 +928,7 @@ describe "C-API String function" do # - s.should == "\xA4\xA2\xA4\xEC".dup.force_encoding("euc-jp") # + x = [0xA4, 0xA2, 0xA4, 0xEC].pack('C4')#.force_encoding('binary') # + s.should == x -# s.encoding.should equal(Encoding::EUC_JP) +# s.encoding.should.equal?(Encoding::EUC_JP) # end it "transcodes a String to Encoding.default_internal if it is set" do @@ -875,7 +938,7 @@ describe "C-API String function" do s = @s.rb_external_str_new_with_enc(a, a.bytesize, Encoding::UTF_8) x = [0xA4, 0xA2, 0xA4, 0xEC].pack('C4').force_encoding('euc-jp') s.should == x - s.encoding.should equal(Encoding::EUC_JP) + s.encoding.should.equal?(Encoding::EUC_JP) end end @@ -883,7 +946,7 @@ describe "C-API String function" do it "returns a String with 'locale' encoding" do s = @s.rb_locale_str_new("abc", 3) s.should == "abc".dup.force_encoding(Encoding.find("locale")) - s.encoding.should equal(Encoding.find("locale")) + s.encoding.should.equal?(Encoding.find("locale")) end end @@ -891,14 +954,14 @@ describe "C-API String function" do it "returns a String with 'locale' encoding" do s = @s.rb_locale_str_new_cstr("abc") s.should == "abc".dup.force_encoding(Encoding.find("locale")) - s.encoding.should equal(Encoding.find("locale")) + s.encoding.should.equal?(Encoding.find("locale")) end end describe "rb_str_conv_enc" do it "returns the original String when to encoding is not specified" do a = "abc".dup.force_encoding("us-ascii") - @s.rb_str_conv_enc(a, Encoding::US_ASCII, nil).should equal(a) + @s.rb_str_conv_enc(a, Encoding::US_ASCII, nil).should.equal?(a) end it "returns the original String if a transcoding error occurs" do @@ -921,17 +984,17 @@ describe "C-API String function" do describe "when the String encoding is equal to the destination encoding" do it "returns the original String" do a = "abc".dup.force_encoding("us-ascii") - @s.rb_str_conv_enc(a, Encoding::US_ASCII, Encoding::US_ASCII).should equal(a) + @s.rb_str_conv_enc(a, Encoding::US_ASCII, Encoding::US_ASCII).should.equal?(a) end it "returns the original String if the destination encoding is ASCII compatible and the String has no high bits set" do a = "abc".encode("us-ascii") - @s.rb_str_conv_enc(a, Encoding::UTF_8, Encoding::US_ASCII).should equal(a) + @s.rb_str_conv_enc(a, Encoding::UTF_8, Encoding::US_ASCII).should.equal?(a) end it "returns the origin String if the destination encoding is BINARY" do a = "abc".dup.force_encoding("binary") - @s.rb_str_conv_enc(a, Encoding::US_ASCII, Encoding::BINARY).should equal(a) + @s.rb_str_conv_enc(a, Encoding::US_ASCII, Encoding::BINARY).should.equal?(a) end end end @@ -939,13 +1002,13 @@ describe "C-API String function" do describe "rb_str_conv_enc_opts" do it "returns the original String when to encoding is not specified" do a = "abc".dup.force_encoding("us-ascii") - @s.rb_str_conv_enc_opts(a, Encoding::US_ASCII, nil, 0, nil).should equal(a) + @s.rb_str_conv_enc_opts(a, Encoding::US_ASCII, nil, 0, nil).should.equal?(a) end it "returns the original String if a transcoding error occurs" do a = [0xEE].pack('C').force_encoding("utf-8") @s.rb_str_conv_enc_opts(a, Encoding::UTF_8, - Encoding::EUC_JP, 0, nil).should equal(a) + Encoding::EUC_JP, 0, nil).should.equal?(a) end it "returns a transcoded String" do @@ -953,26 +1016,26 @@ describe "C-API String function" do result = @s.rb_str_conv_enc_opts(a, Encoding::UTF_8, Encoding::EUC_JP, 0, nil) x = [0xA4, 0xA2, 0xA4, 0xEC].pack('C4').force_encoding('utf-8') result.should == x.force_encoding("euc-jp") - result.encoding.should equal(Encoding::EUC_JP) + result.encoding.should.equal?(Encoding::EUC_JP) end describe "when the String encoding is equal to the destination encoding" do it "returns the original String" do a = "abc".dup.force_encoding("us-ascii") @s.rb_str_conv_enc_opts(a, Encoding::US_ASCII, - Encoding::US_ASCII, 0, nil).should equal(a) + Encoding::US_ASCII, 0, nil).should.equal?(a) end it "returns the original String if the destination encoding is ASCII compatible and the String has no high bits set" do a = "abc".encode("us-ascii") @s.rb_str_conv_enc_opts(a, Encoding::UTF_8, - Encoding::US_ASCII, 0, nil).should equal(a) + Encoding::US_ASCII, 0, nil).should.equal?(a) end it "returns the origin String if the destination encoding is BINARY" do a = "abc".dup.force_encoding("binary") @s.rb_str_conv_enc_opts(a, Encoding::US_ASCII, - Encoding::BINARY, 0, nil).should equal(a) + Encoding::BINARY, 0, nil).should.equal?(a) end end end @@ -981,7 +1044,7 @@ describe "C-API String function" do it "returns the original String with the external encoding" do Encoding.default_external = Encoding::ISO_8859_1 s = @s.rb_str_export("Hëllo") - s.encoding.should equal(Encoding::ISO_8859_1) + s.encoding.should.equal?(Encoding::ISO_8859_1) end end @@ -989,7 +1052,7 @@ describe "C-API String function" do it "returns the original String with the locale encoding" do s = @s.rb_str_export_locale("abc") s.should == "abc".dup.force_encoding(Encoding.find("locale")) - s.encoding.should equal(Encoding.find("locale")) + s.encoding.should.equal?(Encoding.find("locale")) end end @@ -1004,7 +1067,7 @@ describe "C-API String function" do 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) + result.should.equal?(source) end it "does not alter the source string if it can not be converted" do @@ -1012,7 +1075,7 @@ describe "C-API String function" do result = @s.rb_str_export_to_enc(source, Encoding::UTF_8) source.bytes.should == [0, 255] end -end + end describe "rb_sprintf" do it "replaces the parts like sprintf" do @@ -1040,11 +1103,19 @@ end @s.rb_sprintf3(true.class).should == s end - ruby_bug "#19167", ""..."3.2" do - it "formats a TrueClass VALUE as 'true' if sign specified in format" do - s = 'Result: TrueClass.' - @s.rb_sprintf4(true.class).should == s - end + it "formats a TrueClass VALUE as 'true' if sign specified in format" do + s = 'Result: TrueClass.' + @s.rb_sprintf4(true.class).should == s + end + + it "formats nil using to_s if sign not specified in format" do + s = 'Result: .' + @s.rb_sprintf3(nil).should == s + end + + it "formats nil using inspect if sign specified in format" do + s = 'Result: nil.' + @s.rb_sprintf4(nil).should == s end it "truncates a string to a supplied precision if that is shorter than the string" do @@ -1110,7 +1181,7 @@ end end it "raises a TypeError if #to_str does not return a string" do - -> { @s.rb_String(CApiStringSpecs::InvalidTostrTest.new) }.should raise_error(TypeError) + -> { @s.rb_String(CApiStringSpecs::InvalidTostrTest.new) }.should.raise(TypeError) end it "tries to convert the passed argument to a string by calling #to_s" do @@ -1128,11 +1199,11 @@ end end it "raises an error if a string contains a null" do - -> { @s.rb_string_value_cstr("Hello\0 with a null.") }.should raise_error(ArgumentError) + -> { @s.rb_string_value_cstr("Hello\0 with a null.") }.should.raise(ArgumentError) end it "raises an error if a UTF-16 string contains a null" do - -> { @s.rb_string_value_cstr("Hello\0 with a null.".encode('UTF-16BE')) }.should raise_error(ArgumentError) + -> { @s.rb_string_value_cstr("Hello\0 with a null.".encode('UTF-16BE')) }.should.raise(ArgumentError) end end @@ -1159,9 +1230,11 @@ end describe "rb_utf8_str_new_static" do it "returns a UTF-8 string of the correct characters and length" do - str = @s.rb_utf8_str_new_static + str, ptr = @s.rb_utf8_str_new_static str.should == "nokogiri" str.encoding.should == Encoding::UTF_8 + + @s.RSTRING_PTR(str).should == ptr end end @@ -1203,28 +1276,50 @@ end describe "rb_str_locktmp" do it "raises an error when trying to lock an already locked string" do - str = "test" + str = +"test" @s.rb_str_locktmp(str).should == str - -> { @s.rb_str_locktmp(str) }.should raise_error(RuntimeError, 'temporal locking already locked string') + -> { @s.rb_str_locktmp(str) }.should.raise(RuntimeError, 'temporal locking already locked string') end it "locks a string so that modifications would raise an error" do - str = "test" + str = +"test" @s.rb_str_locktmp(str).should == str - -> { str.upcase! }.should raise_error(RuntimeError, 'can\'t modify string; temporarily locked') + -> { str.upcase! }.should.raise(RuntimeError, 'can\'t modify string; temporarily locked') + end + + ruby_version_is "4.0" do + it "raises FrozenError if string is frozen" do + str = -"rb_str_locktmp" + -> { @s.rb_str_locktmp(str) }.should.raise(FrozenError) + + str = +"rb_str_locktmp" + str.freeze + -> { @s.rb_str_locktmp(str) }.should.raise(FrozenError) + end end end describe "rb_str_unlocktmp" do it "unlocks a locked string" do - str = "test" + str = +"test" @s.rb_str_locktmp(str) @s.rb_str_unlocktmp(str).should == str str.upcase!.should == "TEST" end it "raises an error when trying to unlock an already unlocked string" do - -> { @s.rb_str_unlocktmp("test") }.should raise_error(RuntimeError, 'temporal unlocking already unlocked string') + -> { @s.rb_str_unlocktmp(+"test") }.should.raise(RuntimeError, 'temporal unlocking already unlocked string') + end + + ruby_version_is "4.0" do + it "raises FrozenError if string is frozen" do + str = -"rb_str_locktmp" + -> { @s.rb_str_unlocktmp(str) }.should.raise(FrozenError) + + str = +"rb_str_locktmp" + str.freeze + -> { @s.rb_str_unlocktmp(str) }.should.raise(FrozenError) + end end end @@ -1331,8 +1426,133 @@ end result1.should_not.equal?(result2) end + it "preserves the encoding of the original string" do + result1 = @s.rb_str_to_interned_str("hello".dup.force_encoding(Encoding::US_ASCII)) + result2 = @s.rb_str_to_interned_str("hello".dup.force_encoding(Encoding::UTF_8)) + result1.encoding.should == Encoding::US_ASCII + result2.encoding.should == Encoding::UTF_8 + end + it "returns the same string as String#-@" do @s.rb_str_to_interned_str("hello").should.equal?(-"hello") end end + + describe "rb_interned_str" do + it "returns a frozen string" do + str = "hello" + result = @s.rb_interned_str(str, str.bytesize) + result.should.is_a?(String) + result.should.frozen? + result.encoding.should == Encoding::US_ASCII + end + + it "returns the same frozen string" do + str = "hello" + result1 = @s.rb_interned_str(str, str.bytesize) + result2 = @s.rb_interned_str(str, str.bytesize) + result1.should.equal?(result2) + end + + it "supports strings with embedded null bytes" do + str = "foo\x00bar\x00baz".b + result = @s.rb_interned_str(str, str.bytesize) + result.should == str + end + + it "return US_ASCII encoding for an empty string" do + result = @s.rb_interned_str("", 0) + result.should == "" + result.encoding.should == Encoding::US_ASCII + end + + it "returns US_ASCII encoding for strings of only 7 bit ASCII" do + 0x00.upto(0x7f).each do |char| + result = @s.rb_interned_str(char.chr, 1) + result.encoding.should == Encoding::US_ASCII + end + end + + ruby_bug "21842", ""..."4.1" do + it "returns BINARY encoding for strings that use the 8th bit" do + 0x80.upto(0xff) do |char| + result = @s.rb_interned_str(char.chr, 1) + result.encoding.should == Encoding::BINARY + end + end + end + + it 'returns the same string when using non-ascii characters' do + str = 'こんにちは' + result1 = @s.rb_interned_str(str, str.bytesize) + result2 = @s.rb_interned_str(str, str.bytesize) + result1.should.equal?(result2) + end + + ruby_bug "21842", ""..."4.1" do + it "returns the same string as String#-@" do + str = "hello".dup.force_encoding(Encoding::US_ASCII) + @s.rb_interned_str(str, str.bytesize).should.equal?(-str) + end + end + end + + describe "rb_interned_str_cstr" do + it "returns a frozen string" do + str = "hello" + result = @s.rb_interned_str_cstr(str) + result.should.is_a?(String) + result.should.frozen? + result.encoding.should == Encoding::US_ASCII + end + + it "returns the same frozen string" do + str = "hello" + result1 = @s.rb_interned_str_cstr(str) + result2 = @s.rb_interned_str_cstr(str) + result1.should.equal?(result2) + end + + it "does not support strings with embedded null bytes" do + str = "foo\x00bar\x00baz".b + result = @s.rb_interned_str_cstr(str) + result.should == "foo" + end + + it "return US_ASCII encoding for an empty string" do + result = @s.rb_interned_str_cstr("") + result.should == "" + result.encoding.should == Encoding::US_ASCII + end + + it "returns US_ASCII encoding for strings of only 7 bit ASCII" do + 0x01.upto(0x7f).each do |char| + result = @s.rb_interned_str_cstr(char.chr) + result.encoding.should == Encoding::US_ASCII + end + end + + ruby_bug "21842", ""..."4.1" do + it "returns BINARY encoding for strings that use the 8th bit" do + 0x80.upto(0xff) do |char| + result = @s.rb_interned_str_cstr(char.chr) + result.encoding.should == Encoding::BINARY + end + end + end + + it 'returns the same string when using non-ascii characters' do + str = 'こんにちは' + result1 = @s.rb_interned_str_cstr(str) + result2 = @s.rb_interned_str_cstr(str) + result1.should.equal?(result2) + end + + ruby_bug "21842", ""..."4.1" do + it "returns the same string as String#-@" do + str = "hello".dup.force_encoding(Encoding::US_ASCII) + @s.rb_interned_str_cstr(str).should.equal?(-str) + end + end + end end diff --git a/spec/ruby/optional/capi/struct_spec.rb b/spec/ruby/optional/capi/struct_spec.rb index 474c397956..843387bc19 100644 --- a/spec/ruby/optional/capi/struct_spec.rb +++ b/spec/ruby/optional/capi/struct_spec.rb @@ -25,11 +25,11 @@ describe "C-API Struct function" do it "has a value of nil for the member of a newly created instance" do # Verify that attributes are on an instance basis - Struct::CAPIStruct.new.b.should be_nil + Struct::CAPIStruct.new.b.should == nil end it "creates a constant scoped under Struct for the named Struct" do - Struct.should have_constant(:CAPIStruct) + Struct.should.const_defined?(:CAPIStruct, false) end it "returns the member names as Symbols" do @@ -80,15 +80,15 @@ describe "C-API Struct function" do it "has a value of nil for the member of a newly created instance" do # Verify that attributes are on an instance basis - CApiStructSpecs::CAPIStructUnder.new.b.should be_nil + CApiStructSpecs::CAPIStructUnder.new.b.should == nil end it "does not create a constant scoped under Struct for the named Struct" do - Struct.should_not have_constant(:CAPIStructUnder) + Struct.should_not.const_defined?(:CAPIStructUnder) end it "creates a constant scoped under the namespace of the given class" do - CApiStructSpecs.should have_constant(:CAPIStructUnder) + CApiStructSpecs.should.const_defined?(:CAPIStructUnder, false) end it "returns the member names as Symbols" do @@ -106,11 +106,11 @@ describe "C-API Struct function" do describe "rb_struct_define" do it "raises an ArgumentError if arguments contain duplicate member name" do - -> { @s.rb_struct_define(nil, "a", "b", "a") }.should raise_error(ArgumentError) + -> { @s.rb_struct_define(nil, "a", "b", "a") }.should.raise(ArgumentError) end it "raises a NameError if an invalid constant name is given" do - -> { @s.rb_struct_define("foo", "a", "b", "c") }.should raise_error(NameError) + -> { @s.rb_struct_define("foo", "a", "b", "c") }.should.raise(NameError) end end @@ -131,12 +131,12 @@ describe "C-API Struct function" do end it "raises a NameError if the struct member does not exist" do - -> { @s.rb_struct_aref(@struct, :d) }.should raise_error(NameError) + -> { @s.rb_struct_aref(@struct, :d) }.should.raise(NameError) end it "raises an IndexError if the given index is out of range" do - -> { @s.rb_struct_aref(@struct, -4) }.should raise_error(IndexError) - -> { @s.rb_struct_aref(@struct, 3) }.should raise_error(IndexError) + -> { @s.rb_struct_aref(@struct, -4) }.should.raise(IndexError) + -> { @s.rb_struct_aref(@struct, 3) }.should.raise(IndexError) end end @@ -147,7 +147,7 @@ describe "C-API Struct function" do end it "raises a NameError if the struct member does not exist" do - -> { @s.rb_struct_getmember(@struct, :d) }.should raise_error(NameError) + -> { @s.rb_struct_getmember(@struct, :d) }.should.raise(NameError) end end @@ -180,17 +180,17 @@ describe "C-API Struct function" do end it "raises a NameError if the struct member does not exist" do - -> { @s.rb_struct_aset(@struct, :d, 1) }.should raise_error(NameError) + -> { @s.rb_struct_aset(@struct, :d, 1) }.should.raise(NameError) end it "raises an IndexError if the given index is out of range" do - -> { @s.rb_struct_aset(@struct, -4, 1) }.should raise_error(IndexError) - -> { @s.rb_struct_aset(@struct, 3, 1) }.should raise_error(IndexError) + -> { @s.rb_struct_aset(@struct, -4, 1) }.should.raise(IndexError) + -> { @s.rb_struct_aset(@struct, 3, 1) }.should.raise(IndexError) end it "raises a FrozenError if the struct is frozen" do @struct.freeze - -> { @s.rb_struct_aset(@struct, :a, 1) }.should raise_error(FrozenError) + -> { @s.rb_struct_aset(@struct, :a, 1) }.should.raise(FrozenError) end end @@ -208,53 +208,107 @@ describe "C-API Struct function" do @s.rb_struct_size(@struct).should == 3 end end + + describe "rb_struct_initialize" do + it "sets all members" do + @s.rb_struct_initialize(@struct, [1, 2, 3]).should == nil + @struct.a.should == 1 + @struct.b.should == 2 + @struct.c.should == 3 + end + + it "does not freeze the Struct instance" do + @s.rb_struct_initialize(@struct, [1, 2, 3]).should == nil + @struct.should_not.frozen? + @s.rb_struct_initialize(@struct, [4, 5, 6]).should == nil + @struct.a.should == 4 + @struct.b.should == 5 + @struct.c.should == 6 + end + + it "raises ArgumentError if too many values" do + -> { @s.rb_struct_initialize(@struct, [1, 2, 3, 4]) }.should.raise(ArgumentError, "struct size differs") + end + + it "treats missing values as nil" do + @s.rb_struct_initialize(@struct, [1, 2]).should == nil + @struct.a.should == 1 + @struct.b.should == 2 + @struct.c.should == nil + end + end end -ruby_version_is "3.3" do - describe "C-API Data function" do - before :each do - @s = CApiStructSpecs.new +describe "C-API Data function" do + before :all do + @s = CApiStructSpecs.new + @klass = @s.rb_data_define(nil, "a", "b", "c") + end + + describe "rb_data_define" do + it "returns a subclass of Data class when passed nil as the first argument" do + @klass.should.is_a? Class + @klass.superclass.should == Data end - describe "rb_data_define" do - it "returns a subclass of Data class when passed nil as the first argument" do - klass = @s.rb_data_define(nil, "a", "b", "c") + it "returns a subclass of a class when passed as the first argument" do + superclass = Class.new(Data) + klass = @s.rb_data_define(superclass, "a", "b", "c") - klass.should.is_a? Class - klass.superclass.should == Data - end + klass.should.is_a? Class + klass.superclass.should == superclass + end - it "returns a subclass of a class when passed as the first argument" do - superclass = Class.new(Data) - klass = @s.rb_data_define(superclass, "a", "b", "c") + it "creates readers for the members" do + obj = @klass.new(1, 2, 3) - klass.should.is_a? Class - klass.superclass.should == superclass - end + obj.a.should == 1 + obj.b.should == 2 + obj.c.should == 3 + end - it "creates readers for the members" do - klass = @s.rb_data_define(nil, "a", "b", "c") - obj = klass.new(1, 2, 3) + it "returns the member names as Symbols" do + obj = @klass.new(0, 0, 0) - obj.a.should == 1 - obj.b.should == 2 - obj.c.should == 3 - end + obj.members.should == [:a, :b, :c] + end - it "returns the member names as Symbols" do - klass = @s.rb_data_define(nil, "a", "b", "c") - obj = klass.new(0, 0, 0) + it "raises an ArgumentError if arguments contain duplicate member name" do + -> { @s.rb_data_define(nil, "a", "b", "a") }.should.raise(ArgumentError) + end - obj.members.should == [:a, :b, :c] - end + it "raises when first argument is not a class" do + -> { @s.rb_data_define([], "a", "b", "c") }.should.raise(TypeError, "wrong argument type Array (expected Class)") + end + end + + describe "rb_struct_initialize" do + it "sets all members for a Data instance" do + data = @klass.allocate + @s.rb_struct_initialize(data, [1, 2, 3]).should == nil + data.a.should == 1 + data.b.should == 2 + data.c.should == 3 + end - it "raises an ArgumentError if arguments contain duplicate member name" do - -> { @s.rb_data_define(nil, "a", "b", "a") }.should raise_error(ArgumentError) - end + it "freezes the Data instance" do + data = @klass.allocate + @s.rb_struct_initialize(data, [1, 2, 3]).should == nil + data.should.frozen? + -> { @s.rb_struct_initialize(data, [1, 2, 3]) }.should.raise(FrozenError) + end + + it "raises ArgumentError if too many values" do + data = @klass.allocate + -> { @s.rb_struct_initialize(data, [1, 2, 3, 4]) }.should.raise(ArgumentError, "struct size differs") + end - it "raises when first argument is not a class" do - -> { @s.rb_data_define([], "a", "b", "c") }.should raise_error(TypeError, "wrong argument type Array (expected Class)") - end + it "treats missing values as nil" do + data = @klass.allocate + @s.rb_struct_initialize(data, [1, 2]).should == nil + data.a.should == 1 + data.b.should == 2 + data.c.should == nil end end end diff --git a/spec/ruby/optional/capi/thread_spec.rb b/spec/ruby/optional/capi/thread_spec.rb index af641f0564..75e0b94fdf 100644 --- a/spec/ruby/optional/capi/thread_spec.rb +++ b/spec/ruby/optional/capi/thread_spec.rb @@ -50,7 +50,7 @@ describe "C-API Thread function" do end it "returns nil if the value has not been set" do - @t.rb_thread_local_aref(Thread.current, :thread_capi_specs_undefined).should be_nil + @t.rb_thread_local_aref(Thread.current, :thread_capi_specs_undefined).should == nil end end @@ -72,7 +72,7 @@ describe "C-API Thread function" do obj = Object.new proc = -> x { ScratchPad.record x } thr = @t.rb_thread_create(proc, obj) - thr.should be_kind_of(Thread) + thr.should.is_a?(Thread) thr.join ScratchPad.recorded.should == obj end @@ -83,18 +83,18 @@ describe "C-API Thread function" do raise "my error" } thr = @t.rb_thread_create(prc, nil) - thr.should be_kind_of(Thread) + thr.should.is_a?(Thread) -> { thr.join - }.should raise_error(RuntimeError, "my error") + }.should.raise(RuntimeError, "my error") end it "sets the thread's group" do thr = @t.rb_thread_create(-> x { }, nil) begin thread_group = thr.group - thread_group.should be_an_instance_of(ThreadGroup) + thread_group.should.instance_of?(ThreadGroup) ensure thr.join end @@ -103,11 +103,11 @@ describe "C-API Thread function" do describe "ruby_native_thread_p" do it "returns non-zero for a ruby thread" do - @t.ruby_native_thread_p.should be_true + @t.ruby_native_thread_p.should == true end it "returns zero for a non ruby thread" do - @t.ruby_native_thread_p_new_thread.should be_false + @t.ruby_native_thread_p_new_thread.should == false end end @@ -128,7 +128,7 @@ describe "C-API Thread function" do thr.wakeup # Make sure it stopped and we got a proper value - thr.value.should be_true + thr.value.should == true end platform_is_not :windows do @@ -159,7 +159,7 @@ describe "C-API Thread function" do going_to_block = true # Make sure it stopped and we got a proper value - @t.rb_thread_call_without_gvl.should be_true + @t.rb_thread_call_without_gvl.should == true interrupter.join end @@ -181,7 +181,15 @@ describe "C-API Thread function" do thr.wakeup # Make sure it stopped and we got a proper value - thr.value.should be_true + thr.value.should == true + end + end + + ruby_version_is "4.0" do + describe "ruby_thread_has_gvl_p" do + it "returns true if the current thread has the GVL" do + @t.ruby_thread_has_gvl_p.should == true + end end end end diff --git a/spec/ruby/optional/capi/time_spec.rb b/spec/ruby/optional/capi/time_spec.rb index ca5fc5952a..dc0b09376e 100644 --- a/spec/ruby/optional/capi/time_spec.rb +++ b/spec/ruby/optional/capi/time_spec.rb @@ -16,7 +16,7 @@ describe "CApiTimeSpecs" do describe "TIMET2NUM" do it "returns an Integer" do - @s.TIMET2NUM.should be_kind_of(Integer) + @s.TIMET2NUM.should.is_a?(Integer) end end @@ -32,7 +32,7 @@ describe "CApiTimeSpecs" do it "creates a Time in the local zone with only a timestamp" do with_timezone("Europe/Amsterdam") do time = @s.rb_time_num_new(1232141421, nil) - time.should be_an_instance_of(Time) + time.should.instance_of?(Time) time.to_i.should == 1232141421 platform_is_not :windows do time.gmt_offset.should == 3600 @@ -43,7 +43,7 @@ describe "CApiTimeSpecs" do it "creates a Time with the given offset" do with_timezone("Europe/Amsterdam") do time = @s.rb_time_num_new(1232141421, 7200) - time.should be_an_instance_of(Time) + time.should.instance_of?(Time) time.to_i.should == 1232141421 time.gmt_offset.should == 7200 end @@ -52,7 +52,7 @@ describe "CApiTimeSpecs" do it "creates a Time with a Float timestamp" do with_timezone("Europe/Amsterdam") do time = @s.rb_time_num_new(1.5, 7200) - time.should be_an_instance_of(Time) + time.should.instance_of?(Time) time.to_i.should == 1 time.nsec.should == 500000000 time.gmt_offset.should == 7200 @@ -62,7 +62,7 @@ describe "CApiTimeSpecs" do it "creates a Time with a Rational timestamp" do with_timezone("Europe/Amsterdam") do time = @s.rb_time_num_new(Rational(3, 2), 7200) - time.should be_an_instance_of(Time) + time.should.instance_of?(Time) time.to_i.should == 1 time.nsec.should == 500000000 time.gmt_offset.should == 7200 @@ -73,32 +73,32 @@ describe "CApiTimeSpecs" do describe "rb_time_interval" do it "creates a timeval interval for a Fixnum" do sec, usec = @s.rb_time_interval(1232141421) - sec.should be_kind_of(Integer) + sec.should.is_a?(Integer) sec.should == 1232141421 - usec.should be_kind_of(Integer) + usec.should.is_a?(Integer) usec.should == 0 end it "creates a timeval interval for a Float" do sec, usec = @s.rb_time_interval(1.5) - sec.should be_kind_of(Integer) + sec.should.is_a?(Integer) sec.should == 1 - usec.should be_kind_of(Integer) + usec.should.is_a?(Integer) usec.should == 500000 end it "creates a timeval interval for a Rational" do sec, usec = @s.rb_time_interval(Rational(3, 2)) - sec.should be_kind_of(Integer) + sec.should.is_a?(Integer) sec.should == 1 - usec.should be_kind_of(Integer) + usec.should.is_a?(Integer) usec.should == 500000 end it "throws an argument error for a negative value" do - -> { @s.rb_time_interval(-1232141421) }.should raise_error(ArgumentError) - -> { @s.rb_time_interval(Rational(-3, 2)) }.should raise_error(ArgumentError) - -> { @s.rb_time_interval(-1.5) }.should raise_error(ArgumentError) + -> { @s.rb_time_interval(-1232141421) }.should.raise(ArgumentError) + -> { @s.rb_time_interval(Rational(-3, 2)) }.should.raise(ArgumentError) + -> { @s.rb_time_interval(-1.5) }.should.raise(ArgumentError) end end @@ -106,36 +106,36 @@ describe "CApiTimeSpecs" do describe "rb_time_interval" do it "creates a timeval interval for a Fixnum" do sec, usec = @s.rb_time_interval(1232141421) - sec.should be_kind_of(Integer) + sec.should.is_a?(Integer) sec.should == 1232141421 - usec.should be_kind_of(Integer) + usec.should.is_a?(Integer) usec.should == 0 end it "creates a timeval interval for a Float" do sec, usec = @s.rb_time_interval(1.5) - sec.should be_kind_of(Integer) + sec.should.is_a?(Integer) sec.should == 1 - usec.should be_kind_of(Integer) + usec.should.is_a?(Integer) usec.should == 500000 end it "creates a timeval interval for a Rational" do sec, usec = @s.rb_time_interval(Rational(3, 2)) - sec.should be_kind_of(Integer) + sec.should.is_a?(Integer) sec.should == 1 - usec.should be_kind_of(Integer) + usec.should.is_a?(Integer) usec.should == 500000 end it "throws an argument error for a negative value" do - -> { @s.rb_time_interval(-1232141421) }.should raise_error(ArgumentError) - -> { @s.rb_time_interval(Rational(-3, 2)) }.should raise_error(ArgumentError) - -> { @s.rb_time_interval(-1.5) }.should raise_error(ArgumentError) + -> { @s.rb_time_interval(-1232141421) }.should.raise(ArgumentError) + -> { @s.rb_time_interval(Rational(-3, 2)) }.should.raise(ArgumentError) + -> { @s.rb_time_interval(-1.5) }.should.raise(ArgumentError) end it "throws an argument error when given a Time instance" do - -> { @s.rb_time_interval(Time.now) }.should raise_error(TypeError) + -> { @s.rb_time_interval(Time.now) }.should.raise(TypeError) end end @@ -143,49 +143,49 @@ describe "CApiTimeSpecs" do describe "rb_time_timeval" do it "creates a timeval for a Fixnum" do sec, usec = @s.rb_time_timeval(1232141421) - sec.should be_kind_of(Integer) + sec.should.is_a?(Integer) sec.should == 1232141421 - usec.should be_kind_of(Integer) + usec.should.is_a?(Integer) usec.should == 0 end it "creates a timeval for a Float" do sec, usec = @s.rb_time_timeval(1.5) - sec.should be_kind_of(Integer) + sec.should.is_a?(Integer) sec.should == 1 - usec.should be_kind_of(Integer) + usec.should.is_a?(Integer) usec.should == 500000 end it "creates a timeval for a Rational" do sec, usec = @s.rb_time_timeval(Rational(3, 2)) - sec.should be_kind_of(Integer) + sec.should.is_a?(Integer) sec.should == 1 - usec.should be_kind_of(Integer) + usec.should.is_a?(Integer) usec.should == 500000 end it "creates a timeval for a negative Fixnum" do sec, usec = @s.rb_time_timeval(-1232141421) - sec.should be_kind_of(Integer) + sec.should.is_a?(Integer) sec.should == -1232141421 - usec.should be_kind_of(Integer) + usec.should.is_a?(Integer) usec.should == 0 end it "creates a timeval for a negative Float" do sec, usec = @s.rb_time_timeval(-1.5) - sec.should be_kind_of(Integer) + sec.should.is_a?(Integer) sec.should == -2 - usec.should be_kind_of(Integer) + usec.should.is_a?(Integer) usec.should == 500000 end it "creates a timeval for a negative Rational" do sec, usec = @s.rb_time_timeval(Rational(-3, 2)) - sec.should be_kind_of(Integer) + sec.should.is_a?(Integer) sec.should == -2 - usec.should be_kind_of(Integer) + usec.should.is_a?(Integer) usec.should == 500000 end @@ -200,49 +200,49 @@ describe "CApiTimeSpecs" do describe "rb_time_timespec" do it "creates a timespec for a Fixnum" do sec, nsec = @s.rb_time_timespec(1232141421) - sec.should be_kind_of(Integer) + sec.should.is_a?(Integer) sec.should == 1232141421 - nsec.should be_kind_of(Integer) + nsec.should.is_a?(Integer) nsec.should == 0 end it "creates a timespec for a Float" do sec, nsec = @s.rb_time_timespec(1.5) - sec.should be_kind_of(Integer) + sec.should.is_a?(Integer) sec.should == 1 - nsec.should be_kind_of(Integer) + nsec.should.is_a?(Integer) nsec.should == 500000000 end it "creates a timespec for a Rational" do sec, nsec = @s.rb_time_timespec(Rational(3, 2)) - sec.should be_kind_of(Integer) + sec.should.is_a?(Integer) sec.should == 1 - nsec.should be_kind_of(Integer) + nsec.should.is_a?(Integer) nsec.should == 500000000 end it "creates a timespec for a negative Fixnum" do sec, nsec = @s.rb_time_timespec(-1232141421) - sec.should be_kind_of(Integer) + sec.should.is_a?(Integer) sec.should == -1232141421 - nsec.should be_kind_of(Integer) + nsec.should.is_a?(Integer) nsec.should == 0 end it "creates a timespec for a negative Float" do sec, nsec = @s.rb_time_timespec(-1.5) - sec.should be_kind_of(Integer) + sec.should.is_a?(Integer) sec.should == -2 - nsec.should be_kind_of(Integer) + nsec.should.is_a?(Integer) nsec.should == 500000000 end it "creates a timespec for a negative Rational" do sec, nsec = @s.rb_time_timespec(Rational(-3, 2)) - sec.should be_kind_of(Integer) + sec.should.is_a?(Integer) sec.should == -2 - nsec.should be_kind_of(Integer) + nsec.should.is_a?(Integer) nsec.should == 500000000 end @@ -261,7 +261,7 @@ describe "CApiTimeSpecs" do describe "when offset given is within range of -86400 and 86400 (exclusive)" do it "sets time's is_gmt to false" do - @s.rb_time_timespec_new(1447087832, 476451125, 0).gmt?.should be_false + @s.rb_time_timespec_new(1447087832, 476451125, 0).gmt?.should == false end it "sets time's offset to the offset given" do @@ -270,7 +270,7 @@ describe "CApiTimeSpecs" do end it "returns time object in UTC if offset given equals INT_MAX - 1" do - @s.rb_time_timespec_new(1447087832, 476451125, 0x7ffffffe).utc?.should be_true + @s.rb_time_timespec_new(1447087832, 476451125, 0x7ffffffe).utc?.should == true end it "returns time object in localtime if offset given equals INT_MAX" do @@ -280,13 +280,13 @@ describe "CApiTimeSpecs" do end it "raises an ArgumentError if offset passed is not within range of -86400 and 86400 (exclusive)" do - -> { @s.rb_time_timespec_new(1447087832, 476451125, 86400) }.should raise_error(ArgumentError) - -> { @s.rb_time_timespec_new(1447087832, 476451125, -86400) }.should raise_error(ArgumentError) + -> { @s.rb_time_timespec_new(1447087832, 476451125, 86400) }.should.raise(ArgumentError) + -> { @s.rb_time_timespec_new(1447087832, 476451125, -86400) }.should.raise(ArgumentError) end it "doesn't call Time.at directly" do Time.should_not_receive(:at) - @s.rb_time_timespec_new(1447087832, 476451125, 32400).should be_kind_of(Time) + @s.rb_time_timespec_new(1447087832, 476451125, 32400).should.is_a?(Time) end end @@ -294,7 +294,7 @@ describe "CApiTimeSpecs" do it "fills a struct timespec with the current time" do now = Time.now time = @s.rb_time_from_timespec(now.utc_offset) - time.should be_an_instance_of(Time) + time.should.instance_of?(Time) (time - now).should be_close(0, TIME_TOLERANCE) end end diff --git a/spec/ruby/optional/capi/tracepoint_spec.rb b/spec/ruby/optional/capi/tracepoint_spec.rb index 2043b7c941..8775715e04 100644 --- a/spec/ruby/optional/capi/tracepoint_spec.rb +++ b/spec/ruby/optional/capi/tracepoint_spec.rb @@ -14,7 +14,7 @@ describe "CApiTracePointSpecs" do describe "rb_tracepoint_new" do it "returns a tracepoint object" do @trace = @s.rb_tracepoint_new(7) - @trace.should be_an_instance_of(TracePoint) + @trace.should.instance_of?(TracePoint) @trace.should_not.enabled? end diff --git a/spec/ruby/optional/capi/typed_data_spec.rb b/spec/ruby/optional/capi/typed_data_spec.rb index 6d1398a1a0..376cfe417f 100644 --- a/spec/ruby/optional/capi/typed_data_spec.rb +++ b/spec/ruby/optional/capi/typed_data_spec.rb @@ -30,7 +30,7 @@ describe "CApiWrappedTypedStruct" do it "throws an exception for a wrong type" do a = @s.typed_wrap_struct(1024) - -> { @s.typed_get_struct_other(a) }.should raise_error(TypeError) + -> { @s.typed_get_struct_other(a) }.should.raise(TypeError) end it "unwraps data for a parent type" do @@ -63,7 +63,7 @@ describe "CApiWrappedTypedStruct" do -> { a = @s.typed_wrap_struct(1024) @s.rb_check_type(a, a) - }.should raise_error(TypeError) { |e| + }.should.raise(TypeError) { |e| e.message.should == 'wrong argument type Object (expected Data)' } end @@ -82,19 +82,21 @@ describe "CApiWrappedTypedStruct" do it "raises an error for different types" do a = @s.typed_wrap_struct(1024) - -> { @s.rb_check_typeddata_different_type(a) }.should raise_error(TypeError) + -> { @s.rb_check_typeddata_different_type(a) }.should.raise(TypeError) end end - describe "RTYPEDDATA_P" do - it "returns true for a typed data" do - a = @s.typed_wrap_struct(1024) - @s.RTYPEDDATA_P(a).should == true - end + ruby_version_is ""..."4.1" do + describe "RTYPEDDATA_P" do + it "returns true for a typed data" do + a = @s.typed_wrap_struct(1024) + @s.RTYPEDDATA_P(a).should == true + end - it "returns false for an untyped data object" do - a = @s.untyped_wrap_struct(1024) - @s.RTYPEDDATA_P(a).should == false + it "returns false for an untyped data object" do + a = @s.untyped_wrap_struct(1024) + @s.RTYPEDDATA_P(a).should == false + end end end end diff --git a/spec/ruby/optional/capi/util_spec.rb b/spec/ruby/optional/capi/util_spec.rb index 6cf064bf97..dd3cbb6549 100644 --- a/spec/ruby/optional/capi/util_spec.rb +++ b/spec/ruby/optional/capi/util_spec.rb @@ -21,11 +21,11 @@ describe "C-API Util function" do end it "raises an ArgumentError if there are insufficient arguments" do - -> { @o.rb_scan_args([1, 2], "3", 0, @acc) }.should raise_error(ArgumentError) + -> { @o.rb_scan_args([1, 2], "3", 0, @acc) }.should.raise(ArgumentError, "wrong number of arguments (given 2, expected 3)") end it "raises an ArgumentError if there are too many arguments" do - -> { @o.rb_scan_args([1, 2, 3, 4], "3", 0, @acc) }.should raise_error(ArgumentError) + -> { @o.rb_scan_args([1, 2, 3, 4], "3", 0, @acc) }.should.raise(ArgumentError, "wrong number of arguments (given 4, expected 3)") end it "assigns the required and optional arguments scanned" do @@ -117,8 +117,15 @@ describe "C-API Util function" do it "rejects the use of nil as a hash" do -> { - @o.rb_scan_args([1, nil], "1:", 2, @acc).should == 1 - }.should raise_error(ArgumentError) + @o.rb_scan_args([1, nil], "1:", 2, @acc) + }.should.raise(ArgumentError, "wrong number of arguments (given 2, expected 1)") + ScratchPad.recorded.should == [] + end + + it "rejects the use of of a non-Hash as keywords" do + -> { + @o.rb_scan_args([42], ":", 1, @acc) + }.should.raise(ArgumentError, "wrong number of arguments (given 1, expected 0)") ScratchPad.recorded.should == [] end @@ -186,7 +193,7 @@ describe "C-API Util function" do it "raises an error if a required argument is not in the hash" do h = { :a => 7, :c => 12, :b => 5 } - -> { @o.rb_get_kwargs(h, [:b, :d], 2, 0) }.should raise_error(ArgumentError, /missing keyword: :?d/) + -> { @o.rb_get_kwargs(h, [:b, :d], 2, 0) }.should.raise(ArgumentError, "missing keyword: :d") h.should == {:a => 7, :c => 12} end @@ -198,7 +205,7 @@ describe "C-API Util function" do it "raises an error if there are additional arguments and optional is positive" do h = { :a => 7, :c => 12, :b => 5 } - -> { @o.rb_get_kwargs(h, [:b, :a], 2, 0) }.should raise_error(ArgumentError, /unknown keyword: :?c/) + -> { @o.rb_get_kwargs(h, [:b, :a], 2, 0) }.should.raise(ArgumentError, "unknown keyword: :c") h.should == {:c => 12} end @@ -212,7 +219,7 @@ describe "C-API Util function" do platform_is c_long_size: 64 do describe "rb_long2int" do it "raises a RangeError if the value is outside the range of a C int" do - -> { @o.rb_long2int(0xffff_ffff_ffff) }.should raise_error(RangeError) + -> { @o.rb_long2int(0xffff_ffff_ffff) }.should.raise(RangeError) end end @@ -258,7 +265,7 @@ describe "C-API Util function" do describe "rb_sourceline" do it "returns the current ruby file" do - @o.rb_sourceline.should be_kind_of(Integer) + @o.rb_sourceline.should.is_a?(Integer) end end diff --git a/spec/ruby/optional/thread_safety/fixtures/classes.rb b/spec/ruby/optional/thread_safety/fixtures/classes.rb new file mode 100644 index 0000000000..4f0ad030e5 --- /dev/null +++ b/spec/ruby/optional/thread_safety/fixtures/classes.rb @@ -0,0 +1,39 @@ +module ThreadSafetySpecs + # Returns the number of processors, rounded up so it's always a multiple of 2 + def self.processors + require 'etc' + n = Etc.nprocessors + raise "expected at least 1 processor" if n < 1 + n += 1 if n.odd? # ensure it's a multiple of 2 + n + end + + class Counter + def initialize + @value = 0 + @mutex = Mutex.new + end + + def get + @mutex.synchronize { @value } + end + + def increment + @mutex.synchronize do + @value += 1 + end + end + end + + class Barrier + def initialize(parties) + @parties = parties + @counter = Counter.new + end + + def wait + @counter.increment + Thread.pass until @counter.get == @parties + end + end +end diff --git a/spec/ruby/optional/thread_safety/hash_spec.rb b/spec/ruby/optional/thread_safety/hash_spec.rb new file mode 100644 index 0000000000..53127fc973 --- /dev/null +++ b/spec/ruby/optional/thread_safety/hash_spec.rb @@ -0,0 +1,210 @@ +require_relative '../../spec_helper' +require_relative 'fixtures/classes' + +describe "Hash thread safety" do + it "supports concurrent #[]=" do + n_threads = ThreadSafetySpecs.processors + + n = 1_000 + operations = n * n_threads + + h = {} + barrier = ThreadSafetySpecs::Barrier.new(n_threads + 1) + + threads = n_threads.times.map { |t| + Thread.new { + barrier.wait + base = t * n + n.times do |j| + h[base+j] = t + end + } + } + + barrier.wait + threads.each(&:join) + + h.size.should == operations + h.each_pair { |key, value| + (key / n).should == value + } + end + + # can't add a new key into hash during iteration (RuntimeError) on CRuby. + # Yet it's good to test this for implementations that support it. + guard_not -> { PlatformGuard.standard? } do + it "supports concurrent #[]= and #delete and iteration" do + n_threads = ThreadSafetySpecs.processors + + n = 1_000 + operations = n * n_threads / 2 + + h = {} + barrier1 = ThreadSafetySpecs::Barrier.new(n_threads + 2) + barrier2 = ThreadSafetySpecs::Barrier.new(n_threads + 1) + + threads = n_threads.times.map { |t| + Thread.new { + barrier1.wait + base = t * n + n.times do |j| + h[base+j] = t + end + + barrier2.wait + n.times do |j| + # delete only even keys + h.delete(base+j).should == t if (base+j).even? + end + } + } + + read = true + reader = Thread.new { + barrier1.wait + while read + h.each_pair { |k,v| + k.should.is_a?(Integer) + v.should.is_a?(Integer) + (k / n).should == v + } + end + } + + barrier1.wait + barrier2.wait + threads.each(&:join) + read = false + reader.join + + # odd keys are left + h.size.should == operations + h.each_pair { |key, value| + key.should.odd? + (key / n).should == value + } + end + end + + it "supports concurrent #[]= and #[]" do + n_threads = ThreadSafetySpecs.processors + + n = 1_000 + operations = n * n_threads / 2 + + h = {} + barrier = ThreadSafetySpecs::Barrier.new(n_threads + 1) + + threads = n_threads.times.map { |t| + Thread.new { + barrier.wait + base = (t / 2) * n + + if t.even? + n.times do |j| + k = base + j + h[k] = k + end + else + n.times do |j| + k = base + j + Thread.pass until v = h[k] + v.should == k + end + end + } + } + + barrier.wait + threads.each(&:join) + + h.size.should == operations + h.each_pair { |key, value| + key.should == value + } + end + + it "supports concurrent #[]= and #[] with change to #compare_by_identity in the middle" do + n_threads = ThreadSafetySpecs.processors + + n = 1_000 + operations = n * n_threads / 2 + + h = {} + barrier = ThreadSafetySpecs::Barrier.new(n_threads + 1) + + threads = n_threads.times.map { |t| + Thread.new { + barrier.wait + base = (t / 2) * n + + if t.even? + n.times do |j| + k = base + j + h[k] = k + end + else + n.times do |j| + k = base + j + Thread.pass until v = h[k] + v.should == k + end + end + } + } + + changer = Thread.new { + Thread.pass until h.size >= operations / 2 + h.should_not.compare_by_identity? + h.compare_by_identity + h.should.compare_by_identity? + } + + barrier.wait + threads.each(&:join) + changer.join + + h.size.should == operations + h.each_pair { |key, value| + key.should == value + } + end + + it "supports repeated concurrent #[]= and #delete and always returns a #size >= 0" do + n_threads = ThreadSafetySpecs.processors + + n = 1_000 + operations = n * n_threads / 2 + + h = {} + barrier = ThreadSafetySpecs::Barrier.new(n_threads + 1) + deleted = ThreadSafetySpecs::Counter.new + + threads = n_threads.times.map { |t| + Thread.new { + barrier.wait + key = t / 2 + + if t.even? + n.times { + Thread.pass until h.delete(key) + h.size.should >= 0 + deleted.increment + } + else + n.times { + h[key] = key + Thread.pass while h.key?(key) + } + end + } + } + + barrier.wait + threads.each(&:join) + + deleted.get.should == operations + h.size.should == 0 + h.should.empty? + end +end |
