diff options
Diffstat (limited to 'spec/ruby/core/struct')
26 files changed, 179 insertions, 187 deletions
diff --git a/spec/ruby/core/struct/deconstruct_keys_spec.rb b/spec/ruby/core/struct/deconstruct_keys_spec.rb index e16b50f930..590e1ab40b 100644 --- a/spec/ruby/core/struct/deconstruct_keys_spec.rb +++ b/spec/ruby/core/struct/deconstruct_keys_spec.rb @@ -14,7 +14,7 @@ describe "Struct#deconstruct_keys" do -> { obj.deconstruct_keys - }.should raise_error(ArgumentError, /wrong number of arguments \(given 0, expected 1\)/) + }.should.raise(ArgumentError, /wrong number of arguments \(given 0, expected 1\)/) end it "returns only specified keys" do @@ -106,7 +106,7 @@ describe "Struct#deconstruct_keys" do -> { s.deconstruct_keys([key]) - }.should raise_error(TypeError, /can't convert MockObject to Integer/) + }.should raise_consistent_error(TypeError, /can't convert MockObject into Integer/) end it "raises TypeError if index is not a String, a Symbol and not convertible to Integer" do @@ -115,16 +115,16 @@ describe "Struct#deconstruct_keys" do -> { s.deconstruct_keys([0, []]) - }.should raise_error(TypeError, "no implicit conversion of Array into Integer") + }.should.raise(TypeError, "no implicit conversion of Array into Integer") end it "raise TypeError if passed anything except nil or array" do struct = Struct.new(:x, :y) s = struct.new(1, 2) - -> { s.deconstruct_keys('x') }.should raise_error(TypeError, /expected Array or nil/) - -> { s.deconstruct_keys(1) }.should raise_error(TypeError, /expected Array or nil/) - -> { s.deconstruct_keys(:x) }.should raise_error(TypeError, /expected Array or nil/) - -> { s.deconstruct_keys({}) }.should raise_error(TypeError, /expected Array or nil/) + -> { s.deconstruct_keys('x') }.should.raise(TypeError, /expected Array or nil/) + -> { s.deconstruct_keys(1) }.should.raise(TypeError, /expected Array or nil/) + -> { s.deconstruct_keys(:x) }.should.raise(TypeError, /expected Array or nil/) + -> { s.deconstruct_keys({}) }.should.raise(TypeError, /expected Array or nil/) end end diff --git a/spec/ruby/core/struct/deconstruct_spec.rb b/spec/ruby/core/struct/deconstruct_spec.rb index 32d4f6bac4..aad82ac2ba 100644 --- a/spec/ruby/core/struct/deconstruct_spec.rb +++ b/spec/ruby/core/struct/deconstruct_spec.rb @@ -1,10 +1,7 @@ require_relative '../../spec_helper' describe "Struct#deconstruct" do - it "returns an array of attribute values" do - struct = Struct.new(:x, :y) - s = struct.new(1, 2) - - s.deconstruct.should == [1, 2] + it "is an alias of Struct#to_a" do + Struct.instance_method(:deconstruct).should == Struct.instance_method(:to_a) end end diff --git a/spec/ruby/core/struct/dig_spec.rb b/spec/ruby/core/struct/dig_spec.rb index 93a52dbbe1..52e4d1dd9a 100644 --- a/spec/ruby/core/struct/dig_spec.rb +++ b/spec/ruby/core/struct/dig_spec.rb @@ -32,11 +32,11 @@ describe "Struct#dig" do instance = @klass.new(1) -> { instance.dig(:a, 3) - }.should raise_error(TypeError) + }.should.raise(TypeError) end it "raises an ArgumentError if no arguments provided" do - -> { @instance.dig }.should raise_error(ArgumentError) + -> { @instance.dig }.should.raise(ArgumentError) end it "calls #dig on any intermediate step with the rest of the sequence as arguments" do diff --git a/spec/ruby/core/struct/each_pair_spec.rb b/spec/ruby/core/struct/each_pair_spec.rb index 1230ca9026..db146c81e9 100644 --- a/spec/ruby/core/struct/each_pair_spec.rb +++ b/spec/ruby/core/struct/each_pair_spec.rb @@ -21,11 +21,11 @@ describe "Struct#each_pair" do end it "returns self if passed a block" do - @car.each_pair {}.should equal(@car) + @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) + @car.each_pair.should.instance_of?(Enumerator) end it_behaves_like :struct_accessor, :each_pair diff --git a/spec/ruby/core/struct/each_spec.rb b/spec/ruby/core/struct/each_spec.rb index 41c0fbd4d0..4fbdfee02a 100644 --- a/spec/ruby/core/struct/each_spec.rb +++ b/spec/ruby/core/struct/each_spec.rb @@ -19,7 +19,7 @@ describe "Struct#each" do it "returns an Enumerator if not passed a block" do car = StructClasses::Car.new('Ford', 'Ranger') - car.each.should be_an_instance_of(Enumerator) + car.each.should.instance_of?(Enumerator) end it_behaves_like :struct_accessor, :each diff --git a/spec/ruby/core/struct/element_reference_spec.rb b/spec/ruby/core/struct/element_reference_spec.rb index 0f6d547f66..b94f3aae8c 100644 --- a/spec/ruby/core/struct/element_reference_spec.rb +++ b/spec/ruby/core/struct/element_reference_spec.rb @@ -3,7 +3,7 @@ require_relative 'fixtures/classes' describe "Struct[]" do it "is a synonym for new" do - StructClasses::Ruby['2.0', 'i686'].should be_kind_of(StructClasses::Ruby) + StructClasses::Ruby['2.0', 'i686'].should.is_a?(StructClasses::Ruby) end end @@ -26,20 +26,20 @@ describe "Struct#[]" do it "fails when it does not know about the requested attribute" do car = StructClasses::Car.new('Ford', 'Ranger') - -> { car[3] }.should raise_error(IndexError) - -> { car[-4] }.should raise_error(IndexError) - -> { car[:body] }.should raise_error(NameError) - -> { car['wheels'] }.should raise_error(NameError) + -> { car[3] }.should.raise(IndexError) + -> { car[-4] }.should.raise(IndexError) + -> { car[:body] }.should.raise(NameError) + -> { car['wheels'] }.should.raise(NameError) end it "fails if passed too many arguments" do car = StructClasses::Car.new('Ford', 'Ranger') - -> { car[:make, :model] }.should raise_error(ArgumentError) + -> { car[:make, :model] }.should.raise(ArgumentError) end it "fails if not passed a string, symbol, or integer" do car = StructClasses::Car.new('Ford', 'Ranger') - -> { car[Object.new] }.should raise_error(TypeError) + -> { car[Object.new] }.should.raise(TypeError) end it "returns attribute names that contain hyphens" do diff --git a/spec/ruby/core/struct/element_set_spec.rb b/spec/ruby/core/struct/element_set_spec.rb index 0a0e34a5ee..e5438ca09a 100644 --- a/spec/ruby/core/struct/element_set_spec.rb +++ b/spec/ruby/core/struct/element_set_spec.rb @@ -21,16 +21,16 @@ describe "Struct#[]=" do it "fails when trying to assign attributes which don't exist" do car = StructClasses::Car.new('Ford', 'Ranger') - -> { car[:something] = true }.should raise_error(NameError) - -> { car[3] = true }.should raise_error(IndexError) - -> { car[-4] = true }.should raise_error(IndexError) - -> { car[Object.new] = true }.should raise_error(TypeError) + -> { car[:something] = true }.should.raise(NameError) + -> { car[3] = true }.should.raise(IndexError) + -> { car[-4] = true }.should.raise(IndexError) + -> { car[Object.new] = true }.should.raise(TypeError) end it "raises a FrozenError on a frozen struct" do car = StructClasses::Car.new('Ford', 'Ranger') car.freeze - -> { car[:model] = 'Escape' }.should raise_error(FrozenError) + -> { car[:model] = 'Escape' }.should.raise(FrozenError) end end diff --git a/spec/ruby/core/struct/eql_spec.rb b/spec/ruby/core/struct/eql_spec.rb index c864b2b943..327c927278 100644 --- a/spec/ruby/core/struct/eql_spec.rb +++ b/spec/ruby/core/struct/eql_spec.rb @@ -8,6 +8,6 @@ describe "Struct#eql?" do 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.should_not eql(similar_car) + car.should_not.eql?(similar_car) end end diff --git a/spec/ruby/core/struct/filter_spec.rb b/spec/ruby/core/struct/filter_spec.rb index 0ccd8ad6b2..5d11f47e6b 100644 --- a/spec/ruby/core/struct/filter_spec.rb +++ b/spec/ruby/core/struct/filter_spec.rb @@ -1,10 +1,8 @@ require_relative '../../spec_helper' -require_relative 'shared/select' -require_relative 'shared/accessor' -require_relative '../enumerable/shared/enumeratorized' +require_relative 'fixtures/classes' describe "Struct#filter" do - it_behaves_like :struct_select, :filter - it_behaves_like :struct_accessor, :filter - it_behaves_like :enumeratorized_with_origin_size, :filter, Struct.new(:foo).new + it "is an alias of Struct#select" do + StructClasses::Car.instance_method(:filter).should == StructClasses::Car.instance_method(:select) + end end diff --git a/spec/ruby/core/struct/fixtures/classes.rb b/spec/ruby/core/struct/fixtures/classes.rb index 7b80b814ef..675d403abd 100644 --- a/spec/ruby/core/struct/fixtures/classes.rb +++ b/spec/ruby/core/struct/fixtures/classes.rb @@ -3,6 +3,7 @@ module StructClasses class Apple < Struct; end Ruby = Struct.new(:version, :platform) + Single = Struct.new(:value) Car = Struct.new(:make, :model, :year) diff --git a/spec/ruby/core/struct/hash_spec.rb b/spec/ruby/core/struct/hash_spec.rb index 53361eb7a9..750387b326 100644 --- a/spec/ruby/core/struct/hash_spec.rb +++ b/spec/ruby/core/struct/hash_spec.rb @@ -8,14 +8,14 @@ describe "Struct#hash" 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(Integer) + stc.hash.should.is_a?(Integer) 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.should.eql?(similar_car) car.hash.should == similar_car.hash end diff --git a/spec/ruby/core/struct/initialize_spec.rb b/spec/ruby/core/struct/initialize_spec.rb index 06055594d5..c824f52e13 100644 --- a/spec/ruby/core/struct/initialize_spec.rb +++ b/spec/ruby/core/struct/initialize_spec.rb @@ -4,7 +4,7 @@ require_relative 'fixtures/classes' describe "Struct#initialize" do it "is private" do - StructClasses::Car.should have_private_instance_method(:initialize) + StructClasses::Car.private_instance_methods(true).should.include?(:initialize) end it 'allows valid Ruby method names for members' do @@ -48,4 +48,27 @@ describe "Struct#initialize" do positional_args.version.should == keyword_args.version positional_args.platform.should == keyword_args.platform end + + it "accepts positional arguments with empty keyword arguments" do + data = StructClasses::Single.new(42, **{}) + + data.value.should == 42 + + data = StructClasses::Ruby.new("3.2", "OS", **{}) + + data.version.should == "3.2" + data.platform.should == "OS" + end + + it "can be called via delegated ... from a prepended module" do + wrapper = Module.new do + def initialize(...) + super(...) + end + end + + klass = Class.new(Struct.new(:a)) { prepend wrapper } + s = klass.new("x") + s.a.should == "x" + end end diff --git a/spec/ruby/core/struct/inspect_spec.rb b/spec/ruby/core/struct/inspect_spec.rb index 657b06abc1..13fd0bef41 100644 --- a/spec/ruby/core/struct/inspect_spec.rb +++ b/spec/ruby/core/struct/inspect_spec.rb @@ -1,7 +1,8 @@ require_relative '../../spec_helper' require_relative 'fixtures/classes' -require_relative 'shared/inspect' describe "Struct#inspect" do - it_behaves_like :struct_inspect, :inspect + it "is an alias of Struct#to_s" do + StructClasses::Car.instance_method(:inspect).should == StructClasses::Car.instance_method(:to_s) + end end diff --git a/spec/ruby/core/struct/instance_variable_get_spec.rb b/spec/ruby/core/struct/instance_variable_get_spec.rb index e4a3ea87dc..e3555cd246 100644 --- a/spec/ruby/core/struct/instance_variable_get_spec.rb +++ b/spec/ruby/core/struct/instance_variable_get_spec.rb @@ -4,7 +4,7 @@ require_relative 'fixtures/classes' describe "Struct#instance_variable_get" do it "returns nil for attributes" do car = StructClasses::Car.new("Hugo", "Foo", "1972") - car.instance_variable_get(:@make).should be_nil + car.instance_variable_get(:@make).should == nil end it "returns a user value for variables with the same name as attributes" do diff --git a/spec/ruby/core/struct/keyword_init_spec.rb b/spec/ruby/core/struct/keyword_init_spec.rb index 536b82041a..42fcd3cc29 100644 --- a/spec/ruby/core/struct/keyword_init_spec.rb +++ b/spec/ruby/core/struct/keyword_init_spec.rb @@ -5,36 +5,36 @@ require_relative 'fixtures/classes' describe "StructClass#keyword_init?" do it "returns true for a struct that accepts keyword arguments to initialize" do struct = Struct.new(:arg, keyword_init: true) - struct.keyword_init?.should be_true + struct.keyword_init?.should == true end it "returns false for a struct that does not accept keyword arguments to initialize" do struct = Struct.new(:arg, keyword_init: false) - struct.keyword_init?.should be_false + struct.keyword_init?.should == false end it "returns nil for a struct that did not explicitly specify keyword_init" do struct = Struct.new(:arg) - struct.keyword_init?.should be_nil + struct.keyword_init?.should == nil end it "returns nil for a struct that does specify keyword_init to be nil" do struct = Struct.new(:arg, keyword_init: nil) - struct.keyword_init?.should be_nil + struct.keyword_init?.should == nil end it "returns true for any truthy value, not just for true" do struct = Struct.new(:arg, keyword_init: 1) - struct.keyword_init?.should be_true + struct.keyword_init?.should == true struct = Struct.new(:arg, keyword_init: "") - struct.keyword_init?.should be_true + struct.keyword_init?.should == true struct = Struct.new(:arg, keyword_init: []) - struct.keyword_init?.should be_true + struct.keyword_init?.should == true struct = Struct.new(:arg, keyword_init: {}) - struct.keyword_init?.should be_true + struct.keyword_init?.should == true end context "class inheriting Struct" do diff --git a/spec/ruby/core/struct/length_spec.rb b/spec/ruby/core/struct/length_spec.rb index 1143676122..8d48f4118d 100644 --- a/spec/ruby/core/struct/length_spec.rb +++ b/spec/ruby/core/struct/length_spec.rb @@ -1,12 +1,8 @@ require_relative '../../spec_helper' require_relative 'fixtures/classes' -require_relative 'shared/accessor' 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 + it "is an alias of Struct#size" do + StructClasses::Car.instance_method(:length).should == StructClasses::Car.instance_method(:size) end - - it_behaves_like :struct_accessor, :length end diff --git a/spec/ruby/core/struct/new_spec.rb b/spec/ruby/core/struct/new_spec.rb index 1d35de7b87..b3ece2efed 100644 --- a/spec/ruby/core/struct/new_spec.rb +++ b/spec/ruby/core/struct/new_spec.rb @@ -38,19 +38,19 @@ describe "Struct.new" do 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 + struct.should.is_a?(Class) + struct.name.should == nil end it "creates a new anonymous class with symbol arguments" do struct = Struct.new(:make, :model) - struct.should be_kind_of(Class) + struct.should.is_a?(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 + Struct.const_defined?("Animal2").should == false end it "allows non-ASCII member name" do @@ -60,50 +60,42 @@ describe "Struct.new" do end it "fails with invalid constant name as first argument" do - -> { Struct.new('animal', :name, :legs, :eyeballs) }.should raise_error(NameError) + -> { Struct.new('animal', :name, :legs, :eyeballs) }.should.raise(NameError) end it "raises a TypeError if object doesn't respond to to_sym" do - -> { Struct.new(:animal, mock('giraffe')) }.should raise_error(TypeError) - -> { Struct.new(:animal, 1.0) }.should raise_error(TypeError) - -> { Struct.new(:animal, Time.now) }.should raise_error(TypeError) - -> { Struct.new(:animal, Class) }.should raise_error(TypeError) - -> { Struct.new(:animal, nil) }.should raise_error(TypeError) - -> { Struct.new(:animal, true) }.should raise_error(TypeError) - -> { Struct.new(:animal, ['chris', 'evan']) }.should raise_error(TypeError) + -> { Struct.new(:animal, mock('giraffe')) }.should.raise(TypeError) + -> { Struct.new(:animal, 1.0) }.should.raise(TypeError) + -> { Struct.new(:animal, Time.now) }.should.raise(TypeError) + -> { Struct.new(:animal, Class) }.should.raise(TypeError) + -> { Struct.new(:animal, nil) }.should.raise(TypeError) + -> { Struct.new(:animal, true) }.should.raise(TypeError) + -> { Struct.new(:animal, ['chris', 'evan']) }.should.raise(TypeError) end it "raises a TypeError if passed a Hash with an unknown key" do - -> { Struct.new(:animal, { name: 'chris' }) }.should raise_error(TypeError) + -> { Struct.new(:animal, { name: 'chris' }) }.should.raise(TypeError) end - ruby_version_is ""..."3.3" do - it "raises ArgumentError if not provided any arguments" do - -> { Struct.new }.should raise_error(ArgumentError) - end - end - - ruby_version_is "3.3" do - it "works when not provided any arguments" do - c = Struct.new - c.should be_kind_of(Class) - c.superclass.should == Struct - end + it "works when not provided any arguments" do + c = Struct.new + c.should.is_a?(Class) + c.superclass.should == Struct end it "raises ArgumentError when there is a duplicate member" do - -> { Struct.new(:foo, :foo) }.should raise_error(ArgumentError, "duplicate member: foo") + -> { Struct.new(:foo, :foo) }.should.raise(ArgumentError, "duplicate member: foo") end it "raises a TypeError if object is not a Symbol" do obj = mock(':ruby') def obj.to_sym() :ruby end - -> { Struct.new(:animal, obj) }.should raise_error(TypeError) + -> { Struct.new(:animal, obj) }.should.raise(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) + klass.instance_variables.should.include?(:@something_else) end context "with a block" do @@ -124,7 +116,7 @@ describe "Struct.new" do klass = Struct.new(:attr) do |block_parameter| given = block_parameter end - klass.should equal(given) + klass.should.equal?(given) end end @@ -141,17 +133,17 @@ describe "Struct.new" do end it "creates reader methods" do - StructClasses::Ruby.new.should have_method(:version) - StructClasses::Ruby.new.should have_method(:platform) + StructClasses::Ruby.new.should.respond_to?(:version) + StructClasses::Ruby.new.should.respond_to?(:platform) end it "creates writer methods" do - StructClasses::Ruby.new.should have_method(:version=) - StructClasses::Ruby.new.should have_method(:platform=) + StructClasses::Ruby.new.should.respond_to?(:version=) + StructClasses::Ruby.new.should.respond_to?(:platform=) end it "fails with too many arguments" do - -> { StructClasses::Ruby.new('2.0', 'i686', true) }.should raise_error(ArgumentError) + -> { StructClasses::Ruby.new('2.0', 'i686', true) }.should.raise(ArgumentError) end it "accepts keyword arguments to initialize" do @@ -190,7 +182,7 @@ describe "Struct.new" do it "raises ArgumentError when all struct attribute values are specified" do type = Struct.new(:a, :b) - -> { type.new("a", "b", c: "c") }.should raise_error(ArgumentError, "struct size differs") + -> { type.new("a", "b", c: "c") }.should.raise(ArgumentError, "struct size differs") end end end @@ -207,7 +199,7 @@ describe "Struct.new" do end it "raises when there is a duplicate member" do - -> { Struct.new(:foo, :foo, keyword_init: true) }.should raise_error(ArgumentError, "duplicate member: foo") + -> { Struct.new(:foo, :foo, keyword_init: true) }.should.raise(ArgumentError, "duplicate member: foo") end describe "new class instantiation" do @@ -220,31 +212,31 @@ describe "Struct.new" do it "allows missing arguments" do obj = @struct_with_kwa.new(name: "elefant") obj.name.should == "elefant" - obj.legs.should be_nil + obj.legs.should == nil end it "allows no arguments" do obj = @struct_with_kwa.new - obj.name.should be_nil - obj.legs.should be_nil + obj.name.should == nil + obj.legs.should == nil end it "raises ArgumentError when passed not declared keyword argument" do -> { @struct_with_kwa.new(name: "elefant", legs: 4, foo: "bar") - }.should raise_error(ArgumentError, /unknown keywords: foo/) + }.should.raise(ArgumentError, /unknown keywords: foo/) end it "raises ArgumentError when passed a list of arguments" do -> { @struct_with_kwa.new("elefant", 4) - }.should raise_error(ArgumentError, /wrong number of arguments/) + }.should.raise(ArgumentError, /wrong number of arguments/) end it "raises ArgumentError when passed a single non-hash argument" do -> { @struct_with_kwa.new("elefant") - }.should raise_error(ArgumentError, /wrong number of arguments/) + }.should.raise(ArgumentError, /wrong number of arguments/) end end end diff --git a/spec/ruby/core/struct/select_spec.rb b/spec/ruby/core/struct/select_spec.rb index ee846ec45f..5f26a177eb 100644 --- a/spec/ruby/core/struct/select_spec.rb +++ b/spec/ruby/core/struct/select_spec.rb @@ -1,10 +1,31 @@ require_relative '../../spec_helper' -require_relative 'shared/select' +require_relative 'fixtures/classes' require_relative 'shared/accessor' require_relative '../enumerable/shared/enumeratorized' describe "Struct#select" do - it_behaves_like :struct_select, :select it_behaves_like :struct_accessor, :select it_behaves_like :enumeratorized_with_origin_size, :select, Struct.new(:foo).new + + it "raises an ArgumentError if given any non-block arguments" do + struct = StructClasses::Car.new + -> { struct.select(1) { } }.should.raise(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.instance_of?(Array) + end + + describe "without block" do + it "returns an instance of Enumerator" do + struct = Struct.new(:foo).new + struct.select.should.instance_of?(Enumerator) + end + end end diff --git a/spec/ruby/core/struct/shared/inspect.rb b/spec/ruby/core/struct/shared/inspect.rb deleted file mode 100644 index 1a0fb6a6b2..0000000000 --- a/spec/ruby/core/struct/shared/inspect.rb +++ /dev/null @@ -1,40 +0,0 @@ -describe :struct_inspect, shared: true do - it "returns a string representation showing members and values" do - car = StructClasses::Car.new('Ford', 'Ranger') - car.send(@method).should == '#<struct StructClasses::Car make="Ford", model="Ranger", year=nil>' - end - - it "returns a string representation without the class name for anonymous structs" do - Struct.new(:a).new("").send(@method).should == '#<struct a="">' - end - - it "returns a string representation without the class name for structs nested in anonymous classes" do - c = Class.new - c.class_eval <<~DOC - class Foo < Struct.new(:a); end - DOC - - c::Foo.new("").send(@method).should == '#<struct a="">' - end - - it "returns a string representation without the class name for structs nested in anonymous modules" do - m = Module.new - m.module_eval <<~DOC - class Foo < Struct.new(:a); end - DOC - - m::Foo.new("").send(@method).should == '#<struct a="">' - end - - it "does not call #name method" do - struct = StructClasses::StructWithOverriddenName.new("") - struct.send(@method).should == '#<struct StructClasses::StructWithOverriddenName a="">' - end - - it "does not call #name method when struct is anonymous" do - struct = Struct.new(:a) - def struct.name; "A"; end - - struct.new("").send(@method).should == '#<struct a="">' - end -end diff --git a/spec/ruby/core/struct/shared/select.rb b/spec/ruby/core/struct/shared/select.rb deleted file mode 100644 index 35abee461b..0000000000 --- a/spec/ruby/core/struct/shared/select.rb +++ /dev/null @@ -1,26 +0,0 @@ -require_relative '../../../spec_helper' -require_relative '../fixtures/classes' - -describe :struct_select, shared: true do - it "raises an ArgumentError if given any non-block arguments" do - struct = StructClasses::Car.new - -> { struct.send(@method, 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.send(@method) { |i| i == "2000" }.should == [ "2000" ] - end - - it "returns an instance of Array" do - struct = StructClasses::Car.new("Ford", "Escort", "1995") - struct.send(@method) { 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.send(@method).should be_an_instance_of(Enumerator) - end - end -end diff --git a/spec/ruby/core/struct/size_spec.rb b/spec/ruby/core/struct/size_spec.rb index 09f260cf20..5f07320bb9 100644 --- a/spec/ruby/core/struct/size_spec.rb +++ b/spec/ruby/core/struct/size_spec.rb @@ -3,8 +3,9 @@ require_relative 'fixtures/classes' require_relative 'shared/accessor' describe "Struct#size" do - it "is a synonym for length" do - StructClasses::Car.new.size.should == StructClasses::Car.new.length + 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, :size diff --git a/spec/ruby/core/struct/struct_spec.rb b/spec/ruby/core/struct/struct_spec.rb index 1b6a4488ce..9fab9c0629 100644 --- a/spec/ruby/core/struct/struct_spec.rb +++ b/spec/ruby/core/struct/struct_spec.rb @@ -21,7 +21,7 @@ describe "Struct anonymous class instance methods" do it "reader method should not interfere with undefined methods" do car = StructClasses::Car.new('Ford', 'Ranger') - -> { car.something_weird }.should raise_error(NoMethodError) + -> { car.something_weird }.should.raise(NoMethodError) end it "writer method be a synonym for []=" do @@ -38,7 +38,7 @@ describe "Struct anonymous class instance methods" do car = StructClasses::Car.new('Ford', 'Ranger') car.freeze - -> { car.model = 'Escape' }.should raise_error(FrozenError) + -> { car.model = 'Escape' }.should.raise(FrozenError) end end diff --git a/spec/ruby/core/struct/to_h_spec.rb b/spec/ruby/core/struct/to_h_spec.rb index 861ce3f49d..e0846ef268 100644 --- a/spec/ruby/core/struct/to_h_spec.rb +++ b/spec/ruby/core/struct/to_h_spec.rb @@ -36,17 +36,17 @@ describe "Struct#to_h" do it "raises ArgumentError if block returns longer or shorter array" do -> do StructClasses::Car.new.to_h { |k, v| [k.to_s, "#{v}".downcase, 1] } - end.should raise_error(ArgumentError, /element has wrong array length/) + end.should.raise(ArgumentError, /element has wrong array length/) -> do StructClasses::Car.new.to_h { |k, v| [k] } - end.should raise_error(ArgumentError, /element has wrong array length/) + end.should.raise(ArgumentError, /element has wrong array length/) end it "raises TypeError if block returns something other than Array" do -> do StructClasses::Car.new.to_h { |k, v| "not-array" } - end.should raise_error(TypeError, /wrong element type String/) + end.should.raise(TypeError, /wrong element type String/) end it "coerces returned pair to Array with #to_ary" do @@ -62,7 +62,7 @@ describe "Struct#to_h" do -> do StructClasses::Car.new.to_h { |k| x } - end.should raise_error(TypeError, /wrong element type MockObject/) + end.should.raise(TypeError, /wrong element type MockObject/) end end end diff --git a/spec/ruby/core/struct/to_s_spec.rb b/spec/ruby/core/struct/to_s_spec.rb index 94c672d3d5..9648b8af9b 100644 --- a/spec/ruby/core/struct/to_s_spec.rb +++ b/spec/ruby/core/struct/to_s_spec.rb @@ -1,12 +1,43 @@ require_relative '../../spec_helper' require_relative 'fixtures/classes' -require_relative 'shared/inspect' describe "Struct#to_s" do - it "is a synonym for inspect" do + it "returns a string representation showing members and values" do car = StructClasses::Car.new('Ford', 'Ranger') - car.inspect.should == car.to_s + car.to_s.should == '#<struct StructClasses::Car make="Ford", model="Ranger", year=nil>' end - it_behaves_like :struct_inspect, :to_s + it "returns a string representation without the class name for anonymous structs" do + Struct.new(:a).new("").to_s.should == '#<struct a="">' + end + + it "returns a string representation without the class name for structs nested in anonymous classes" do + c = Class.new + c.class_eval <<~DOC + class Foo < Struct.new(:a); end + DOC + + c::Foo.new("").to_s.should == '#<struct a="">' + end + + it "returns a string representation without the class name for structs nested in anonymous modules" do + m = Module.new + m.module_eval <<~DOC + class Foo < Struct.new(:a); end + DOC + + m::Foo.new("").to_s.should == '#<struct a="">' + end + + it "does not call #name method" do + struct = StructClasses::StructWithOverriddenName.new("") + struct.to_s.should == '#<struct StructClasses::StructWithOverriddenName a="">' + end + + it "does not call #name method when struct is anonymous" do + struct = Struct.new(:a) + def struct.name; "A"; end + + struct.new("").to_s.should == '#<struct a="">' + end end diff --git a/spec/ruby/core/struct/values_at_spec.rb b/spec/ruby/core/struct/values_at_spec.rb index 5e5a496600..6aac5d96b3 100644 --- a/spec/ruby/core/struct/values_at_spec.rb +++ b/spec/ruby/core/struct/values_at_spec.rb @@ -14,8 +14,8 @@ describe "Struct#values_at" do end it "raises IndexError if any of integers is out of range" do - -> { @movie.values_at(3) }.should raise_error(IndexError, "offset 3 too large for struct(size:3)") - -> { @movie.values_at(-4) }.should raise_error(IndexError, "offset -4 too small for struct(size:3)") + -> { @movie.values_at(3) }.should.raise(IndexError, "offset 3 too large for struct(size:3)") + -> { @movie.values_at(-4) }.should.raise(IndexError, "offset -4 too small for struct(size:3)") end end @@ -29,7 +29,7 @@ describe "Struct#values_at" do end it "raises RangeError if any element of the range is negative and out of range" do - -> { @movie.values_at(-4..3) }.should raise_error(RangeError, "-4..3 out of range") + -> { @movie.values_at(-4..3) }.should.raise(RangeError, "-4..3 out of range") end it "supports endless Range" do @@ -54,6 +54,6 @@ describe "Struct#values_at" do end it "fails when passed unsupported types" do - -> { @movie.values_at('make') }.should raise_error(TypeError, "no implicit conversion of String into Integer") + -> { @movie.values_at('make') }.should.raise(TypeError, "no implicit conversion of String into Integer") end end diff --git a/spec/ruby/core/struct/values_spec.rb b/spec/ruby/core/struct/values_spec.rb index b2d11725b9..16583253d7 100644 --- a/spec/ruby/core/struct/values_spec.rb +++ b/spec/ruby/core/struct/values_spec.rb @@ -2,10 +2,7 @@ require_relative '../../spec_helper' require_relative 'fixtures/classes' 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 + it "is an alias of Struct#to_a" do + StructClasses::Car.instance_method(:values).should == StructClasses::Car.instance_method(:to_a) end end |
