diff options
Diffstat (limited to 'spec/ruby/core/struct')
21 files changed, 389 insertions, 105 deletions
diff --git a/spec/ruby/core/struct/constants_spec.rb b/spec/ruby/core/struct/constants_spec.rb new file mode 100644 index 0000000000..7e8af1a211 --- /dev/null +++ b/spec/ruby/core/struct/constants_spec.rb @@ -0,0 +1,13 @@ +require_relative '../../spec_helper' + +describe "Struct::Group" do + it "is no longer defined" do + Struct.should_not.const_defined?(:Group) + end +end + +describe "Struct::Passwd" do + it "is no longer defined" do + Struct.should_not.const_defined?(:Passwd) + end +end diff --git a/spec/ruby/core/struct/deconstruct_keys_spec.rb b/spec/ruby/core/struct/deconstruct_keys_spec.rb index 088803d028..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 @@ -40,6 +40,21 @@ describe "Struct#deconstruct_keys" do s.deconstruct_keys([0, 1, 2]).should == {0 => 10, 1 => 20, 2 => 30} s.deconstruct_keys([0, 1] ).should == {0 => 10, 1 => 20} s.deconstruct_keys([0] ).should == {0 => 10} + s.deconstruct_keys([-1] ).should == {-1 => 30} + end + + it "ignores incorrect position numbers" do + struct = Struct.new(:x, :y, :z) + s = struct.new(10, 20, 30) + + s.deconstruct_keys([0, 3]).should == {0 => 10} + end + + it "support mixing attribute names and argument position numbers" do + struct = Struct.new(:x, :y) + s = struct.new(1, 2) + + s.deconstruct_keys([0, :x]).should == {0 => 1, :x => 1} end it "returns an empty hash when there are more keys than attributes" do @@ -57,6 +72,14 @@ describe "Struct#deconstruct_keys" do s.deconstruct_keys([:x, :a]).should == {x: 1} end + it "returns at first not existing argument position number" do + struct = Struct.new(:x, :y) + s = struct.new(1, 2) + + s.deconstruct_keys([3, 0]).should == {} + s.deconstruct_keys([0, 3]).should == {0 => 1} + end + it "accepts nil argument and return all the attributes" do struct = Struct.new(:x, :y) obj = struct.new(1, 2) @@ -64,13 +87,44 @@ describe "Struct#deconstruct_keys" do obj.deconstruct_keys(nil).should == {x: 1, y: 2} end - it "raise TypeError if passed anything accept nil or array" do + it "tries to convert a key with #to_int if index is not a String nor a Symbol, but responds to #to_int" do + struct = Struct.new(:x, :y) + s = struct.new(1, 2) + + key = mock("to_int") + key.should_receive(:to_int).and_return(1) + + s.deconstruct_keys([key]).should == { key => 2 } + end + + it "raises a TypeError if the conversion with #to_int does not return an Integer" do + struct = Struct.new(:x, :y) + s = struct.new(1, 2) + + key = mock("to_int") + key.should_receive(:to_int).and_return("not an Integer") + + -> { + s.deconstruct_keys([key]) + }.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 + struct = Struct.new(:x, :y) + s = struct.new(1, 2) + + -> { + s.deconstruct_keys([0, []]) + }.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/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 6ba7b081a9..e5438ca09a 100644 --- a/spec/ruby/core/struct/element_set_spec.rb +++ b/spec/ruby/core/struct/element_set_spec.rb @@ -21,9 +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(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/fixtures/classes.rb b/spec/ruby/core/struct/fixtures/classes.rb index 6d620f9060..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) @@ -13,6 +14,12 @@ module StructClasses end end + class StructWithOverriddenName < Struct.new(:a) + def self.name + "A" + end + end + class SubclassX < Struct end @@ -23,4 +30,6 @@ module StructClasses super end end + + class StructSubclass < Struct; end end 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 e82289071a..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 @@ -40,4 +40,35 @@ describe "Struct#initialize" do it "can be overridden" do StructClasses::SubclassX.new(:y).new.key.should == :value end + + it "can be initialized with keyword arguments" do + positional_args = StructClasses::Ruby.new("3.2", "OS") + keyword_args = StructClasses::Ruby.new(version: "3.2", platform: "OS") + + 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 83e13597ba..657b06abc1 100644 --- a/spec/ruby/core/struct/inspect_spec.rb +++ b/spec/ruby/core/struct/inspect_spec.rb @@ -3,10 +3,5 @@ require_relative 'fixtures/classes' require_relative 'shared/inspect' describe "Struct#inspect" do - it "returns a string representation showing members and values" do - car = StructClasses::Car.new('Ford', 'Ranger') - car.inspect.should == '#<struct StructClasses::Car make="Ford", model="Ranger", year=nil>' - end - it_behaves_like :struct_inspect, :inspect 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 new file mode 100644 index 0000000000..42fcd3cc29 --- /dev/null +++ b/spec/ruby/core/struct/keyword_init_spec.rb @@ -0,0 +1,45 @@ +require_relative '../../spec_helper' +require_relative 'fixtures/classes' + +# See https://bugs.ruby-lang.org/issues/18008 +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 == 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 == false + end + + it "returns nil for a struct that did not explicitly specify keyword_init" do + struct = Struct.new(:arg) + 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 == 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 == true + + struct = Struct.new(:arg, keyword_init: "") + struct.keyword_init?.should == true + + struct = Struct.new(:arg, keyword_init: []) + struct.keyword_init?.should == true + + struct = Struct.new(:arg, keyword_init: {}) + struct.keyword_init?.should == true + end + + context "class inheriting Struct" do + it "isn't available in a subclass" do + StructClasses::StructSubclass.should_not.respond_to?(:keyword_init?) + end + end +end diff --git a/spec/ruby/core/struct/members_spec.rb b/spec/ruby/core/struct/members_spec.rb index 1f2ff950d9..1ff7b9387a 100644 --- a/spec/ruby/core/struct/members_spec.rb +++ b/spec/ruby/core/struct/members_spec.rb @@ -11,3 +11,15 @@ describe "Struct#members" do it_behaves_like :struct_accessor, :members end + +describe "StructClass#members" do + it "returns an array of attribute names" do + StructClasses::Car.members.should == [:make, :model, :year] + end + + context "class inheriting Struct" do + it "isn't available in a subclass" do + StructClasses::StructSubclass.should_not.respond_to?(:members) + end + end +end diff --git a/spec/ruby/core/struct/new_spec.rb b/spec/ruby/core/struct/new_spec.rb index 7b4a4f7980..b3ece2efed 100644 --- a/spec/ruby/core/struct/new_spec.rb +++ b/spec/ruby/core/struct/new_spec.rb @@ -6,6 +6,8 @@ describe "Struct.new" do struct = Struct.new('Animal', :name, :legs, :eyeballs) struct.should == Struct::Animal struct.name.should == "Struct::Animal" + ensure + Struct.send(:remove_const, :Animal) end it "overwrites previously defined constants with string as first argument" do @@ -19,6 +21,8 @@ describe "Struct.new" do second.should == Struct::Person first.members.should_not == second.members + ensure + Struct.send(:remove_const, :Person) end it "calls to_str on its first argument (constant name)" do @@ -27,70 +31,71 @@ describe "Struct.new" do struct = Struct.new(obj) struct.should == Struct::Foo struct.name.should == "Struct::Foo" + ensure + Struct.send(:remove_const, :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 + 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 + name = "r\xe9sum\xe9".dup.force_encoding(Encoding::ISO_8859_1).to_sym + struct = Struct.new(name) + struct.new("foo").send(name).should == "foo" + 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) - end - - ruby_version_is ""..."3.2" do - it "raises a TypeError or ArgumentError if passed a Hash with an unknown key" do - # CRuby < 3.2 raises ArgumentError: unknown keyword: :name, but that seems a bug: - # https://bugs.ruby-lang.org/issues/18632 - -> { Struct.new(:animal, { name: 'chris' }) }.should raise_error(StandardError) { |e| - [ArgumentError, TypeError].should.include?(e.class) - } - end + -> { 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 - ruby_version_is "3.2" do - it "raises a TypeError if passed a Hash with an unknown key" do - -> { Struct.new(:animal, { name: 'chris' }) }.should raise_error(TypeError) - end + it "raises a TypeError if passed a Hash with an unknown key" do + -> { Struct.new(:animal, { name: 'chris' }) }.should.raise(TypeError) + 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 @@ -111,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 @@ -119,6 +124,8 @@ describe "Struct.new" do it "creates a constant in subclass' namespace" do struct = StructClasses::Apple.new('Computer', :size) struct.should == StructClasses::Apple::Computer + ensure + StructClasses::Apple.send(:remove_const, :Computer) end it "creates an instance" do @@ -126,42 +133,56 @@ 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 - ruby_version_is ''...'3.1' do - it "passes a hash as a normal argument" do - type = Struct.new(:args) + it "accepts keyword arguments to initialize" do + type = Struct.new(:args) - obj = suppress_warning {type.new(keyword: :arg)} - obj2 = type.new(*[{keyword: :arg}]) + obj = type.new(args: 42) + obj2 = type.new(42) - obj.should == obj2 - obj.args.should == {keyword: :arg} - obj2.args.should == {keyword: :arg} - end + obj.should == obj2 + obj.args.should == 42 + obj2.args.should == 42 end - ruby_version_is '3.2' do - it "accepts keyword arguments to initialize" do - type = Struct.new(:args) + context "given positional and keyword arguments" do + it "treats keyword arguments as a positional parameter" do + type = Struct.new(:a, :b) + s = type.new("a", b: "b") + s.a.should == "a" + s.b.should == {b: "b"} + + type = Struct.new(:a, :b, :c) + s = type.new("a", b: "b", c: "c") + s.a.should == "a" + s.b.should == {b: "b", c: "c"} + s.c.should == nil + end - obj = type.new(args: 42) - obj2 = type.new(42) + it "ignores empty keyword arguments" do + type = Struct.new(:a, :b) + h = {} + s = type.new("a", **h) + + s.a.should == "a" + s.b.should == nil + end - obj.should == obj2 - obj.args.should == 42 - obj2.args.should == 42 + it "raises ArgumentError when all struct attribute values are specified" do + type = Struct.new(:a, :b) + -> { type.new("a", "b", c: "c") }.should.raise(ArgumentError, "struct size differs") end end end @@ -178,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 @@ -191,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/shared/inspect.rb b/spec/ruby/core/struct/shared/inspect.rb index 90594a5452..1a0fb6a6b2 100644 --- a/spec/ruby/core/struct/shared/inspect.rb +++ b/spec/ruby/core/struct/shared/inspect.rb @@ -1,5 +1,40 @@ 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 index 35abee461b..dfa1a809fe 100644 --- a/spec/ruby/core/struct/shared/select.rb +++ b/spec/ruby/core/struct/shared/select.rb @@ -4,7 +4,7 @@ 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) + -> { struct.send(@method, 1) { } }.should.raise(ArgumentError) end it "returns a new array of elements for which block is true" do @@ -14,13 +14,13 @@ describe :struct_select, shared: true do it "returns an instance of Array" do struct = StructClasses::Car.new("Ford", "Escort", "1995") - struct.send(@method) { true }.should be_an_instance_of(Array) + struct.send(@method) { true }.should.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) + struct.send(@method).should.instance_of?(Enumerator) end end end diff --git a/spec/ruby/core/struct/struct_spec.rb b/spec/ruby/core/struct/struct_spec.rb index 8817dc1a58..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 @@ -33,6 +33,13 @@ describe "Struct anonymous class instance methods" do car['model'].should == 'F150' car[1].should == 'F150' end + + it "writer methods raise a FrozenError on a frozen struct" do + car = StructClasses::Car.new('Ford', 'Ranger') + car.freeze + + -> { car.model = 'Escape' }.should.raise(FrozenError) + end end describe "Struct subclasses" do diff --git a/spec/ruby/core/struct/to_h_spec.rb b/spec/ruby/core/struct/to_h_spec.rb index bfb0af07ba..e0846ef268 100644 --- a/spec/ruby/core/struct/to_h_spec.rb +++ b/spec/ruby/core/struct/to_h_spec.rb @@ -21,20 +21,32 @@ describe "Struct#to_h" do h.should == { "make" => "ford", "model" => "ranger", "year" => "" } end + it "passes to a block each pair's key and value as separate arguments" do + s = StructClasses::Ruby.new('3.2.4', 'macos') + + ScratchPad.record [] + s.to_h { |k, v| ScratchPad << [k, v]; [k, v] } + ScratchPad.recorded.sort.should == [[:platform, 'macos'], [:version, '3.2.4']] + + ScratchPad.record [] + s.to_h { |*args| ScratchPad << args; [args[0], args[1]] } + ScratchPad.recorded.sort.should == [[:platform, 'macos'], [:version, '3.2.4']] + end + 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 @@ -50,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/values_at_spec.rb b/spec/ruby/core/struct/values_at_spec.rb index e7d287cba2..6aac5d96b3 100644 --- a/spec/ruby/core/struct/values_at_spec.rb +++ b/spec/ruby/core/struct/values_at_spec.rb @@ -1,16 +1,59 @@ require_relative '../../spec_helper' require_relative 'fixtures/classes' +# Should be synchronized with core/array/values_at_spec.rb describe "Struct#values_at" do - it "returns an array of values" do + before do clazz = Struct.new(:name, :director, :year) - movie = clazz.new('Sympathy for Mr. Vengeance', 'Chan-wook Park', 2002) - movie.values_at(0, 1).should == ['Sympathy for Mr. Vengeance', 'Chan-wook Park'] - movie.values_at(0..2).should == ['Sympathy for Mr. Vengeance', 'Chan-wook Park', 2002] + @movie = clazz.new('Sympathy for Mr. Vengeance', 'Chan-wook Park', 2002) + end + + context "when passed a list of Integers" do + it "returns an array containing each value given by one of integers" do + @movie.values_at(0, 1).should == ['Sympathy for Mr. Vengeance', 'Chan-wook Park'] + end + + it "raises IndexError if any of integers is out of range" do + -> { @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 + + context "when passed an integer Range" do + it "returns an array containing each value given by the elements of the range" do + @movie.values_at(0..2).should == ['Sympathy for Mr. Vengeance', 'Chan-wook Park', 2002] + end + + it "fills with nil values for range elements larger than the structure" do + @movie.values_at(0..3).should == ['Sympathy for Mr. Vengeance', 'Chan-wook Park', 2002, nil] + end + + it "raises RangeError if any element of the range is negative and out of range" do + -> { @movie.values_at(-4..3) }.should.raise(RangeError, "-4..3 out of range") + end + + it "supports endless Range" do + @movie.values_at(0..).should == ["Sympathy for Mr. Vengeance", "Chan-wook Park", 2002] + end + + it "supports beginningless Range" do + @movie.values_at(..2).should == ["Sympathy for Mr. Vengeance", "Chan-wook Park", 2002] + end + end + + it "supports multiple integer Ranges" do + @movie.values_at(0..2, 1..2).should == ['Sympathy for Mr. Vengeance', 'Chan-wook Park', 2002, 'Chan-wook Park', 2002] + end + + it "supports mixing integer Ranges and Integers" do + @movie.values_at(0..2, 2).should == ['Sympathy for Mr. Vengeance', 'Chan-wook Park', 2002, 2002] + end + + it "returns a new empty Array if no arguments given" do + @movie.values_at().should == [] end it "fails when passed unsupported types" do - car = StructClasses::Car.new('Ford', 'Ranger') - -> { car.values_at('make') }.should raise_error(TypeError) + -> { @movie.values_at('make') }.should.raise(TypeError, "no implicit conversion of String into Integer") end end |
