|
208 | 208 | @s.rb_struct_size(@struct).should == 3 |
209 | 209 | end |
210 | 210 | end |
| 211 | + |
| 212 | + describe "rb_struct_initialize" do |
| 213 | + it "sets all members" do |
| 214 | + @s.rb_struct_initialize(@struct, [1, 2, 3]).should == nil |
| 215 | + @struct.a.should == 1 |
| 216 | + @struct.b.should == 2 |
| 217 | + @struct.c.should == 3 |
| 218 | + end |
| 219 | + |
| 220 | + it "does not freeze the Struct instance" do |
| 221 | + @s.rb_struct_initialize(@struct, [1, 2, 3]).should == nil |
| 222 | + @struct.should_not.frozen? |
| 223 | + @s.rb_struct_initialize(@struct, [4, 5, 6]).should == nil |
| 224 | + @struct.a.should == 4 |
| 225 | + @struct.b.should == 5 |
| 226 | + @struct.c.should == 6 |
| 227 | + end |
| 228 | + |
| 229 | + it "raises ArgumentError if too many values" do |
| 230 | + -> { @s.rb_struct_initialize(@struct, [1, 2, 3, 4]) }.should raise_error(ArgumentError, "struct size differs") |
| 231 | + end |
| 232 | + |
| 233 | + it "treats missing values as nil" do |
| 234 | + @s.rb_struct_initialize(@struct, [1, 2]).should == nil |
| 235 | + @struct.a.should == 1 |
| 236 | + @struct.b.should == 2 |
| 237 | + @struct.c.should == nil |
| 238 | + end |
| 239 | + end |
211 | 240 | end |
212 | 241 |
|
213 | 242 | ruby_version_is "3.3" do |
214 | 243 | describe "C-API Data function" do |
215 | | - before :each do |
| 244 | + before :all do |
216 | 245 | @s = CApiStructSpecs.new |
| 246 | + @klass = @s.rb_data_define(nil, "a", "b", "c") |
217 | 247 | end |
218 | 248 |
|
219 | 249 | describe "rb_data_define" do |
220 | 250 | it "returns a subclass of Data class when passed nil as the first argument" do |
221 | | - klass = @s.rb_data_define(nil, "a", "b", "c") |
222 | | - |
223 | | - klass.should.is_a? Class |
224 | | - klass.superclass.should == Data |
| 251 | + @klass.should.is_a? Class |
| 252 | + @klass.superclass.should == Data |
225 | 253 | end |
226 | 254 |
|
227 | 255 | it "returns a subclass of a class when passed as the first argument" do |
|
233 | 261 | end |
234 | 262 |
|
235 | 263 | it "creates readers for the members" do |
236 | | - klass = @s.rb_data_define(nil, "a", "b", "c") |
237 | | - obj = klass.new(1, 2, 3) |
| 264 | + obj = @klass.new(1, 2, 3) |
238 | 265 |
|
239 | 266 | obj.a.should == 1 |
240 | 267 | obj.b.should == 2 |
241 | 268 | obj.c.should == 3 |
242 | 269 | end |
243 | 270 |
|
244 | 271 | it "returns the member names as Symbols" do |
245 | | - klass = @s.rb_data_define(nil, "a", "b", "c") |
246 | | - obj = klass.new(0, 0, 0) |
| 272 | + obj = @klass.new(0, 0, 0) |
247 | 273 |
|
248 | 274 | obj.members.should == [:a, :b, :c] |
249 | 275 | end |
|
256 | 282 | -> { @s.rb_data_define([], "a", "b", "c") }.should raise_error(TypeError, "wrong argument type Array (expected Class)") |
257 | 283 | end |
258 | 284 | end |
| 285 | + |
| 286 | + describe "rb_struct_initialize" do |
| 287 | + it "sets all members for a Data instance" do |
| 288 | + data = @klass.allocate |
| 289 | + @s.rb_struct_initialize(data, [1, 2, 3]).should == nil |
| 290 | + data.a.should == 1 |
| 291 | + data.b.should == 2 |
| 292 | + data.c.should == 3 |
| 293 | + end |
| 294 | + |
| 295 | + it "freezes the Data instance" do |
| 296 | + data = @klass.allocate |
| 297 | + @s.rb_struct_initialize(data, [1, 2, 3]).should == nil |
| 298 | + data.should.frozen? |
| 299 | + -> { @s.rb_struct_initialize(data, [1, 2, 3]) }.should raise_error(FrozenError) |
| 300 | + end |
| 301 | + |
| 302 | + it "raises ArgumentError if too many values" do |
| 303 | + data = @klass.allocate |
| 304 | + -> { @s.rb_struct_initialize(data, [1, 2, 3, 4]) }.should raise_error(ArgumentError, "struct size differs") |
| 305 | + end |
| 306 | + |
| 307 | + it "treats missing values as nil" do |
| 308 | + data = @klass.allocate |
| 309 | + @s.rb_struct_initialize(data, [1, 2]).should == nil |
| 310 | + data.a.should == 1 |
| 311 | + data.b.should == 2 |
| 312 | + data.c.should == nil |
| 313 | + end |
| 314 | + end |
259 | 315 | end |
260 | 316 | end |
0 commit comments