summaryrefslogtreecommitdiff
path: root/spec/ruby/optional/capi/object_spec.rb
diff options
context:
space:
mode:
Diffstat (limited to 'spec/ruby/optional/capi/object_spec.rb')
-rw-r--r--spec/ruby/optional/capi/object_spec.rb63
1 files changed, 57 insertions, 6 deletions
diff --git a/spec/ruby/optional/capi/object_spec.rb b/spec/ruby/optional/capi/object_spec.rb
index 294247910b..7bc7bd992a 100644
--- a/spec/ruby/optional/capi/object_spec.rb
+++ b/spec/ruby/optional/capi/object_spec.rb
@@ -1,4 +1,5 @@
require_relative 'spec_helper'
+require_relative 'fixtures/object'
load_extension("object")
@@ -30,6 +31,7 @@ describe "CApiObject" do
class ObjectTest
def initialize
@foo = 7
+ yield if block_given?
end
def foo
@@ -88,6 +90,15 @@ describe "CApiObject" do
o.initialized.should be_true
o.arguments.should == [:one, :two]
end
+
+ it "passes the block to #initialize" do
+ v = nil
+ o = @o.rb_obj_alloc(ObjectTest)
+ @o.rb_obj_call_init(o, 0, []) do
+ v = :foo
+ end
+ v.should == :foo
+ end
end
describe "rb_is_instance_of" do
@@ -208,7 +219,7 @@ describe "CApiObject" do
end
it "requires a ruby file" do
- $:.unshift File.dirname(__FILE__)
+ $:.unshift __dir__
@o.rb_require()
$foo.should == 7
end
@@ -513,6 +524,21 @@ describe "CApiObject" do
@o.rb_is_type_class(ObjectTest).should == true
@o.rb_is_type_data(Time.now).should == true
end
+
+ it "returns T_FILE for instances of IO and subclasses" do
+ STDERR.class.should == IO
+ @o.rb_is_rb_type_p_file(STDERR).should == true
+
+ File.open(__FILE__) do |f|
+ f.class.should == File
+ @o.rb_is_rb_type_p_file(f).should == true
+ end
+
+ require 'socket'
+ TCPServer.open(0) do |s|
+ @o.rb_is_rb_type_p_file(s).should == true
+ end
+ end
end
describe "rb_check_type" do
@@ -660,7 +686,7 @@ describe "CApiObject" do
end
it "returns false if object passed to it is not frozen" do
- obj = ""
+ obj = +""
@o.rb_obj_frozen_p(obj).should == false
end
end
@@ -674,7 +700,7 @@ describe "CApiObject" do
end
it "does nothing when object isn't frozen" do
- obj = ""
+ obj = +""
-> { @o.rb_check_frozen(obj) }.should_not raise_error(TypeError)
end
end
@@ -868,9 +894,9 @@ describe "CApiObject" do
describe "rb_copy_generic_ivar for objects which do not store ivars directly" do
it "copies the instance variables from one object to another" do
- original = "abc"
+ original = +"abc"
original.instance_variable_set(:@foo, :bar)
- clone = "def"
+ clone = +"def"
@o.rb_copy_generic_ivar(clone, original)
clone.instance_variable_get(:@foo).should == :bar
end
@@ -878,7 +904,7 @@ describe "CApiObject" do
describe "rb_free_generic_ivar for objects which do not store ivars directly" do
it "removes the instance variables from an object" do
- o = "abc"
+ o = +"abc"
o.instance_variable_set(:@baz, :flibble)
@o.rb_free_generic_ivar(o)
o.instance_variables.should == []
@@ -958,5 +984,30 @@ describe "CApiObject" do
@o.speced_allocator?(parent).should == true
end
end
+
+ describe "rb_ivar_foreach" do
+ it "calls the callback function for each instance variable on an object" do
+ o = CApiObjectSpecs::IVars.new
+ ary = @o.rb_ivar_foreach(o)
+ ary.should == [:@a, 3, :@b, 7, :@c, 4]
+ end
+
+ 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
+ end
+
+ 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
+ end
+
+ end
end
end