Skip to content

Commit

Permalink
Merge pull request rubinius#1725 from carlosgaldino/fix-struct
Browse files Browse the repository at this point in the history
  • Loading branch information
dbussink committed May 16, 2012
2 parents d1b731d + d5253bb commit ac70c40
Show file tree
Hide file tree
Showing 10 changed files with 31 additions and 1 deletion.
2 changes: 1 addition & 1 deletion kernel/common/struct.rb
Expand Up @@ -35,6 +35,7 @@ def self.new(klass_name, *attrs, &block)

klass = Class.new self do

_specialize attrs
attr_accessor(*attrs)

def self.new(*args, &block)
Expand All @@ -46,7 +47,6 @@ def self.[](*args)
end

const_set :STRUCT_ATTRS, attrs
_specialize attrs
end

Struct.const_set klass_name, klass if klass_name
Expand Down
3 changes: 3 additions & 0 deletions spec/ruby/core/struct/each_pair_spec.rb
@@ -1,5 +1,6 @@
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../fixtures/classes', __FILE__)
require File.expand_path('../shared/accessor', __FILE__)

describe "Struct#each_pair" do
it "passes each key value pair to the given block" do
Expand Down Expand Up @@ -27,4 +28,6 @@
car.each_pair.should be_an_instance_of(enumerator_class)
end
end

it_behaves_like :struct_accessor, :length
end
3 changes: 3 additions & 0 deletions spec/ruby/core/struct/each_spec.rb
@@ -1,5 +1,6 @@
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../fixtures/classes', __FILE__)
require File.expand_path('../shared/accessor', __FILE__)

describe "Struct#each" do
it "passes each value to the given block" do
Expand Down Expand Up @@ -28,4 +29,6 @@
car.each.should be_an_instance_of(enumerator_class)
end
end

it_behaves_like :struct_accessor, :length
end
2 changes: 2 additions & 0 deletions spec/ruby/core/struct/hash_spec.rb
@@ -1,5 +1,6 @@
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../fixtures/classes', __FILE__)
require File.expand_path('../shared/accessor', __FILE__)

describe "Struct#hash" do

Expand Down Expand Up @@ -31,4 +32,5 @@
end
end

it_behaves_like :struct_accessor, :hash
end
3 changes: 3 additions & 0 deletions spec/ruby/core/struct/length_spec.rb
@@ -1,9 +1,12 @@
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../fixtures/classes', __FILE__)
require File.expand_path('../shared/accessor', __FILE__)

describe "Struct#length" do
it "returns the number of attributes" do
StructClasses::Car.new('Cadillac', 'DeVille').length.should == 3
StructClasses::Car.new.length.should == 3
end

it_behaves_like :struct_accessor, :length
end
3 changes: 3 additions & 0 deletions spec/ruby/core/struct/members_spec.rb
@@ -1,5 +1,6 @@
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../fixtures/classes', __FILE__)
require File.expand_path('../shared/accessor', __FILE__)

describe "Struct#members" do
ruby_version_is ""..."1.9" do
Expand All @@ -17,4 +18,6 @@
StructClasses::Ruby.members.should == [:version, :platform]
end
end

it_behaves_like :struct_accessor, :length
end
3 changes: 3 additions & 0 deletions spec/ruby/core/struct/select_spec.rb
@@ -1,5 +1,6 @@
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../fixtures/classes', __FILE__)
require File.expand_path('../shared/accessor', __FILE__)

describe "Struct#select" do
it "raises an ArgumentError if given any non-block arguments" do
Expand All @@ -15,4 +16,6 @@
struct = StructClasses::Car.new("Ford", "Escort", "1995")
struct.select { true }.should be_kind_of(Array)
end

it_behaves_like :struct_accessor, :length
end
7 changes: 7 additions & 0 deletions spec/ruby/core/struct/shared/accessor.rb
@@ -0,0 +1,7 @@
describe :struct_accessor, :shared => true do
it "does not override the instance accessor method" do
struct = Struct.new(@method.to_sym)
instance = struct.new 42
instance.send(@method).should == 42
end
end
3 changes: 3 additions & 0 deletions spec/ruby/core/struct/size_spec.rb
@@ -1,8 +1,11 @@
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../fixtures/classes', __FILE__)
require File.expand_path('../shared/accessor', __FILE__)

describe "Struct#size" do
it "is a synonym for length" do
StructClasses::Car.new.size.should == StructClasses::Car.new.length
end

it_behaves_like :struct_accessor, :length
end
3 changes: 3 additions & 0 deletions spec/ruby/core/struct/to_a_spec.rb
@@ -1,9 +1,12 @@
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../fixtures/classes', __FILE__)
require File.expand_path('../shared/accessor', __FILE__)

describe "Struct#to_a" do
it "returns the values for this instance as an array" do
StructClasses::Car.new('Geo', 'Metro', 1995).to_a.should == ['Geo', 'Metro', 1995]
StructClasses::Car.new('Ford').to_a.should == ['Ford', nil, nil]
end

it_behaves_like :struct_accessor, :length
end

0 comments on commit ac70c40

Please sign in to comment.