-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
268 additions
and
213 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,7 @@ | ||
source 'https://rubygems.org' | ||
|
||
gemspec | ||
|
||
group :test do | ||
gem 'rspec' | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
describe Hash do | ||
describe 'deep_symbolize_keys' do | ||
it 'symbolizes keys on nested hashes' do | ||
hash = { 'foo' => { 'bar' => 'bar' } } | ||
expected = { :foo => { :bar => 'bar' } } | ||
expect(hash.deep_symbolize_keys).to eq(expected) | ||
end | ||
|
||
it 'walks arrays' do | ||
hash = { 'foo' => [{ 'bar' => 'bar', 'baz' => { 'buz' => 'buz' } }] } | ||
expected = { :foo => [{ :bar => 'bar', :baz => { :buz => 'buz' } }] } | ||
expect(hash.deep_symbolize_keys).to eq(expected) | ||
end | ||
end | ||
|
||
describe 'deep_symbolize_keys!' do | ||
it 'replaces with deep_symbolize' do | ||
hash = { 'foo' => { 'bar' => 'baz' } } | ||
expected = { :foo => { :bar => 'baz' } } | ||
hash.deep_symbolize_keys! | ||
expect(hash).to eq(expected) | ||
end | ||
end | ||
|
||
describe 'slice' do | ||
it 'returns a new hash containing the given keys' do | ||
hash = { :foo => 'foo', :bar => 'bar', :baz => 'baz' } | ||
expected = { :foo => 'foo', :bar => 'bar' } | ||
expect(hash.slice(:foo, :bar)).to eq(expected) | ||
end | ||
|
||
it 'does not explode on a missing key' do | ||
expect({}.slice(:foo)).to eq({}) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
describe Hashr do | ||
shared_examples_for 'converts to a hash' do | ||
it 'converts the Hashr instance to a hash' do | ||
expect(hash.class).to eq(Hash) | ||
end | ||
|
||
it 'converts nested instances to hashes' do | ||
expect(hash[:foo].class).to eq(Hash) | ||
end | ||
|
||
it 'populates the hash with symbolized keys' do | ||
expect(hash[:foo][:bar]).to eq('baz') | ||
end | ||
end | ||
|
||
let(:hashr) { Hashr.new(:foo => { :bar => 'baz' }) } | ||
|
||
# describe 'to_h' do | ||
# let(:hash) { hashr.to_h } | ||
# include_examples 'converts to a hash' | ||
# end | ||
|
||
describe 'to_hash' do | ||
let(:hash) { hashr.to_hash } | ||
include_examples 'converts to a hash' | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
describe Hashr, 'defaults' do | ||
shared_examples_for 'defaults' do |method| | ||
describe 'using a symbolized hash' do | ||
let(:klass) { Class.new(Hashr) { send method, foo: 'foo' } } | ||
|
||
it 'defines the default' do | ||
expect(klass.new.foo).to eq('foo') | ||
end | ||
end | ||
|
||
describe 'using a stringified hash' do | ||
let(:klass) { Class.new(Hashr) { send method, 'foo' => 'foo' } } | ||
|
||
it 'defines the default' do | ||
expect(klass.new.foo).to eq('foo') | ||
end | ||
end | ||
|
||
describe 'with a nested hash' do | ||
let(:klass) { Class.new(Hashr) { send method, foo: { bar: { baz: 'baz' } } } } | ||
|
||
it 'defines the default' do | ||
expect(klass.new.foo.bar.baz).to eq('baz') | ||
end | ||
end | ||
|
||
describe 'with a nested array' do | ||
let(:klass) { Class.new(Hashr) { send method, foo: ['bar'] } } | ||
|
||
it 'defines the default' do | ||
expect(klass.new.foo.first).to eq('bar') | ||
end | ||
end | ||
end | ||
|
||
# describe 'definition using default' do | ||
# include_examples 'defaults', :default | ||
# end | ||
|
||
describe 'definition using define (deprecated)' do | ||
include_examples 'defaults', :define | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
describe Hashr, 'hash access' do | ||
it 'is indifferent about symbols/strings (string data given, symbol keys used)' do | ||
expect(Hashr.new('foo' => { 'bar' => 'bar' })[:foo][:bar]).to eq('bar') | ||
end | ||
|
||
it 'is indifferent about symbols/strings (symbol data given, string keys used)' do | ||
expect(Hashr.new(foo: { bar: 'bar' })['foo']['bar']).to eq('bar') | ||
end | ||
|
||
# it 'allows accessing keys with Hash core method names (count)' do | ||
# expect(Hashr.new(count: 2).count).to eq(2) | ||
# end | ||
|
||
# it 'allows accessing keys with Hash core method names (key)' do | ||
# expect(Hashr.new(key: 'key').key).to eq('key') | ||
# end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
describe Hashr, 'hash assignment' do | ||
let(:hashr) { Hashr.new } | ||
|
||
it 'works with a string key' do | ||
hashr['foo'] = 'foo' | ||
expect(hashr.foo).to eq('foo') | ||
end | ||
|
||
it 'works with a symbol key' do | ||
hashr[:foo] = 'foo' | ||
expect(hashr.foo).to eq('foo') | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
describe Hashr, 'initialize' do | ||
it 'takes nil' do | ||
expect { Hashr.new(nil) }.to_not raise_error | ||
end | ||
|
||
it 'raises an ArgumentError when given a string' do | ||
expect { Hashr.new('foo') }.to raise_error(ArgumentError) | ||
end | ||
end |
Oops, something went wrong.