spec/acfs/resource/attributes/dict_spec.rb in acfs-0.45.0 vs spec/acfs/resource/attributes/dict_spec.rb in acfs-0.46.0

- old
+ new

@@ -1,50 +1,75 @@ require 'spec_helper' describe Acfs::Resource::Attributes::Dict do - let(:model) { Class.new(Acfs::Resource) } - subject { Acfs::Resource::Attributes::Dict.new } + let(:type) { Acfs::Resource::Attributes::Dict.new } - describe '.cast' do - context 'with hash' do - let(:sample) { {3 => true, 'asfd' => 4} } + describe '#cast' do + subject { -> { type.cast value } } - it 'should return unmodified hash' do - expect(subject.cast(sample)).to be sample - end + context 'with nil' do + let(:value) { nil } + it { expect(subject.call).to eq nil } end - context 'with not hashable object' do - let(:sample) { Object.new } + context 'with blank string (I)' do + let(:value) { '' } + it { expect(subject.call).to eq nil } + end - it 'should raise a TypeError' do - expect do - subject.cast(sample) - end.to raise_error TypeError + context 'with blank string (II)' do + let(:value) { " \t" } + it { expect(subject.call).to eq nil } + end + + context 'with hash' do + let(:value) { {3 => true, abc: 4} } + it { expect(subject.call).to eq value } + end + + context 'with non hashable object' do + let(:value) { Object.new } + it { is_expected.to raise_error TypeError } + end + + context 'with hashable object (I)' do + let(:value) do + Class.new do + def to_hash + {id: object_id} + end + end.new end + + it { expect(subject.call).to eq id: value.object_id } end - context 'with hashable object' do - let(:sample) do - o = Object.new - class << o + context 'with hashable object (II)' do + let(:value) do + Class.new do def to_h - {3 => 4, 'test' => true} + {id: object_id} end - end - o + end.new end - it 'should cast object to hash' do - expect(subject.cast(sample)).to eq 3 => 4, 'test' => true - end + it { expect(subject.call).to eq id: value.object_id } end - context 'with hash subclass' do - let(:sample) { HashWithIndifferentAccess.new :test => :foo, 34 => 12 } - - it 'should return obj unmodified' do - expect(subject.cast(sample)).to be sample + context 'with serializable object' do + let(:value) do + Class.new do + def serializable_hash + {id: object_id} + end + end.new end + + it { expect(subject.call).to eq id: value.object_id } + end + + context 'with hash subclass object' do + let(:value) { HashWithIndifferentAccess.new test: :foo } + it { expect(subject.call).to be value } end end end