diff options
Diffstat (limited to 'spec/ruby/core/data/deconstruct_keys_spec.rb')
| -rw-r--r-- | spec/ruby/core/data/deconstruct_keys_spec.rb | 96 |
1 files changed, 38 insertions, 58 deletions
diff --git a/spec/ruby/core/data/deconstruct_keys_spec.rb b/spec/ruby/core/data/deconstruct_keys_spec.rb index df378f8b98..7e81f966ea 100644 --- a/spec/ruby/core/data/deconstruct_keys_spec.rb +++ b/spec/ruby/core/data/deconstruct_keys_spec.rb @@ -15,7 +15,7 @@ describe "Data#deconstruct_keys" do -> { d.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 @@ -34,29 +34,6 @@ describe "Data#deconstruct_keys" do d.deconstruct_keys(['x', 'y']).should == {'x' => 1, 'y' => 2} end - it "accepts argument position number as well but returns them as keys" do - klass = Data.define(:x, :y) - d = klass.new(1, 2) - - d.deconstruct_keys([0, 1]).should == {0 => 1, 1 => 2} - d.deconstruct_keys([0] ).should == {0 => 1} - d.deconstruct_keys([-1] ).should == {-1 => 2} - end - - it "ignores incorrect position numbers" do - klass = Data.define(:x, :y) - d = klass.new(1, 2) - - d.deconstruct_keys([0, 3]).should == {0 => 1} - end - - it "support mixing attribute names and argument position numbers" do - klass = Data.define(:x, :y) - d = klass.new(1, 2) - - d.deconstruct_keys([0, :x]).should == {0 => 1, :x => 1} - end - it "returns an empty hash when there are more keys than attributes" do klass = Data.define(:x, :y) d = klass.new(1, 2) @@ -72,14 +49,6 @@ describe "Data#deconstruct_keys" do d.deconstruct_keys([:x, :a]).should == {x: 1} end - it "returns at first not existing argument position number" do - klass = Data.define(:x, :y) - d = klass.new(1, 2) - - d.deconstruct_keys([3, 0]).should == {} - d.deconstruct_keys([0, 3]).should == {0 => 1} - end - it "accepts nil argument and return all the attributes" do klass = Data.define(:x, :y) d = klass.new(1, 2) @@ -87,44 +56,55 @@ describe "Data#deconstruct_keys" do d.deconstruct_keys(nil).should == {x: 1, y: 2} end - it "tries to convert a key with #to_int if index is not a String nor a Symbol, but responds to #to_int" do - klass = Data.define(:x, :y) - d = klass.new(1, 2) + ruby_version_is "4.0" do # https://bugs.ruby-lang.org/issues/21844 + it "tries to convert a key with #to_str if index is not a String nor a Symbol, but responds to #to_str" do + klass = Data.define(:x, :y) + d = klass.new(1, 2) - key = mock("to_int") - key.should_receive(:to_int).and_return(1) + key = mock("to_str") + key.should_receive(:to_str).and_return("y") - d.deconstruct_keys([key]).should == { key => 2 } - end + d.deconstruct_keys([key]).should == { "y" => 2 } + end - it "raises a TypeError if the conversion with #to_int does not return an Integer" do - klass = Data.define(:x, :y) - d = klass.new(1, 2) + it "raise an error on argument position number" do + klass = Data.define(:x, :y) + d = klass.new(1, 2) - key = mock("to_int") - key.should_receive(:to_int).and_return("not an Integer") + -> { + d.deconstruct_keys([0, 1]) + }.should.raise(TypeError, "0 is not a symbol nor a string") + end - -> { - d.deconstruct_keys([key]) - }.should raise_error(TypeError, /can't convert MockObject to Integer/) - end + it "raises a TypeError if the conversion with #to_str does not return a String" do + klass = Data.define(:x, :y) + d = klass.new(1, 2) - it "raises TypeError if index is not a String, a Symbol and not convertible to Integer " do - klass = Data.define(:x, :y) - d = klass.new(1, 2) + key = mock("to_str") + key.should_receive(:to_str).and_return(0) - -> { - d.deconstruct_keys([0, []]) - }.should raise_error(TypeError, "no implicit conversion of Array into Integer") + -> { + d.deconstruct_keys([key]) + }.should raise_consistent_error(TypeError, /can't convert MockObject into String/) + end + + it "raises TypeError if index is not a Symbol and not convertible to String" do + klass = Data.define(:x, :y) + d = klass.new(1, 2) + + -> { + d.deconstruct_keys([0, []]) + }.should.raise(TypeError, "0 is not a symbol nor a string") + end end it "raise TypeError if passed anything except nil or array" do klass = Data.define(:x, :y) d = klass.new(1, 2) - -> { d.deconstruct_keys('x') }.should raise_error(TypeError, /expected Array or nil/) - -> { d.deconstruct_keys(1) }.should raise_error(TypeError, /expected Array or nil/) - -> { d.deconstruct_keys(:x) }.should raise_error(TypeError, /expected Array or nil/) - -> { d.deconstruct_keys({}) }.should raise_error(TypeError, /expected Array or nil/) + -> { d.deconstruct_keys('x') }.should.raise(TypeError, /expected Array or nil/) + -> { d.deconstruct_keys(1) }.should.raise(TypeError, /expected Array or nil/) + -> { d.deconstruct_keys(:x) }.should.raise(TypeError, /expected Array or nil/) + -> { d.deconstruct_keys({}) }.should.raise(TypeError, /expected Array or nil/) end end |
