diff options
author | dblock <dblock@dblock.org> | 2014-04-06 15:09:37 -0400 |
---|---|---|
committer | dblock <dblock@dblock.org> | 2014-04-06 15:09:37 -0400 |
commit | 6dec18c47d4e6a6eeb38f7896b60e3e5cce112d2 (patch) | |
tree | 9a5efe257063abc0d3f1aaa963cfaf33844e86a7 | |
parent | 736fd088921f235728acb743f42ef348e3f3f43a (diff) | |
download | hashie-6dec18c47d4e6a6eeb38f7896b60e3e5cce112d2.tar.gz |
Present tense for specs.
-rw-r--r-- | spec/hashie/clash_spec.rb | 58 | ||||
-rw-r--r-- | spec/hashie/dash_spec.rb | 17 | ||||
-rw-r--r-- | spec/hashie/extensions/coercion_spec.rb | 32 | ||||
-rw-r--r-- | spec/hashie/extensions/deep_merge_spec.rb | 5 | ||||
-rw-r--r-- | spec/hashie/extensions/ignore_undeclared_spec.rb | 6 | ||||
-rw-r--r-- | spec/hashie/extensions/indifferent_access_spec.rb | 28 | ||||
-rw-r--r-- | spec/hashie/extensions/key_conversion_spec.rb | 25 | ||||
-rw-r--r-- | spec/hashie/extensions/merge_initializer_spec.rb | 11 | ||||
-rw-r--r-- | spec/hashie/extensions/method_access_spec.rb | 55 | ||||
-rw-r--r-- | spec/hashie/hash_spec.rb | 10 | ||||
-rw-r--r-- | spec/hashie/mash_spec.rb | 229 | ||||
-rw-r--r-- | spec/hashie/rash_spec.rb | 46 | ||||
-rw-r--r-- | spec/hashie/trash_spec.rb | 35 | ||||
-rw-r--r-- | spec/spec_helper.rb | 4 |
14 files changed, 281 insertions, 280 deletions
diff --git a/spec/hashie/clash_spec.rb b/spec/hashie/clash_spec.rb index cf2a8cf..8560532 100644 --- a/spec/hashie/clash_spec.rb +++ b/spec/hashie/clash_spec.rb @@ -1,50 +1,48 @@ require 'spec_helper' describe Hashie::Clash do - before do - @c = Hashie::Clash.new - end + subject { Hashie::Clash.new } - it 'should be able to set an attribute via method_missing' do - @c.foo('bar') - @c[:foo].should eq 'bar' + it 'is able to set an attribute via method_missing' do + subject.foo('bar') + subject[:foo].should eq 'bar' end - it 'should be able to set multiple attributes' do - @c.foo('bar').baz('wok') - @c.should eq(foo: 'bar', baz: 'wok') + it 'is able to set multiple attributes' do + subject.foo('bar').baz('wok') + subject.should eq(foo: 'bar', baz: 'wok') end - it 'should convert multiple arguments into an array' do - @c.foo(1, 2, 3) - @c[:foo].should eq [1, 2, 3] + it 'converts multiple arguments into an array' do + subject.foo(1, 2, 3) + subject[:foo].should eq [1, 2, 3] end - it 'should be able to use bang notation to create a new Clash on a key' do - @c.foo! - @c[:foo].should be_kind_of(Hashie::Clash) + it 'is able to use bang notation to create a new Clash on a key' do + subject.foo! + subject[:foo].should be_kind_of(Hashie::Clash) end - it 'should be able to chain onto the new Clash when using bang notation' do - @c.foo!.bar('abc').baz(123) - @c.should eq(foo: { bar: 'abc', baz: 123 }) + it 'is able to chain onto the new Clash when using bang notation' do + subject.foo!.bar('abc').baz(123) + subject.should eq(foo: { bar: 'abc', baz: 123 }) end - it 'should be able to jump back up to the parent in the chain with #_end!' do - @c.foo!.bar('abc')._end!.baz(123) - @c.should eq(foo: { bar: 'abc' }, baz: 123) + it 'is able to jump back up to the parent in the chain with #_end!' do + subject.foo!.bar('abc')._end!.baz(123) + subject.should eq(foo: { bar: 'abc' }, baz: 123) end - it 'should merge rather than replace existing keys' do - @c.where(abc: 'def').where(hgi: 123) - @c.should eq(where: { abc: 'def', hgi: 123 }) + it 'merges rather than replaces existing keys' do + subject.where(abc: 'def').where(hgi: 123) + subject.should eq(where: { abc: 'def', hgi: 123 }) end - it 'should be able to replace all of its own keys with #replace' do - @c.foo(:bar).hello(:world) - @c.replace(baz: 123, hgi: 123).should eq(baz: 123, hgi: 123) - @c.should eq(baz: 123, hgi: 123) - @c[:foo].should be_nil - @c[:hello].should be_nil + it 'is able to replace all of its own keys with #replace' do + subject.foo(:bar).hello(:world) + subject.replace(baz: 123, hgi: 123).should eq(baz: 123, hgi: 123) + subject.should eq(baz: 123, hgi: 123) + subject[:foo].should be_nil + subject[:hello].should be_nil end end diff --git a/spec/hashie/dash_spec.rb b/spec/hashie/dash_spec.rb index f6c5fe4..606e38e 100644 --- a/spec/hashie/dash_spec.rb +++ b/spec/hashie/dash_spec.rb @@ -63,7 +63,6 @@ describe DashTest do end context 'writing to properties' do - it 'fails writing a required property to nil' do lambda { subject.first_name = nil }.should raise_error(ArgumentError) end @@ -93,14 +92,14 @@ describe DashTest do lambda { subject['nonexistent'] }.should raise_error(NoMethodError) end - it 'should be able to retrieve properties through blocks' do + it 'is able to retrieve properties through blocks' do subject['first_name'] = 'Aiden' value = nil subject.[]('first_name') { |v| value = v } value.should eq 'Aiden' end - it 'should be able to retrieve properties through blocks with method calls' do + it 'is able to retrieve properties through blocks with method calls' do subject['first_name'] = 'Frodo' value = nil subject.first_name { |v| value = v } @@ -109,22 +108,22 @@ describe DashTest do end context 'reading from deferred properties' do - it 'should evaluate proc after initial read' do + it 'evaluates proc after initial read' do DeferredTest.new['created_at'].should be_instance_of(Time) end - it 'should not evalute proc after subsequent reads' do + it 'does not evalute proc after subsequent reads' do deferred = DeferredTest.new deferred['created_at'].object_id.should eq deferred['created_at'].object_id end end - describe '.new' do + describe '#new' do it 'fails with non-existent properties' do lambda { described_class.new(bork: '') }.should raise_error(NoMethodError) end - it 'should set properties that it is able to' do + it 'sets properties that it is able to' do obj = described_class.new first_name: 'Michael' obj.first_name.should eq 'Michael' end @@ -303,7 +302,7 @@ describe Hashie::Dash, 'inheritance' do @bottom.new.echo.should be_nil end - it 'should allow nil defaults' do + it 'allows nil defaults' do @bottom.property :echo, default: nil @bottom.new.should have_key('echo') end @@ -311,7 +310,6 @@ describe Hashie::Dash, 'inheritance' do end describe Subclassed do - subject { Subclassed.new(first_name: 'Bob', last_name: 'McNob', email: 'bob@example.com') } its(:count) { should be_zero } @@ -328,5 +326,4 @@ describe Subclassed do it "didn't override superclass inheritance logic" do described_class.instance_variable_get('@inheritance_test').should be_true end - end diff --git a/spec/hashie/extensions/coercion_spec.rb b/spec/hashie/extensions/coercion_spec.rb index 93f4dec..3781b23 100644 --- a/spec/hashie/extensions/coercion_spec.rb +++ b/spec/hashie/extensions/coercion_spec.rb @@ -31,17 +31,17 @@ describe Hashie::Extensions::Coercion do let(:instance) { subject.new } - describe '.coerce_key' do + describe '#coerce_key' do it { subject.should be_respond_to(:coerce_key) } - it 'should run through coerce on a specified key' do + it 'runs through coerce on a specified key' do subject.coerce_key :foo, Coercable instance[:foo] = 'bar' instance[:foo].should be_coerced end - it 'should support an array of keys' do + it 'supports an array of keys' do subject.coerce_keys :foo, :bar, Coercable instance[:foo] = 'bar' @@ -50,7 +50,7 @@ describe Hashie::Extensions::Coercion do instance[:bar].should be_coerced end - it 'should just call #new if no coerce method is available' do + it 'calls #new if no coerce method is available' do subject.coerce_key :foo, Initializable instance[:foo] = 'bar' @@ -58,7 +58,7 @@ describe Hashie::Extensions::Coercion do instance[:foo].should_not be_coerced end - it 'should coerce when the merge initializer is used' do + it 'coerces when the merge initializer is used' do subject.coerce_key :foo, Coercable instance = subject.new(foo: 'bar') @@ -72,13 +72,13 @@ describe Hashie::Extensions::Coercion do subject.new(foo: 'bar').replace(foo: 'foz', bar: 'baz', hi: 'bye') end - it 'should coerce relevant keys' do + it 'coerces relevant keys' do instance[:foo].should be_coerced instance[:bar].should be_coerced instance[:hi].should_not respond_to(:coerced?) end - it 'should set correct values' do + it 'sets correct values' do instance[:hi].should eq 'bye' end end @@ -91,24 +91,24 @@ describe Hashie::Extensions::Coercion do coerce_key :user, UserMash end - it 'should coerce with instance initialization' do + it 'coerces with instance initialization' do tweet = TweetMash.new(user: { email: 'foo@bar.com' }) tweet[:user].should be_a(UserMash) end - it 'should coerce when setting with attribute style' do + it 'coerces when setting with attribute style' do tweet = TweetMash.new tweet.user = { email: 'foo@bar.com' } tweet[:user].should be_a(UserMash) end - it 'should coerce when setting with string index' do + it 'coerces when setting with string index' do tweet = TweetMash.new tweet['user'] = { email: 'foo@bar.com' } tweet[:user].should be_a(UserMash) end - it 'should coerce when setting with symbol index' do + it 'coerces when setting with symbol index' do tweet = TweetMash.new tweet[:user] = { email: 'foo@bar.com' } tweet[:user].should be_a(UserMash) @@ -116,9 +116,9 @@ describe Hashie::Extensions::Coercion do end end - describe '.coerce_value' do - context 'with :strict => true' do - it 'should coerce any value of the exact right class' do + describe '#coerce_value' do + context 'with strict: true' do + it 'coerces any value of the exact right class' do subject.coerce_value String, Coercable instance[:foo] = 'bar' @@ -129,7 +129,7 @@ describe Hashie::Extensions::Coercion do instance[:hi].should_not respond_to(:coerced?) end - it 'should coerce values from a #replace call' do + it 'coerces values from a #replace call' do subject.coerce_value String, Coercable instance[:foo] = :bar @@ -138,7 +138,7 @@ describe Hashie::Extensions::Coercion do instance[:bar].should be_coerced end - it 'should not coerce superclasses' do + it 'does not coerce superclasses' do klass = Class.new(String) subject.coerce_value klass, Coercable diff --git a/spec/hashie/extensions/deep_merge_spec.rb b/spec/hashie/extensions/deep_merge_spec.rb index 34ff6a2..eff604d 100644 --- a/spec/hashie/extensions/deep_merge_spec.rb +++ b/spec/hashie/extensions/deep_merge_spec.rb @@ -1,7 +1,9 @@ require 'spec_helper' describe Hashie::Extensions::DeepMerge do - class DeepMergeHash < Hash; include Hashie::Extensions::DeepMerge end + class DeepMergeHash < Hash + include Hashie::Extensions::DeepMerge + end subject { DeepMergeHash } @@ -17,5 +19,4 @@ describe Hashie::Extensions::DeepMerge do h1.deep_merge!(h2) h1.should eq expected_hash end - end diff --git a/spec/hashie/extensions/ignore_undeclared_spec.rb b/spec/hashie/extensions/ignore_undeclared_spec.rb index b7d7ad5..4a6d19c 100644 --- a/spec/hashie/extensions/ignore_undeclared_spec.rb +++ b/spec/hashie/extensions/ignore_undeclared_spec.rb @@ -9,15 +9,15 @@ describe Hashie::Extensions::IgnoreUndeclared do subject { ForgivingTrash } - it 'should silently ignore undeclared properties on initialization' do + it 'silently ignores undeclared properties on initialization' do expect { subject.new(city: 'Toronto', provence: 'ON', country: 'Canada') }.to_not raise_error end - it 'should work with translated properties (with symbol keys)' do + it 'works with translated properties (with symbol keys)' do expect(subject.new(provence: 'Ontario').state).to eq('Ontario') end - it 'should work with translated properties (with string keys)' do + it 'works with translated properties (with string keys)' do expect(subject.new(provence: 'Ontario').state).to eq('Ontario') end end diff --git a/spec/hashie/extensions/indifferent_access_spec.rb b/spec/hashie/extensions/indifferent_access_spec.rb index 4dd6a03..54337d5 100644 --- a/spec/hashie/extensions/indifferent_access_spec.rb +++ b/spec/hashie/extensions/indifferent_access_spec.rb @@ -1,7 +1,6 @@ require 'spec_helper' describe Hashie::Extensions::IndifferentAccess do - class IndifferentHashWithMergeInitializer < Hash include Hashie::Extensions::MergeInitializer include Hashie::Extensions::IndifferentAccess @@ -28,21 +27,21 @@ describe Hashie::Extensions::IndifferentAccess do end shared_examples_for 'hash with indifferent access' do - it 'should be able to access via string or symbol' do + it 'is able to access via string or symbol' do h = subject.build(abc: 123) h[:abc].should eq 123 h['abc'].should eq 123 end describe '#values_at' do - it 'should indifferently find values' do + it 'indifferently finds values' do h = subject.build(:foo => 'bar', 'baz' => 'qux') h.values_at('foo', :baz).should eq %w(bar qux) end end describe '#fetch' do - it 'should work like normal fetch, but indifferent' do + it 'works like normal fetch, but indifferent' do h = subject.build(foo: 'bar') h.fetch(:foo).should eq h.fetch('foo') h.fetch(:foo).should eq 'bar' @@ -50,7 +49,7 @@ describe Hashie::Extensions::IndifferentAccess do end describe '#delete' do - it 'should delete indifferently' do + it 'deletes indifferently' do h = subject.build(:foo => 'bar', 'baz' => 'qux') h.delete('foo') h.delete(:baz) @@ -61,13 +60,13 @@ describe Hashie::Extensions::IndifferentAccess do describe '#key?' do let(:h) { subject.build(foo: 'bar') } - it 'should find it indifferently' do + it 'finds it indifferently' do h.should be_key(:foo) h.should be_key('foo') end %w(include? member? has_key?).each do |key_alias| - it "should be aliased as #{key_alias}" do + it "is aliased as #{key_alias}" do h.send(key_alias.to_sym, :foo).should be(true) h.send(key_alias.to_sym, 'foo').should be(true) end @@ -76,18 +75,19 @@ describe Hashie::Extensions::IndifferentAccess do describe '#update' do let(:h) { subject.build(foo: 'bar') } - it 'should allow keys to be indifferent still' do + + it 'allows keys to be indifferent still' do h.update(baz: 'qux') h['foo'].should eq 'bar' h['baz'].should eq 'qux' end - it 'should recursively inject indifference into sub-hashes' do + it 'recursively injects indifference into sub-hashes' do h.update(baz: { qux: 'abc' }) h['baz']['qux'].should eq 'abc' end - it 'should not change the ancestors of the injected object class' do + it 'does not change the ancestors of the injected object class' do h.update(baz: { qux: 'abc' }) Hash.new.should_not be_respond_to(:indifferent_access?) end @@ -100,7 +100,7 @@ describe Hashie::Extensions::IndifferentAccess do h.should be_a(subject) end - it 'should remove old keys' do + it 'removes old keys' do [:foo, 'foo'].each do |k| h[k].should be_nil h.key?(k).should be_false @@ -116,11 +116,11 @@ describe Hashie::Extensions::IndifferentAccess do end end - describe '::try_convert' do + describe '#try_convert' do describe 'with conversion' do let(:h) { subject.try_convert(foo: 'bar') } - it 'should be a subject' do + it 'is a subject' do h.should be_a(subject) end end @@ -128,7 +128,7 @@ describe Hashie::Extensions::IndifferentAccess do describe 'without conversion' do let(:h) { subject.try_convert('{ :foo => bar }') } - it 'should be nil' do + it 'is nil' do h.should be_nil end end diff --git a/spec/hashie/extensions/key_conversion_spec.rb b/spec/hashie/extensions/key_conversion_spec.rb index b5a977b..a7fe6a7 100644 --- a/spec/hashie/extensions/key_conversion_spec.rb +++ b/spec/hashie/extensions/key_conversion_spec.rb @@ -6,17 +6,18 @@ describe Hashie::Extensions::KeyConversion do klass.send :include, Hashie::Extensions::KeyConversion klass end + let(:instance) { subject.new } describe '#stringify_keys!' do - it 'should convert keys to strings' do + it 'converts keys to strings' do instance[:abc] = 'abc' instance[123] = '123' instance.stringify_keys! (instance.keys & %w(abc 123)).size.should eq 2 end - it 'should do deep conversion within nested hashes' do + it 'performs deep conversion within nested hashes' do instance[:ab] = subject.new instance[:ab][:cd] = subject.new instance[:ab][:cd][:ef] = 'abcdef' @@ -24,7 +25,7 @@ describe Hashie::Extensions::KeyConversion do instance.should eq('ab' => { 'cd' => { 'ef' => 'abcdef' } }) end - it 'should do deep conversion within nested arrays' do + it 'performs deep conversion within nested arrays' do instance[:ab] = [] instance[:ab] << subject.new instance[:ab] << subject.new @@ -34,19 +35,19 @@ describe Hashie::Extensions::KeyConversion do instance.should eq('ab' => [{ 'cd' => 'abcd' }, { 'ef' => 'abef' }]) end - it 'should return itself' do + it 'returns itself' do instance.stringify_keys!.should eq instance end end describe '#stringify_keys' do - it 'should convert keys to strings' do + it 'converts keys to strings' do instance[:abc] = 'def' copy = instance.stringify_keys copy['abc'].should eq 'def' end - it 'should not alter the original' do + it 'does not alter the original' do instance[:abc] = 'def' copy = instance.stringify_keys instance.keys.should eq [:abc] @@ -55,14 +56,14 @@ describe Hashie::Extensions::KeyConversion do end describe '#symbolize_keys!' do - it 'should convert keys to symbols' do + it 'converts keys to symbols' do instance['abc'] = 'abc' instance['def'] = 'def' instance.symbolize_keys! (instance.keys & [:abc, :def]).size.should eq 2 end - it 'should do deep conversion within nested hashes' do + it 'performs deep conversion within nested hashes' do instance['ab'] = subject.new instance['ab']['cd'] = subject.new instance['ab']['cd']['ef'] = 'abcdef' @@ -70,7 +71,7 @@ describe Hashie::Extensions::KeyConversion do instance.should eq(ab: { cd: { ef: 'abcdef' } }) end - it 'should do deep conversion within nested arrays' do + it 'performs deep conversion within nested arrays' do instance['ab'] = [] instance['ab'] << subject.new instance['ab'] << subject.new @@ -80,19 +81,19 @@ describe Hashie::Extensions::KeyConversion do instance.should eq(ab: [{ cd: 'abcd' }, { ef: 'abef' }]) end - it 'should return itself' do + it 'returns itself' do instance.symbolize_keys!.should eq instance end end describe '#symbolize_keys' do - it 'should convert keys to symbols' do + it 'converts keys to symbols' do instance['abc'] = 'def' copy = instance.symbolize_keys copy[:abc].should eq 'def' end - it 'should not alter the original' do + it 'does not alter the original' do instance['abc'] = 'def' copy = instance.symbolize_keys instance.keys.should eq ['abc'] diff --git a/spec/hashie/extensions/merge_initializer_spec.rb b/spec/hashie/extensions/merge_initializer_spec.rb index bbf5a6f..42dc1da 100644 --- a/spec/hashie/extensions/merge_initializer_spec.rb +++ b/spec/hashie/extensions/merge_initializer_spec.rb @@ -1,18 +1,21 @@ require 'spec_helper' describe Hashie::Extensions::MergeInitializer do - class MergeInitializerHash < Hash; include Hashie::Extensions::MergeInitializer end + class MergeInitializerHash < Hash + include Hashie::Extensions::MergeInitializer + end + subject { MergeInitializerHash } - it 'should initialize fine with no arguments' do + it 'initializes with no arguments' do subject.new.should eq({}) end - it 'should initialize with a hash' do + it 'initializes with a hash' do subject.new(abc: 'def').should eq(abc: 'def') end - it 'should initialize with a hash and a default' do + it 'initializes with a hash and a default' do h = subject.new({ abc: 'def' }, 'bar') h[:foo].should eq 'bar' h[:abc].should eq 'def' diff --git a/spec/hashie/extensions/method_access_spec.rb b/spec/hashie/extensions/method_access_spec.rb index 76b9dbf..8268430 100644 --- a/spec/hashie/extensions/method_access_spec.rb +++ b/spec/hashie/extensions/method_access_spec.rb @@ -2,41 +2,43 @@ require 'spec_helper' describe Hashie::Extensions::MethodReader do class ReaderHash < Hash - def initialize(hash = { - }); update(hash) - end include Hashie::Extensions::MethodReader + + def initialize(hash = {}) + update(hash) + end end + subject { ReaderHash } - it 'should read string keys from the method' do + it 'reads string keys from the method' do subject.new('awesome' => 'sauce').awesome.should eq 'sauce' end - it 'should read symbol keys from the method' do + it 'reads symbol keys from the method' do subject.new(awesome: 'sauce').awesome.should eq 'sauce' end - it 'should read nil and false values out properly' do + it 'reads nil and false values out properly' do h = subject.new(nil: nil, false: false) h.nil.should eq nil h.false.should eq false end - it 'should raise a NoMethodError for undefined keys' do + it 'raises a NoMethodError for undefined keys' do lambda { subject.new.awesome }.should raise_error(NoMethodError) end describe '#respond_to?' do - it 'should be true for string keys' do + it 'is true for string keys' do subject.new('awesome' => 'sauce').should be_respond_to(:awesome) end - it 'should be true for symbol keys' do + it 'is true for symbol keys' do subject.new(awesome: 'sauce').should be_respond_to(:awesome) end - it 'should be false for non-keys' do + it 'is false for non-keys' do subject.new.should_not be_respond_to(:awesome) end end @@ -46,24 +48,25 @@ describe Hashie::Extensions::MethodWriter do class WriterHash < Hash include Hashie::Extensions::MethodWriter end + subject { WriterHash.new } - it 'should write from a method call' do + it 'writes from a method call' do subject.awesome = 'sauce' subject['awesome'].should eq 'sauce' end - it 'should convert the key using the #convert_key method' do + it 'converts the key using the #convert_key method' do subject.stub!(:convert_key).and_return(:awesome) subject.awesome = 'sauce' subject[:awesome].should eq 'sauce' end - it 'should still NoMethodError on non equals-ending methods' do + it 'raises NoMethodError on non equals-ending methods' do lambda { subject.awesome }.should raise_error(NoMethodError) end - it 'should #respond_to? properly' do + it '#respond_to? correctly' do subject.should be_respond_to(:abc=) subject.should_not be_respond_to(:abc) end @@ -71,44 +74,46 @@ end describe Hashie::Extensions::MethodQuery do class QueryHash < Hash - def initialize(hash = { - }); update(hash) - end include Hashie::Extensions::MethodQuery + + def initialize(hash = {}) + update(hash) + end end + subject { QueryHash } - it 'should be true for non-nil string key values' do + it 'is true for non-nil string key values' do subject.new('abc' => 123).should be_abc end - it 'should be true for non-nil symbol key values' do + it 'is true for non-nil symbol key values' do subject.new(abc: 123).should be_abc end - it 'should be false for nil key values' do + it 'is false for nil key values' do subject.new(abc: false).should_not be_abc end - it 'should raise a NoMethodError for non-set keys' do + it 'raises a NoMethodError for non-set keys' do lambda { subject.new.abc? }.should raise_error(NoMethodError) end - it 'should respond_to? for existing string keys' do + it '#respond_to? for existing string keys' do subject.new('abc' => 'def').should be_respond_to('abc?') end - it 'should respond_to? for existing symbol keys' do + it '#respond_to? for existing symbol keys' do subject.new(abc: 'def').should be_respond_to(:abc?) end - it 'should not respond_to? for non-existent keys' do + it 'does not #respond_to? for non-existent keys' do subject.new.should_not be_respond_to('abc?') end end describe Hashie::Extensions::MethodAccess do - it 'should include all of the other method mixins' do + it 'includes all of the other method mixins' do klass = Class.new(Hash) klass.send :include, Hashie::Extensions::MethodAccess (klass.ancestors & [Hashie::Extensions::MethodReader, Hashie::Extensions::MethodWriter, Hashie::Extensions::MethodQuery]).size.should eq 3 diff --git a/spec/hashie/hash_spec.rb b/spec/hashie/hash_spec.rb index 99e04b4..47aa511 100644 --- a/spec/hashie/hash_spec.rb +++ b/spec/hashie/hash_spec.rb @@ -1,32 +1,32 @@ require 'spec_helper' describe Hash do - it 'should be convertible to a Hashie::Mash' do + it 'is convertible to a Hashie::Mash' do mash = Hashie::Hash[some: 'hash'].to_mash mash.is_a?(Hashie::Mash).should be_true mash.some.should eq 'hash' end - it '#stringify_keys! should turn all keys into strings' do + it '#stringify_keys! turns all keys into strings' do hash = Hashie::Hash[:a => 'hey', 123 => 'bob'] hash.stringify_keys! hash.should eq Hashie::Hash['a' => 'hey', '123' => 'bob'] end - it '#stringify_keys should return a hash with stringified keys' do + it '#stringify_keys returns a hash with stringified keys' do hash = Hashie::Hash[:a => 'hey', 123 => 'bob'] stringified_hash = hash.stringify_keys hash.should eq Hashie::Hash[:a => 'hey', 123 => 'bob'] stringified_hash.should eq Hashie::Hash['a' => 'hey', '123' => 'bob'] end - it '#to_hash should return a hash with stringified keys' do + it '#to_hash returns a hash with stringified keys' do hash = Hashie::Hash['a' => 'hey', 123 => 'bob', 'array' => [1, 2, 3]] stringified_hash = hash.to_hash stringified_hash.should eq('a' => 'hey', '123' => 'bob', 'array' => [1, 2, 3]) end - it '#to_hash with symbolize_keys set to true should return a hash with symbolized keys' do + it '#to_hash with symbolize_keys set to true returns a hash with symbolized keys' do hash = Hashie::Hash['a' => 'hey', 123 => 'bob', 'array' => [1, 2, 3]] symbolized_hash = hash.to_hash(symbolize_keys: true) symbolized_hash.should eq(:a => 'hey', :"123" => 'bob', :array => [1, 2, 3]) diff --git a/spec/hashie/mash_spec.rb b/spec/hashie/mash_spec.rb index 6487dd8..210abed 100644 --- a/spec/hashie/mash_spec.rb +++ b/spec/hashie/mash_spec.rb @@ -2,138 +2,141 @@ require 'spec_helper' require 'delegate' describe Hashie::Mash do - before(:each) do - @mash = Hashie::Mash.new - end + subject { Hashie::Mash.new } - it 'should inherit from hash' do - @mash.is_a?(Hash).should be_true + it 'inherits from Hash' do + subject.is_a?(Hash).should be_true end - it 'should be able to set hash values through method= calls' do - @mash.test = 'abc' - @mash['test'].should eq 'abc' + it 'sets hash values through method= calls' do + subject.test = 'abc' + subject['test'].should eq 'abc' end - it 'should be able to retrieve set values through method calls' do - @mash['test'] = 'abc' - @mash.test.should eq 'abc' + it 'retrieves set values through method calls' do + subject['test'] = 'abc' + subject.test.should eq 'abc' end - it 'should be able to retrieve set values through blocks' do - @mash['test'] = 'abc' + it 'retrieves set values through blocks' do + subject['test'] = 'abc' value = nil - @mash.[]('test') { |v| value = v } + subject.[]('test') { |v| value = v } value.should eq 'abc' end - it 'should be able to retrieve set values through blocks with method calls' do - @mash['test'] = 'abc' + it 'retrieves set values through blocks with method calls' do + subject['test'] = 'abc' value = nil - @mash.test { |v| value = v } + subject.test { |v| value = v } value.should eq 'abc' end - it 'should test for already set values when passed a ? method' do - @mash.test?.should be_false - @mash.test = 'abc' - @mash.test?.should be_true + it 'tests for already set values when passed a ? method' do + subject.test?.should be_false + subject.test = 'abc' + subject.test?.should be_true end - it 'should return false on a ? method if a value has been set to nil or false' do - @mash.test = nil - @mash.should_not be_test - @mash.test = false - @mash.should_not be_test + it 'returns false on a ? method if a value has been set to nil or false' do + subject.test = nil + subject.should_not be_test + subject.test = false + subject.should_not be_test end - it 'should make all [] and []= into strings for consistency' do - @mash['abc'] = 123 - @mash.key?('abc').should be_true - @mash['abc'].should eq 123 + it 'makes all [] and []= into strings for consistency' do + subject['abc'] = 123 + subject.key?('abc').should be_true + subject['abc'].should eq 123 end - it 'should have a to_s that is identical to its inspect' do - @mash.abc = 123 - @mash.to_s.should eq @mash.inspect + it 'has a to_s that is identical to its inspect' do + subject.abc = 123 + subject.to_s.should eq subject.inspect end - it 'should return nil instead of raising an error for attribute-esque method calls' do - @mash.abc.should be_nil + it 'returns nil instead of raising an error for attribute-esque method calls' do + subject.abc.should be_nil end - it 'should return the default value if set like Hash' do - @mash.default = 123 - @mash.abc.should eq 123 + it 'returns the default value if set like Hash' do + subject.default = 123 + subject.abc.should eq 123 end - it 'should gracefully handle being accessed with arguments' do - @mash.abc('foobar').should eq nil - @mash.abc = 123 - @mash.abc('foobar').should eq 123 + it 'gracefully handles being accessed with arguments' do + subject.abc('foobar').should eq nil + subject.abc = 123 + subject.abc('foobar').should eq 123 end - it 'should return a Hashie::Mash when passed a bang method to a non-existenct key' do - @mash.abc!.is_a?(Hashie::Mash).should be_true + it 'returns a Hashie::Mash when passed a bang method to a non-existenct key' do + subject.abc!.is_a?(Hashie::Mash).should be_true end - it 'should return the existing value when passed a bang method for an existing key' do - @mash.name = 'Bob' - @mash.name!.should eq 'Bob' + it 'returns the existing value when passed a bang method for an existing key' do + subject.name = 'Bob' + subject.name!.should eq 'Bob' end - it 'should return a Hashie::Mash when passed an under bang method to a non-existenct key' do - @mash.abc_.is_a?(Hashie::Mash).should be_true + it 'returns a Hashie::Mash when passed an under bang method to a non-existenct key' do + subject.abc_.is_a?(Hashie::Mash).should be_true end - it 'should return the existing value when passed an under bang method for an existing key' do - @mash.name = 'Bob' - @mash.name_.should eq 'Bob' + it 'returns the existing value when passed an under bang method for an existing key' do + subject.name = 'Bob' + subject.name_.should eq 'Bob' end - it '#initializing_reader should return a Hashie::Mash when passed a non-existent key' do - @mash.initializing_reader(:abc).is_a?(Hashie::Mash).should be_true + it '#initializing_reader returns a Hashie::Mash when passed a non-existent key' do + subject.initializing_reader(:abc).is_a?(Hashie::Mash).should be_true end - it 'should allow for multi-level assignment through bang methods' do - @mash.author!.name = 'Michael Bleigh' - @mash.author.should eq Hashie::Mash.new(name: 'Michael Bleigh') - @mash.author!.website!.url = 'http://www.mbleigh.com/' - @mash.author.website.should eq Hashie::Mash.new(url: 'http://www.mbleigh.com/') + it 'allows for multi-level assignment through bang methods' do + subject.author!.name = 'Michael Bleigh' + subject.author.should eq Hashie::Mash.new(name: 'Michael Bleigh') + subject.author!.website!.url = 'http://www.mbleigh.com/' + subject.author.website.should eq Hashie::Mash.new(url: 'http://www.mbleigh.com/') end - it 'should allow for multi-level under bang testing' do - @mash.author_.website_.url.should be_nil - @mash.author_.website_.url?.should eq false - @mash.author.should be_nil + it 'allows for multi-level under bang testing' do + subject.author_.website_.url.should be_nil + subject.author_.website_.url?.should eq false + subject.author.should be_nil end - it 'should not call super if id is not a key' do - @mash.id.should eq nil + it 'does not call super if id is not a key' do + subject.id.should eq nil end - it 'should return the value if id is a key' do - @mash.id = 'Steve' - @mash.id.should eq 'Steve' + it 'returns the value if id is a key' do + subject.id = 'Steve' + subject.id.should eq 'Steve' end - it 'should not call super if type is not a key' do - @mash.type.should eq nil + it 'does not call super if type is not a key' do + subject.type.should eq nil end - it 'should return the value if type is a key' do - @mash.type = 'Steve' - @mash.type.should eq 'Steve' + it 'returns the value if type is a key' do + subject.type = 'Steve' + subject.type.should eq 'Steve' end context 'updating' do subject do - described_class.new first_name: 'Michael', last_name: 'Bleigh', - details: { email: 'michael@asf.com', address: 'Nowhere road' } + described_class.new( + first_name: 'Michael', + last_name: 'Bleigh', + details: { + email: 'michael@asf.com', + address: 'Nowhere road' + }) end describe '#deep_update' do - it 'should recursively Hashie::Mash Hashie::Mashes and hashes together' do + it 'recursively Hashie::Mash Hashie::Mashes and hashes together' do subject.deep_update(details: { email: 'michael@intridea.com', city: 'Imagineton' }) subject.first_name.should eq 'Michael' subject.details.email.should eq 'michael@intridea.com' @@ -141,7 +144,7 @@ describe Hashie::Mash do subject.details.city.should eq 'Imagineton' end - it 'should convert values only once' do + it 'converts values only once' do class ConvertedMash < Hashie::Mash end @@ -150,13 +153,13 @@ describe Hashie::Mash do subject.deep_update(rhs) end - it 'should make #update deep by default' do + it 'makes #update deep by default' do subject.update(details: { address: 'Fake street' }).should eql(subject) subject.details.address.should eq 'Fake street' subject.details.email.should eq 'michael@asf.com' end - it 'should clone before a #deep_merge' do + it 'clones before a #deep_merge' do duped = subject.deep_merge(details: { address: 'Fake street' }) duped.should_not eql(subject) duped.details.address.should eq 'Fake street' @@ -164,7 +167,7 @@ describe Hashie::Mash do duped.details.email.should eq 'michael@asf.com' end - it 'regular #merge should be deep' do + it 'default #merge is deep' do duped = subject.merge(details: { email: 'michael@intridea.com' }) duped.should_not eql(subject) duped.details.email.should eq 'michael@intridea.com' @@ -179,7 +182,7 @@ describe Hashie::Mash do end describe 'shallow update' do - it 'should shallowly Hashie::Mash Hashie::Mashes and hashes together' do + it 'shallowly Hashie::Mash Hashie::Mashes and hashes together' do subject.shallow_update(details: { email: 'michael@intridea.com', city: 'Imagineton' }).should eql(subject) @@ -190,12 +193,12 @@ describe Hashie::Mash do subject.details.city.should eq 'Imagineton' end - it 'should clone before a #regular_merge' do + it 'clones before a #regular_merge' do duped = subject.shallow_merge(details: { address: 'Fake street' }) duped.should_not eql(subject) end - it 'regular merge should be shallow' do + it 'default #merge is shallow' do duped = subject.shallow_merge(details: { address: 'Fake street' }) duped.details.address.should eq 'Fake street' subject.details.address.should eq 'Nowhere road' @@ -205,11 +208,13 @@ describe Hashie::Mash do describe '#replace' do before do - subject.replace(middle_name: 'Cain', - details: { city: 'Imagination' }) + subject.replace( + middle_name: 'Cain', + details: { city: 'Imagination' } + ) end - it 'return self' do + it 'returns self' do subject.replace(foo: 'bar').to_hash.should eq('foo' => 'bar') end @@ -231,13 +236,13 @@ describe Hashie::Mash do end describe 'delete' do - it 'should delete with String key' do + it 'deletes with String key' do subject.delete('details') subject.details.should be_nil subject.should_not be_respond_to :details end - it 'should delete with Symbol key' do + it 'deletes with Symbol key' do subject.delete(:details) subject.details.should be_nil subject.should_not be_respond_to :details @@ -245,13 +250,13 @@ describe Hashie::Mash do end end - it 'should convert hash assignments into Hashie::Mashes' do - @mash.details = { email: 'randy@asf.com', address: { state: 'TX' } } - @mash.details.email.should eq 'randy@asf.com' - @mash.details.address.state.should eq 'TX' + it 'converts hash assignments into Hashie::Mashes' do + subject.details = { email: 'randy@asf.com', address: { state: 'TX' } } + subject.details.email.should eq 'randy@asf.com' + subject.details.address.state.should eq 'TX' end - it 'should not convert the type of Hashie::Mashes childs to Hashie::Mash' do + it 'does not convert the type of Hashie::Mashes childs to Hashie::Mash' do class MyMash < Hashie::Mash end @@ -260,7 +265,7 @@ describe Hashie::Mash do record.son.class.should eq MyMash end - it 'should not change the class of Mashes when converted' do + it 'does not change the class of Mashes when converted' do class SubMash < Hashie::Mash end @@ -270,7 +275,7 @@ describe Hashie::Mash do record['submash'].should be_kind_of(SubMash) end - it 'should respect the class when passed a bang method for a non-existent key' do + it 'respects the class when passed a bang method for a non-existent key' do record = Hashie::Mash.new record.non_existent!.should be_kind_of(Hashie::Mash) @@ -281,7 +286,7 @@ describe Hashie::Mash do son.non_existent!.should be_kind_of(SubMash) end - it 'should respect the class when passed an under bang method for a non-existent key' do + it 'respects the class when passed an under bang method for a non-existent key' do record = Hashie::Mash.new record.non_existent_.should be_kind_of(Hashie::Mash) @@ -292,13 +297,13 @@ describe Hashie::Mash do son.non_existent_.should be_kind_of(SubMash) end - it 'should respect the class when converting the value' do + it 'respects the class when converting the value' do record = Hashie::Mash.new record.details = Hashie::Mash.new(email: 'randy@asf.com') record.details.should be_kind_of(Hashie::Mash) end - it 'should respect another subclass when converting the value' do + it 'respects another subclass when converting the value' do record = Hashie::Mash.new class SubMash < Hashie::Mash @@ -310,56 +315,56 @@ describe Hashie::Mash do end describe '#respond_to?' do - it 'should respond to a normal method' do + it 'responds to a normal method' do Hashie::Mash.new.should be_respond_to(:key?) end - it 'should respond to a set key' do + it 'responds to a set key' do Hashie::Mash.new(abc: 'def').should be_respond_to(:abc) end - it 'should respond to a set key with a suffix' do + it 'responds to a set key with a suffix' do %w(= ? ! _).each do |suffix| Hashie::Mash.new(abc: 'def').should be_respond_to(:"abc#{suffix}") end end - it 'should not respond to an unknown key with a suffix' do + it 'does not respond to an unknown key with a suffix' do %w(= ? ! _).each do |suffix| Hashie::Mash.new(abc: 'def').should_not be_respond_to(:"xyz#{suffix}") end end - it 'should not respond to an unknown key without a suffix' do + it 'does not respond to an unknown key without a suffix' do Hashie::Mash.new(abc: 'def').should_not be_respond_to(:xyz) end - it 'should not respond to permitted?' do + it 'does not respond to permitted?' do Hashie::Mash.new.should_not be_respond_to(:permitted?) end end context '#initialize' do - it 'should convert an existing hash to a Hashie::Mash' do + it 'converts an existing hash to a Hashie::Mash' do converted = Hashie::Mash.new(abc: 123, name: 'Bob') converted.abc.should eq 123 converted.name.should eq 'Bob' end - it 'should convert hashes recursively into Hashie::Mashes' do + it 'converts hashes recursively into Hashie::Mashes' do converted = Hashie::Mash.new(a: { b: 1, c: { d: 23 } }) converted.a.is_a?(Hashie::Mash).should be_true converted.a.b.should eq 1 converted.a.c.d.should eq 23 end - it 'should convert hashes in arrays into Hashie::Mashes' do + it 'converts hashes in arrays into Hashie::Mashes' do converted = Hashie::Mash.new(a: [{ b: 12 }, 23]) converted.a.first.b.should eq 12 converted.a.last.should eq 23 end - it 'should convert an existing Hashie::Mash into a Hashie::Mash' do + it 'converts an existing Hashie::Mash into a Hashie::Mash' do initial = Hashie::Mash.new(name: 'randy', address: { state: 'TX' }) copy = Hashie::Mash.new(initial) initial.name.should eq copy.name @@ -370,7 +375,7 @@ describe Hashie::Mash do copy.address.__id__.should_not eq initial.address.__id__ end - it 'should accept a default block' do + it 'accepts a default block' do initial = Hashie::Mash.new { |h, i| h[i] = [] } initial.default_proc.should_not be_nil initial.default.should be_nil @@ -378,7 +383,7 @@ describe Hashie::Mash do initial.test?.should be_true end - it 'should convert Hashie::Mashes within Arrays back to Hashes' do + it 'converts Hashie::Mashes within Arrays back to Hashes' do initial_hash = { 'a' => [{ 'b' => 12, 'c' => ['d' => 50, 'e' => 51] }, 23] } converted = Hashie::Mash.new(initial_hash) converted.to_hash['a'].first.is_a?(Hashie::Mash).should be_false @@ -408,7 +413,7 @@ describe Hashie::Mash do end context 'when key does not exist' do - it 'should raise KeyError' do + it 'raises KeyError' do error = RUBY_VERSION =~ /1.8/ ? IndexError : KeyError expect { mash.fetch(:two) }.to raise_error(error) end @@ -425,7 +430,7 @@ describe Hashie::Mash do context 'with block given' do it 'returns default value' do - mash.fetch(:two) do|key| + mash.fetch(:two) do |key| 'block default value' end.should eql('block default value') end diff --git a/spec/hashie/rash_spec.rb b/spec/hashie/rash_spec.rb index 33a237f..b79fe44 100644 --- a/spec/hashie/rash_spec.rb +++ b/spec/hashie/rash_spec.rb @@ -1,48 +1,44 @@ require 'spec_helper' describe Hashie::Rash do - - attr_accessor :r - - before :each do - @r = Hashie::Rash.new( + subject do + Hashie::Rash.new( /hello/ => 'hello', /world/ => 'world', 'other' => 'whee', true => false, 1 => 'awesome', 1..1000 => 'rangey', + /(bcd)/ => proc { |m| m[1] } # /.+/ => "EVERYTHING" ) end - it 'should lookup strings' do - r['other'].should eq 'whee' - r['well hello there'].should eq 'hello' - r['the world is round'].should eq 'world' - r.all('hello world').sort.should eq %w(hello world) + it 'finds strings' do + subject['other'].should eq 'whee' + subject['well hello there'].should eq 'hello' + subject['the world is round'].should eq 'world' + subject.all('hello world').sort.should eq %w(hello world) end - it 'should lookup regexps' do - r[/other/].should eq 'whee' + it 'finds regexps' do + subject[/other/].should eq 'whee' end - it 'should lookup other objects' do - r[true].should eq false - r[1].should eq 'awesome' + it 'finds other objects' do + subject[true].should eq false + subject[1].should eq 'awesome' end - it 'should lookup numbers from ranges' do - @r[250].should eq 'rangey' - @r[999].should eq 'rangey' - @r[1000].should eq 'rangey' - @r[1001].should be_nil + it 'finds numbers from ranges' do + subject[250].should eq 'rangey' + subject[999].should eq 'rangey' + subject[1000].should eq 'rangey' + subject[1001].should be_nil end - it 'should call values which are procs' do - r = Hashie::Rash.new(/(ello)/ => proc { |m| m[1] }) - r['hello'].should eq 'ello' - r['ffffff'].should be_nil + it 'evaluates proc values' do + subject['abcdef'].should eq 'bcd' + subject['ffffff'].should be_nil end - end diff --git a/spec/hashie/trash_spec.rb b/spec/hashie/trash_spec.rb index f63790a..4bf4e45 100644 --- a/spec/hashie/trash_spec.rb +++ b/spec/hashie/trash_spec.rb @@ -35,24 +35,23 @@ describe Hashie::Trash do it 'maintains inverse translations hash mapping from the translated to the original name' do TrashTest.inverse_translations[:first_name].should eq :firstName end - end - describe 'permitted input keys' do - class TrashTest < Hashie::Trash - property :id + it '#permitted_input_keys contain the :from key of properties with translations' do + TrashTest.permitted_input_keys.should include :firstName end + end - it 'contain names of properties without translations' do - TrashTest.permitted_input_keys.should include :id + describe 'standard properties' do + class TrashTestPermitted < Hashie::Trash + property :id end - it 'contain the :from key of properties with translations' do - TrashTest.permitted_input_keys.should include :firstName + it '#permitted_input_keys contain names of properties without translations' do + TrashTestPermitted.permitted_input_keys.should include :id end end describe 'writing to properties' do - it 'does not write to a non-existent property using []=' do lambda { trash['abc'] = 123 }.should raise_error(NoMethodError) end @@ -113,20 +112,20 @@ describe Hashie::Trash do let(:lambda_trash) { TrashLambdaTest.new } - it 'should translate the value given on initialization with the given lambda' do + it 'translates the value given on initialization with the given lambda' do TrashLambdaTest.new(firstName: 'Michael').first_name.should eq 'Michael'.reverse end - it 'should not translate the value if given with the right property' do + it 'does not translate the value if given with the right property' do TrashTest.new(first_name: 'Michael').first_name.should eq 'Michael' end - it 'should translate the value given as property with the given lambda' do + it 'translates the value given as property with the given lambda' do lambda_trash.firstName = 'Michael' lambda_trash.first_name.should eq 'Michael'.reverse end - it 'should not translate the value given as right property' do + it 'does not translate the value given as right property' do lambda_trash.first_name = 'Michael' lambda_trash.first_name.should eq 'Michael' end @@ -157,12 +156,12 @@ describe Hashie::Trash do let(:lambda_trash) { TrashLambdaTestWithProperties.new } - it 'should translate the value given as property with the given lambda' do + it 'translates the value given as property with the given lambda' do lambda_trash.first_name = 'Michael' lambda_trash.first_name.should eq 'Michael'.reverse end - it 'should transform the value when given in constructor' do + it 'transforms the value when given in constructor' do TrashLambdaTestWithProperties.new(first_name: 'Michael').first_name.should eq 'Michael'.reverse end @@ -171,11 +170,11 @@ describe Hashie::Trash do property :first_name, from: :firstName, transform_with: lambda { |value| value.reverse } end - it 'should not override the :from option in the constructor' do + it 'does not override the :from option in the constructor' do TrashLambdaTest3.new(first_name: 'Michael').first_name.should eq 'Michael' end - it 'should not override the :from option when given as property' do + it 'does not override the :from option when given as property' do t = TrashLambdaTest3.new t.first_name = 'Michael' t.first_name.should eq 'Michael' @@ -184,7 +183,7 @@ describe Hashie::Trash do end end - it 'should raise an error when :from have the same value as property' do + it 'raises an error when :from have the same value as property' do expect do class WrongTrash < Hashie::Trash property :first_name, from: :first_name diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index c8043e9..d41e334 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -6,7 +6,3 @@ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib')) require 'hashie' require 'rspec' require 'rspec/autorun' - -RSpec.configure do |config| - -end |