diff options
Diffstat (limited to 'spec/hashie/dash_spec.rb')
-rw-r--r-- | spec/hashie/dash_spec.rb | 129 |
1 files changed, 69 insertions, 60 deletions
diff --git a/spec/hashie/dash_spec.rb b/spec/hashie/dash_spec.rb index 606e38e..d651eea 100644 --- a/spec/hashie/dash_spec.rb +++ b/spec/hashie/dash_spec.rb @@ -40,102 +40,108 @@ describe DashTest do it('subclasses Hashie::Hash') { should respond_to(:to_mash) } - its(:to_s) { should eq '#<DashTest count=0 email="bob@example.com" first_name="Bob">' } + describe '#to_s' do + subject { super().to_s } + it { should eq '#<DashTest count=0 email="bob@example.com" first_name="Bob">' } + end it 'lists all set properties in inspect' do subject.first_name = 'Bob' subject.email = 'bob@example.com' - subject.inspect.should eq '#<DashTest count=0 email="bob@example.com" first_name="Bob">' + expect(subject.inspect).to eq '#<DashTest count=0 email="bob@example.com" first_name="Bob">' end - its(:count) { should be_zero } + describe '#count' do + subject { super().count } + it { should be_zero } + end it { should respond_to(:first_name) } it { should respond_to(:first_name=) } it { should_not respond_to(:nonexistent) } it 'errors out for a non-existent property' do - lambda { subject['nonexistent'] }.should raise_error(NoMethodError) + expect { subject['nonexistent'] }.to raise_error(NoMethodError) end it 'errors out when attempting to set a required property to nil' do - lambda { subject.first_name = nil }.should raise_error(ArgumentError) + expect { subject.first_name = nil }.to raise_error(ArgumentError) end context 'writing to properties' do it 'fails writing a required property to nil' do - lambda { subject.first_name = nil }.should raise_error(ArgumentError) + expect { subject.first_name = nil }.to raise_error(ArgumentError) end it 'fails writing a required property to nil using []=' do - lambda { subject['first_name'] = nil }.should raise_error(ArgumentError) + expect { subject['first_name'] = nil }.to raise_error(ArgumentError) end it 'fails writing to a non-existent property using []=' do - lambda { subject['nonexistent'] = 123 }.should raise_error(NoMethodError) + expect { subject['nonexistent'] = 123 }.to raise_error(NoMethodError) end it 'works for an existing property using []=' do subject['first_name'] = 'Bob' - subject['first_name'].should eq 'Bob' - subject[:first_name].should eq 'Bob' + expect(subject['first_name']).to eq 'Bob' + expect(subject[:first_name]).to eq 'Bob' end it 'works for an existing property using a method call' do subject.first_name = 'Franklin' - subject.first_name.should eq 'Franklin' + expect(subject.first_name).to eq 'Franklin' end end context 'reading from properties' do it 'fails reading from a non-existent property using []' do - lambda { subject['nonexistent'] }.should raise_error(NoMethodError) + expect { subject['nonexistent'] }.to raise_error(NoMethodError) end 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' + expect(value).to eq 'Aiden' end it 'is able to retrieve properties through blocks with method calls' do subject['first_name'] = 'Frodo' value = nil subject.first_name { |v| value = v } - value.should eq 'Frodo' + expect(value).to eq 'Frodo' end end context 'reading from deferred properties' do it 'evaluates proc after initial read' do - DeferredTest.new['created_at'].should be_instance_of(Time) + expect(DeferredTest.new['created_at']).to be_instance_of(Time) end it 'does not evalute proc after subsequent reads' do deferred = DeferredTest.new - deferred['created_at'].object_id.should eq deferred['created_at'].object_id + expect(deferred['created_at'].object_id).to eq deferred['created_at'].object_id end end describe '#new' do it 'fails with non-existent properties' do - lambda { described_class.new(bork: '') }.should raise_error(NoMethodError) + expect { described_class.new(bork: '') }.to raise_error(NoMethodError) end it 'sets properties that it is able to' do obj = described_class.new first_name: 'Michael' - obj.first_name.should eq 'Michael' + expect(obj.first_name).to eq 'Michael' end it 'accepts nil' do - lambda { DashNoRequiredTest.new(nil) }.should_not raise_error + expect { DashNoRequiredTest.new(nil) }.not_to raise_error end it 'accepts block to define a global default' do obj = described_class.new { |hash, key| key.to_s.upcase } - obj.first_name.should eq 'FIRST_NAME' - obj.count.should be_zero + expect(obj.first_name).to eq 'FIRST_NAME' + expect(obj.count).to be_zero end it 'fails when required values are missing' do @@ -146,20 +152,20 @@ describe DashTest do obj1 = DashDefaultTest.new obj1.aliases << 'El Rey' obj2 = DashDefaultTest.new - obj2.aliases.should_not include 'El Rey' + expect(obj2.aliases).not_to include 'El Rey' end end describe '#merge' do it 'creates a new instance of the Dash' do new_dash = subject.merge(first_name: 'Robert') - subject.object_id.should_not eq new_dash.object_id + expect(subject.object_id).not_to eq new_dash.object_id end it 'merges the given hash' do new_dash = subject.merge(first_name: 'Robert', email: 'robert@example.com') - new_dash.first_name.should eq 'Robert' - new_dash.email.should eq 'robert@example.com' + expect(new_dash.first_name).to eq 'Robert' + expect(new_dash.email).to eq 'robert@example.com' end it 'fails with non-existent properties' do @@ -172,9 +178,9 @@ describe DashTest do context 'given a block' do it "sets merged key's values to the block's return value" do - subject.merge(first_name: 'Jim') do |key, oldval, newval| + expect(subject.merge(first_name: 'Jim') do |key, oldval, newval| "#{key}: #{newval} #{oldval}" - end.first_name.should eq 'first_name: Jim Bob' + end.first_name).to eq 'first_name: Jim Bob' end end end @@ -182,13 +188,13 @@ describe DashTest do describe '#merge!' do it 'modifies the existing instance of the Dash' do original_dash = subject.merge!(first_name: 'Robert') - subject.object_id.should eq original_dash.object_id + expect(subject.object_id).to eq original_dash.object_id end it 'merges the given hash' do subject.merge!(first_name: 'Robert', email: 'robert@example.com') - subject.first_name.should eq 'Robert' - subject.email.should eq 'robert@example.com' + expect(subject.first_name).to eq 'Robert' + expect(subject.email).to eq 'robert@example.com' end it 'fails with non-existent properties' do @@ -201,38 +207,38 @@ describe DashTest do context 'given a block' do it "sets merged key's values to the block's return value" do - subject.merge!(first_name: 'Jim') do |key, oldval, newval| + expect(subject.merge!(first_name: 'Jim') do |key, oldval, newval| "#{key}: #{newval} #{oldval}" - end.first_name.should eq 'first_name: Jim Bob' + end.first_name).to eq 'first_name: Jim Bob' end end end describe 'properties' do it 'lists defined properties' do - described_class.properties.should eq Set.new([:first_name, :email, :count]) + expect(described_class.properties).to eq Set.new([:first_name, :email, :count]) end it 'checks if a property exists' do - described_class.property?('first_name').should be_true - described_class.property?(:first_name).should be_true + expect(described_class.property?('first_name')).to be_true + expect(described_class.property?(:first_name)).to be_true end it 'checks if a property is required' do - described_class.required?('first_name').should be_true - described_class.required?(:first_name).should be_true + expect(described_class.required?('first_name')).to be_true + expect(described_class.required?(:first_name)).to be_true end it 'doesnt include property from subclass' do - described_class.property?(:last_name).should be_false + expect(described_class.property?(:last_name)).to be_false end it 'lists declared defaults' do - described_class.defaults.should eq(count: 0) + expect(described_class.defaults).to eq(count: 0) end it 'allows properties that end in bang' do - PropertyBangTest.property?(:important!).should be_true + expect(PropertyBangTest.property?(:important!)).to be_true end end @@ -240,24 +246,24 @@ describe DashTest do before { subject.replace(first_name: 'Cain') } it 'return self' do - subject.replace(email: 'bar').to_hash.should eq('email' => 'bar', 'count' => 0) + expect(subject.replace(email: 'bar').to_hash).to eq('email' => 'bar', 'count' => 0) end it 'sets all specified keys to their corresponding values' do - subject.first_name.should eq 'Cain' + expect(subject.first_name).to eq 'Cain' end it 'leaves only specified keys and keys with default values' do - subject.keys.sort.should eq %w(count first_name) - subject.email.should be_nil - subject.count.should eq 0 + expect(subject.keys.sort).to eq %w(count first_name) + expect(subject.email).to be_nil + expect(subject.count).to eq 0 end context 'when replacing keys with default values' do before { subject.replace(count: 3) } it 'sets all specified keys to their corresponding values' do - subject.count.should eq 3 + expect(subject.count).to eq 3 end end end @@ -271,40 +277,40 @@ describe Hashie::Dash, 'inheritance' do end it 'reports empty properties when nothing defined' do - @top.properties.should be_empty - @top.defaults.should be_empty + expect(@top.properties).to be_empty + expect(@top.defaults).to be_empty end it 'inherits properties downwards' do @top.property :echo - @middle.properties.should include(:echo) - @bottom.properties.should include(:echo) + expect(@middle.properties).to include(:echo) + expect(@bottom.properties).to include(:echo) end it 'doesnt inherit properties upwards' do @middle.property :echo - @top.properties.should_not include(:echo) - @bottom.properties.should include(:echo) + expect(@top.properties).not_to include(:echo) + expect(@bottom.properties).to include(:echo) end it 'allows overriding a default on an existing property' do @top.property :echo @middle.property :echo, default: 123 - @bottom.properties.to_a.should eq [:echo] - @bottom.new.echo.should eq 123 + expect(@bottom.properties.to_a).to eq [:echo] + expect(@bottom.new.echo).to eq 123 end it 'allows clearing an existing default' do @top.property :echo @middle.property :echo, default: 123 @bottom.property :echo - @bottom.properties.to_a.should eq [:echo] - @bottom.new.echo.should be_nil + expect(@bottom.properties.to_a).to eq [:echo] + expect(@bottom.new.echo).to be_nil end it 'allows nil defaults' do @bottom.property :echo, default: nil - @bottom.new.should have_key('echo') + expect(@bottom.new).to have_key('echo') end end @@ -312,7 +318,10 @@ end describe Subclassed do subject { Subclassed.new(first_name: 'Bob', last_name: 'McNob', email: 'bob@example.com') } - its(:count) { should be_zero } + describe '#count' do + subject { super().count } + it { should be_zero } + end it { should respond_to(:first_name) } it { should respond_to(:first_name=) } @@ -320,10 +329,10 @@ describe Subclassed do it { should respond_to(:last_name=) } it 'has one additional property' do - described_class.property?(:last_name).should be_true + expect(described_class.property?(:last_name)).to be_true end it "didn't override superclass inheritance logic" do - described_class.instance_variable_get('@inheritance_test').should be_true + expect(described_class.instance_variable_get('@inheritance_test')).to be_true end end |