summaryrefslogtreecommitdiff
path: root/spec
diff options
context:
space:
mode:
Diffstat (limited to 'spec')
-rw-r--r--spec/hashie/dash_spec.rb26
-rw-r--r--spec/hashie/extensions/mash/keep_original_keys_spec.rb46
2 files changed, 72 insertions, 0 deletions
diff --git a/spec/hashie/dash_spec.rb b/spec/hashie/dash_spec.rb
index c6e6753..87e5bc3 100644
--- a/spec/hashie/dash_spec.rb
+++ b/spec/hashie/dash_spec.rb
@@ -192,6 +192,32 @@ describe DashTest do
end
end
+ context 'converting from a Mash' do
+ class ConvertingFromMash < Hashie::Dash
+ property :property, required: true
+ end
+
+ context 'without keeping the original keys' do
+ let(:mash) { Hashie::Mash.new(property: 'test') }
+
+ it 'does not pick up the property from the stringified key' do
+ expect { ConvertingFromMash.new(mash) }.to raise_error(NoMethodError)
+ end
+ end
+
+ context 'when keeping the original keys' do
+ class KeepingMash < Hashie::Mash
+ include Hashie::Extensions::Mash::KeepOriginalKeys
+ end
+
+ let(:mash) { KeepingMash.new(property: 'test') }
+
+ it 'picks up the property from the original key' do
+ expect { ConvertingFromMash.new(mash) }.not_to raise_error
+ end
+ end
+ end
+
describe '#new' do
it 'fails with non-existent properties' do
expect { described_class.new(bork: '') }.to raise_error(*no_property_error('bork'))
diff --git a/spec/hashie/extensions/mash/keep_original_keys_spec.rb b/spec/hashie/extensions/mash/keep_original_keys_spec.rb
new file mode 100644
index 0000000..6e3aa06
--- /dev/null
+++ b/spec/hashie/extensions/mash/keep_original_keys_spec.rb
@@ -0,0 +1,46 @@
+require 'spec_helper'
+
+RSpec.describe Hashie::Extensions::Mash::KeepOriginalKeys do
+ let(:keeping_mash) do
+ Class.new(Hashie::Mash) do
+ include Hashie::Extensions::Mash::KeepOriginalKeys
+ end
+ end
+
+ it 'keeps the keys in the resulting hash identical to the original' do
+ original = { :a => 'apple', 'b' => 'bottle' }
+ mash = keeping_mash.new(original)
+
+ expect(mash.to_hash).to eq(original)
+ end
+
+ it 'indifferently responds to keys' do
+ original = { :a => 'apple', 'b' => 'bottle' }
+ mash = keeping_mash.new(original)
+
+ expect(mash['a']).to eq(mash[:a])
+ expect(mash['b']).to eq(mash[:b])
+ end
+
+ it 'responds to all method accessors like a Mash' do
+ original = { :a => 'apple', 'b' => 'bottle' }
+ mash = keeping_mash.new(original)
+
+ expect(mash.a).to eq('apple')
+ expect(mash.a?).to eq(true)
+ expect(mash.b).to eq('bottle')
+ expect(mash.b?).to eq(true)
+ expect(mash.underbang_).to be_a(keeping_mash)
+ expect(mash.bang!).to be_a(keeping_mash)
+ expect(mash.predicate?).to eq(false)
+ end
+
+ it 'keeps the keys that are directly passed without converting them' do
+ original = { :a => 'apple', 'b' => 'bottle' }
+ mash = keeping_mash.new(original)
+
+ mash[:c] = 'cat'
+ mash['d'] = 'dog'
+ expect(mash.to_hash).to eq(:a => 'apple', 'b' => 'bottle', :c => 'cat', 'd' => 'dog')
+ end
+end