1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
|
require 'spec_helper'
describe Hashie::Extensions::Dash::IndifferentAccess do
class TrashWithIndifferentAccess < Hashie::Trash
include Hashie::Extensions::Dash::IndifferentAccess
property :per_page, transform_with: ->(v) { v.to_i }
property :total, from: :total_pages
end
class DashWithIndifferentAccess < Hashie::Dash
include Hashie::Extensions::Dash::IndifferentAccess
property :name
end
context 'when included in Trash' do
let(:params) { { per_page: '1', total_pages: 2 } }
subject { TrashWithIndifferentAccess.new(params) }
it 'gets the expected behaviour' do
expect(subject.per_page).to eq params[:per_page].to_i
expect(subject.total).to eq params[:total_pages]
end
end
context 'when included in Dash' do
let(:patch) { Hashie::Extensions::Dash::IndifferentAccess::ClassMethods }
let(:dash_class) { Class.new(Hashie::Dash) }
it 'extends with the patch once' do
expect(patch).to receive(:extended).with(dash_class).once
dash_class.send(:include, Hashie::Extensions::Dash::IndifferentAccess)
end
end
context 'initialized with' do
it 'string' do
instance = DashWithIndifferentAccess.new('name' => 'Name')
expect(instance.name).to eq('Name')
expect(instance['name']).to eq('Name')
expect(instance[:name]).to eq('Name')
expect(instance.inspect).to eq('#<DashWithIndifferentAccess name="Name">')
expect(instance.to_hash).to eq('name' => 'Name')
end
it 'key' do
instance = DashWithIndifferentAccess.new(name: 'Name')
expect(instance.name).to eq('Name')
expect(instance['name']).to eq('Name')
expect(instance[:name]).to eq('Name')
expect(instance.inspect).to eq('#<DashWithIndifferentAccess name="Name">')
expect(instance.to_hash).to eq('name' => 'Name')
end
end
it 'updates' do
instance = DashWithIndifferentAccess.new
instance['name'] = 'Updated String'
expect(instance.name).to eq('Updated String')
instance[:name] = 'Updated Symbol'
expect(instance.name).to eq('Updated Symbol')
instance.name = 'Updated Method'
expect(instance.name).to eq('Updated Method')
end
context 'initialized with both prefers last assignment' do
it 'string, then symbol' do
instance = DashWithIndifferentAccess.new('name' => 'First', name: 'Last')
expect(instance.name).to eq('Last')
expect(instance['name']).to eq('Last')
expect(instance[:name]).to eq('Last')
expect(instance.inspect).to eq('#<DashWithIndifferentAccess name="Last">')
expect(instance.to_hash).to eq('name' => 'Last')
end
it 'symbol then string' do
instance = DashWithIndifferentAccess.new(name: 'Last', 'name' => 'First')
expect(instance.name).to eq('First')
expect(instance['name']).to eq('First')
expect(instance[:name]).to eq('First')
expect(instance.inspect).to eq('#<DashWithIndifferentAccess name="First">')
expect(instance.to_hash).to eq('name' => 'First')
end
end
end
|