mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
Move spec/rubyspec to spec/ruby for consistency
* Other ruby implementations use the spec/ruby directory. [Misc #13792] [ruby-core:82287] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@59979 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
75bfc6440d
commit
1d15d5f080
4370 changed files with 0 additions and 0 deletions
44
spec/ruby/core/struct/dig_spec.rb
Normal file
44
spec/ruby/core/struct/dig_spec.rb
Normal file
|
@ -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
|
20
spec/ruby/core/struct/dup_spec.rb
Normal file
20
spec/ruby/core/struct/dup_spec.rb
Normal file
|
@ -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
|
33
spec/ruby/core/struct/each_pair_spec.rb
Normal file
33
spec/ruby/core/struct/each_pair_spec.rb
Normal file
|
@ -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
|
27
spec/ruby/core/struct/each_spec.rb
Normal file
27
spec/ruby/core/struct/each_spec.rb
Normal file
|
@ -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
|
52
spec/ruby/core/struct/element_reference_spec.rb
Normal file
52
spec/ruby/core/struct/element_reference_spec.rb
Normal file
|
@ -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
|
29
spec/ruby/core/struct/element_set_spec.rb
Normal file
29
spec/ruby/core/struct/element_set_spec.rb
Normal file
|
@ -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
|
13
spec/ruby/core/struct/eql_spec.rb
Normal file
13
spec/ruby/core/struct/eql_spec.rb
Normal file
|
@ -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
|
7
spec/ruby/core/struct/equal_value_spec.rb
Normal file
7
spec/ruby/core/struct/equal_value_spec.rb
Normal file
|
@ -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
|
26
spec/ruby/core/struct/fixtures/classes.rb
Normal file
26
spec/ruby/core/struct/fixtures/classes.rb
Normal file
|
@ -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
|
46
spec/ruby/core/struct/hash_spec.rb
Normal file
46
spec/ruby/core/struct/hash_spec.rb
Normal file
|
@ -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
|
43
spec/ruby/core/struct/initialize_spec.rb
Normal file
43
spec/ruby/core/struct/initialize_spec.rb
Normal file
|
@ -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
|
15
spec/ruby/core/struct/inspect_spec.rb
Normal file
15
spec/ruby/core/struct/inspect_spec.rb
Normal file
|
@ -0,0 +1,15 @@
|
|||
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>'
|
||||
|
||||
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
|
16
spec/ruby/core/struct/instance_variables_spec.rb
Normal file
16
spec/ruby/core/struct/instance_variables_spec.rb
Normal file
|
@ -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
|
12
spec/ruby/core/struct/length_spec.rb
Normal file
12
spec/ruby/core/struct/length_spec.rb
Normal file
|
@ -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
|
13
spec/ruby/core/struct/members_spec.rb
Normal file
13
spec/ruby/core/struct/members_spec.rb
Normal file
|
@ -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
|
123
spec/ruby/core/struct/new_spec.rb
Normal file
123
spec/ruby/core/struct/new_spec.rb
Normal file
|
@ -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
|
30
spec/ruby/core/struct/select_spec.rb
Normal file
30
spec/ruby/core/struct/select_spec.rb
Normal file
|
@ -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
|
7
spec/ruby/core/struct/shared/accessor.rb
Normal file
7
spec/ruby/core/struct/shared/accessor.rb
Normal file
|
@ -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
|
30
spec/ruby/core/struct/shared/equal_value.rb
Normal file
30
spec/ruby/core/struct/shared/equal_value.rb
Normal file
|
@ -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
|
5
spec/ruby/core/struct/shared/inspect.rb
Normal file
5
spec/ruby/core/struct/shared/inspect.rb
Normal file
|
@ -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
|
11
spec/ruby/core/struct/size_spec.rb
Normal file
11
spec/ruby/core/struct/size_spec.rb
Normal file
|
@ -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
|
43
spec/ruby/core/struct/struct_spec.rb
Normal file
43
spec/ruby/core/struct/struct_spec.rb
Normal file
|
@ -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
|
9
spec/ruby/core/struct/tms/cstime_spec.rb
Normal file
9
spec/ruby/core/struct/tms/cstime_spec.rb
Normal file
|
@ -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
|
9
spec/ruby/core/struct/tms/cutime_spec.rb
Normal file
9
spec/ruby/core/struct/tms/cutime_spec.rb
Normal file
|
@ -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
|
5
spec/ruby/core/struct/tms/element_reference_spec.rb
Normal file
5
spec/ruby/core/struct/tms/element_reference_spec.rb
Normal file
|
@ -0,0 +1,5 @@
|
|||
require File.expand_path('../../../../spec_helper', __FILE__)
|
||||
|
||||
describe "Struct::Tms.[]" do
|
||||
it "needs to be reviewed for spec completeness"
|
||||
end
|
5
spec/ruby/core/struct/tms/members_spec.rb
Normal file
5
spec/ruby/core/struct/tms/members_spec.rb
Normal file
|
@ -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
|
5
spec/ruby/core/struct/tms/new_spec.rb
Normal file
5
spec/ruby/core/struct/tms/new_spec.rb
Normal file
|
@ -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
|
9
spec/ruby/core/struct/tms/stime_spec.rb
Normal file
9
spec/ruby/core/struct/tms/stime_spec.rb
Normal file
|
@ -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
|
9
spec/ruby/core/struct/tms/utime_spec.rb
Normal file
9
spec/ruby/core/struct/tms/utime_spec.rb
Normal file
|
@ -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
|
12
spec/ruby/core/struct/to_a_spec.rb
Normal file
12
spec/ruby/core/struct/to_a_spec.rb
Normal file
|
@ -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
|
15
spec/ruby/core/struct/to_h_spec.rb
Normal file
15
spec/ruby/core/struct/to_h_spec.rb
Normal file
|
@ -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
|
12
spec/ruby/core/struct/to_s_spec.rb
Normal file
12
spec/ruby/core/struct/to_s_spec.rb
Normal file
|
@ -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
|
16
spec/ruby/core/struct/values_at_spec.rb
Normal file
16
spec/ruby/core/struct/values_at_spec.rb
Normal file
|
@ -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
|
11
spec/ruby/core/struct/values_spec.rb
Normal file
11
spec/ruby/core/struct/values_spec.rb
Normal file
|
@ -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
|
Loading…
Add table
Add a link
Reference in a new issue