diff --git a/core/data/deconstruct_keys_spec.rb b/core/data/deconstruct_keys_spec.rb index df08652f24..df378f8b98 100644 --- a/core/data/deconstruct_keys_spec.rb +++ b/core/data/deconstruct_keys_spec.rb @@ -1,10 +1,11 @@ require_relative '../../spec_helper' require_relative 'fixtures/classes' -describe "Data#deconstruct" do +describe "Data#deconstruct_keys" do it "returns a hash of attributes" do klass = Data.define(:x, :y) d = klass.new(1, 2) + d.deconstruct_keys([:x, :y]).should == {x: 1, y: 2} end @@ -29,6 +30,7 @@ it "accepts string attribute names" do klass = Data.define(:x, :y) d = klass.new(1, 2) + d.deconstruct_keys(['x', 'y']).should == {'x' => 1, 'y' => 2} end @@ -58,6 +60,7 @@ it "returns an empty hash when there are more keys than attributes" do klass = Data.define(:x, :y) d = klass.new(1, 2) + d.deconstruct_keys([:x, :y, :x]).should == {} end diff --git a/core/struct/deconstruct_keys_spec.rb b/core/struct/deconstruct_keys_spec.rb index 1639a1abbb..e16b50f930 100644 --- a/core/struct/deconstruct_keys_spec.rb +++ b/core/struct/deconstruct_keys_spec.rb @@ -43,6 +43,13 @@ 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)