Skip to content

Commit 3f8a989

Browse files
committed
Add missing specs for Struct class
1 parent 6f6793e commit 3f8a989

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

spec/ruby/core/struct/deconstruct_keys_spec.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,14 @@
4040
s.deconstruct_keys([0, 1, 2]).should == {0 => 10, 1 => 20, 2 => 30}
4141
s.deconstruct_keys([0, 1] ).should == {0 => 10, 1 => 20}
4242
s.deconstruct_keys([0] ).should == {0 => 10}
43+
s.deconstruct_keys([-1] ).should == {-1 => 30}
44+
end
45+
46+
it "support mixing attribute names and argument position numbers" do
47+
struct = Struct.new(:x, :y)
48+
s = struct.new(1, 2)
49+
50+
s.deconstruct_keys([0, :x]).should == {0 => 1, :x => 1}
4351
end
4452

4553
it "returns an empty hash when there are more keys than attributes" do
@@ -57,13 +65,30 @@
5765
s.deconstruct_keys([:x, :a]).should == {x: 1}
5866
end
5967

68+
it "returns at first not existing argument position number" do
69+
struct = Struct.new(:x, :y)
70+
s = struct.new(1, 2)
71+
72+
s.deconstruct_keys([3, 0]).should == {}
73+
s.deconstruct_keys([0, 3]).should == {0 => 1}
74+
end
75+
6076
it "accepts nil argument and return all the attributes" do
6177
struct = Struct.new(:x, :y)
6278
obj = struct.new(1, 2)
6379

6480
obj.deconstruct_keys(nil).should == {x: 1, y: 2}
6581
end
6682

83+
it "raises TypeError if index is not a String, a Symbol and not convertible to Integer " do
84+
struct = Struct.new(:x, :y)
85+
s = struct.new(1, 2)
86+
87+
-> {
88+
s.deconstruct_keys([0, []])
89+
}.should raise_error(TypeError, "no implicit conversion of Array into Integer")
90+
end
91+
6792
it "raise TypeError if passed anything except nil or array" do
6893
struct = Struct.new(:x, :y)
6994
s = struct.new(1, 2)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
fails:Struct#deconstruct_keys returns at first not existing argument position number
2+
fails:Struct#deconstruct_keys raises TypeError if index is not a String, a Symbol and not convertible to Integer

0 commit comments

Comments
 (0)