spec/freckle_spec.rb in dottie-0.0.1 vs spec/freckle_spec.rb in dottie-0.0.2
- old
+ new
@@ -1,9 +1,25 @@
require 'spec_helper'
describe Dottie::Freckle do
+ describe 'instantiation' do
+
+ it 'creates a Dottie::Freckle from a Hash' do
+ expect(Dottie::Freckle.new({ 'a' => 'b' })).to be_a Dottie::Freckle
+ end
+ it 'creates a Dottie::Freckle from an Array' do
+ expect(Dottie::Freckle.new(['a', 'b', 'c'])).to be_a Dottie::Freckle
+ end
+ ['a', nil, 1].each do |val|
+ it "fails to create a Dottie::Freckle from an invalid type (#{val.class})" do
+ expect{ Dottie::Freckle.new(val) }.to raise_error(TypeError)
+ end
+ end
+
+ end
+
describe 'general' do
context 'Hash' do
let(:freckle) { Dottie::Freckle.new({ 'a' => 'b' }) }
@@ -264,9 +280,45 @@
expect(@freckle.hash).to eq({ 'a' => 'b', 'c' => ['d', 'e', 'f'], 'r' => ['s'] })
end
it 'adds an array element' do
@freckle['c[3]'] = 'g'
expect(@freckle.hash).to eq({ 'a' => 'b', 'c' => ['d', 'e', 'f', 'g'] })
+ end
+ end
+
+ end
+
+ describe 'flattening' do
+
+ context 'hash' do
+ let(:freckle) { Dottie::Freckle.new({ 'a' => 'b', 'c' => { 'd' => ['e', 'f', 'g'] } }) }
+
+ it 'flattens a hash' do
+ expect(freckle.dottie_flatten).to eq({ 'a' => 'b', 'c.d[0]' => 'e', 'c.d[1]' => 'f', 'c.d[2]' => 'g' })
+ end
+ it 'gets flattened hash keys' do
+ expect(freckle.dottie_keys).to eq ['a', 'c.d[0]', 'c.d[1]', 'c.d[2]']
+ end
+ it 'gets all flattened hash keys' do
+ expect(freckle.dottie_keys(true)).to eq ['a', 'c', 'c.d', 'c.d[0]', 'c.d[1]', 'c.d[2]']
+ end
+ end
+
+ context 'array' do
+ let(:freckle) { Dottie::Freckle.new(['x', { 'a' => 'b', 'c' => { 'd' => ['e', 'f', 'g'] } }, 'y']) }
+
+ it 'flattens an array' do
+ expect(freckle.dottie_flatten).to eq({
+ '[0]' => 'x',
+ '[1].a' => 'b', '[1].c.d[0]' => 'e', '[1].c.d[1]' => 'f', '[1].c.d[2]' => 'g',
+ '[2]' => 'y' })
+ end
+ it 'gets flattened array keys' do
+ expect(freckle.dottie_keys).to eq ['[0]', '[1].a', '[1].c.d[0]', '[1].c.d[1]', '[1].c.d[2]', '[2]']
+ end
+ it 'gets all flattened array keys' do
+ expect(freckle.dottie_keys(true)).to eq [
+ '[0]', '[1]', '[1].a', '[1].c', '[1].c.d', '[1].c.d[0]', '[1].c.d[1]', '[1].c.d[2]', '[2]']
end
end
end