summaryrefslogtreecommitdiff
path: root/spec
diff options
context:
space:
mode:
authorMichael Herold <michael.j.herold@gmail.com>2017-02-19 23:31:10 -0600
committerMichael Herold <michael.j.herold@gmail.com>2017-02-20 07:58:24 -0600
commit5fa3ebc13218e961455cfe3c75220a8bc064a702 (patch)
tree5e79bf4fad585bf5e2d45b743fb05c6bb67293c1 /spec
parentcc359a22b66e910f3dfc66d6ebb24b3230b1486f (diff)
downloadhashie-5fa3ebc13218e961455cfe3c75220a8bc064a702.tar.gz
Add Hashie::Extensions::Mash::SymbolizeKeys
We often have requests to make Mash symbolize keys by default. Since Hashie is used across so many different version of Ruby, we have been hesitant to make this behavior the default. However, there are valid use cases for wanting symbol keys. To satisfy both the needs of those on older Rubies and the needs of those who want symbol keys, this extension gives the end-user the ability to toggle on symbolized keys in their Mash subclasses. By adding this ability, we can wait to implement the symbol keys as a default for a while longer. See #341, #342 for more information. This is a half-measure toward the implementation of #342 (which makes Mash symbolize keys by default).
Diffstat (limited to 'spec')
-rw-r--r--spec/hashie/extensions/mash/symbolize_keys_spec.rb39
1 files changed, 39 insertions, 0 deletions
diff --git a/spec/hashie/extensions/mash/symbolize_keys_spec.rb b/spec/hashie/extensions/mash/symbolize_keys_spec.rb
new file mode 100644
index 0000000..1e52534
--- /dev/null
+++ b/spec/hashie/extensions/mash/symbolize_keys_spec.rb
@@ -0,0 +1,39 @@
+require 'spec_helper'
+
+RSpec.describe Hashie::Extensions::Mash::SymbolizeKeys do
+ it 'raises an error when included in a class that is not a Mash' do
+ expect do
+ Class.new do
+ include Hashie::Extensions::Mash::SymbolizeKeys
+ end
+ end.to raise_error(ArgumentError)
+ end
+
+ it 'symbolizes all keys in the Mash' do
+ my_mash = Class.new(Hashie::Mash) do
+ include Hashie::Extensions::Mash::SymbolizeKeys
+ end
+
+ expect(my_mash.new('test' => 'value').to_h).to eq(test: 'value')
+ end
+
+ context 'implicit to_hash on double splat' do
+ let(:destructure) { ->(**opts) { opts } }
+ let(:my_mash) do
+ Class.new(Hashie::Mash) do
+ include Hashie::Extensions::Mash::SymbolizeKeys
+ end
+ end
+ let(:instance) { my_mash.new('outer' => { 'inner' => 42 }, 'testing' => [1, 2, 3]) }
+
+ subject { destructure.call(instance) }
+
+ it 'is converted on method calls' do
+ expect(subject).to eq(outer: { inner: 42 }, testing: [1, 2, 3])
+ end
+
+ it 'is converted on explicit operator call' do
+ expect(**instance).to eq(outer: { inner: 42 }, testing: [1, 2, 3])
+ end
+ end
+end