Skip to content

Commit f24f1fd

Browse files
committed
Add specs for Integer conversion for Struct#deconstruct_keys and Data#deconstruct_keys
1 parent f8c065e commit f24f1fd

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

core/data/deconstruct_keys_spec.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,28 @@
8484
d.deconstruct_keys(nil).should == {x: 1, y: 2}
8585
end
8686

87+
it "tries to convert a key with #to_int if index is not a String nor a Symbol, but responds to #to_int" do
88+
klass = Data.define(:x, :y)
89+
d = klass.new(1, 2)
90+
91+
key = mock("to_int")
92+
key.should_receive(:to_int).and_return(1)
93+
94+
d.deconstruct_keys([key]).should == { key => 2 }
95+
end
96+
97+
it "raises a TypeError if the conversion with #to_int does not return an Integer" do
98+
klass = Data.define(:x, :y)
99+
d = klass.new(1, 2)
100+
101+
key = mock("to_int")
102+
key.should_receive(:to_int).and_return("not an Integer")
103+
104+
-> {
105+
d.deconstruct_keys([key])
106+
}.should raise_error(TypeError, /can't convert MockObject to Integer/)
107+
end
108+
87109
it "raises TypeError if index is not a String, a Symbol and not convertible to Integer " do
88110
klass = Data.define(:x, :y)
89111
d = klass.new(1, 2)

core/struct/deconstruct_keys_spec.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,28 @@
8080
obj.deconstruct_keys(nil).should == {x: 1, y: 2}
8181
end
8282

83+
it "tries to convert a key with #to_int if index is not a String nor a Symbol, but responds to #to_int" do
84+
struct = Struct.new(:x, :y)
85+
s = struct.new(1, 2)
86+
87+
key = mock("to_int")
88+
key.should_receive(:to_int).and_return(1)
89+
90+
s.deconstruct_keys([key]).should == { key => 2 }
91+
end
92+
93+
it "raises a TypeError if the conversion with #to_int does not return an Integer" do
94+
struct = Struct.new(:x, :y)
95+
s = struct.new(1, 2)
96+
97+
key = mock("to_int")
98+
key.should_receive(:to_int).and_return("not an Integer")
99+
100+
-> {
101+
s.deconstruct_keys([key])
102+
}.should raise_error(TypeError, /can't convert MockObject to Integer/)
103+
end
104+
83105
it "raises TypeError if index is not a String, a Symbol and not convertible to Integer" do
84106
struct = Struct.new(:x, :y)
85107
s = struct.new(1, 2)

0 commit comments

Comments
 (0)