summaryrefslogtreecommitdiff
path: root/spec
diff options
context:
space:
mode:
authorMichael Herold <opensource@michaeljherold.com>2018-08-11 11:10:14 -0500
committerMichael Herold <opensource@michaeljherold.com>2018-08-11 11:53:22 -0500
commitb7209f6f580816bc2d030e1c52f2f2c688454bdc (patch)
tree06b5db063241fb1ee4d2590db02b39cd6be9b911 /spec
parent50898af55a4fd9fac96e5748e59abee3614e786b (diff)
downloadhashie-b7209f6f580816bc2d030e1c52f2f2c688454bdc.tar.gz
Allow Trash to copy properties from others
Trash should be able to copy properties from other properties without causing a problem. Trash should also, when doing this, not set any properties that it doesn't define.
Diffstat (limited to 'spec')
-rw-r--r--spec/hashie/trash_spec.rb48
1 files changed, 48 insertions, 0 deletions
diff --git a/spec/hashie/trash_spec.rb b/spec/hashie/trash_spec.rb
index 20b717c..a5e5d02 100644
--- a/spec/hashie/trash_spec.rb
+++ b/spec/hashie/trash_spec.rb
@@ -265,4 +265,52 @@ describe Hashie::Trash do
expect(subject.first_name).to eq('Frodo')
end
end
+
+ context 'when copying properties from other properties' do
+ it 'retains the original and also sets the copy' do
+ simple = Class.new(Hashie::Trash) do
+ property :id
+ property :copy_of_id, from: :id
+ end
+
+ subject = simple.new(id: 1)
+
+ expect(subject.id).to eq(1)
+ expect(subject.copy_of_id).to eq(1)
+ end
+
+ it 'grabs the default for the original if it is not set' do
+ with_default = Class.new(Hashie::Trash) do
+ property :id, default: 0
+ property :copy_of_id, from: :id
+ end
+
+ subject = with_default.new
+
+ expect(subject.id).to eq(0)
+ expect(subject.copy_of_id).to eq(0)
+ end
+
+ it 'can be a required value' do
+ with_required = Class.new(Hashie::Trash) do
+ property :id
+ property :copy_of_id, from: :id, required: true, message: 'must be set'
+ end
+
+ expect { with_required.new }.to raise_error(ArgumentError, "The property 'copy_of_id' must be set")
+ end
+
+ it 'does not set properties that do not exist' do
+ from_non_property = Class.new(Hashie::Trash) do
+ property :copy_of_value, from: :value
+ end
+
+ subject = from_non_property.new(value: 0)
+
+ expect(subject).not_to respond_to(:value)
+ expect { subject[:value] }.to raise_error(NoMethodError, "The property 'value' is not defined for .")
+ expect(subject.to_h[:value]).to eq(nil)
+ expect(subject.copy_of_value).to eq(0)
+ end
+ end
end