1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
|
require_relative '../spec_helper'
require 'recursive_open_struct'
describe RecursiveOpenStruct do
let(:hash) { {:foo => 'foo', 'bar' => :bar} }
subject(:ros) { RecursiveOpenStruct.new(hash) }
describe "OpenStruct 2.0+ methods" do
context "Hash style setter" do
it "method exists" do
expect(ros.respond_to?('[]=')).to be_truthy
end
it "changes the value" do
ros[:foo] = :foo
ros.foo = :foo
end
end
context "delete_field" do
before(:each) { ros.delete_field :foo }
it "removes the value" do
expect(ros.foo).to be_nil
expect(ros.to_h).to_not include(:foo)
end
it "removes the getter method" do
is_expected.to_not respond_to :foo
end
it "removes the setter method" do
expect(ros.respond_to? 'foo=').to be_falsey
end
it "works with indifferent access" do
expect(ros.delete_field :bar).to eq :bar
is_expected.to_not respond_to :bar
is_expected.to_not respond_to 'bar='
expect(ros.to_h).to be_empty
end
end
context "eql?" do
subject(:new_ros) { ros.dup }
context "with identical ROS" do
subject { ros }
it { is_expected.to be_eql ros }
end
context "with similar ROS" do
subject { RecursiveOpenStruct.new(hash) }
it { is_expected.to be_eql ros }
end
context "with same Hash" do
subject { RecursiveOpenStruct.new(hash, recurse_over_arrays: true) }
it { is_expected.to be_eql ros }
end
context "with duplicated ROS" do
subject { ros.dup }
it "fails on different value" do
subject.foo = 'bar'
is_expected.not_to be_eql ros
end
it "fails on missing field" do
subject.delete_field :bar
is_expected.not_to be_eql ros
end
it "fails on added field" do
subject.baz = :baz
is_expected.not_to be_eql ros
end
end
end
context "hash" do
it "calculates table hash" do
expect(ros.hash).to be ros.instance_variable_get('@table').hash
end
end
context "each_pair" do
it "iterates over hash keys, with keys as symbol" do
ros_pairs = []
ros.each_pair {|k,v| ros_pairs << [k,v]}
hash_pairs = []
{:foo => 'foo', :bar => :bar}.each_pair {|k,v| hash_pairs << [k,v]}
expect(ros_pairs).to match (hash_pairs)
end
end
end # describe OpenStruct 2.0+ methods
end
|