summaryrefslogtreecommitdiff
path: root/spec/ruby/optional/capi/struct_spec.rb
diff options
context:
space:
mode:
Diffstat (limited to 'spec/ruby/optional/capi/struct_spec.rb')
-rw-r--r--spec/ruby/optional/capi/struct_spec.rb133
1 files changed, 118 insertions, 15 deletions
diff --git a/spec/ruby/optional/capi/struct_spec.rb b/spec/ruby/optional/capi/struct_spec.rb
index 0e9e366908..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,4 +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
+
+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
+
+ 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 == superclass
+ end
+
+ it "creates readers for the members" do
+ obj = @klass.new(1, 2, 3)
+
+ obj.a.should == 1
+ obj.b.should == 2
+ obj.c.should == 3
+ end
+
+ it "returns the member names as Symbols" do
+ obj = @klass.new(0, 0, 0)
+
+ obj.members.should == [:a, :b, :c]
+ end
+
+ it "raises an ArgumentError if arguments contain duplicate member name" do
+ -> { @s.rb_data_define(nil, "a", "b", "a") }.should.raise(ArgumentError)
+ 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 "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 "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