summaryrefslogtreecommitdiff
path: root/spec/rubyspec/core/struct
diff options
context:
space:
mode:
Diffstat (limited to 'spec/rubyspec/core/struct')
-rw-r--r--spec/rubyspec/core/struct/dig_spec.rb44
-rw-r--r--spec/rubyspec/core/struct/dup_spec.rb20
-rw-r--r--spec/rubyspec/core/struct/each_pair_spec.rb33
-rw-r--r--spec/rubyspec/core/struct/each_spec.rb27
-rw-r--r--spec/rubyspec/core/struct/element_reference_spec.rb52
-rw-r--r--spec/rubyspec/core/struct/element_set_spec.rb29
-rw-r--r--spec/rubyspec/core/struct/eql_spec.rb13
-rw-r--r--spec/rubyspec/core/struct/equal_value_spec.rb7
-rw-r--r--spec/rubyspec/core/struct/fixtures/classes.rb26
-rw-r--r--spec/rubyspec/core/struct/hash_spec.rb46
-rw-r--r--spec/rubyspec/core/struct/initialize_spec.rb43
-rw-r--r--spec/rubyspec/core/struct/inspect_spec.rb18
-rw-r--r--spec/rubyspec/core/struct/instance_variables_spec.rb16
-rw-r--r--spec/rubyspec/core/struct/length_spec.rb12
-rw-r--r--spec/rubyspec/core/struct/members_spec.rb13
-rw-r--r--spec/rubyspec/core/struct/new_spec.rb123
-rw-r--r--spec/rubyspec/core/struct/select_spec.rb30
-rw-r--r--spec/rubyspec/core/struct/shared/accessor.rb7
-rw-r--r--spec/rubyspec/core/struct/shared/equal_value.rb30
-rw-r--r--spec/rubyspec/core/struct/shared/inspect.rb5
-rw-r--r--spec/rubyspec/core/struct/size_spec.rb11
-rw-r--r--spec/rubyspec/core/struct/struct_spec.rb43
-rw-r--r--spec/rubyspec/core/struct/tms/cstime_spec.rb9
-rw-r--r--spec/rubyspec/core/struct/tms/cutime_spec.rb9
-rw-r--r--spec/rubyspec/core/struct/tms/element_reference_spec.rb5
-rw-r--r--spec/rubyspec/core/struct/tms/members_spec.rb5
-rw-r--r--spec/rubyspec/core/struct/tms/new_spec.rb5
-rw-r--r--spec/rubyspec/core/struct/tms/stime_spec.rb9
-rw-r--r--spec/rubyspec/core/struct/tms/utime_spec.rb9
-rw-r--r--spec/rubyspec/core/struct/to_a_spec.rb12
-rw-r--r--spec/rubyspec/core/struct/to_h_spec.rb15
-rw-r--r--spec/rubyspec/core/struct/to_s_spec.rb12
-rw-r--r--spec/rubyspec/core/struct/values_at_spec.rb16
-rw-r--r--spec/rubyspec/core/struct/values_spec.rb11
34 files changed, 765 insertions, 0 deletions
diff --git a/spec/rubyspec/core/struct/dig_spec.rb b/spec/rubyspec/core/struct/dig_spec.rb
new file mode 100644
index 0000000000..c222eec9e4
--- /dev/null
+++ b/spec/rubyspec/core/struct/dig_spec.rb
@@ -0,0 +1,44 @@
+require File.expand_path('../../../spec_helper', __FILE__)
+
+ruby_version_is "2.3" do
+ describe "Struct#dig" do
+ before(:each) do
+ @klass = Struct.new(:a)
+ @instance = @klass.new(@klass.new({ b: [1, 2, 3] }))
+ end
+
+ it "returns the nested value specified by the sequence of keys" do
+ @instance.dig(:a, :a).should == { b: [1, 2, 3] }
+ end
+
+ it "returns the nested value specified if the sequence includes an index" do
+ @instance.dig(:a, :a, :b, 0).should == 1
+ end
+
+ it "returns nil if any intermediate step is nil" do
+ @instance.dig(:b, 0).should == nil
+ end
+
+ it "raises a TypeError if any intermediate step does not respond to #dig" do
+ instance = @klass.new(1)
+ lambda {
+ instance.dig(:a, 3)
+ }.should raise_error(TypeError)
+ end
+
+ it "raises an ArgumentError if no arguments provided" do
+ lambda { @instance.dig }.should raise_error(ArgumentError)
+ end
+
+ it "calls #dig on any intermediate step with the rest of the sequence as arguments" do
+ obj = Object.new
+ instance = @klass.new(obj)
+
+ def obj.dig(*args)
+ {dug: args}
+ end
+
+ instance.dig(:a, :bar, :baz).should == { dug: [:bar, :baz] }
+ end
+ end
+end
diff --git a/spec/rubyspec/core/struct/dup_spec.rb b/spec/rubyspec/core/struct/dup_spec.rb
new file mode 100644
index 0000000000..d1da31d6d5
--- /dev/null
+++ b/spec/rubyspec/core/struct/dup_spec.rb
@@ -0,0 +1,20 @@
+require File.expand_path('../../../spec_helper', __FILE__)
+require File.expand_path('../fixtures/classes', __FILE__)
+
+describe "Struct-based class#dup" do
+
+ # From https://github.com/jruby/jruby/issues/3686
+ it "retains an included module in the ancestor chain for the struct's singleton class" do
+ klass = Struct.new(:foo)
+ mod = Module.new do
+ def hello
+ "hello"
+ end
+ end
+
+ klass.extend(mod)
+ klass_dup = klass.dup
+ klass_dup.hello.should == "hello"
+ end
+
+end
diff --git a/spec/rubyspec/core/struct/each_pair_spec.rb b/spec/rubyspec/core/struct/each_pair_spec.rb
new file mode 100644
index 0000000000..79a962a6ad
--- /dev/null
+++ b/spec/rubyspec/core/struct/each_pair_spec.rb
@@ -0,0 +1,33 @@
+require File.expand_path('../../../spec_helper', __FILE__)
+require File.expand_path('../fixtures/classes', __FILE__)
+require File.expand_path('../shared/accessor', __FILE__)
+require File.expand_path('../../enumerable/shared/enumeratorized', __FILE__)
+
+describe "Struct#each_pair" do
+ before :each do
+ @car = StructClasses::Car.new('Ford', 'Ranger', 2001)
+ end
+
+ it "passes each key value pair to the given block" do
+ @car.each_pair do |key, value|
+ value.should == @car[key]
+ end
+ end
+
+ context "with a block variable" do
+ it "passes an array to the given block" do
+ @car.each_pair.map { |var| var }.should == StructClasses::Car.members.zip(@car.values)
+ end
+ end
+
+ it "returns self if passed a block" do
+ @car.each_pair {}.should equal(@car)
+ end
+
+ it "returns an Enumerator if not passed a block" do
+ @car.each_pair.should be_an_instance_of(Enumerator)
+ end
+
+ it_behaves_like :struct_accessor, :each_pair
+ it_behaves_like :enumeratorized_with_origin_size, :each_pair, StructClasses::Car.new('Ford', 'Ranger')
+end
diff --git a/spec/rubyspec/core/struct/each_spec.rb b/spec/rubyspec/core/struct/each_spec.rb
new file mode 100644
index 0000000000..86302d91c6
--- /dev/null
+++ b/spec/rubyspec/core/struct/each_spec.rb
@@ -0,0 +1,27 @@
+require File.expand_path('../../../spec_helper', __FILE__)
+require File.expand_path('../fixtures/classes', __FILE__)
+require File.expand_path('../shared/accessor', __FILE__)
+require File.expand_path('../../enumerable/shared/enumeratorized', __FILE__)
+
+describe "Struct#each" do
+ it "passes each value to the given block" do
+ car = StructClasses::Car.new('Ford', 'Ranger')
+ i = -1
+ car.each do |value|
+ value.should == car[i += 1]
+ end
+ end
+
+ it "returns self if passed a block" do
+ car = StructClasses::Car.new('Ford', 'Ranger')
+ car.each {}.should == car
+ end
+
+ it "returns an Enumerator if not passed a block" do
+ car = StructClasses::Car.new('Ford', 'Ranger')
+ car.each.should be_an_instance_of(Enumerator)
+ end
+
+ it_behaves_like :struct_accessor, :each
+ it_behaves_like :enumeratorized_with_origin_size, :each, StructClasses::Car.new('Ford', 'Ranger')
+end
diff --git a/spec/rubyspec/core/struct/element_reference_spec.rb b/spec/rubyspec/core/struct/element_reference_spec.rb
new file mode 100644
index 0000000000..dc51fbfff1
--- /dev/null
+++ b/spec/rubyspec/core/struct/element_reference_spec.rb
@@ -0,0 +1,52 @@
+require File.expand_path('../../../spec_helper', __FILE__)
+require File.expand_path('../fixtures/classes', __FILE__)
+
+describe "Struct[]" do
+ it "is a synonym for new" do
+ StructClasses::Ruby['2.0', 'i686'].should be_kind_of(StructClasses::Ruby)
+ end
+end
+
+describe "Struct#[]" do
+ it "returns the attribute referenced" do
+ car = StructClasses::Car.new('Ford', 'Ranger', 1983)
+ car['make'].should == 'Ford'
+ car['model'].should == 'Ranger'
+ car['year'].should == 1983
+ car[:make].should == 'Ford'
+ car[:model].should == 'Ranger'
+ car[:year].should == 1983
+ car[0].should == 'Ford'
+ car[1].should == 'Ranger'
+ car[2].should == 1983
+ car[-3].should == 'Ford'
+ car[-2].should == 'Ranger'
+ car[-1].should == 1983
+ end
+
+ it "fails when it does not know about the requested attribute" do
+ car = StructClasses::Car.new('Ford', 'Ranger')
+ lambda { car[3] }.should raise_error(IndexError)
+ lambda { car[-4] }.should raise_error(IndexError)
+ lambda { car[:body] }.should raise_error(NameError)
+ lambda { car['wheels'] }.should raise_error(NameError)
+ end
+
+ it "fails if passed too many arguments" do
+ car = StructClasses::Car.new('Ford', 'Ranger')
+ lambda { car[:make, :model] }.should raise_error(ArgumentError)
+ end
+
+ it "fails if not passed a string, symbol, or integer" do
+ car = StructClasses::Car.new('Ford', 'Ranger')
+ lambda { car[Object.new] }.should raise_error(TypeError)
+ end
+
+ it "returns attribute names that contain hyphens" do
+ klass = Struct.new(:'current-state')
+ tuple = klass.new(0)
+ tuple['current-state'].should == 0
+ tuple[:'current-state'].should == 0
+ tuple[0].should == 0
+ end
+end
diff --git a/spec/rubyspec/core/struct/element_set_spec.rb b/spec/rubyspec/core/struct/element_set_spec.rb
new file mode 100644
index 0000000000..3d482bdb71
--- /dev/null
+++ b/spec/rubyspec/core/struct/element_set_spec.rb
@@ -0,0 +1,29 @@
+require File.expand_path('../../../spec_helper', __FILE__)
+require File.expand_path('../fixtures/classes', __FILE__)
+
+describe "Struct#[]=" do
+ it "assigns the passed value" do
+ car = StructClasses::Car.new('Ford', 'Ranger')
+
+ car[:model] = 'Escape'
+ car[:model].should == 'Escape'
+
+ car['model'] = 'Fusion'
+ car[:model].should == 'Fusion'
+
+ car[1] = 'Excursion'
+ car[:model].should == 'Excursion'
+
+ car[-1] = '2000-2005'
+ car[:year].should == '2000-2005'
+ end
+
+ it "fails when trying to assign attributes which don't exist" do
+ car = StructClasses::Car.new('Ford', 'Ranger')
+
+ lambda { car[:something] = true }.should raise_error(NameError)
+ lambda { car[3] = true }.should raise_error(IndexError)
+ lambda { car[-4] = true }.should raise_error(IndexError)
+ lambda { car[Object.new] = true }.should raise_error(TypeError)
+ end
+end
diff --git a/spec/rubyspec/core/struct/eql_spec.rb b/spec/rubyspec/core/struct/eql_spec.rb
new file mode 100644
index 0000000000..dfa97811c6
--- /dev/null
+++ b/spec/rubyspec/core/struct/eql_spec.rb
@@ -0,0 +1,13 @@
+require File.expand_path('../../../spec_helper', __FILE__)
+require File.expand_path('../fixtures/classes', __FILE__)
+require File.expand_path('../shared/equal_value', __FILE__)
+
+describe "Struct#eql?" do
+ it_behaves_like(:struct_equal_value, :eql?)
+
+ it "returns false if any corresponding elements are not #eql?" do
+ car = StructClasses::Car.new("Honda", "Accord", 1998)
+ similar_car = StructClasses::Car.new("Honda", "Accord", 1998.0)
+ car.send(@method, similar_car).should be_false
+ end
+end
diff --git a/spec/rubyspec/core/struct/equal_value_spec.rb b/spec/rubyspec/core/struct/equal_value_spec.rb
new file mode 100644
index 0000000000..a343213417
--- /dev/null
+++ b/spec/rubyspec/core/struct/equal_value_spec.rb
@@ -0,0 +1,7 @@
+require File.expand_path('../../../spec_helper', __FILE__)
+require File.expand_path('../fixtures/classes', __FILE__)
+require File.expand_path('../shared/equal_value', __FILE__)
+
+describe "Struct#==" do
+ it_behaves_like(:struct_equal_value, :==)
+end
diff --git a/spec/rubyspec/core/struct/fixtures/classes.rb b/spec/rubyspec/core/struct/fixtures/classes.rb
new file mode 100644
index 0000000000..6d620f9060
--- /dev/null
+++ b/spec/rubyspec/core/struct/fixtures/classes.rb
@@ -0,0 +1,26 @@
+module StructClasses
+
+ class Apple < Struct; end
+
+ Ruby = Struct.new(:version, :platform)
+
+ Car = Struct.new(:make, :model, :year)
+
+ class Honda < Car
+ def initialize(*args)
+ self.make = "Honda"
+ super(*args)
+ end
+ end
+
+ class SubclassX < Struct
+ end
+
+ class SubclassX
+ attr_reader :key
+ def initialize(*)
+ @key = :value
+ super
+ end
+ end
+end
diff --git a/spec/rubyspec/core/struct/hash_spec.rb b/spec/rubyspec/core/struct/hash_spec.rb
new file mode 100644
index 0000000000..517d3ab44e
--- /dev/null
+++ b/spec/rubyspec/core/struct/hash_spec.rb
@@ -0,0 +1,46 @@
+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
+
+ it "returns the same fixnum for structs with the same content" do
+ [StructClasses::Ruby.new("1.8.6", "PPC"),
+ StructClasses::Car.new("Hugo", "Foo", "1972")].each do |stc|
+ stc.hash.should == stc.dup.hash
+ stc.hash.should be_kind_of(Fixnum)
+ end
+ end
+
+ it "returns the same value if structs are #eql?" do
+ car = StructClasses::Car.new("Honda", "Accord", "1998")
+ similar_car = StructClasses::Car.new("Honda", "Accord", "1998")
+ car.should eql(similar_car)
+ car.hash.should == similar_car.hash
+ end
+
+ it "allows for overriding methods in an included module" do
+ mod = Module.new do
+ def hash
+ "different"
+ end
+ end
+ s = Struct.new(:arg) do
+ include mod
+ end
+ s.new.hash.should == "different"
+ end
+
+ it "returns the same hash for recursive structs" do
+ car = StructClasses::Car.new("Honda", "Accord", "1998")
+ similar_car = StructClasses::Car.new("Honda", "Accord", "1998")
+ car[:make] = car
+ similar_car[:make] = car
+ car.hash.should == similar_car.hash
+ # This is because car.eql?(similar_car).
+ # Objects that are eql? must return the same hash.
+ # See the Struct#eql? specs
+ end
+
+ it_behaves_like :struct_accessor, :hash
+end
diff --git a/spec/rubyspec/core/struct/initialize_spec.rb b/spec/rubyspec/core/struct/initialize_spec.rb
new file mode 100644
index 0000000000..59fc5ef9aa
--- /dev/null
+++ b/spec/rubyspec/core/struct/initialize_spec.rb
@@ -0,0 +1,43 @@
+require File.expand_path('../../../spec_helper', __FILE__)
+require File.expand_path('../fixtures/classes', __FILE__)
+
+describe "Struct#initialize" do
+
+ it "is private" do
+ StructClasses::Car.should have_private_instance_method(:initialize)
+ end
+
+ it 'allows valid Ruby method names for members' do
+ valid_method_names = [
+ :method1,
+ :method_1,
+ :method_1?,
+ :method_1!,
+ :a_method
+ ]
+ valid_method_names.each do |method_name|
+ klass = Struct.new(method_name)
+ instance = klass.new(:value)
+ instance.send(method_name).should == :value
+ writer_method = "#{method_name}=".to_sym
+ result = instance.send(writer_method, :new_value)
+ result.should == :new_value
+ instance.send(method_name).should == :new_value
+ end
+ end
+
+ it "does nothing when passed a set of fields equal to self" do
+ car = same_car = StructClasses::Car.new("Honda", "Accord", "1998")
+ car.instance_eval { initialize("Honda", "Accord", "1998") }
+ car.should == same_car
+ end
+
+ it "explicitly sets instance variables to nil when args not provided to initialize" do
+ car = StructClasses::Honda.new
+ car.make.should == nil # still nil despite override in Honda#initialize b/c of super order
+ end
+
+ it "can be overriden" do
+ StructClasses::SubclassX.new(:y).new.key.should == :value
+ end
+end
diff --git a/spec/rubyspec/core/struct/inspect_spec.rb b/spec/rubyspec/core/struct/inspect_spec.rb
new file mode 100644
index 0000000000..a85466e1a2
--- /dev/null
+++ b/spec/rubyspec/core/struct/inspect_spec.rb
@@ -0,0 +1,18 @@
+require File.expand_path('../../../spec_helper', __FILE__)
+require File.expand_path('../fixtures/classes', __FILE__)
+require File.expand_path('../shared/inspect', __FILE__)
+
+describe "Struct#inspect" do
+ it "returns a string representation of some kind" do
+ car = StructClasses::Car.new('Ford', 'Ranger')
+ car.inspect.should == '#<struct StructClasses::Car make="Ford", model="Ranger", year=nil>'
+ # ujihisa reported in http://rubyspec.org/issues/show/130 that the
+ # following example failed under mspec. Prefixing 'Whiskey' with a double
+ # colon causes it to work. Given that this is an mspec bug, as opposed to
+ # a problem with a spec, I've used the workaround below.
+ ::Whiskey = Struct.new(:name, :ounces)
+ ::Whiskey.new('Jack', 100).inspect.should == '#<struct Whiskey name="Jack", ounces=100>'
+ end
+
+ it_behaves_like(:struct_inspect, :inspect)
+end
diff --git a/spec/rubyspec/core/struct/instance_variables_spec.rb b/spec/rubyspec/core/struct/instance_variables_spec.rb
new file mode 100644
index 0000000000..3abb8578a5
--- /dev/null
+++ b/spec/rubyspec/core/struct/instance_variables_spec.rb
@@ -0,0 +1,16 @@
+require File.expand_path('../../../spec_helper', __FILE__)
+require File.expand_path('../fixtures/classes', __FILE__)
+
+describe "Struct#instance_variables" do
+ it "returns an empty array if only attributes are defined" do
+ car = StructClasses::Car.new("Hugo", "Foo", "1972")
+ car.instance_variables.should == []
+ end
+
+ it "returns an array with one name if an instance variable is added" do
+ car = StructClasses::Car.new("Hugo", "Foo", "1972")
+ car.instance_variables.should == []
+ car.instance_variable_set("@test", 1)
+ car.instance_variables.should == [:@test]
+ end
+end
diff --git a/spec/rubyspec/core/struct/length_spec.rb b/spec/rubyspec/core/struct/length_spec.rb
new file mode 100644
index 0000000000..067bb08f88
--- /dev/null
+++ b/spec/rubyspec/core/struct/length_spec.rb
@@ -0,0 +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
diff --git a/spec/rubyspec/core/struct/members_spec.rb b/spec/rubyspec/core/struct/members_spec.rb
new file mode 100644
index 0000000000..702e536a55
--- /dev/null
+++ b/spec/rubyspec/core/struct/members_spec.rb
@@ -0,0 +1,13 @@
+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
+ it "returns an array of attribute names" do
+ StructClasses::Car.new.members.should == [:make, :model, :year]
+ StructClasses::Car.new('Cadillac').members.should == [:make, :model, :year]
+ StructClasses::Ruby.members.should == [:version, :platform]
+ end
+
+ it_behaves_like :struct_accessor, :members
+end
diff --git a/spec/rubyspec/core/struct/new_spec.rb b/spec/rubyspec/core/struct/new_spec.rb
new file mode 100644
index 0000000000..314f749955
--- /dev/null
+++ b/spec/rubyspec/core/struct/new_spec.rb
@@ -0,0 +1,123 @@
+require File.expand_path('../../../spec_helper', __FILE__)
+require File.expand_path('../fixtures/classes', __FILE__)
+
+describe "Struct.new" do
+ it "creates a constant in Struct namespace with string as first argument" do
+ struct = Struct.new('Animal', :name, :legs, :eyeballs)
+ struct.should == Struct::Animal
+ struct.name.should == "Struct::Animal"
+ end
+
+ it "overwrites previously defined constants with string as first argument" do
+ first = Struct.new('Person', :height, :weight)
+ first.should == Struct::Person
+
+ second = nil
+ lambda {
+ second = Struct.new('Person', :hair, :sex)
+ }.should complain(/redefining constant/)
+ second.should == Struct::Person
+
+ first.members.should_not == second.members
+ end
+
+ it "calls to_str on its first argument (constant name)" do
+ obj = mock('Foo')
+ def obj.to_str() "Foo" end
+ struct = Struct.new(obj)
+ struct.should == Struct::Foo
+ struct.name.should == "Struct::Foo"
+ end
+
+ it "creates a new anonymous class with nil first argument" do
+ struct = Struct.new(nil, :foo)
+ struct.new("bar").foo.should == "bar"
+ struct.should be_kind_of(Class)
+ struct.name.should be_nil
+ end
+
+ it "creates a new anonymous class with symbol arguments" do
+ struct = Struct.new(:make, :model)
+ struct.should be_kind_of(Class)
+ struct.name.should == nil
+ end
+
+ it "does not create a constant with symbol as first argument" do
+ Struct.new(:Animal2, :name, :legs, :eyeballs)
+ Struct.const_defined?("Animal2").should be_false
+ end
+
+
+ it "fails with invalid constant name as first argument" do
+ lambda { Struct.new('animal', :name, :legs, :eyeballs) }.should raise_error(NameError)
+ end
+
+ it "raises a TypeError if object doesn't respond to to_sym" do
+ lambda { Struct.new(:animal, mock('giraffe')) }.should raise_error(TypeError)
+ lambda { Struct.new(:animal, 1.0) }.should raise_error(TypeError)
+ lambda { Struct.new(:animal, Time.now) }.should raise_error(TypeError)
+ lambda { Struct.new(:animal, Class) }.should raise_error(TypeError)
+ lambda { Struct.new(:animal, nil) }.should raise_error(TypeError)
+ lambda { Struct.new(:animal, true) }.should raise_error(TypeError)
+ lambda { Struct.new(:animal, ['chris', 'evan']) }.should raise_error(TypeError)
+ lambda { Struct.new(:animal, { name: 'chris' }) }.should raise_error(TypeError)
+ end
+
+ it "raises a TypeError if object is not a Symbol" do
+ obj = mock(':ruby')
+ def obj.to_sym() :ruby end
+ lambda { Struct.new(:animal, obj) }.should raise_error(TypeError)
+ end
+
+ it "processes passed block with instance_eval" do
+ klass = Struct.new(:something) { @something_else = 'something else entirely!' }
+ klass.instance_variables.should include(:@something_else)
+ end
+
+ context "with a block" do
+ it "allows class to be modified via the block" do
+ klass = Struct.new(:version) do
+ def platform
+ :ruby
+ end
+ end
+ instance = klass.new('2.2')
+
+ instance.version.should == '2.2'
+ instance.platform.should == :ruby
+ end
+
+ it "passes same struct class to the block" do
+ given = nil
+ klass = Struct.new(:attr) do |block_parameter|
+ given = block_parameter
+ end
+ klass.should equal(given)
+ end
+ end
+
+ context "on subclasses" do
+ it "creates a constant in subclass' namespace" do
+ struct = StructClasses::Apple.new('Computer', :size)
+ struct.should == StructClasses::Apple::Computer
+ end
+
+ it "creates an instance" do
+ StructClasses::Ruby.new.kind_of?(StructClasses::Ruby).should == true
+ end
+
+ it "creates reader methods" do
+ StructClasses::Ruby.new.should have_method(:version)
+ StructClasses::Ruby.new.should have_method(:platform)
+ end
+
+ it "creates writer methods" do
+ StructClasses::Ruby.new.should have_method(:version=)
+ StructClasses::Ruby.new.should have_method(:platform=)
+ end
+
+ it "fails with too many arguments" do
+ lambda { StructClasses::Ruby.new('2.0', 'i686', true) }.should raise_error(ArgumentError)
+ end
+ end
+end
diff --git a/spec/rubyspec/core/struct/select_spec.rb b/spec/rubyspec/core/struct/select_spec.rb
new file mode 100644
index 0000000000..da80eea0e8
--- /dev/null
+++ b/spec/rubyspec/core/struct/select_spec.rb
@@ -0,0 +1,30 @@
+require File.expand_path('../../../spec_helper', __FILE__)
+require File.expand_path('../fixtures/classes', __FILE__)
+require File.expand_path('../shared/accessor', __FILE__)
+require File.expand_path('../../enumerable/shared/enumeratorized', __FILE__)
+
+describe "Struct#select" do
+ it "raises an ArgumentError if given any non-block arguments" do
+ lambda { StructClasses::Car.new.select(1) { } }.should raise_error(ArgumentError)
+ end
+
+ it "returns a new array of elements for which block is true" do
+ struct = StructClasses::Car.new("Toyota", "Tercel", "2000")
+ struct.select { |i| i == "2000" }.should == [ "2000" ]
+ end
+
+ it "returns an instance of Array" do
+ struct = StructClasses::Car.new("Ford", "Escort", "1995")
+ struct.select { true }.should be_an_instance_of(Array)
+ end
+
+ describe "without block" do
+ it "returns an instance of Enumerator" do
+ struct = Struct.new(:foo).new
+ struct.select.should be_an_instance_of(Enumerator)
+ end
+ end
+
+ it_behaves_like :struct_accessor, :select
+ it_behaves_like :enumeratorized_with_origin_size, :select, Struct.new(:foo).new
+end
diff --git a/spec/rubyspec/core/struct/shared/accessor.rb b/spec/rubyspec/core/struct/shared/accessor.rb
new file mode 100644
index 0000000000..dbf5e78f43
--- /dev/null
+++ b/spec/rubyspec/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
diff --git a/spec/rubyspec/core/struct/shared/equal_value.rb b/spec/rubyspec/core/struct/shared/equal_value.rb
new file mode 100644
index 0000000000..711862cb44
--- /dev/null
+++ b/spec/rubyspec/core/struct/shared/equal_value.rb
@@ -0,0 +1,30 @@
+describe :struct_equal_value, shared: true do
+ it "returns true if the other is the same object" do
+ car = same_car = StructClasses::Car.new("Honda", "Accord", "1998")
+ car.send(@method, same_car).should == true
+ end
+
+ it "returns true if the other has all the same fields" do
+ car = StructClasses::Car.new("Honda", "Accord", "1998")
+ similar_car = StructClasses::Car.new("Honda", "Accord", "1998")
+ car.send(@method, similar_car).should == true
+ end
+
+ it "returns false if the other is a different object or has different fields" do
+ car = StructClasses::Car.new("Honda", "Accord", "1998")
+ different_car = StructClasses::Car.new("Honda", "Accord", "1995")
+ car.send(@method, different_car).should == false
+ end
+
+ it "handles recursive structures by returning false if a difference can be found" do
+ x = StructClasses::Car.new("Honda", "Accord", "1998")
+ x[:make] = x
+ stepping = StructClasses::Car.new("Honda", "Accord", "1998")
+ stone = StructClasses::Car.new(stepping, "Accord", "1998")
+ stepping[:make] = stone
+ x.send(@method, stepping).should == true
+
+ stone[:year] = "1999" # introduce a difference
+ x.send(@method, stepping).should == false
+ end
+end
diff --git a/spec/rubyspec/core/struct/shared/inspect.rb b/spec/rubyspec/core/struct/shared/inspect.rb
new file mode 100644
index 0000000000..90594a5452
--- /dev/null
+++ b/spec/rubyspec/core/struct/shared/inspect.rb
@@ -0,0 +1,5 @@
+describe :struct_inspect, shared: true do
+ it "returns a string representation without the class name for anonymous structs" do
+ Struct.new(:a).new("").send(@method).should == '#<struct a="">'
+ end
+end
diff --git a/spec/rubyspec/core/struct/size_spec.rb b/spec/rubyspec/core/struct/size_spec.rb
new file mode 100644
index 0000000000..29b1d2bfba
--- /dev/null
+++ b/spec/rubyspec/core/struct/size_spec.rb
@@ -0,0 +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, :size
+end
diff --git a/spec/rubyspec/core/struct/struct_spec.rb b/spec/rubyspec/core/struct/struct_spec.rb
new file mode 100644
index 0000000000..6c1941aed5
--- /dev/null
+++ b/spec/rubyspec/core/struct/struct_spec.rb
@@ -0,0 +1,43 @@
+require File.expand_path('../../../spec_helper', __FILE__)
+require File.expand_path('../fixtures/classes', __FILE__)
+
+describe "Struct" do
+ it "includes Enumerable" do
+ Struct.include?(Enumerable).should == true
+ end
+end
+
+describe "Struct anonymous class instance methods" do
+ it "includes Enumerable" do
+ StructClasses::Car.include?(Enumerable).should == true
+ end
+
+ it "reader method should be a synonym for []" do
+ klass = Struct.new(:clock, :radio)
+ alarm = klass.new(true)
+ alarm.clock.should == alarm[:clock]
+ alarm.radio.should == alarm['radio']
+ end
+
+ it "reader method should not interfere with undefined methods" do
+ car = StructClasses::Car.new('Ford', 'Ranger')
+ lambda { car.something_weird }.should raise_error(NoMethodError)
+ end
+
+ it "writer method be a synonym for []=" do
+ car = StructClasses::Car.new('Ford', 'Ranger')
+ car.model.should == 'Ranger'
+ car.model = 'F150'
+ car.model.should == 'F150'
+ car[:model].should == 'F150'
+ car['model'].should == 'F150'
+ car[1].should == 'F150'
+ end
+end
+
+describe "Struct subclasses" do
+ it "can be subclassed" do
+ compact = Class.new StructClasses::Car
+ compact.new.class.should == compact
+ end
+end
diff --git a/spec/rubyspec/core/struct/tms/cstime_spec.rb b/spec/rubyspec/core/struct/tms/cstime_spec.rb
new file mode 100644
index 0000000000..839b02b6e9
--- /dev/null
+++ b/spec/rubyspec/core/struct/tms/cstime_spec.rb
@@ -0,0 +1,9 @@
+require File.expand_path('../../../../spec_helper', __FILE__)
+
+describe "Struct::Tms#cstime" do
+ it "needs to be reviewed for spec completeness"
+end
+
+describe "Struct::Tms#cstime=" do
+ it "needs to be reviewed for spec completeness"
+end
diff --git a/spec/rubyspec/core/struct/tms/cutime_spec.rb b/spec/rubyspec/core/struct/tms/cutime_spec.rb
new file mode 100644
index 0000000000..235f378fab
--- /dev/null
+++ b/spec/rubyspec/core/struct/tms/cutime_spec.rb
@@ -0,0 +1,9 @@
+require File.expand_path('../../../../spec_helper', __FILE__)
+
+describe "Struct::Tms#cutime" do
+ it "needs to be reviewed for spec completeness"
+end
+
+describe "Struct::Tms#cutime=" do
+ it "needs to be reviewed for spec completeness"
+end
diff --git a/spec/rubyspec/core/struct/tms/element_reference_spec.rb b/spec/rubyspec/core/struct/tms/element_reference_spec.rb
new file mode 100644
index 0000000000..f1735341b4
--- /dev/null
+++ b/spec/rubyspec/core/struct/tms/element_reference_spec.rb
@@ -0,0 +1,5 @@
+require File.expand_path('../../../../spec_helper', __FILE__)
+
+describe "Struct::Tms.[]" do
+ it "needs to be reviewed for spec completeness"
+end
diff --git a/spec/rubyspec/core/struct/tms/members_spec.rb b/spec/rubyspec/core/struct/tms/members_spec.rb
new file mode 100644
index 0000000000..ddfca83659
--- /dev/null
+++ b/spec/rubyspec/core/struct/tms/members_spec.rb
@@ -0,0 +1,5 @@
+require File.expand_path('../../../../spec_helper', __FILE__)
+
+describe "Struct::Tms.members" do
+ it "needs to be reviewed for spec completeness"
+end
diff --git a/spec/rubyspec/core/struct/tms/new_spec.rb b/spec/rubyspec/core/struct/tms/new_spec.rb
new file mode 100644
index 0000000000..cf7a501aa2
--- /dev/null
+++ b/spec/rubyspec/core/struct/tms/new_spec.rb
@@ -0,0 +1,5 @@
+require File.expand_path('../../../../spec_helper', __FILE__)
+
+describe "Struct::Tms.new" do
+ it "needs to be reviewed for spec completeness"
+end
diff --git a/spec/rubyspec/core/struct/tms/stime_spec.rb b/spec/rubyspec/core/struct/tms/stime_spec.rb
new file mode 100644
index 0000000000..f45253cf44
--- /dev/null
+++ b/spec/rubyspec/core/struct/tms/stime_spec.rb
@@ -0,0 +1,9 @@
+require File.expand_path('../../../../spec_helper', __FILE__)
+
+describe "Struct::Tms#stime" do
+ it "needs to be reviewed for spec completeness"
+end
+
+describe "Struct::Tms#stime=" do
+ it "needs to be reviewed for spec completeness"
+end
diff --git a/spec/rubyspec/core/struct/tms/utime_spec.rb b/spec/rubyspec/core/struct/tms/utime_spec.rb
new file mode 100644
index 0000000000..ea6783a17b
--- /dev/null
+++ b/spec/rubyspec/core/struct/tms/utime_spec.rb
@@ -0,0 +1,9 @@
+require File.expand_path('../../../../spec_helper', __FILE__)
+
+describe "Struct::Tms#utime" do
+ it "needs to be reviewed for spec completeness"
+end
+
+describe "Struct::Tms#utime=" do
+ it "needs to be reviewed for spec completeness"
+end
diff --git a/spec/rubyspec/core/struct/to_a_spec.rb b/spec/rubyspec/core/struct/to_a_spec.rb
new file mode 100644
index 0000000000..f8e9a3658d
--- /dev/null
+++ b/spec/rubyspec/core/struct/to_a_spec.rb
@@ -0,0 +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, :to_a
+end
diff --git a/spec/rubyspec/core/struct/to_h_spec.rb b/spec/rubyspec/core/struct/to_h_spec.rb
new file mode 100644
index 0000000000..6b8037f950
--- /dev/null
+++ b/spec/rubyspec/core/struct/to_h_spec.rb
@@ -0,0 +1,15 @@
+require File.expand_path('../../../spec_helper', __FILE__)
+require File.expand_path('../fixtures/classes', __FILE__)
+
+describe "Struct#to_h" do
+ it "returns a Hash with members as keys" do
+ car = StructClasses::Car.new('Ford', 'Ranger')
+ car.to_h.should == {make: "Ford", model: "Ranger", year: nil}
+ end
+
+ it "returns a Hash that is independent from the struct" do
+ car = StructClasses::Car.new('Ford', 'Ranger')
+ car.to_h[:make] = 'Suzuki'
+ car.make.should == 'Ford'
+ end
+end
diff --git a/spec/rubyspec/core/struct/to_s_spec.rb b/spec/rubyspec/core/struct/to_s_spec.rb
new file mode 100644
index 0000000000..b9fd413093
--- /dev/null
+++ b/spec/rubyspec/core/struct/to_s_spec.rb
@@ -0,0 +1,12 @@
+require File.expand_path('../../../spec_helper', __FILE__)
+require File.expand_path('../fixtures/classes', __FILE__)
+require File.expand_path('../shared/inspect', __FILE__)
+
+describe "Struct#to_s" do
+ it "is a synonym for inspect" do
+ car = StructClasses::Car.new('Ford', 'Ranger')
+ car.inspect.should == car.to_s
+ end
+
+ it_behaves_like(:struct_inspect, :to_s)
+end
diff --git a/spec/rubyspec/core/struct/values_at_spec.rb b/spec/rubyspec/core/struct/values_at_spec.rb
new file mode 100644
index 0000000000..58016e2f2a
--- /dev/null
+++ b/spec/rubyspec/core/struct/values_at_spec.rb
@@ -0,0 +1,16 @@
+require File.expand_path('../../../spec_helper', __FILE__)
+require File.expand_path('../fixtures/classes', __FILE__)
+
+describe "Struct#values_at" do
+ it "returns an array of values" do
+ clazz = Struct.new(:name, :director, :year)
+ movie = clazz.new('Sympathy for Mr. Vengence', 'Chan-wook Park', 2002)
+ movie.values_at(0, 1).should == ['Sympathy for Mr. Vengence', 'Chan-wook Park']
+ movie.values_at(0..2).should == ['Sympathy for Mr. Vengence', 'Chan-wook Park', 2002]
+ end
+
+ it "fails when passed unsupported types" do
+ car = StructClasses::Car.new('Ford', 'Ranger')
+ lambda { car.values_at('make') }.should raise_error(TypeError)
+ end
+end
diff --git a/spec/rubyspec/core/struct/values_spec.rb b/spec/rubyspec/core/struct/values_spec.rb
new file mode 100644
index 0000000000..0e86d33cb5
--- /dev/null
+++ b/spec/rubyspec/core/struct/values_spec.rb
@@ -0,0 +1,11 @@
+require File.expand_path('../../../spec_helper', __FILE__)
+require File.expand_path('../fixtures/classes', __FILE__)
+
+describe "Struct#values" do
+ it "is a synonym for to_a" do
+ car = StructClasses::Car.new('Nissan', 'Maxima')
+ car.values.should == car.to_a
+
+ StructClasses::Car.new.values.should == StructClasses::Car.new.to_a
+ end
+end