|
40 | 40 | s.deconstruct_keys([0, 1, 2]).should == {0 => 10, 1 => 20, 2 => 30}
|
41 | 41 | s.deconstruct_keys([0, 1] ).should == {0 => 10, 1 => 20}
|
42 | 42 | 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} |
43 | 51 | end
|
44 | 52 |
|
45 | 53 | it "returns an empty hash when there are more keys than attributes" do
|
|
57 | 65 | s.deconstruct_keys([:x, :a]).should == {x: 1}
|
58 | 66 | end
|
59 | 67 |
|
| 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 | + |
60 | 76 | it "accepts nil argument and return all the attributes" do
|
61 | 77 | struct = Struct.new(:x, :y)
|
62 | 78 | obj = struct.new(1, 2)
|
63 | 79 |
|
64 | 80 | obj.deconstruct_keys(nil).should == {x: 1, y: 2}
|
65 | 81 | end
|
66 | 82 |
|
| 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 | + |
67 | 92 | it "raise TypeError if passed anything except nil or array" do
|
68 | 93 | struct = Struct.new(:x, :y)
|
69 | 94 | s = struct.new(1, 2)
|
|
0 commit comments