|
| 1 | +require_relative '../../spec_helper' |
| 2 | +require_relative 'fixtures/classes' |
| 3 | + |
| 4 | +ruby_version_is "3.2" do |
| 5 | + describe "Data#deconstruct" do |
| 6 | + it "returns a hash of attributes" do |
| 7 | + klass = Data.define(:x, :y) |
| 8 | + d = klass.new(1, 2) |
| 9 | + d.deconstruct_keys([:x, :y]).should == {x: 1, y: 2} |
| 10 | + end |
| 11 | + |
| 12 | + it "requires one argument" do |
| 13 | + klass = Data.define(:x, :y) |
| 14 | + d = klass.new(1, 2) |
| 15 | + |
| 16 | + -> { |
| 17 | + d.deconstruct_keys |
| 18 | + }.should raise_error(ArgumentError, /wrong number of arguments \(given 0, expected 1\)/) |
| 19 | + end |
| 20 | + |
| 21 | + it "returns only specified keys" do |
| 22 | + klass = Data.define(:x, :y) |
| 23 | + d = klass.new(1, 2) |
| 24 | + |
| 25 | + d.deconstruct_keys([:x, :y]).should == {x: 1, y: 2} |
| 26 | + d.deconstruct_keys([:x] ).should == {x: 1} |
| 27 | + d.deconstruct_keys([] ).should == {} |
| 28 | + end |
| 29 | + |
| 30 | + it "accepts string attribute names" do |
| 31 | + klass = Data.define(:x, :y) |
| 32 | + d = klass.new(1, 2) |
| 33 | + d.deconstruct_keys(['x', 'y']).should == {'x' => 1, 'y' => 2} |
| 34 | + end |
| 35 | + |
| 36 | + it "accepts argument position number as well but returns them as keys" do |
| 37 | + klass = Data.define(:x, :y) |
| 38 | + d = klass.new(1, 2) |
| 39 | + |
| 40 | + d.deconstruct_keys([0, 1]).should == {0 => 1, 1 => 2} |
| 41 | + d.deconstruct_keys([0] ).should == {0 => 1} |
| 42 | + d.deconstruct_keys([-1] ).should == {-1 => 2} |
| 43 | + end |
| 44 | + |
| 45 | + it "ignores incorrect position numbers" do |
| 46 | + klass = Data.define(:x, :y) |
| 47 | + d = klass.new(1, 2) |
| 48 | + |
| 49 | + d.deconstruct_keys([0, 3]).should == {0 => 1} |
| 50 | + end |
| 51 | + |
| 52 | + it "support mixing attribute names and argument position numbers" do |
| 53 | + klass = Data.define(:x, :y) |
| 54 | + d = klass.new(1, 2) |
| 55 | + |
| 56 | + d.deconstruct_keys([0, :x]).should == {0 => 1, :x => 1} |
| 57 | + end |
| 58 | + |
| 59 | + it "returns an empty hash when there are more keys than attributes" do |
| 60 | + klass = Data.define(:x, :y) |
| 61 | + d = klass.new(1, 2) |
| 62 | + d.deconstruct_keys([:x, :y, :x]).should == {} |
| 63 | + end |
| 64 | + |
| 65 | + it "returns at first not existing attribute name" do |
| 66 | + klass = Data.define(:x, :y) |
| 67 | + d = klass.new(1, 2) |
| 68 | + |
| 69 | + d.deconstruct_keys([:a, :x]).should == {} |
| 70 | + d.deconstruct_keys([:x, :a]).should == {x: 1} |
| 71 | + end |
| 72 | + |
| 73 | + it "returns at first not existing argument position number" do |
| 74 | + klass = Data.define(:x, :y) |
| 75 | + d = klass.new(1, 2) |
| 76 | + |
| 77 | + d.deconstruct_keys([3, 0]).should == {} |
| 78 | + d.deconstruct_keys([0, 3]).should == {0 => 1} |
| 79 | + end |
| 80 | + |
| 81 | + it "accepts nil argument and return all the attributes" do |
| 82 | + klass = Data.define(:x, :y) |
| 83 | + d = klass.new(1, 2) |
| 84 | + |
| 85 | + d.deconstruct_keys(nil).should == {x: 1, y: 2} |
| 86 | + end |
| 87 | + |
| 88 | + it "raises TypeError if index is not a String, a Symbol and not convertible to Integer " do |
| 89 | + klass = Data.define(:x, :y) |
| 90 | + d = klass.new(1, 2) |
| 91 | + |
| 92 | + -> { |
| 93 | + d.deconstruct_keys([0, []]) |
| 94 | + }.should raise_error(TypeError, "no implicit conversion of Array into Integer") |
| 95 | + end |
| 96 | + |
| 97 | + it "raise TypeError if passed anything except nil or array" do |
| 98 | + klass = Data.define(:x, :y) |
| 99 | + d = klass.new(1, 2) |
| 100 | + |
| 101 | + -> { d.deconstruct_keys('x') }.should raise_error(TypeError, /expected Array or nil/) |
| 102 | + -> { d.deconstruct_keys(1) }.should raise_error(TypeError, /expected Array or nil/) |
| 103 | + -> { d.deconstruct_keys(:x) }.should raise_error(TypeError, /expected Array or nil/) |
| 104 | + -> { d.deconstruct_keys({}) }.should raise_error(TypeError, /expected Array or nil/) |
| 105 | + end |
| 106 | + end |
| 107 | +end |
0 commit comments