spec/lib/sinclair/caster/class_methods_spec.rb in sinclair-1.15.0 vs spec/lib/sinclair/caster/class_methods_spec.rb in sinclair-1.16.0
- old
+ new
@@ -1,12 +1,21 @@
# frozen_string_literal: true
require 'spec_helper'
describe Sinclair::Caster::ClassMethods do
- subject(:caster) { Class.new(Sinclair::Caster) }
+ subject(:caster) { Class.new(superclass) }
+ let(:superclass) do
+ Class.new(Sinclair::Caster) do
+ cast_with(:string, :to_s)
+ cast_with(:integer, :to_i)
+ cast_with(:float, :to_f)
+ cast_with(String, :to_s)
+ end
+ end
+
describe '.cast_with' do
let(:value) { instance_double('value', to_p: final_value) }
let(:final_value) { Random.rand(100) }
context 'when a proc is given' do
@@ -118,9 +127,22 @@
end
end
end
describe '.caster_for' do
+ context 'when nil key is given' do
+ let(:value) { values.sample }
+ let(:values) do
+ [Random.rand, 'some string', { key: 10 }, Object.new, Class.new, [2, 3]]
+ end
+
+ it { expect(caster.caster_for(nil)).to be_a(Sinclair::Caster) }
+
+ it 'returns the default caster' do
+ expect(caster.caster_for(nil).cast(value)).to eq(value)
+ end
+ end
+
context 'when the key has been defined with a symbol key' do
before { caster.cast_with(:problem, :to_p) }
it do
expect(caster.caster_for(:problem))